6350
|
1 |
package org.hedgewars.hedgeroid.Downloader;
|
|
2 |
|
|
3 |
import java.io.BufferedInputStream;
|
|
4 |
import java.io.BufferedOutputStream;
|
|
5 |
import java.io.File;
|
|
6 |
import java.io.FileOutputStream;
|
|
7 |
import java.io.IOException;
|
|
8 |
import java.io.InputStream;
|
|
9 |
import java.io.OutputStream;
|
|
10 |
|
|
11 |
import org.hedgewars.hedgeroid.MainActivity;
|
|
12 |
import org.hedgewars.hedgeroid.Utils;
|
|
13 |
|
|
14 |
import android.content.Context;
|
|
15 |
import android.content.res.AssetManager;
|
|
16 |
import android.os.AsyncTask;
|
|
17 |
import android.util.Log;
|
|
18 |
|
|
19 |
public class DownloadAssets extends AsyncTask<Object, Long, Long>{
|
|
20 |
|
|
21 |
private MainActivity act;
|
|
22 |
private static byte[] buffer = null;
|
|
23 |
|
|
24 |
public DownloadAssets(MainActivity _act){
|
|
25 |
act = _act;
|
|
26 |
}
|
|
27 |
|
|
28 |
public static Long copyFileOrDir(Context c, String path) {
|
|
29 |
AssetManager assetManager = c.getAssets();
|
|
30 |
String assets[] = null;
|
|
31 |
try {
|
|
32 |
assets = assetManager.list(path);
|
|
33 |
if (assets.length == 0) {
|
|
34 |
return DownloadAssets.copyFile(c, path);
|
|
35 |
} else {
|
|
36 |
String fullPath = Utils.getCachePath(c) + path;
|
|
37 |
File dir = new File(fullPath);
|
|
38 |
if (!dir.exists())
|
|
39 |
dir.mkdir();
|
|
40 |
for (int i = 0; i < assets.length; ++i) {
|
|
41 |
Long result = DownloadAssets.copyFileOrDir(c, path + "/" + assets[i]);
|
|
42 |
if(result > 0) return 1l;
|
|
43 |
}
|
|
44 |
}
|
|
45 |
} catch (IOException ex) {
|
|
46 |
ex.printStackTrace();
|
|
47 |
Log.e("tag", "I/O Exception", ex);
|
|
48 |
return 1l;
|
|
49 |
}
|
|
50 |
return 0l;
|
|
51 |
}
|
|
52 |
|
|
53 |
private static Long copyFile(Context c, String filename) {
|
|
54 |
AssetManager assetManager = c.getAssets();
|
|
55 |
|
|
56 |
InputStream in = null;
|
|
57 |
OutputStream out = null;
|
|
58 |
try {
|
|
59 |
in = assetManager.open(filename);
|
|
60 |
in = new BufferedInputStream(in, 8192);
|
|
61 |
|
|
62 |
String newFileName = Utils.getCachePath(c) + filename;
|
|
63 |
out = new FileOutputStream(newFileName);
|
|
64 |
out = new BufferedOutputStream(out, 8192);
|
|
65 |
|
|
66 |
int read;
|
|
67 |
while ((read = in.read(buffer)) != -1) {
|
|
68 |
out.write(buffer, 0, read);
|
|
69 |
}
|
|
70 |
in.close();
|
|
71 |
in = null;
|
|
72 |
out.flush();
|
|
73 |
out.close();
|
|
74 |
out = null;
|
|
75 |
} catch (Exception e) {
|
|
76 |
e.printStackTrace();
|
|
77 |
Log.e("tag", e.getMessage());
|
|
78 |
return 1l;
|
|
79 |
}
|
|
80 |
return 0l;
|
|
81 |
|
|
82 |
}
|
|
83 |
|
|
84 |
protected Long doInBackground(Object... params) {
|
|
85 |
buffer = new byte[8192];//allocate the buffer
|
|
86 |
return DownloadAssets.copyFileOrDir(act, "Data");
|
|
87 |
}
|
|
88 |
|
|
89 |
protected void onPostExecute(Long result){
|
|
90 |
act.onAssetsDownloaded(result == 0);
|
|
91 |
buffer = null;
|
|
92 |
}
|
|
93 |
}
|