Hedgeroid: Modified detection of assets on SD card (should be more reliable)
authorMedo <smaxein@googlemail.com>
Thu, 19 Jul 2012 22:53:39 +0200
changeset 7344 25b8906f901a
parent 7342 0e29eec2df5c
child 7346 b0f67c5b4215
Hedgeroid: Modified detection of assets on SD card (should be more reliable)
project_files/Android-build/SDL-android-project/assets/assetsversion.txt
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/assets/assetsversion.txt	Thu Jul 19 22:53:39 2012 +0200
@@ -0,0 +1,1 @@
+1
\ No newline at end of file
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java	Thu Jul 19 18:58:18 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java	Thu Jul 19 22:53:39 2012 +0200
@@ -16,6 +16,7 @@
 import android.util.Log;
 
 public class DownloadAssets extends AsyncTask<Object, Long, Boolean> {
+	private static final String VERSION_FILENAME = "assetsversion.txt";
 	private final MainActivity act;
 	
 	public DownloadAssets(MainActivity act){
@@ -47,6 +48,7 @@
 			Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON);
 			Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS);
 			copyFileOrDir(act.getAssets(), Utils.getDataPathFile(act), "Data");
+			copyFileOrDir(act.getAssets(), new File(Utils.getCachePath(act), VERSION_FILENAME), VERSION_FILENAME);
 			return Boolean.TRUE;
 		} catch(IOException e) {
 			Log.e("DownloadAssets", e.getMessage(), e);
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java	Thu Jul 19 18:58:18 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java	Thu Jul 19 22:53:39 2012 +0200
@@ -18,6 +18,10 @@
 
 package org.hedgewars.hedgeroid;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
 import org.hedgewars.hedgeroid.Downloader.DownloadAssets;
 import org.hedgewars.hedgeroid.Downloader.DownloadListActivity;
 import org.hedgewars.hedgeroid.netplay.LobbyActivity;
@@ -32,6 +36,7 @@
 import android.os.Bundle;
 import android.preference.PreferenceManager;
 import android.support.v4.app.FragmentActivity;
+import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.Button;
@@ -60,15 +65,20 @@
 		if(!Utils.isDataPathAvailable()){
 			showDialog(0);
 		} else {
-			int versionCode = 0;
+			String existingVersion = "";
 			try {
-				versionCode = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
-			} catch (NameNotFoundException e) {
-
+				File versionFile = new File(Utils.getCachePath(this), "assetsversion.txt");
+				existingVersion = Utils.readToString(new FileInputStream(versionFile));
+			} catch(IOException e) {
 			}
-			boolean assetsCopied = PreferenceManager.getDefaultSharedPreferences(this).getInt("latestAssets", 0) >= versionCode;
-
-			if(!assetsCopied){
+			
+			String newVersion = "";
+			try {
+				newVersion = Utils.readToString(getAssets().open("assetsversion.txt"));
+			} catch(IOException e) {
+			}
+			
+			if(!existingVersion.equals(newVersion)) {
 				DownloadAssets assetsAsyncTask = new DownloadAssets(this);
 				assetsDialog = ProgressDialog.show(this, "Please wait a moment", "Moving assets to SD card...");
 				assetsAsyncTask.execute();
@@ -90,21 +100,14 @@
 	}
 
 	public void onAssetsDownloaded(boolean result){
-		if(result){
-			try {
-				int versionCode = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;
-				PreferenceManager.getDefaultSharedPreferences(this).edit().putInt("latestAssets", versionCode).commit();
-			} catch (NameNotFoundException e) {}
-			
-		}else{
-			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG);
+		if(!result){
+			Toast.makeText(this, R.string.download_failed, Toast.LENGTH_LONG).show();
 		}
 		assetsDialog.dismiss();
 	}
 
 	private OnClickListener downloadClicker = new OnClickListener(){
 		public void onClick(View v){
-			//startActivityForResult(new Intent(getApplicationContext(), DownloadActivity.class), 0);
 			startActivityForResult(new Intent(getApplicationContext(), DownloadListActivity.class), 0);
 		}
 	};
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java	Thu Jul 19 18:58:18 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java	Thu Jul 19 22:53:39 2012 +0200
@@ -19,6 +19,7 @@
 
 package org.hedgewars.hedgeroid;
 
+import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -30,7 +31,6 @@
 import java.util.List;
 
 import android.annotation.TargetApi;
-import android.app.Application;
 import android.content.Context;
 import android.content.res.Resources;
 import android.content.res.TypedArray;
@@ -256,4 +256,18 @@
 			throw new RuntimeException("Result is not zero: " + text);
 		}
 	}
+
+	public static String readToString(InputStream is) throws IOException {
+		try {
+			ByteArrayOutputStream os = new ByteArrayOutputStream();
+			byte[] buffer = new byte[8192];
+			int size;
+			while((size=is.read(buffer)) != -1) {
+				os.write(buffer, 0, size);
+			}
+			return new String(os.toByteArray());
+		} finally {
+			closeQuietly(is);
+		}
+	}
 }