android: check if the sdcard directory is writable, if it isn't close the app
authorXeli
Mon, 28 Nov 2011 16:20:48 +0100
changeset 6459 2ca787ac8fc3
parent 6457 ef16d50d598e
child 6461 c476061f0466
android: check if the sdcard directory is writable, if it isn't close the app
project_files/Android-build/SDL-android-project/res/values/strings.xml
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
--- a/project_files/Android-build/SDL-android-project/res/values/strings.xml	Mon Nov 28 16:19:27 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/res/values/strings.xml	Mon Nov 28 16:20:48 2011 +0100
@@ -8,7 +8,8 @@
     <string name="saved">Saved succesfully</string>
     
     <!-- SDCARD -->
-    <string name="sdcard_not_mounted">There\'s been an error when accessing the sdcard, is it connected to another computer?</string>
+    <string name="sdcard_not_mounted_title">Sorry! Could not find the SDCard</string>
+    <string name="sdcard_not_mounted">There\'s been an error when accessing the SDcard. Please check if there is an SDcard present in the device (internal or external) and if the SDcard is not mounted (via usb to your computer for example). Hedgewars for Android will now quit</string>
         
     <!-- Notification -->
     <string name="notification_title">Downloading hedgewars files...</string>
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java	Mon Nov 28 16:19:27 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/MainActivity.java	Mon Nov 28 16:20:48 2011 +0100
@@ -19,11 +19,12 @@
 package org.hedgewars.hedgeroid;
 
 import org.hedgewars.hedgeroid.Downloader.DownloadAssets;
-import org.hedgewars.hedgeroid.Downloader.DownloadFragment;
 import org.hedgewars.hedgeroid.Downloader.DownloadListActivity;
-import org.hedgewars.hedgeroid.Downloader.DownloadService;
 
+import android.app.AlertDialog;
+import android.app.Dialog;
 import android.app.ProgressDialog;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.os.Bundle;
 import android.preference.PreferenceManager;
@@ -48,15 +49,34 @@
 		downloader.setOnClickListener(downloadClicker);
 		startGame.setOnClickListener(startGameClicker);
 
-		boolean assetsCopied = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("assetscopied", false);
+
+		String cacheDir = Utils.getCachePath(this);
+		if(cacheDir == null){
+			showDialog(0);
+		}else{
+			boolean assetsCopied = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("assetscopied", false);
 
-		if(!assetsCopied){
-			DownloadAssets assetsAsyncTask = new DownloadAssets(this);
-			assetsDialog = ProgressDialog.show(this, "Please wait a moment", "Moving assets...");
-			assetsAsyncTask.execute((Object[])null);
+			if(!assetsCopied){
+				DownloadAssets assetsAsyncTask = new DownloadAssets(this);
+				assetsDialog = ProgressDialog.show(this, "Please wait a moment", "Moving assets...");
+				assetsAsyncTask.execute((Object[])null);
+			}
 		}
 	}
 
+	public Dialog onCreateDialog(int id, Bundle args){
+		AlertDialog.Builder builder = new AlertDialog.Builder(this);
+		builder.setTitle(R.string.sdcard_not_mounted_title);
+		builder.setMessage(R.string.sdcard_not_mounted);
+		builder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener(){
+			public void onClick(DialogInterface dialog, int which) {
+				finish();				
+			}
+		});
+
+		return builder.create();
+	}
+
 	public void onAssetsDownloaded(boolean result){
 		if(result){
 			PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("assetscopied", true).commit();
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java	Mon Nov 28 16:19:27 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java	Mon Nov 28 16:20:48 2011 +0100
@@ -26,6 +26,7 @@
 import java.io.InputStream;
 import java.util.ArrayList;
 
+import android.app.Activity;
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.os.Build;
@@ -36,7 +37,7 @@
 public class Utils {
 
 	private static final String ROOT_DIR = "Data/";
-	
+
 	/**
 	 * get the path to which we should download all the data files
 	 * @param c context 
@@ -49,34 +50,32 @@
 			return FroyoSDCardDir.getDownloadPath(c) + '/';
 		}
 	}
-	
+
 	public static String getDataPath(Context c){
 		return getCachePath(c) + ROOT_DIR;
 	}
-	
+
 	static class FroyoSDCardDir{
 		public static String getDownloadPath(Context c){
 			File f =  c.getExternalCacheDir();
 			if(f != null){
 				return f.getAbsolutePath();
 			}else{
-				Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
 				return null;
 			}	
 		}
 	}
-	
+
 	static class PreFroyoSDCardDir{
 		public static String getDownloadPath(Context c){
 			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
 				if(Environment.getExternalStorageDirectory() != null)
 					return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/";				
 			}
-			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
 			return null;
 		}
 	}
-	
+
 	/**
 	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
 	 * @param dirName
@@ -86,15 +85,15 @@
 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
 		String prefix = getDataPath(c);
 		File f = new File(prefix + dirName);
-		
+
 		if(f.exists() && f.isDirectory()) return f.list();
 		else{
-			
+
 			Log.e("Utils::", "Couldn't find dir: " + dirName);
 			return new String[0];
 		}
 	}
-	
+
 	/**
 	 * Return a File array with all the files from dirName
 	 * @param c
@@ -104,14 +103,14 @@
 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
 		String prefix = getDataPath(c);
 		File f = new File(prefix + dirName);
-		
+
 		if(f.exists() && f.isDirectory()) return f.listFiles();
 		else {
 			Log.e("Utils::", "Dir not found: " + dirName);
 			return new File[0];
 		}
 	}
-	
+
 	/**
 	 * Checks if this directory has a file with suffix suffix
 	 * @param f - directory
@@ -127,7 +126,7 @@
 			return false;
 		}
 	}
-	
+
 	/**
 	 * Gives back all dirs which contain a file with suffix fileSuffix
 	 * @param c
@@ -139,7 +138,7 @@
 		File[] files = getFilesFromRelativeDir(c,path);
 		String[] validFiles = new String[files.length];
 		int validCounter = 0;
-		
+
 		for(File f : files){
 			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
 		}
@@ -147,7 +146,7 @@
 		System.arraycopy(validFiles, 0, ret, 0, validCounter);
 		return ret;
 	}
-	
+
 	/**
 	 * Get all files from directory dir which have the given suffix
 	 * @param c
@@ -167,13 +166,13 @@
 		}
 		return ret;
 	}
-	
-    /**
-     * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
-     * @param c
-     * @param sourceResId
-     * @param directory
-     */
+
+	/**
+	 * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
+	 * @param c
+	 * @param sourceResId
+	 * @param directory
+	 */
 	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
 		byte[] buffer = new byte[1024];
 		InputStream bis = null;
@@ -214,12 +213,12 @@
 					} catch (IOException e) {
 						e.printStackTrace();
 					}
-				if(bos != null)
-					try {
-						bos.close();
-					} catch (IOException e) {
-						e.printStackTrace();
-					}
+					if(bos != null)
+						try {
+							bos.close();
+						} catch (IOException e) {
+							e.printStackTrace();
+						}
 			}
 		}
 	}