author | Xeli |
Sun, 21 Aug 2011 18:13:11 +0200 | |
branch | hedgeroid |
changeset 5623 | df23b477609d |
parent 5621 | ea796c83ea47 |
child 5663 | b13d1897d06f |
permissions | -rw-r--r-- |
5621 | 1 |
/* |
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2011 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 |
||
5414 | 20 |
package org.hedgewars.mobile; |
21 |
||
22 |
import java.io.BufferedOutputStream; |
|
23 |
import java.io.File; |
|
24 |
import java.io.FileOutputStream; |
|
25 |
import java.io.IOException; |
|
26 |
import java.io.InputStream; |
|
5473
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
27 |
import java.util.ArrayList; |
5414 | 28 |
|
29 |
import android.content.Context; |
|
30 |
import android.content.res.TypedArray; |
|
31 |
import android.widget.Toast; |
|
32 |
||
33 |
public class Utils { |
|
34 |
||
35 |
||
36 |
/** |
|
37 |
* get the path to which we should download all the data files |
|
38 |
* @param c context |
|
39 |
* @return absolute path |
|
40 |
*/ |
|
41 |
public static String getDownloadPath(Context c){ |
|
42 |
File f = c.getExternalCacheDir(); |
|
43 |
if(f != null){ |
|
44 |
return f.getAbsolutePath() + "/Data/"; |
|
45 |
}else{ |
|
46 |
Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG); |
|
47 |
return null; |
|
48 |
} |
|
49 |
} |
|
50 |
||
51 |
/** |
|
52 |
* Get files from dirName, dir name is relative to {@link getDownloadPath} |
|
53 |
* @param dirName |
|
54 |
* @param c context |
|
55 |
* @return string of files |
|
56 |
*/ |
|
57 |
public static String[] getFileNamesFromRelativeDir(Context c, String dirName){ |
|
58 |
String prefix = getDownloadPath(c); |
|
59 |
File f = new File(prefix + dirName); |
|
60 |
||
61 |
if(f.exists() && f.isDirectory()) return f.list(); |
|
62 |
else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath()); |
|
63 |
} |
|
64 |
||
65 |
/** |
|
5473
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
66 |
* Return a File array with all the files from dirName |
5414 | 67 |
* @param c |
68 |
* @param dirName |
|
69 |
* @return |
|
70 |
*/ |
|
71 |
public static File[] getFilesFromRelativeDir(Context c, String dirName){ |
|
72 |
String prefix = getDownloadPath(c); |
|
73 |
File f = new File(prefix + dirName); |
|
74 |
||
75 |
if(f.exists() && f.isDirectory()) return f.listFiles(); |
|
76 |
else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath()); |
|
77 |
} |
|
78 |
||
79 |
/** |
|
5473
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
80 |
* Checks if this directory has a file with suffix suffix |
5414 | 81 |
* @param f - directory |
82 |
* @return |
|
83 |
*/ |
|
84 |
public static boolean hasFileWithSuffix(File f, String suffix){ |
|
85 |
if(f.isDirectory()){ |
|
86 |
for(String s : f.list()){ |
|
87 |
if(s.endsWith(suffix)) return true; |
|
88 |
} |
|
89 |
return false; |
|
90 |
}else{ |
|
5428 | 91 |
return false; |
5414 | 92 |
} |
93 |
} |
|
94 |
||
5473
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
95 |
/** |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
96 |
* Gives back all dirs which contain a file with suffix fileSuffix |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
97 |
* @param c |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
98 |
* @param path |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
99 |
* @param fileSuffix |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
100 |
* @return |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
101 |
*/ |
5414 | 102 |
public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){ |
103 |
File[] files = getFilesFromRelativeDir(c,path); |
|
104 |
String[] validFiles = new String[files.length]; |
|
105 |
int validCounter = 0; |
|
106 |
||
107 |
for(File f : files){ |
|
108 |
if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName(); |
|
109 |
} |
|
110 |
String[] ret = new String[validCounter]; |
|
111 |
System.arraycopy(validFiles, 0, ret, 0, validCounter); |
|
112 |
return ret; |
|
113 |
} |
|
114 |
||
5473
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
115 |
/** |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
116 |
* Get all files from directory dir which have the given suffix |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
117 |
* @param c |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
118 |
* @param dir |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
119 |
* @param suffix |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
120 |
* @param removeSuffix |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
121 |
* @return |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
122 |
*/ |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
123 |
public static ArrayList<String> getFilesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix){ |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
124 |
String[] files = Utils.getFileNamesFromRelativeDir(c, dir); |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
125 |
ArrayList<String> ret = new ArrayList<String>(); |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
126 |
for(String s : files){ |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
127 |
if(s.endsWith(suffix)){ |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
128 |
if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length())); |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
129 |
else ret.add(s); |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
130 |
} |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
131 |
} |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
132 |
return ret; |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
133 |
} |
a68f900c4e8c
Utility functions which are context wide, mostly SD methods right now
Xeli
parents:
5428
diff
changeset
|
134 |
|
5414 | 135 |
/** |
136 |
* Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory |
|
137 |
* @param c |
|
138 |
* @param sourceResId |
|
139 |
* @param directory |
|
140 |
*/ |
|
141 |
public static void resRawToFilesDir(Context c, int sourceResId, String directory){ |
|
142 |
byte[] buffer = new byte[1024]; |
|
143 |
InputStream bis = null; |
|
144 |
BufferedOutputStream bos = null; |
|
145 |
File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory); |
|
146 |
schemesDirFile.mkdirs(); |
|
147 |
String schemesDirPath = schemesDirFile.getAbsolutePath() + '/'; |
|
148 |
||
149 |
//Get an array with the resource files ID |
|
150 |
TypedArray ta = c.getResources().obtainTypedArray(sourceResId); |
|
151 |
int[] resIds = new int[ta.length()]; |
|
152 |
for(int i = 0; i < ta.length(); i++){ |
|
153 |
resIds[i] = ta.getResourceId(i, 0); |
|
154 |
} |
|
155 |
||
156 |
for(int id : resIds){ |
|
157 |
String fileName = c.getResources().getResourceEntryName(id); |
|
158 |
File f = new File(schemesDirPath + fileName); |
|
159 |
try { |
|
160 |
if(!f.createNewFile()){ |
|
161 |
f.delete(); |
|
162 |
f.createNewFile(); |
|
163 |
} |
|
164 |
||
165 |
bis = c.getResources().openRawResource(id); |
|
166 |
bos = new BufferedOutputStream(new FileOutputStream(f), 1024); |
|
167 |
int read = 0; |
|
168 |
while((read = bis.read(buffer)) != -1){ |
|
169 |
bos.write(buffer, 0, read); |
|
170 |
} |
|
171 |
||
172 |
} catch (IOException e) { |
|
173 |
e.printStackTrace(); |
|
174 |
}finally{ |
|
175 |
if(bis != null) |
|
176 |
try { |
|
177 |
bis.close(); |
|
178 |
} catch (IOException e) { |
|
179 |
e.printStackTrace(); |
|
180 |
} |
|
181 |
if(bos != null) |
|
182 |
try { |
|
183 |
bos.close(); |
|
184 |
} catch (IOException e) { |
|
185 |
e.printStackTrace(); |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
} |
|
190 |
} |