project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
author Medo <smaxein@googlemail.com>
Wed, 11 Jul 2012 01:51:28 +0200
changeset 7326 1fae07363938
parent 7318 a446eafcddeb
child 7330 867e4fda496e
permissions -rw-r--r--
Hedgeroid: Simplified some code
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;
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
     9
import org.hedgewars.hedgeroid.Utils;
6724
03cd33624284 schemes/teams now get replaces on each version update
Xeli
parents: 6485
diff changeset
    10
import org.hedgewars.hedgeroid.Datastructures.Scheme;
03cd33624284 schemes/teams now get replaces on each version update
Xeli
parents: 6485
diff changeset
    11
import org.hedgewars.hedgeroid.Datastructures.Team;
03cd33624284 schemes/teams now get replaces on each version update
Xeli
parents: 6485
diff changeset
    12
import org.hedgewars.hedgeroid.Datastructures.Weapon;
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
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    18
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
    19
	private final MainActivity act;
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    20
	
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    21
	public DownloadAssets(MainActivity _act){
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    22
		act = _act;
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    23
	}
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    24
	
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
    25
	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
    26
		try {
7326
1fae07363938 Hedgeroid: Simplified some code
Medo <smaxein@googlemail.com>
parents: 7318
diff changeset
    27
			Utils.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
    28
		} 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
    29
			/*
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
			 * 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
    31
			 * 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
    32
			 * 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
    33
			 */
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
			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
    35
				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
    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
			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
    38
				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
    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
		}
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    41
	}
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    42
	
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
    43
	@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
    44
	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
    45
		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
    46
			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
    47
			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
    48
			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
    49
			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
    50
			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
    51
		} 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
    52
			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
    53
			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
    54
		}
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
	}
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
	@Override
6350
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    58
	protected void onPostExecute(Long result){
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    59
		act.onAssetsDownloaded(result == 0);
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    60
	}
41b0a9955c47 new download manager \o/
Xeli
parents:
diff changeset
    61
}