project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java
branchhedgeroid
changeset 6350 41b0a9955c47
parent 6049 7bc38086d771
child 6456 fed715edc3ee
equal deleted inserted replaced
6348:162fec525764 6350:41b0a9955c47
    28 
    28 
    29 import android.content.Context;
    29 import android.content.Context;
    30 import android.content.res.TypedArray;
    30 import android.content.res.TypedArray;
    31 import android.os.Build;
    31 import android.os.Build;
    32 import android.os.Environment;
    32 import android.os.Environment;
       
    33 import android.util.Log;
    33 import android.widget.Toast;
    34 import android.widget.Toast;
    34 
    35 
    35 public class Utils {
    36 public class Utils {
    36 
    37 
       
    38 	private static final String ROOT_DIR = "Data/";
    37 	
    39 	
    38 	/**
    40 	/**
    39 	 * get the path to which we should download all the data files
    41 	 * get the path to which we should download all the data files
    40 	 * @param c context 
    42 	 * @param c context 
    41 	 * @return absolute path
    43 	 * @return absolute path
    42 	 */
    44 	 */
    43 	public static String getDownloadPath(Context c){
    45 	public static String getCachePath(Context c){
    44 		if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
    46 		if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
    45 			return PreFroyoSDCardDir.getDownloadPath(c);
    47 			return PreFroyoSDCardDir.getDownloadPath(c) + '/';
    46 		}else{
    48 		}else{
    47 			return FroyoSDCardDir.getDownloadPath(c);
    49 			return FroyoSDCardDir.getDownloadPath(c) + '/';
    48 		}
    50 		}
       
    51 	}
       
    52 	
       
    53 	public static String getDataPath(Context c){
       
    54 		return getCachePath(c) + ROOT_DIR;
    49 	}
    55 	}
    50 	
    56 	
    51 	static class FroyoSDCardDir{
    57 	static class FroyoSDCardDir{
    52 		public static String getDownloadPath(Context c){
    58 		public static String getDownloadPath(Context c){
    53 			File f =  c.getExternalCacheDir();
    59 			File f =  c.getExternalCacheDir();
    54 			if(f != null){
    60 			if(f != null){
    55 				return f.getAbsolutePath() + "/Data/";
    61 				return f.getAbsolutePath();
    56 			}else{
    62 			}else{
    57 				Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
    63 				Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
    58 				return null;
    64 				return null;
    59 			}	
    65 			}	
    60 		}
    66 		}
    76 	 * @param dirName
    82 	 * @param dirName
    77 	 * @param c context
    83 	 * @param c context
    78 	 * @return string of files
    84 	 * @return string of files
    79 	 */
    85 	 */
    80 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
    86 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
    81 		String prefix = getDownloadPath(c);
    87 		String prefix = getDataPath(c);
    82 		File f = new File(prefix + dirName);
    88 		File f = new File(prefix + dirName);
    83 		
    89 		
    84 		if(f.exists() && f.isDirectory()) return f.list();
    90 		if(f.exists() && f.isDirectory()) return f.list();
    85 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
    91 		else{
       
    92 			
       
    93 			Log.e("Utils::", "Couldn't find dir: " + dirName);
       
    94 			return new String[0];
       
    95 		}
    86 	}
    96 	}
    87 	
    97 	
    88 	/**
    98 	/**
    89 	 * Return a File array with all the files from dirName
    99 	 * Return a File array with all the files from dirName
    90 	 * @param c
   100 	 * @param c
    91 	 * @param dirName
   101 	 * @param dirName
    92 	 * @return
   102 	 * @return
    93 	 */
   103 	 */
    94 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
   104 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
    95 		String prefix = getDownloadPath(c);
   105 		String prefix = getDataPath(c);
    96 		File f = new File(prefix + dirName);
   106 		File f = new File(prefix + dirName);
    97 		
   107 		
    98 		if(f.exists() && f.isDirectory()) return f.listFiles();
   108 		if(f.exists() && f.isDirectory()) return f.listFiles();
    99 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
   109 		else {
       
   110 			Log.e("Utils::", "Dir not found: " + dirName);
       
   111 			return new File[0];
       
   112 		}
   100 	}
   113 	}
   101 	
   114 	
   102 	/**
   115 	/**
   103 	 * Checks if this directory has a file with suffix suffix
   116 	 * Checks if this directory has a file with suffix suffix
   104 	 * @param f - directory
   117 	 * @param f - directory