23 import java.io.File; |
23 import java.io.File; |
24 import java.io.FileOutputStream; |
24 import java.io.FileOutputStream; |
25 import java.io.IOException; |
25 import java.io.IOException; |
26 import java.io.InputStream; |
26 import java.io.InputStream; |
27 import java.util.ArrayList; |
27 import java.util.ArrayList; |
|
28 import java.util.List; |
28 |
29 |
29 import android.content.Context; |
30 import android.content.Context; |
30 import android.content.res.TypedArray; |
31 import android.content.res.TypedArray; |
31 import android.os.Build; |
32 import android.os.Build; |
32 import android.os.Environment; |
33 import android.os.Environment; |
33 import android.util.Log; |
34 import android.util.Log; |
34 import android.widget.Toast; |
|
35 |
35 |
36 public class Utils { |
36 public class Utils { |
37 |
37 |
38 private static final String ROOT_DIR = "Data/"; |
38 private static final String ROOT_DIR = "Data/"; |
39 |
39 |
40 /** |
40 /** |
41 * get the path to which we should download all the data files |
41 * get the path to which we should download all the data files |
42 * @param c context |
42 * @param c context |
43 * @return absolute path |
43 * @return absolute path |
44 */ |
44 */ |
47 return PreFroyoSDCardDir.getDownloadPath(c) + '/'; |
47 return PreFroyoSDCardDir.getDownloadPath(c) + '/'; |
48 }else{ |
48 }else{ |
49 return FroyoSDCardDir.getDownloadPath(c) + '/'; |
49 return FroyoSDCardDir.getDownloadPath(c) + '/'; |
50 } |
50 } |
51 } |
51 } |
52 |
52 |
53 public static String getDataPath(Context c){ |
53 public static String getDataPath(Context c){ |
54 return getCachePath(c) + ROOT_DIR; |
54 return getCachePath(c) + ROOT_DIR; |
55 } |
55 } |
56 |
56 |
57 static class FroyoSDCardDir{ |
57 static class FroyoSDCardDir{ |
58 public static String getDownloadPath(Context c){ |
58 public static String getDownloadPath(Context c){ |
59 File f = c.getExternalCacheDir(); |
59 File f = c.getExternalCacheDir(); |
60 if(f != null){ |
60 if(f != null){ |
61 return f.getAbsolutePath(); |
61 return f.getAbsolutePath(); |
62 }else{ |
62 }else{ |
63 Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show(); |
|
64 return null; |
63 return null; |
65 } |
64 } |
66 } |
65 } |
67 } |
66 } |
68 |
67 |
69 static class PreFroyoSDCardDir{ |
68 static class PreFroyoSDCardDir{ |
70 public static String getDownloadPath(Context c){ |
69 public static String getDownloadPath(Context c){ |
71 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ |
70 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ |
72 if(Environment.getExternalStorageDirectory() != null) |
71 if(Environment.getExternalStorageDirectory() != null) |
73 return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/"; |
72 return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/"; |
74 } |
73 } |
75 Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show(); |
|
76 return null; |
74 return null; |
77 } |
75 } |
78 } |
76 } |
79 |
77 |
80 /** |
78 /** |
81 * Get files from dirName, dir name is relative to {@link getDownloadPath} |
79 * Get files from dirName, dir name is relative to {@link getDownloadPath} |
82 * @param dirName |
80 * @param dirName |
83 * @param c context |
81 * @param c context |
84 * @return string of files |
82 * @return string of files |
85 */ |
83 */ |
86 public static String[] getFileNamesFromRelativeDir(Context c, String dirName){ |
84 public static String[] getFileNamesFromRelativeDir(Context c, String dirName){ |
87 String prefix = getDataPath(c); |
85 String prefix = getDataPath(c); |
88 File f = new File(prefix + dirName); |
86 File f = new File(prefix + dirName); |
89 |
87 |
90 if(f.exists() && f.isDirectory()) return f.list(); |
88 if(f.exists() && f.isDirectory()) return f.list(); |
91 else{ |
89 else{ |
92 |
90 |
93 Log.e("Utils::", "Couldn't find dir: " + dirName); |
91 Log.e("Utils::", "Couldn't find dir: " + dirName); |
94 return new String[0]; |
92 return new String[0]; |
95 } |
93 } |
96 } |
94 } |
97 |
95 |
98 /** |
96 /** |
99 * Return a File array with all the files from dirName |
97 * Return a File array with all the files from dirName |
100 * @param c |
98 * @param c |
101 * @param dirName |
99 * @param dirName |
102 * @return |
100 * @return |
103 */ |
101 */ |
104 public static File[] getFilesFromRelativeDir(Context c, String dirName){ |
102 public static File[] getFilesFromRelativeDir(Context c, String dirName){ |
105 String prefix = getDataPath(c); |
103 String prefix = getDataPath(c); |
106 File f = new File(prefix + dirName); |
104 File f = new File(prefix + dirName); |
107 |
105 |
108 if(f.exists() && f.isDirectory()) return f.listFiles(); |
106 if(f.exists() && f.isDirectory()) return f.listFiles(); |
109 else { |
107 else { |
110 Log.e("Utils::", "Dir not found: " + dirName); |
108 Log.e("Utils::", "Dir not found: " + dirName); |
111 return new File[0]; |
109 return new File[0]; |
112 } |
110 } |
113 } |
111 } |
114 |
112 |
115 /** |
113 /** |
116 * Checks if this directory has a file with suffix suffix |
114 * Checks if this directory has a file with suffix suffix |
117 * @param f - directory |
115 * @param f - directory |
118 * @return |
116 * @return |
119 */ |
117 */ |
125 return false; |
123 return false; |
126 }else{ |
124 }else{ |
127 return false; |
125 return false; |
128 } |
126 } |
129 } |
127 } |
130 |
128 |
131 /** |
129 /** |
132 * Gives back all dirs which contain a file with suffix fileSuffix |
130 * Gives back all dirs which contain a file with suffix fileSuffix |
133 * @param c |
131 * @param c |
134 * @param path |
132 * @param path |
135 * @param fileSuffix |
133 * @param fileSuffix |
136 * @return |
134 * @return |
137 */ |
135 */ |
138 public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){ |
136 public static List<String> getDirsWithFileSuffix(Context c, String path, String fileSuffix){ |
139 File[] files = getFilesFromRelativeDir(c,path); |
137 File[] files = getFilesFromRelativeDir(c,path); |
140 String[] validFiles = new String[files.length]; |
138 ArrayList<String> ret = new ArrayList<String>(); |
141 int validCounter = 0; |
139 |
142 |
|
143 for(File f : files){ |
140 for(File f : files){ |
144 if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName(); |
141 if(hasFileWithSuffix(f, fileSuffix)) ret.add(f.getName()); |
145 } |
142 } |
146 String[] ret = new String[validCounter]; |
|
147 System.arraycopy(validFiles, 0, ret, 0, validCounter); |
|
148 return ret; |
143 return ret; |
149 } |
144 } |
150 |
145 |
151 /** |
146 /** |
152 * Get all files from directory dir which have the given suffix |
147 * Get all files from directory dir which have the given suffix |
153 * @param c |
148 * @param c |
154 * @param dir |
149 * @param dir |
155 * @param suffix |
150 * @param suffix |
165 else ret.add(s); |
160 else ret.add(s); |
166 } |
161 } |
167 } |
162 } |
168 return ret; |
163 return ret; |
169 } |
164 } |
170 |
165 |
171 /** |
166 /** |
172 * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory |
167 * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory |
173 * @param c |
168 * @param c |
174 * @param sourceResId |
169 * @param sourceResId |
175 * @param directory |
170 * @param directory |
176 */ |
171 */ |
177 public static void resRawToFilesDir(Context c, int sourceResId, String directory){ |
172 public static void resRawToFilesDir(Context c, int sourceResId, String directory){ |
178 byte[] buffer = new byte[1024]; |
173 byte[] buffer = new byte[1024]; |
179 InputStream bis = null; |
174 InputStream bis = null; |
180 BufferedOutputStream bos = null; |
175 BufferedOutputStream bos = null; |
181 File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory); |
176 File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory); |