project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
author Medo <smaxein@googlemail.com>
Sat, 18 Aug 2012 00:47:51 +0200
changeset 7508 763d3961400b
parent 7485 0481bd74267c
child 7584 7831c84cc644
permissions -rw-r--r--
Hedgeroid: Frantic scrabbling toward the deadline - Added activities for weapon/scheme editors (unfinished) - Completed tablet version of netroom activity - Added map preview - Fixed default team files having the wrong names - Restructuring - Updated frontlib JNA bindings to respect the latest frontlib changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
     1
package org.hedgewars.hedgeroid.Downloader;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
     2
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
     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
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
     5
import java.io.IOException;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
     6
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
     7
import org.hedgewars.hedgeroid.MainActivity;
6724
03cd33624284 schemes/teams now get replaces on each version update
Xeli
parents: 6485
diff changeset
     8
import org.hedgewars.hedgeroid.R;
7476
2fb781bbdd51 Hedgeroid: Start using the frontlib for more operations
Medo <smaxein@googlemail.com>
parents: 7344
diff changeset
     9
import org.hedgewars.hedgeroid.Datastructures.Schemes;
6724
03cd33624284 schemes/teams now get replaces on each version update
Xeli
parents: 6485
diff changeset
    10
import org.hedgewars.hedgeroid.Datastructures.Team;
7485
0481bd74267c Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7476
diff changeset
    11
import org.hedgewars.hedgeroid.Datastructures.Weaponsets;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    12
import org.hedgewars.hedgeroid.util.FileUtils;
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    13
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    14
import android.content.res.AssetManager;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    15
import android.os.AsyncTask;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    16
import android.util.Log;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    17
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    18
public class DownloadAssets extends AsyncTask<Object, Long, Boolean> {
7344
25b8906f901a Hedgeroid: Modified detection of assets on SD card (should be more reliable)
Medo <smaxein@googlemail.com>
parents: 7330
diff changeset
    19
	private static final String VERSION_FILENAME = "assetsversion.txt";
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
    20
	private final MainActivity act;
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    21
	
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    22
	public DownloadAssets(MainActivity act){
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    23
		this.act = act;
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    24
	}
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    25
	
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    26
	private void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
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
    27
		try {
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    28
			FileUtils.writeStreamToFile(assetManager.open(assetPath), target);
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
    29
		} 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
    30
			/*
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
			 * 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
    32
			 * 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
    33
			 * 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
    34
			 */
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
			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
    36
				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
    37
			}
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
			for (String asset : assetManager.list(assetPath)) {
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    39
				copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
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
    40
			}
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
		}
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    42
	}
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    43
	
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
    44
	@Override
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    45
	protected Boolean doInBackground(Object... params) {
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
		try {
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    47
			FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.schemes_builtin), Schemes.getBuiltinSchemesFile(act));
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    48
			FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.weapons_builtin), Weaponsets.getBuiltinWeaponsetsFile(act));
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    49
			FileUtils.resRawToFilesDir(act, R.array.teams, R.array.teamFilenames, Team.DIRECTORY_TEAMS);
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    50
			copyFileOrDir(act.getAssets(), FileUtils.getDataPathFile(act), "Data");
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents: 7485
diff changeset
    51
			copyFileOrDir(act.getAssets(), new File(FileUtils.getCachePath(act), VERSION_FILENAME), VERSION_FILENAME);
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    52
			return Boolean.TRUE;
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
    53
		} catch(IOException e) {
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    54
			Log.e("DownloadAssets", e.getMessage(), e);
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    55
			return Boolean.FALSE;
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
    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
	}
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
	
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
	@Override
7330
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    60
	protected void onPostExecute(Boolean result){
867e4fda496e Hedgeroid: Layout experiments for the lobby page
Medo <smaxein@googlemail.com>
parents: 7326
diff changeset
    61
		act.onAssetsDownloaded(result);
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    62
	}
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    63
}