13 |
13 |
14 import android.content.res.AssetManager; |
14 import android.content.res.AssetManager; |
15 import android.os.AsyncTask; |
15 import android.os.AsyncTask; |
16 import android.util.Log; |
16 import android.util.Log; |
17 |
17 |
18 public class DownloadAssets extends AsyncTask<Object, Long, Long>{ |
18 public class DownloadAssets extends AsyncTask<Object, Long, Boolean> { |
19 private final MainActivity act; |
19 private final MainActivity act; |
20 |
20 |
21 public DownloadAssets(MainActivity _act){ |
21 public DownloadAssets(MainActivity act){ |
22 act = _act; |
22 this.act = act; |
23 } |
23 } |
24 |
24 |
25 private static void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException { |
25 private void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException { |
26 try { |
26 try { |
27 Utils.writeStreamToFile(assetManager.open(assetPath), target); |
27 Utils.writeStreamToFile(assetManager.open(assetPath), target); |
28 } catch(FileNotFoundException e) { |
28 } catch(FileNotFoundException e) { |
29 /* |
29 /* |
30 * I can't find a better way to figure out whether an asset entry is |
30 * I can't find a better way to figure out whether an asset entry is |
33 */ |
33 */ |
34 if (!target.isDirectory() && !target.mkdir()) { |
34 if (!target.isDirectory() && !target.mkdir()) { |
35 throw new IOException("Unable to create directory "+target); |
35 throw new IOException("Unable to create directory "+target); |
36 } |
36 } |
37 for (String asset : assetManager.list(assetPath)) { |
37 for (String asset : assetManager.list(assetPath)) { |
38 DownloadAssets.copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset); |
38 copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset); |
39 } |
39 } |
40 } |
40 } |
41 } |
41 } |
42 |
42 |
43 @Override |
43 @Override |
44 protected Long doInBackground(Object... params) { |
44 protected Boolean doInBackground(Object... params) { |
45 try { |
45 try { |
46 Utils.resRawToFilesDir(act, R.array.schemes, Scheme.DIRECTORY_SCHEME); |
46 Utils.resRawToFilesDir(act, R.array.schemes, Scheme.DIRECTORY_SCHEME); |
47 Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON); |
47 Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON); |
48 Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS); |
48 Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS); |
49 DownloadAssets.copyFileOrDir(act.getAssets(), Utils.getDataPathFile(act), "Data"); |
49 copyFileOrDir(act.getAssets(), Utils.getDataPathFile(act), "Data"); |
50 return 0l; |
50 return Boolean.TRUE; |
51 } catch(IOException e) { |
51 } catch(IOException e) { |
52 Log.e("org.hedgewars.hedgeroid", e.getMessage(), e); |
52 Log.e("DownloadAssets", e.getMessage(), e); |
53 return 1l; |
53 return Boolean.FALSE; |
54 } |
54 } |
55 } |
55 } |
56 |
56 |
57 @Override |
57 @Override |
58 protected void onPostExecute(Long result){ |
58 protected void onPostExecute(Boolean result){ |
59 act.onAssetsDownloaded(result == 0); |
59 act.onAssetsDownloaded(result); |
60 } |
60 } |
61 } |
61 } |