project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java
changeset 6456 fed715edc3ee
parent 6350 41b0a9955c47
child 6486 2a3ee24764bb
equal deleted inserted replaced
6454:24903d5c696d 6456:fed715edc3ee
    24 import java.io.FileOutputStream;
    24 import java.io.FileOutputStream;
    25 import java.io.IOException;
    25 import java.io.IOException;
    26 import java.io.InputStream;
    26 import java.io.InputStream;
    27 import java.util.ArrayList;
    27 import java.util.ArrayList;
    28 
    28 
       
    29 import android.app.Activity;
    29 import android.content.Context;
    30 import android.content.Context;
    30 import android.content.res.TypedArray;
    31 import android.content.res.TypedArray;
    31 import android.os.Build;
    32 import android.os.Build;
    32 import android.os.Environment;
    33 import android.os.Environment;
    33 import android.util.Log;
    34 import android.util.Log;
    34 import android.widget.Toast;
    35 import android.widget.Toast;
    35 
    36 
    36 public class Utils {
    37 public class Utils {
    37 
    38 
    38 	private static final String ROOT_DIR = "Data/";
    39 	private static final String ROOT_DIR = "Data/";
    39 	
    40 
    40 	/**
    41 	/**
    41 	 * get the path to which we should download all the data files
    42 	 * get the path to which we should download all the data files
    42 	 * @param c context 
    43 	 * @param c context 
    43 	 * @return absolute path
    44 	 * @return absolute path
    44 	 */
    45 	 */
    47 			return PreFroyoSDCardDir.getDownloadPath(c) + '/';
    48 			return PreFroyoSDCardDir.getDownloadPath(c) + '/';
    48 		}else{
    49 		}else{
    49 			return FroyoSDCardDir.getDownloadPath(c) + '/';
    50 			return FroyoSDCardDir.getDownloadPath(c) + '/';
    50 		}
    51 		}
    51 	}
    52 	}
    52 	
    53 
    53 	public static String getDataPath(Context c){
    54 	public static String getDataPath(Context c){
    54 		return getCachePath(c) + ROOT_DIR;
    55 		return getCachePath(c) + ROOT_DIR;
    55 	}
    56 	}
    56 	
    57 
    57 	static class FroyoSDCardDir{
    58 	static class FroyoSDCardDir{
    58 		public static String getDownloadPath(Context c){
    59 		public static String getDownloadPath(Context c){
    59 			File f =  c.getExternalCacheDir();
    60 			File f =  c.getExternalCacheDir();
    60 			if(f != null){
    61 			if(f != null){
    61 				return f.getAbsolutePath();
    62 				return f.getAbsolutePath();
    62 			}else{
    63 			}else{
    63 				Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
       
    64 				return null;
    64 				return null;
    65 			}	
    65 			}	
    66 		}
    66 		}
    67 	}
    67 	}
    68 	
    68 
    69 	static class PreFroyoSDCardDir{
    69 	static class PreFroyoSDCardDir{
    70 		public static String getDownloadPath(Context c){
    70 		public static String getDownloadPath(Context c){
    71 			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    71 			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    72 				if(Environment.getExternalStorageDirectory() != null)
    72 				if(Environment.getExternalStorageDirectory() != null)
    73 					return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/";				
    73 					return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/";				
    74 			}
    74 			}
    75 			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
       
    76 			return null;
    75 			return null;
    77 		}
    76 		}
    78 	}
    77 	}
    79 	
    78 
    80 	/**
    79 	/**
    81 	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
    80 	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
    82 	 * @param dirName
    81 	 * @param dirName
    83 	 * @param c context
    82 	 * @param c context
    84 	 * @return string of files
    83 	 * @return string of files
    85 	 */
    84 	 */
    86 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
    85 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
    87 		String prefix = getDataPath(c);
    86 		String prefix = getDataPath(c);
    88 		File f = new File(prefix + dirName);
    87 		File f = new File(prefix + dirName);
    89 		
    88 
    90 		if(f.exists() && f.isDirectory()) return f.list();
    89 		if(f.exists() && f.isDirectory()) return f.list();
    91 		else{
    90 		else{
    92 			
    91 
    93 			Log.e("Utils::", "Couldn't find dir: " + dirName);
    92 			Log.e("Utils::", "Couldn't find dir: " + dirName);
    94 			return new String[0];
    93 			return new String[0];
    95 		}
    94 		}
    96 	}
    95 	}
    97 	
    96 
    98 	/**
    97 	/**
    99 	 * Return a File array with all the files from dirName
    98 	 * Return a File array with all the files from dirName
   100 	 * @param c
    99 	 * @param c
   101 	 * @param dirName
   100 	 * @param dirName
   102 	 * @return
   101 	 * @return
   103 	 */
   102 	 */
   104 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
   103 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
   105 		String prefix = getDataPath(c);
   104 		String prefix = getDataPath(c);
   106 		File f = new File(prefix + dirName);
   105 		File f = new File(prefix + dirName);
   107 		
   106 
   108 		if(f.exists() && f.isDirectory()) return f.listFiles();
   107 		if(f.exists() && f.isDirectory()) return f.listFiles();
   109 		else {
   108 		else {
   110 			Log.e("Utils::", "Dir not found: " + dirName);
   109 			Log.e("Utils::", "Dir not found: " + dirName);
   111 			return new File[0];
   110 			return new File[0];
   112 		}
   111 		}
   113 	}
   112 	}
   114 	
   113 
   115 	/**
   114 	/**
   116 	 * Checks if this directory has a file with suffix suffix
   115 	 * Checks if this directory has a file with suffix suffix
   117 	 * @param f - directory
   116 	 * @param f - directory
   118 	 * @return
   117 	 * @return
   119 	 */
   118 	 */
   125 			return false;
   124 			return false;
   126 		}else{
   125 		}else{
   127 			return false;
   126 			return false;
   128 		}
   127 		}
   129 	}
   128 	}
   130 	
   129 
   131 	/**
   130 	/**
   132 	 * Gives back all dirs which contain a file with suffix fileSuffix
   131 	 * Gives back all dirs which contain a file with suffix fileSuffix
   133 	 * @param c
   132 	 * @param c
   134 	 * @param path
   133 	 * @param path
   135 	 * @param fileSuffix
   134 	 * @param fileSuffix
   137 	 */
   136 	 */
   138 	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
   137 	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
   139 		File[] files = getFilesFromRelativeDir(c,path);
   138 		File[] files = getFilesFromRelativeDir(c,path);
   140 		String[] validFiles = new String[files.length];
   139 		String[] validFiles = new String[files.length];
   141 		int validCounter = 0;
   140 		int validCounter = 0;
   142 		
   141 
   143 		for(File f : files){
   142 		for(File f : files){
   144 			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
   143 			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
   145 		}
   144 		}
   146 		String[] ret = new String[validCounter];
   145 		String[] ret = new String[validCounter];
   147 		System.arraycopy(validFiles, 0, ret, 0, validCounter);
   146 		System.arraycopy(validFiles, 0, ret, 0, validCounter);
   148 		return ret;
   147 		return ret;
   149 	}
   148 	}
   150 	
   149 
   151 	/**
   150 	/**
   152 	 * Get all files from directory dir which have the given suffix
   151 	 * Get all files from directory dir which have the given suffix
   153 	 * @param c
   152 	 * @param c
   154 	 * @param dir
   153 	 * @param dir
   155 	 * @param suffix
   154 	 * @param suffix
   165 				else ret.add(s);
   164 				else ret.add(s);
   166 			}
   165 			}
   167 		}
   166 		}
   168 		return ret;
   167 		return ret;
   169 	}
   168 	}
   170 	
   169 
   171     /**
   170 	/**
   172      * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
   171 	 * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
   173      * @param c
   172 	 * @param c
   174      * @param sourceResId
   173 	 * @param sourceResId
   175      * @param directory
   174 	 * @param directory
   176      */
   175 	 */
   177 	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
   176 	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
   178 		byte[] buffer = new byte[1024];
   177 		byte[] buffer = new byte[1024];
   179 		InputStream bis = null;
   178 		InputStream bis = null;
   180 		BufferedOutputStream bos = null;
   179 		BufferedOutputStream bos = null;
   181 		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
   180 		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
   212 					try { 
   211 					try { 
   213 						bis.close();
   212 						bis.close();
   214 					} catch (IOException e) {
   213 					} catch (IOException e) {
   215 						e.printStackTrace();
   214 						e.printStackTrace();
   216 					}
   215 					}
   217 				if(bos != null)
   216 					if(bos != null)
   218 					try {
   217 						try {
   219 						bos.close();
   218 							bos.close();
   220 					} catch (IOException e) {
   219 						} catch (IOException e) {
   221 						e.printStackTrace();
   220 							e.printStackTrace();
   222 					}
   221 						}
   223 			}
   222 			}
   224 		}
   223 		}
   225 	}
   224 	}
   226 }
   225 }