author | Medo <smaxein@googlemail.com> |
Thu, 05 Jul 2012 22:22:48 +0200 | |
changeset 7318 | a446eafcddeb |
parent 6724 | 03cd33624284 |
child 7326 | 1fae07363938 |
permissions | -rw-r--r-- |
6350 | 1 |
package org.hedgewars.hedgeroid.Downloader; |
2 |
||
3 |
import java.io.File; |
|
7318
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
4 |
import java.io.FileNotFoundException; |
6350 | 5 |
import java.io.FileOutputStream; |
6 |
import java.io.IOException; |
|
7 |
import java.io.InputStream; |
|
8 |
import java.io.OutputStream; |
|
9 |
||
10 |
import org.hedgewars.hedgeroid.MainActivity; |
|
6724
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
11 |
import org.hedgewars.hedgeroid.R; |
6350 | 12 |
import org.hedgewars.hedgeroid.Utils; |
6724
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
13 |
import org.hedgewars.hedgeroid.Datastructures.Scheme; |
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
14 |
import org.hedgewars.hedgeroid.Datastructures.Team; |
03cd33624284
schemes/teams now get replaces on each version update
Xeli
parents:
6485
diff
changeset
|
15 |
import org.hedgewars.hedgeroid.Datastructures.Weapon; |
6350 | 16 |
|
17 |
import android.content.res.AssetManager; |
|
18 |
import android.os.AsyncTask; |
|
19 |
import android.util.Log; |
|
20 |
||
21 |
public class DownloadAssets extends AsyncTask<Object, Long, Long>{ |
|
7318
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
22 |
private final MainActivity act; |
6350 | 23 |
|
24 |
public DownloadAssets(MainActivity _act){ |
|
25 |
act = _act; |
|
26 |
} |
|
27 |
||
7318
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
28 |
private static void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
29 |
try { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
30 |
copyFile(assetManager, target, assetPath); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
31 |
} catch(FileNotFoundException e) { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
32 |
/* |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
33 |
* I can't find a better way to figure out whether an asset entry is |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
34 |
* a file or a directory. Checking if assetManager.list(assetPath) |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
35 |
* is empty is a bit cleaner, but SLOW. |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
36 |
*/ |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
37 |
if (!target.isDirectory() && !target.mkdir()) { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
38 |
throw new IOException("Unable to create directory "+target); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
39 |
} |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
40 |
for (String asset : assetManager.list(assetPath)) { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
41 |
DownloadAssets.copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
42 |
} |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
43 |
} |
6350 | 44 |
} |
45 |
||
7318
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
46 |
private static void copyFile(AssetManager assetManager, File target, String assetPath) throws IOException { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
47 |
InputStream is = null; |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
48 |
OutputStream os = null; |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
49 |
byte[] buffer = new byte[8192]; |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
50 |
try { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
51 |
is = assetManager.open(assetPath); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
52 |
os = new FileOutputStream(target); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
53 |
int size; |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
54 |
while((size=is.read(buffer)) != -1) { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
55 |
os.write(buffer, 0, size); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
56 |
} |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
57 |
os.close(); // Important to close this non-quietly, in case of exceptions when flushing |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
58 |
} finally { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
59 |
Utils.closeQuietly(is); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
60 |
Utils.closeQuietly(os); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
61 |
} |
6350 | 62 |
} |
63 |
||
7318
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
64 |
@Override |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
65 |
protected Long doInBackground(Object... params) { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
66 |
try { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
67 |
Utils.resRawToFilesDir(act, R.array.schemes, Scheme.DIRECTORY_SCHEME); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
68 |
Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
69 |
Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
70 |
DownloadAssets.copyFileOrDir(act.getAssets(), Utils.getDataPathFile(act), "Data"); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
71 |
return 0l; |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
72 |
} catch(IOException e) { |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
73 |
Log.e("org.hedgewars.hedgeroid", e.getMessage(), e); |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
74 |
return 1l; |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
75 |
} |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
76 |
} |
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
77 |
|
a446eafcddeb
Improved asset moving time by an order of magnitude (3s vs. 25s on my device)
Medo <smaxein@googlemail.com>
parents:
6724
diff
changeset
|
78 |
@Override |
6350 | 79 |
protected void onPostExecute(Long result){ |
80 |
act.onAssetsDownloaded(result == 0); |
|
81 |
} |
|
82 |
} |