author | sheepluva |
Fri, 27 Apr 2012 11:47:37 +0200 | |
changeset 6938 | 217ed62e872c |
parent 6724 | 03cd33624284 |
child 7318 | a446eafcddeb |
permissions | -rw-r--r-- |
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; |
|
6724
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
12 |
import org.hedgewars.hedgeroid.R; |
6350 | 13 |
import org.hedgewars.hedgeroid.Utils; |
6724
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
14 |
import org.hedgewars.hedgeroid.Datastructures.Scheme; |
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
15 |
import org.hedgewars.hedgeroid.Datastructures.Team; |
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
16 |
import org.hedgewars.hedgeroid.Datastructures.Weapon; |
6350 | 17 |
|
18 |
import android.content.Context; |
|
19 |
import android.content.res.AssetManager; |
|
20 |
import android.os.AsyncTask; |
|
21 |
import android.util.Log; |
|
22 |
||
23 |
public class DownloadAssets extends AsyncTask<Object, Long, Long>{ |
|
24 |
||
25 |
private MainActivity act; |
|
26 |
private static byte[] buffer = null; |
|
27 |
||
28 |
public DownloadAssets(MainActivity _act){ |
|
29 |
act = _act; |
|
30 |
} |
|
31 |
||
32 |
public static Long copyFileOrDir(Context c, String path) { |
|
33 |
AssetManager assetManager = c.getAssets(); |
|
34 |
String assets[] = null; |
|
35 |
try { |
|
36 |
assets = assetManager.list(path); |
|
37 |
if (assets.length == 0) { |
|
38 |
return DownloadAssets.copyFile(c, path); |
|
39 |
} else { |
|
40 |
String fullPath = Utils.getCachePath(c) + path; |
|
41 |
File dir = new File(fullPath); |
|
42 |
if (!dir.exists()) |
|
43 |
dir.mkdir(); |
|
44 |
for (int i = 0; i < assets.length; ++i) { |
|
45 |
Long result = DownloadAssets.copyFileOrDir(c, path + "/" + assets[i]); |
|
46 |
if(result > 0) return 1l; |
|
47 |
} |
|
48 |
} |
|
49 |
} catch (IOException ex) { |
|
50 |
ex.printStackTrace(); |
|
51 |
Log.e("tag", "I/O Exception", ex); |
|
52 |
return 1l; |
|
53 |
} |
|
54 |
return 0l; |
|
55 |
} |
|
56 |
||
57 |
private static Long copyFile(Context c, String filename) { |
|
58 |
AssetManager assetManager = c.getAssets(); |
|
59 |
||
60 |
InputStream in = null; |
|
61 |
OutputStream out = null; |
|
62 |
try { |
|
63 |
in = assetManager.open(filename); |
|
64 |
in = new BufferedInputStream(in, 8192); |
|
65 |
||
66 |
String newFileName = Utils.getCachePath(c) + filename; |
|
67 |
out = new FileOutputStream(newFileName); |
|
68 |
out = new BufferedOutputStream(out, 8192); |
|
69 |
||
70 |
int read; |
|
71 |
while ((read = in.read(buffer)) != -1) { |
|
72 |
out.write(buffer, 0, read); |
|
73 |
} |
|
74 |
in.close(); |
|
75 |
in = null; |
|
76 |
out.flush(); |
|
77 |
out.close(); |
|
78 |
out = null; |
|
79 |
} catch (Exception e) { |
|
80 |
e.printStackTrace(); |
|
81 |
Log.e("tag", e.getMessage()); |
|
82 |
return 1l; |
|
83 |
} |
|
84 |
return 0l; |
|
85 |
||
86 |
} |
|
87 |
||
88 |
protected Long doInBackground(Object... params) { |
|
6724
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
89 |
Utils.resRawToFilesDir(act,R.array.schemes, Scheme.DIRECTORY_SCHEME); |
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
90 |
Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON); |
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
91 |
Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS); |
6350 | 92 |
buffer = new byte[8192];//allocate the buffer |
93 |
return DownloadAssets.copyFileOrDir(act, "Data"); |
|
94 |
} |
|
95 |
||
96 |
protected void onPostExecute(Long result){ |
|
97 |
act.onAssetsDownloaded(result == 0); |
|
98 |
buffer = null; |
|
99 |
} |
|
100 |
} |