1 | /* |
---|
2 | Tibisay Movil |
---|
3 | |
---|
4 | Copyright (C) 2013 Antonio Araujo (aaraujo@cenditel.gob.ve), Jose Ruiz |
---|
5 | (jruiz@cenditel.gob.ve), Fundacion Centro Nacional de Desarrollo e |
---|
6 | Investigacion en Tecnologias Libres - CENDITEL. |
---|
7 | |
---|
8 | La Fundación CENDITEL concede permiso para usar, copiar, distribuir y/o |
---|
9 | modificar este programa, reconociendo el derecho que la humanidad posee al |
---|
10 | libre acceso al conocimiento, bajo los términos de la licencia de software |
---|
11 | GPL versión 2.0 de la Free Software Foundation. |
---|
12 | |
---|
13 | Este programa se distribuye con la esperanza de que sea util, pero SIN |
---|
14 | NINGUNA GARANTIA; tampoco las implicitas garantias de MERCANTILIDAD o |
---|
15 | ADECUACION A UN PROPOSITO PARTICULAR. |
---|
16 | |
---|
17 | Para mayor información sobre los términos de la licencia ver el archivo |
---|
18 | llamado "gpl-2.0.txt" en ingles. |
---|
19 | */ |
---|
20 | |
---|
21 | package ve.gob.cenditel.tibisaymovil; |
---|
22 | |
---|
23 | import java.io.File; |
---|
24 | import java.io.FileInputStream; |
---|
25 | import java.io.FileNotFoundException; |
---|
26 | import java.io.FileOutputStream; |
---|
27 | import java.io.IOException; |
---|
28 | import java.io.InputStream; |
---|
29 | import java.io.OutputStream; |
---|
30 | import java.nio.channels.FileChannel; |
---|
31 | import java.util.Collection; |
---|
32 | |
---|
33 | import android.content.Context; |
---|
34 | import android.net.Uri; |
---|
35 | import android.os.Environment; |
---|
36 | import android.os.StatFs; |
---|
37 | import android.text.format.DateFormat; |
---|
38 | import android.util.Log; |
---|
39 | |
---|
40 | /** |
---|
41 | * File system related utilities. |
---|
42 | * |
---|
43 | * @author José M. Prieto (jmprieto@emergya.com) |
---|
44 | */ |
---|
45 | public class FsUtils { |
---|
46 | |
---|
47 | private static final String TAG = "FsUtils"; |
---|
48 | |
---|
49 | /** |
---|
50 | * Save raw data to a File. |
---|
51 | * |
---|
52 | * @param data the data to be saved |
---|
53 | * @param targetFilename name of the file to be created |
---|
54 | * @throws FileAlreadyExistsException if the file already exists |
---|
55 | * @throws FileNotFoundException if the file cannot be open for writing |
---|
56 | * @throws IOException if there is an I/O error |
---|
57 | */ |
---|
58 | public static Uri saveToFile(byte[] data, String targetFilename) |
---|
59 | throws FileAlreadyExistsException, FileNotFoundException, IOException { |
---|
60 | |
---|
61 | File file = createNewFile(targetFilename); |
---|
62 | OutputStream stream = new FileOutputStream(file); |
---|
63 | stream.write(data); |
---|
64 | stream.close(); |
---|
65 | return Uri.fromFile(file); |
---|
66 | } |
---|
67 | |
---|
68 | public static Uri saveToFile(InputStream data, String targetFilename) |
---|
69 | throws FileAlreadyExistsException, FileNotFoundException, IOException { |
---|
70 | |
---|
71 | File file = createNewFile(targetFilename); |
---|
72 | OutputStream stream = new FileOutputStream(file); |
---|
73 | byte[] buf = new byte[8192]; |
---|
74 | int n; |
---|
75 | while ((n = data.read(buf)) > 0) { |
---|
76 | stream.write(buf, 0, n); |
---|
77 | } |
---|
78 | data.close(); |
---|
79 | stream.close(); |
---|
80 | return Uri.fromFile(file); |
---|
81 | } |
---|
82 | |
---|
83 | public static void copyTo(File file, String directory) |
---|
84 | throws IOException { |
---|
85 | |
---|
86 | String target = directory + File.separator + file.getName(); |
---|
87 | FileChannel source = null; |
---|
88 | FileChannel destination = null; |
---|
89 | |
---|
90 | try { |
---|
91 | destination = new FileOutputStream(createNewFile(target)).getChannel(); |
---|
92 | source = new FileInputStream(file).getChannel(); |
---|
93 | destination.transferFrom(source, 0, source.size()); |
---|
94 | } finally { |
---|
95 | if (source != null) { |
---|
96 | source.close(); |
---|
97 | } |
---|
98 | if (destination != null) { |
---|
99 | destination.close(); |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | private static File createNewFile(String targetFilename) |
---|
105 | throws FileNotFoundException { |
---|
106 | |
---|
107 | File file = new File(targetFilename); |
---|
108 | String base = null; |
---|
109 | String extension = null; |
---|
110 | if (file.exists()) { |
---|
111 | int n; |
---|
112 | if ((n = targetFilename.lastIndexOf(".")) > targetFilename.lastIndexOf(File.separator)) { |
---|
113 | base = targetFilename.substring(0, n); |
---|
114 | extension = targetFilename.substring(n); |
---|
115 | } else { |
---|
116 | base = targetFilename; |
---|
117 | extension = ""; |
---|
118 | } |
---|
119 | } |
---|
120 | for (int i = 1; file.exists(); i++) { |
---|
121 | file = new File(base + "-" + i + extension); |
---|
122 | } |
---|
123 | return file; |
---|
124 | } |
---|
125 | |
---|
126 | public static void checkOutOfSpace(Exception e, boolean external) |
---|
127 | throws OutOfSpaceError { |
---|
128 | |
---|
129 | String path; |
---|
130 | if (external) { |
---|
131 | path = Environment.getExternalStorageDirectory().getAbsolutePath(); |
---|
132 | } else { |
---|
133 | path = Environment.getDataDirectory().getAbsolutePath(); |
---|
134 | } |
---|
135 | StatFs stat = new StatFs(path); |
---|
136 | int available = stat.getAvailableBlocks() * stat.getBlockSize(); |
---|
137 | if (available < 8192) { |
---|
138 | throw new OutOfSpaceError(e); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | public static String createOutputDirectory(Context context) { |
---|
143 | |
---|
144 | File root = new File(Environment.getExternalStorageDirectory(), |
---|
145 | context.getResources().getString(R.string.app_name) + File.separator + |
---|
146 | context.getResources().getString(R.string.signature_directory)); |
---|
147 | root.mkdirs(); |
---|
148 | try { |
---|
149 | new File(root.getAbsolutePath() + File.separator + ".nomedia").createNewFile(); |
---|
150 | } catch (IOException e) { |
---|
151 | Log.e(TAG, "Error creating .nomedia file", e); |
---|
152 | } |
---|
153 | |
---|
154 | CharSequence datePart = DateFormat.format("yyyy-MM-dd_kkmmss", System.currentTimeMillis()); |
---|
155 | String output = root.getAbsolutePath() + File.separator + datePart; |
---|
156 | |
---|
157 | if (new File(output).mkdirs()) { |
---|
158 | return output; |
---|
159 | } else { |
---|
160 | return null; |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | public static boolean isPDF(String inputFilename) { |
---|
165 | |
---|
166 | return inputFilename.toLowerCase().endsWith(".pdf"); |
---|
167 | } |
---|
168 | |
---|
169 | public static boolean containsPDF(Collection<Uri> uris) { |
---|
170 | |
---|
171 | for (Uri uri : uris) { |
---|
172 | if (isPDF(uri.getPath())) { |
---|
173 | return true; |
---|
174 | } |
---|
175 | } |
---|
176 | return false; |
---|
177 | } |
---|
178 | |
---|
179 | public static boolean isHiddenOrNotReadable(File f) { |
---|
180 | |
---|
181 | String name = f.getName(); |
---|
182 | boolean notReadable = !f.canRead(); |
---|
183 | boolean hidden = name.startsWith(".") && !name.equals(".") && !name.equals(".."); |
---|
184 | return notReadable || hidden; |
---|
185 | } |
---|
186 | } |
---|