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;
|
|
8 |
|
|
9 |
import android.content.Context;
|
|
10 |
import android.content.res.TypedArray;
|
|
11 |
import android.widget.Toast;
|
|
12 |
|
|
13 |
public class Utils {
|
|
14 |
|
|
15 |
|
|
16 |
/**
|
|
17 |
* get the path to which we should download all the data files
|
|
18 |
* @param c context
|
|
19 |
* @return absolute path
|
|
20 |
*/
|
|
21 |
public static String getDownloadPath(Context c){
|
|
22 |
File f = c.getExternalCacheDir();
|
|
23 |
if(f != null){
|
|
24 |
return f.getAbsolutePath() + "/Data/";
|
|
25 |
}else{
|
|
26 |
Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG);
|
|
27 |
return null;
|
|
28 |
}
|
|
29 |
}
|
|
30 |
|
|
31 |
/**
|
|
32 |
* Get files from dirName, dir name is relative to {@link getDownloadPath}
|
|
33 |
* @param dirName
|
|
34 |
* @param c context
|
|
35 |
* @return string of files
|
|
36 |
*/
|
|
37 |
public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
|
|
38 |
String prefix = getDownloadPath(c);
|
|
39 |
File f = new File(prefix + dirName);
|
|
40 |
|
|
41 |
if(f.exists() && f.isDirectory()) return f.list();
|
|
42 |
else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
|
|
43 |
}
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Return a File array if all the files from dirName
|
|
47 |
* @param c
|
|
48 |
* @param dirName
|
|
49 |
* @return
|
|
50 |
*/
|
|
51 |
public static File[] getFilesFromRelativeDir(Context c, String dirName){
|
|
52 |
String prefix = getDownloadPath(c);
|
|
53 |
File f = new File(prefix + dirName);
|
|
54 |
|
|
55 |
if(f.exists() && f.isDirectory()) return f.listFiles();
|
|
56 |
else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
|
|
57 |
}
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Checks if this directory has a lua file
|
|
61 |
* @param f - directory
|
|
62 |
* @return
|
|
63 |
*/
|
|
64 |
public static boolean hasFileWithSuffix(File f, String suffix){
|
|
65 |
if(f.isDirectory()){
|
|
66 |
for(String s : f.list()){
|
|
67 |
if(s.endsWith(suffix)) return true;
|
|
68 |
}
|
|
69 |
return false;
|
|
70 |
}else{
|
|
71 |
throw new IllegalArgumentException("Not a directory.. f = " + f.getAbsolutePath());
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
|
|
76 |
File[] files = getFilesFromRelativeDir(c,path);
|
|
77 |
String[] validFiles = new String[files.length];
|
|
78 |
int validCounter = 0;
|
|
79 |
|
|
80 |
for(File f : files){
|
|
81 |
if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
|
|
82 |
}
|
|
83 |
String[] ret = new String[validCounter];
|
|
84 |
System.arraycopy(validFiles, 0, ret, 0, validCounter);
|
|
85 |
return ret;
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
|
|
90 |
* @param c
|
|
91 |
* @param sourceResId
|
|
92 |
* @param directory
|
|
93 |
*/
|
|
94 |
public static void resRawToFilesDir(Context c, int sourceResId, String directory){
|
|
95 |
byte[] buffer = new byte[1024];
|
|
96 |
InputStream bis = null;
|
|
97 |
BufferedOutputStream bos = null;
|
|
98 |
File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
|
|
99 |
schemesDirFile.mkdirs();
|
|
100 |
String schemesDirPath = schemesDirFile.getAbsolutePath() + '/';
|
|
101 |
|
|
102 |
//Get an array with the resource files ID
|
|
103 |
TypedArray ta = c.getResources().obtainTypedArray(sourceResId);
|
|
104 |
int[] resIds = new int[ta.length()];
|
|
105 |
for(int i = 0; i < ta.length(); i++){
|
|
106 |
resIds[i] = ta.getResourceId(i, 0);
|
|
107 |
}
|
|
108 |
|
|
109 |
for(int id : resIds){
|
|
110 |
String fileName = c.getResources().getResourceEntryName(id);
|
|
111 |
File f = new File(schemesDirPath + fileName);
|
|
112 |
try {
|
|
113 |
if(!f.createNewFile()){
|
|
114 |
f.delete();
|
|
115 |
f.createNewFile();
|
|
116 |
}
|
|
117 |
|
|
118 |
bis = c.getResources().openRawResource(id);
|
|
119 |
bos = new BufferedOutputStream(new FileOutputStream(f), 1024);
|
|
120 |
int read = 0;
|
|
121 |
while((read = bis.read(buffer)) != -1){
|
|
122 |
bos.write(buffer, 0, read);
|
|
123 |
}
|
|
124 |
|
|
125 |
} catch (IOException e) {
|
|
126 |
e.printStackTrace();
|
|
127 |
}finally{
|
|
128 |
if(bis != null)
|
|
129 |
try {
|
|
130 |
bis.close();
|
|
131 |
} catch (IOException e) {
|
|
132 |
e.printStackTrace();
|
|
133 |
}
|
|
134 |
if(bos != null)
|
|
135 |
try {
|
|
136 |
bos.close();
|
|
137 |
} catch (IOException e) {
|
|
138 |
e.printStackTrace();
|
|
139 |
}
|
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|
|
143 |
}
|