13 import org.hedgewars.hedgeroid.Utils; |
12 import org.hedgewars.hedgeroid.Utils; |
14 import org.hedgewars.hedgeroid.Datastructures.Scheme; |
13 import org.hedgewars.hedgeroid.Datastructures.Scheme; |
15 import org.hedgewars.hedgeroid.Datastructures.Team; |
14 import org.hedgewars.hedgeroid.Datastructures.Team; |
16 import org.hedgewars.hedgeroid.Datastructures.Weapon; |
15 import org.hedgewars.hedgeroid.Datastructures.Weapon; |
17 |
16 |
18 import android.content.Context; |
|
19 import android.content.res.AssetManager; |
17 import android.content.res.AssetManager; |
20 import android.os.AsyncTask; |
18 import android.os.AsyncTask; |
21 import android.util.Log; |
19 import android.util.Log; |
22 |
20 |
23 public class DownloadAssets extends AsyncTask<Object, Long, Long>{ |
21 public class DownloadAssets extends AsyncTask<Object, Long, Long>{ |
24 |
22 private final MainActivity act; |
25 private MainActivity act; |
|
26 private static byte[] buffer = null; |
|
27 |
23 |
28 public DownloadAssets(MainActivity _act){ |
24 public DownloadAssets(MainActivity _act){ |
29 act = _act; |
25 act = _act; |
30 } |
26 } |
31 |
27 |
32 public static Long copyFileOrDir(Context c, String path) { |
28 private static void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException { |
33 AssetManager assetManager = c.getAssets(); |
29 try { |
34 String assets[] = null; |
30 copyFile(assetManager, target, assetPath); |
35 try { |
31 } catch(FileNotFoundException e) { |
36 assets = assetManager.list(path); |
32 /* |
37 if (assets.length == 0) { |
33 * I can't find a better way to figure out whether an asset entry is |
38 return DownloadAssets.copyFile(c, path); |
34 * a file or a directory. Checking if assetManager.list(assetPath) |
39 } else { |
35 * is empty is a bit cleaner, but SLOW. |
40 String fullPath = Utils.getCachePath(c) + path; |
36 */ |
41 File dir = new File(fullPath); |
37 if (!target.isDirectory() && !target.mkdir()) { |
42 if (!dir.exists()) |
38 throw new IOException("Unable to create directory "+target); |
43 dir.mkdir(); |
39 } |
44 for (int i = 0; i < assets.length; ++i) { |
40 for (String asset : assetManager.list(assetPath)) { |
45 Long result = DownloadAssets.copyFileOrDir(c, path + "/" + assets[i]); |
41 DownloadAssets.copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset); |
46 if(result > 0) return 1l; |
42 } |
47 } |
43 } |
48 } |
|
49 } catch (IOException ex) { |
|
50 ex.printStackTrace(); |
|
51 Log.e("tag", "I/O Exception", ex); |
|
52 return 1l; |
|
53 } |
|
54 return 0l; |
|
55 } |
44 } |
56 |
45 |
57 private static Long copyFile(Context c, String filename) { |
46 private static void copyFile(AssetManager assetManager, File target, String assetPath) throws IOException { |
58 AssetManager assetManager = c.getAssets(); |
47 InputStream is = null; |
59 |
48 OutputStream os = null; |
60 InputStream in = null; |
49 byte[] buffer = new byte[8192]; |
61 OutputStream out = null; |
50 try { |
62 try { |
51 is = assetManager.open(assetPath); |
63 in = assetManager.open(filename); |
52 os = new FileOutputStream(target); |
64 in = new BufferedInputStream(in, 8192); |
53 int size; |
65 |
54 while((size=is.read(buffer)) != -1) { |
66 String newFileName = Utils.getCachePath(c) + filename; |
55 os.write(buffer, 0, size); |
67 out = new FileOutputStream(newFileName); |
56 } |
68 out = new BufferedOutputStream(out, 8192); |
57 os.close(); // Important to close this non-quietly, in case of exceptions when flushing |
69 |
58 } finally { |
70 int read; |
59 Utils.closeQuietly(is); |
71 while ((read = in.read(buffer)) != -1) { |
60 Utils.closeQuietly(os); |
72 out.write(buffer, 0, read); |
61 } |
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) { |
|
89 Utils.resRawToFilesDir(act,R.array.schemes, Scheme.DIRECTORY_SCHEME); |
|
90 Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON); |
|
91 Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS); |
|
92 buffer = new byte[8192];//allocate the buffer |
|
93 return DownloadAssets.copyFileOrDir(act, "Data"); |
|
94 } |
62 } |
95 |
63 |
|
64 @Override |
|
65 protected Long doInBackground(Object... params) { |
|
66 try { |
|
67 Utils.resRawToFilesDir(act, R.array.schemes, Scheme.DIRECTORY_SCHEME); |
|
68 Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON); |
|
69 Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS); |
|
70 DownloadAssets.copyFileOrDir(act.getAssets(), Utils.getDataPathFile(act), "Data"); |
|
71 return 0l; |
|
72 } catch(IOException e) { |
|
73 Log.e("org.hedgewars.hedgeroid", e.getMessage(), e); |
|
74 return 1l; |
|
75 } |
|
76 } |
|
77 |
|
78 @Override |
96 protected void onPostExecute(Long result){ |
79 protected void onPostExecute(Long result){ |
97 act.onAssetsDownloaded(result == 0); |
80 act.onAssetsDownloaded(result == 0); |
98 buffer = null; |
|
99 } |
81 } |
100 } |
82 } |