[09c6eb7] | 1 | package ve.gob.cenditel.tibisaymovil; |
---|
| 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.io.FileInputStream; |
---|
| 5 | import java.io.FileNotFoundException; |
---|
| 6 | import java.io.FileOutputStream; |
---|
| 7 | import java.io.IOException; |
---|
| 8 | import java.io.InputStream; |
---|
| 9 | import java.io.OutputStream; |
---|
| 10 | import java.nio.channels.FileChannel; |
---|
| 11 | import java.util.Collection; |
---|
| 12 | |
---|
| 13 | import android.content.Context; |
---|
| 14 | import android.net.Uri; |
---|
| 15 | import android.os.Environment; |
---|
| 16 | import android.os.StatFs; |
---|
| 17 | import android.text.format.DateFormat; |
---|
| 18 | import android.util.Log; |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * File system related utilities. |
---|
| 22 | * |
---|
| 23 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
| 24 | */ |
---|
| 25 | public class FsUtils { |
---|
| 26 | |
---|
| 27 | private static final String TAG = "FsUtils"; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * Save raw data to a File. |
---|
| 31 | * |
---|
| 32 | * @param data the data to be saved |
---|
| 33 | * @param targetFilename name of the file to be created |
---|
| 34 | * @throws FileAlreadyExistsException if the file already exists |
---|
| 35 | * @throws FileNotFoundException if the file cannot be open for writing |
---|
| 36 | * @throws IOException if there is an I/O error |
---|
| 37 | */ |
---|
| 38 | public static Uri saveToFile(byte[] data, String targetFilename) |
---|
| 39 | throws FileAlreadyExistsException, FileNotFoundException, IOException { |
---|
| 40 | |
---|
| 41 | File file = createNewFile(targetFilename); |
---|
| 42 | OutputStream stream = new FileOutputStream(file); |
---|
| 43 | stream.write(data); |
---|
| 44 | stream.close(); |
---|
| 45 | return Uri.fromFile(file); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | public static Uri saveToFile(InputStream data, String targetFilename) |
---|
| 49 | throws FileAlreadyExistsException, FileNotFoundException, IOException { |
---|
| 50 | |
---|
| 51 | File file = createNewFile(targetFilename); |
---|
| 52 | OutputStream stream = new FileOutputStream(file); |
---|
| 53 | byte[] buf = new byte[8192]; |
---|
| 54 | int n; |
---|
| 55 | while ((n = data.read(buf)) > 0) { |
---|
| 56 | stream.write(buf, 0, n); |
---|
| 57 | } |
---|
| 58 | data.close(); |
---|
| 59 | stream.close(); |
---|
| 60 | return Uri.fromFile(file); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | public static void copyTo(File file, String directory) |
---|
| 64 | throws IOException { |
---|
| 65 | |
---|
| 66 | String target = directory + File.separator + file.getName(); |
---|
| 67 | FileChannel source = null; |
---|
| 68 | FileChannel destination = null; |
---|
| 69 | |
---|
| 70 | try { |
---|
| 71 | destination = new FileOutputStream(createNewFile(target)).getChannel(); |
---|
| 72 | source = new FileInputStream(file).getChannel(); |
---|
| 73 | destination.transferFrom(source, 0, source.size()); |
---|
| 74 | } finally { |
---|
| 75 | if (source != null) { |
---|
| 76 | source.close(); |
---|
| 77 | } |
---|
| 78 | if (destination != null) { |
---|
| 79 | destination.close(); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | private static File createNewFile(String targetFilename) |
---|
| 85 | throws FileNotFoundException { |
---|
| 86 | |
---|
| 87 | File file = new File(targetFilename); |
---|
| 88 | String base = null; |
---|
| 89 | String extension = null; |
---|
| 90 | if (file.exists()) { |
---|
| 91 | int n; |
---|
| 92 | if ((n = targetFilename.lastIndexOf(".")) > targetFilename.lastIndexOf(File.separator)) { |
---|
| 93 | base = targetFilename.substring(0, n); |
---|
| 94 | extension = targetFilename.substring(n); |
---|
| 95 | } else { |
---|
| 96 | base = targetFilename; |
---|
| 97 | extension = ""; |
---|
| 98 | } |
---|
| 99 | } |
---|
| 100 | for (int i = 1; file.exists(); i++) { |
---|
| 101 | file = new File(base + "-" + i + extension); |
---|
| 102 | } |
---|
| 103 | return file; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | public static void checkOutOfSpace(Exception e, boolean external) |
---|
| 107 | throws OutOfSpaceError { |
---|
| 108 | |
---|
| 109 | String path; |
---|
| 110 | if (external) { |
---|
| 111 | path = Environment.getExternalStorageDirectory().getAbsolutePath(); |
---|
| 112 | } else { |
---|
| 113 | path = Environment.getDataDirectory().getAbsolutePath(); |
---|
| 114 | } |
---|
| 115 | StatFs stat = new StatFs(path); |
---|
| 116 | int available = stat.getAvailableBlocks() * stat.getBlockSize(); |
---|
| 117 | if (available < 8192) { |
---|
| 118 | throw new OutOfSpaceError(e); |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | public static String createOutputDirectory(Context context) { |
---|
| 123 | |
---|
| 124 | File root = new File(Environment.getExternalStorageDirectory(), |
---|
[288126d] | 125 | context.getResources().getString(R.string.app_name) + File.separator + |
---|
| 126 | context.getResources().getString(R.string.signature_directory)); |
---|
[09c6eb7] | 127 | root.mkdirs(); |
---|
| 128 | try { |
---|
| 129 | new File(root.getAbsolutePath() + File.separator + ".nomedia").createNewFile(); |
---|
| 130 | } catch (IOException e) { |
---|
| 131 | Log.e(TAG, "Error creating .nomedia file", e); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | CharSequence datePart = DateFormat.format("yyyy-MM-dd_kkmmss", System.currentTimeMillis()); |
---|
| 135 | String output = root.getAbsolutePath() + File.separator + datePart; |
---|
| 136 | |
---|
| 137 | if (new File(output).mkdirs()) { |
---|
| 138 | return output; |
---|
| 139 | } else { |
---|
| 140 | return null; |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | public static boolean isPDF(String inputFilename) { |
---|
| 145 | |
---|
| 146 | return inputFilename.toLowerCase().endsWith(".pdf"); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | public static boolean containsPDF(Collection<Uri> uris) { |
---|
| 150 | |
---|
| 151 | for (Uri uri : uris) { |
---|
| 152 | if (isPDF(uri.getPath())) { |
---|
| 153 | return true; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | return false; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | public static boolean isHiddenOrNotReadable(File f) { |
---|
| 160 | |
---|
| 161 | String name = f.getName(); |
---|
| 162 | boolean notReadable = !f.canRead(); |
---|
| 163 | boolean hidden = name.startsWith(".") && !name.equals(".") && !name.equals(".."); |
---|
| 164 | return notReadable || hidden; |
---|
| 165 | } |
---|
| 166 | } |
---|