--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java Thu Jul 05 00:33:24 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java Thu Jul 05 22:22:48 2012 +0200
@@ -1,8 +1,7 @@
package org.hedgewars.hedgeroid.Downloader;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -15,86 +14,69 @@
import org.hedgewars.hedgeroid.Datastructures.Team;
import org.hedgewars.hedgeroid.Datastructures.Weapon;
-import android.content.Context;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.util.Log;
public class DownloadAssets extends AsyncTask<Object, Long, Long>{
-
- private MainActivity act;
- private static byte[] buffer = null;
+ private final MainActivity act;
public DownloadAssets(MainActivity _act){
act = _act;
}
- public static Long copyFileOrDir(Context c, String path) {
- AssetManager assetManager = c.getAssets();
- String assets[] = null;
- try {
- assets = assetManager.list(path);
- if (assets.length == 0) {
- return DownloadAssets.copyFile(c, path);
- } else {
- String fullPath = Utils.getCachePath(c) + path;
- File dir = new File(fullPath);
- if (!dir.exists())
- dir.mkdir();
- for (int i = 0; i < assets.length; ++i) {
- Long result = DownloadAssets.copyFileOrDir(c, path + "/" + assets[i]);
- if(result > 0) return 1l;
- }
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- Log.e("tag", "I/O Exception", ex);
- return 1l;
- }
- return 0l;
+ private static void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
+ try {
+ copyFile(assetManager, target, assetPath);
+ } catch(FileNotFoundException e) {
+ /*
+ * I can't find a better way to figure out whether an asset entry is
+ * a file or a directory. Checking if assetManager.list(assetPath)
+ * is empty is a bit cleaner, but SLOW.
+ */
+ if (!target.isDirectory() && !target.mkdir()) {
+ throw new IOException("Unable to create directory "+target);
+ }
+ for (String asset : assetManager.list(assetPath)) {
+ DownloadAssets.copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
+ }
+ }
}
- private static Long copyFile(Context c, String filename) {
- AssetManager assetManager = c.getAssets();
-
- InputStream in = null;
- OutputStream out = null;
- try {
- in = assetManager.open(filename);
- in = new BufferedInputStream(in, 8192);
-
- String newFileName = Utils.getCachePath(c) + filename;
- out = new FileOutputStream(newFileName);
- out = new BufferedOutputStream(out, 8192);
-
- int read;
- while ((read = in.read(buffer)) != -1) {
- out.write(buffer, 0, read);
- }
- in.close();
- in = null;
- out.flush();
- out.close();
- out = null;
- } catch (Exception e) {
- e.printStackTrace();
- Log.e("tag", e.getMessage());
- return 1l;
- }
- return 0l;
-
- }
-
- protected Long doInBackground(Object... params) {
- Utils.resRawToFilesDir(act,R.array.schemes, Scheme.DIRECTORY_SCHEME);
- Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON);
- Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS);
- buffer = new byte[8192];//allocate the buffer
- return DownloadAssets.copyFileOrDir(act, "Data");
+ private static void copyFile(AssetManager assetManager, File target, String assetPath) throws IOException {
+ InputStream is = null;
+ OutputStream os = null;
+ byte[] buffer = new byte[8192];
+ try {
+ is = assetManager.open(assetPath);
+ os = new FileOutputStream(target);
+ int size;
+ while((size=is.read(buffer)) != -1) {
+ os.write(buffer, 0, size);
+ }
+ os.close(); // Important to close this non-quietly, in case of exceptions when flushing
+ } finally {
+ Utils.closeQuietly(is);
+ Utils.closeQuietly(os);
+ }
}
+ @Override
+ protected Long doInBackground(Object... params) {
+ try {
+ Utils.resRawToFilesDir(act, R.array.schemes, Scheme.DIRECTORY_SCHEME);
+ Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON);
+ Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS);
+ DownloadAssets.copyFileOrDir(act.getAssets(), Utils.getDataPathFile(act), "Data");
+ return 0l;
+ } catch(IOException e) {
+ Log.e("org.hedgewars.hedgeroid", e.getMessage(), e);
+ return 1l;
+ }
+ }
+
+ @Override
protected void onPostExecute(Long result){
act.onAssetsDownloaded(result == 0);
- buffer = null;
}
}