|
1 /* |
|
2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation; version 2 of the License |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 */ |
|
18 |
|
19 |
|
20 package org.hedgewars.hedgeroid.util; |
|
21 |
|
22 import java.io.ByteArrayOutputStream; |
|
23 import java.io.Closeable; |
|
24 import java.io.File; |
|
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.util.ArrayList; |
|
31 import java.util.List; |
|
32 |
|
33 import android.annotation.TargetApi; |
|
34 import android.content.Context; |
|
35 import android.content.res.Resources; |
|
36 import android.content.res.TypedArray; |
|
37 import android.os.Build; |
|
38 import android.os.Environment; |
|
39 import android.util.Log; |
|
40 |
|
41 public class FileUtils { |
|
42 private static final String ROOT_DIR = "Data"; |
|
43 private static final String TAG = FileUtils.class.getSimpleName(); |
|
44 |
|
45 /** |
|
46 * @return true if the data path is currently available. However, it can vanish at any time so |
|
47 * normally you should just try to use it and rely on the exceptions. |
|
48 */ |
|
49 public static boolean isDataPathAvailable() { |
|
50 return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); |
|
51 } |
|
52 |
|
53 /** |
|
54 * get the path to which we should download all the data files |
|
55 * @param c context |
|
56 * @return The directory |
|
57 * @throws FileNotFoundException if external storage is not available at the moment |
|
58 */ |
|
59 public static File getCachePath(Context c) throws FileNotFoundException { |
|
60 File cachePath = null; |
|
61 if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO |
|
62 cachePath = PreFroyoSDCardDir.getDownloadPath(c); |
|
63 } else { |
|
64 cachePath = FroyoSDCardDir.getDownloadPath(c); |
|
65 } |
|
66 if(cachePath==null) { |
|
67 throw new FileNotFoundException("External storage is currently unavailable"); |
|
68 } else { |
|
69 return cachePath; |
|
70 } |
|
71 } |
|
72 |
|
73 public static File getDataPathFile(Context c) throws FileNotFoundException { |
|
74 return new File(getCachePath(c), ROOT_DIR); |
|
75 } |
|
76 |
|
77 // TODO Several callers are unaware that this may fail, so it throws an RTE now. |
|
78 // Should be handled better though. |
|
79 @Deprecated |
|
80 public static String getDataPath(Context c) { |
|
81 try { |
|
82 return getDataPathFile(c).getAbsolutePath()+"/"; |
|
83 } catch(FileNotFoundException e) { |
|
84 throw new RuntimeException(e); |
|
85 } |
|
86 } |
|
87 |
|
88 @TargetApi(8) |
|
89 private static class FroyoSDCardDir{ |
|
90 public static File getDownloadPath(Context c){ |
|
91 return c.getExternalCacheDir(); |
|
92 } |
|
93 } |
|
94 |
|
95 private static class PreFroyoSDCardDir{ |
|
96 public static File getDownloadPath(Context c){ |
|
97 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ |
|
98 File extStorageDir = Environment.getExternalStorageDirectory(); |
|
99 if(extStorageDir != null) { |
|
100 return new File(extStorageDir, "Hedgewars"); |
|
101 } |
|
102 } |
|
103 return null; |
|
104 } |
|
105 } |
|
106 |
|
107 /** |
|
108 * Return a File array with all the files from dirName |
|
109 * @param c |
|
110 * @param dirName |
|
111 * @return |
|
112 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dirName" does not exist |
|
113 */ |
|
114 public static File[] getFilesFromRelativeDir(Context c, String dirName) throws FileNotFoundException { |
|
115 File f = new File(getDataPathFile(c), dirName); |
|
116 |
|
117 if(f.isDirectory()) { |
|
118 return f.listFiles(); |
|
119 } else { |
|
120 throw new FileNotFoundException("Directory "+dirName+" does not exist."); |
|
121 } |
|
122 } |
|
123 |
|
124 /** |
|
125 * Checks if this directory has a file with suffix suffix |
|
126 * @param f - directory |
|
127 * @return |
|
128 */ |
|
129 public static boolean hasFileWithSuffix(File f, String suffix){ |
|
130 if(f.isDirectory()){ |
|
131 for(String s : f.list()){ |
|
132 if(s.endsWith(suffix)) return true; |
|
133 } |
|
134 return false; |
|
135 }else{ |
|
136 return false; |
|
137 } |
|
138 } |
|
139 |
|
140 /** |
|
141 * Gives back all dirs which contain a file with suffix fileSuffix |
|
142 * @param c |
|
143 * @param path |
|
144 * @param fileSuffix |
|
145 * @return |
|
146 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "path" does not exist |
|
147 */ |
|
148 public static List<String> getDirsWithFileSuffix(Context c, String path, String fileSuffix) throws FileNotFoundException{ |
|
149 File[] files = getFilesFromRelativeDir(c,path); |
|
150 ArrayList<String> ret = new ArrayList<String>(); |
|
151 |
|
152 for(File f : files){ |
|
153 if(hasFileWithSuffix(f, fileSuffix)) ret.add(f.getName()); |
|
154 } |
|
155 return ret; |
|
156 } |
|
157 |
|
158 /** |
|
159 * Get all files from directory dir which have the given suffix |
|
160 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dir" does not exist |
|
161 */ |
|
162 public static ArrayList<String> getFileNamesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix) throws FileNotFoundException{ |
|
163 File[] files = FileUtils.getFilesFromRelativeDir(c, dir); |
|
164 ArrayList<String> ret = new ArrayList<String>(); |
|
165 for(File file : files){ |
|
166 String s = file.getName(); |
|
167 if(s.endsWith(suffix)){ |
|
168 if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length())); |
|
169 else ret.add(s); |
|
170 } |
|
171 } |
|
172 return ret; |
|
173 } |
|
174 |
|
175 /** |
|
176 * Close a resource (possibly null), ignoring any IOException. |
|
177 */ |
|
178 public static void closeQuietly(Closeable c) { |
|
179 if(c!=null) { |
|
180 try { |
|
181 c.close(); |
|
182 } catch(IOException e) { |
|
183 Log.w(TAG, e); |
|
184 } |
|
185 } |
|
186 } |
|
187 |
|
188 /** |
|
189 * Write all data from the input stream to the file, creating or overwriting it. |
|
190 * The input stream will be closed. |
|
191 * |
|
192 * @throws IOException |
|
193 */ |
|
194 public static void writeStreamToFile(InputStream is, File file) throws IOException { |
|
195 OutputStream os = null; |
|
196 byte[] buffer = new byte[8192]; |
|
197 try { |
|
198 os = new FileOutputStream(file); |
|
199 int size; |
|
200 while((size=is.read(buffer)) != -1) { |
|
201 os.write(buffer, 0, size); |
|
202 } |
|
203 os.close(); // Important to close this non-quietly, in case of exceptions when flushing |
|
204 } finally { |
|
205 FileUtils.closeQuietly(is); |
|
206 FileUtils.closeQuietly(os); |
|
207 } |
|
208 } |
|
209 |
|
210 /** |
|
211 * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory |
|
212 * @param c |
|
213 * @param sourceResId |
|
214 * @param directory |
|
215 */ |
|
216 public static void resRawToFilesDir(Context c, int sourceResId, int targetFilenames, String directory) throws IOException { |
|
217 File targetDir = new File(c.getFilesDir(), directory); |
|
218 targetDir.mkdirs(); |
|
219 |
|
220 //Get an array with the resource files ID |
|
221 Resources resources = c.getResources(); |
|
222 TypedArray ta = resources.obtainTypedArray(sourceResId); |
|
223 TypedArray filenames = resources.obtainTypedArray(targetFilenames); |
|
224 for(int i = 0; i < ta.length(); i++){ |
|
225 int resId = ta.getResourceId(i, 0); |
|
226 String fileName = filenames.getString(i); |
|
227 File f = new File(targetDir, fileName); |
|
228 writeStreamToFile(resources.openRawResource(resId), f); |
|
229 } |
|
230 } |
|
231 |
|
232 public static String readToString(InputStream is) throws IOException { |
|
233 try { |
|
234 ByteArrayOutputStream os = new ByteArrayOutputStream(); |
|
235 byte[] buffer = new byte[8192]; |
|
236 int size; |
|
237 while((size=is.read(buffer)) != -1) { |
|
238 os.write(buffer, 0, size); |
|
239 } |
|
240 return new String(os.toByteArray()); |
|
241 } finally { |
|
242 closeQuietly(is); |
|
243 } |
|
244 } |
|
245 |
|
246 private static final char[] badFilenameChars = new char[] { '/', '\\', ':', '*', '?', '\"', '<', '>', '|', '.', '\0' }; |
|
247 |
|
248 /** |
|
249 * Modify the given String so that it can be used as part of a filename |
|
250 * without causing problems from illegal/special characters. |
|
251 * |
|
252 * The result should be similar to the input, but isn't necessarily |
|
253 * reversible. |
|
254 */ |
|
255 public static String replaceBadChars(String name) { |
|
256 if (name == null || name.trim().length()==0) { |
|
257 return "_"; |
|
258 } |
|
259 name = name.trim(); |
|
260 for (char badChar : badFilenameChars) { |
|
261 name = name.replace(badChar, '_'); |
|
262 } |
|
263 return name; |
|
264 } |
|
265 } |