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