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