project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
changeset 10017 de822cd3df3a
parent 7584 7831c84cc644
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    34 import android.content.res.AssetManager;
    34 import android.content.res.AssetManager;
    35 import android.os.AsyncTask;
    35 import android.os.AsyncTask;
    36 import android.util.Log;
    36 import android.util.Log;
    37 
    37 
    38 public class DownloadAssets extends AsyncTask<Object, Long, Boolean> {
    38 public class DownloadAssets extends AsyncTask<Object, Long, Boolean> {
    39 	private static final String VERSION_FILENAME = "assetsversion.txt";
    39     private static final String VERSION_FILENAME = "assetsversion.txt";
    40 	private final MainActivity act;
    40     private final MainActivity act;
    41 	
    41 
    42 	public DownloadAssets(MainActivity act){
    42     public DownloadAssets(MainActivity act){
    43 		this.act = act;
    43         this.act = act;
    44 	}
    44     }
    45 	
    45 
    46 	private void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
    46     private void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
    47 		try {
    47         try {
    48 			FileUtils.writeStreamToFile(assetManager.open(assetPath), target);
    48             FileUtils.writeStreamToFile(assetManager.open(assetPath), target);
    49 		} catch(FileNotFoundException e) {
    49         } catch(FileNotFoundException e) {
    50 			/*
    50             /*
    51 			 * I can't find a better way to figure out whether an asset entry is
    51              * I can't find a better way to figure out whether an asset entry is
    52 			 * a file or a directory. Checking if assetManager.list(assetPath)
    52              * a file or a directory. Checking if assetManager.list(assetPath)
    53 			 * is empty is a bit cleaner, but SLOW.
    53              * is empty is a bit cleaner, but SLOW.
    54 			 */
    54              */
    55 			if (!target.isDirectory() && !target.mkdir()) {
    55             if (!target.isDirectory() && !target.mkdir()) {
    56 				throw new IOException("Unable to create directory "+target);
    56                 throw new IOException("Unable to create directory "+target);
    57 			}
    57             }
    58 			for (String asset : assetManager.list(assetPath)) {
    58             for (String asset : assetManager.list(assetPath)) {
    59 				copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
    59                 copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
    60 			}
    60             }
    61 		}
    61         }
    62 	}
    62     }
    63 	
    63 
    64 	@Override
    64     @Override
    65 	protected Boolean doInBackground(Object... params) {
    65     protected Boolean doInBackground(Object... params) {
    66 		try {
    66         try {
    67 			FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.schemes_builtin), Schemes.getBuiltinSchemesFile(act));
    67             FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.schemes_builtin), Schemes.getBuiltinSchemesFile(act));
    68 			FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.weapons_builtin), Weaponsets.getBuiltinWeaponsetsFile(act));
    68             FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.weapons_builtin), Weaponsets.getBuiltinWeaponsetsFile(act));
    69 			FileUtils.resRawToFilesDir(act, R.array.teams, R.array.teamFilenames, Team.DIRECTORY_TEAMS);
    69             FileUtils.resRawToFilesDir(act, R.array.teams, R.array.teamFilenames, Team.DIRECTORY_TEAMS);
    70 			copyFileOrDir(act.getAssets(), FileUtils.getDataPathFile(act), "Data");
    70             copyFileOrDir(act.getAssets(), FileUtils.getDataPathFile(act), "Data");
    71 			copyFileOrDir(act.getAssets(), new File(FileUtils.getCachePath(act), VERSION_FILENAME), VERSION_FILENAME);
    71             copyFileOrDir(act.getAssets(), new File(FileUtils.getCachePath(act), VERSION_FILENAME), VERSION_FILENAME);
    72 			return Boolean.TRUE;
    72             return Boolean.TRUE;
    73 		} catch(IOException e) {
    73         } catch(IOException e) {
    74 			Log.e("DownloadAssets", e.getMessage(), e);
    74             Log.e("DownloadAssets", e.getMessage(), e);
    75 			return Boolean.FALSE;
    75             return Boolean.FALSE;
    76 		}
    76         }
    77 	}
    77     }
    78 	
    78 
    79 	@Override
    79     @Override
    80 	protected void onPostExecute(Boolean result){
    80     protected void onPostExecute(Boolean result){
    81 		act.onAssetsDownloaded(result);
    81         act.onAssetsDownloaded(result);
    82 	}
    82     }
    83 }
    83 }