project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java
changeset 7485 0481bd74267c
parent 7476 2fb781bbdd51
equal deleted inserted replaced
7482:d70a5b0d1190 7485:0481bd74267c
    52 	
    52 	
    53 	/**
    53 	/**
    54 	 * get the path to which we should download all the data files
    54 	 * get the path to which we should download all the data files
    55 	 * @param c context 
    55 	 * @param c context 
    56 	 * @return The directory
    56 	 * @return The directory
    57 	 * @throws FileNotFoundException if external storage is not avaliable at the moment
    57 	 * @throws FileNotFoundException if external storage is not available at the moment
    58 	 */
    58 	 */
    59 	public static File getCachePath(Context c) throws FileNotFoundException {
    59 	public static File getCachePath(Context c) throws FileNotFoundException {
    60 		File cachePath = null;
    60 		File cachePath = null;
    61 		if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
    61 		if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
    62 			cachePath = PreFroyoSDCardDir.getDownloadPath(c);
    62 			cachePath = PreFroyoSDCardDir.getDownloadPath(c);
   103 			return null;
   103 			return null;
   104 		}
   104 		}
   105 	}
   105 	}
   106 
   106 
   107 	/**
   107 	/**
   108 	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
       
   109 	 * @param dirName
       
   110 	 * @param c context
       
   111 	 * @return string of files
       
   112 	 */
       
   113 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
       
   114 		String prefix = getDataPath(c);
       
   115 		File f = new File(prefix + dirName);
       
   116 
       
   117 		if(f.exists() && f.isDirectory()) return f.list();
       
   118 		else{
       
   119 
       
   120 			Log.e("Utils::", "Couldn't find dir: " + dirName);
       
   121 			return new String[0];
       
   122 		}
       
   123 	}
       
   124 
       
   125 	/**
       
   126 	 * Return a File array with all the files from dirName
   108 	 * Return a File array with all the files from dirName
   127 	 * @param c
   109 	 * @param c
   128 	 * @param dirName
   110 	 * @param dirName
   129 	 * @return
   111 	 * @return
   130 	 */
   112 	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dirName" does not exist
   131 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
   113 	 */
   132 		String prefix = getDataPath(c);
   114 	public static File[] getFilesFromRelativeDir(Context c, String dirName) throws FileNotFoundException {
   133 		File f = new File(prefix + dirName);
   115 		File f = new File(getDataPathFile(c), dirName);
   134 
   116 
   135 		if(f.exists() && f.isDirectory()) return f.listFiles();
   117 		if(f.isDirectory()) {
   136 		else {
   118 			return f.listFiles();
   137 			Log.e("Utils::", "Dir not found: " + dirName);
   119 		} else {
   138 			return new File[0];
   120 			throw new FileNotFoundException("Directory "+dirName+" does not exist.");
   139 		}
   121 		}
   140 	}
   122 	}
   141 
   123 
   142 	/**
   124 	/**
   143 	 * Checks if this directory has a file with suffix suffix
   125 	 * Checks if this directory has a file with suffix suffix
   159 	 * Gives back all dirs which contain a file with suffix fileSuffix
   141 	 * Gives back all dirs which contain a file with suffix fileSuffix
   160 	 * @param c
   142 	 * @param c
   161 	 * @param path
   143 	 * @param path
   162 	 * @param fileSuffix
   144 	 * @param fileSuffix
   163 	 * @return
   145 	 * @return
   164 	 */
   146 	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "path" does not exist
   165 	public static List<String> getDirsWithFileSuffix(Context c, String path, String fileSuffix){
   147 	 */
       
   148 	public static List<String> getDirsWithFileSuffix(Context c, String path, String fileSuffix) throws FileNotFoundException{
   166 		File[] files = getFilesFromRelativeDir(c,path);
   149 		File[] files = getFilesFromRelativeDir(c,path);
   167 		ArrayList<String> ret = new ArrayList<String>();
   150 		ArrayList<String> ret = new ArrayList<String>();
   168 
   151 
   169 		for(File f : files){
   152 		for(File f : files){
   170 			if(hasFileWithSuffix(f, fileSuffix)) ret.add(f.getName());
   153 			if(hasFileWithSuffix(f, fileSuffix)) ret.add(f.getName());
   172 		return ret;
   155 		return ret;
   173 	}
   156 	}
   174 
   157 
   175 	/**
   158 	/**
   176 	 * Get all files from directory dir which have the given suffix
   159 	 * Get all files from directory dir which have the given suffix
   177 	 * @param c
   160 	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dir" does not exist
   178 	 * @param dir
   161 	 */
   179 	 * @param suffix
   162 	public static ArrayList<String> getFileNamesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix) throws FileNotFoundException{
   180 	 * @param removeSuffix
   163 		File[] files = Utils.getFilesFromRelativeDir(c, dir);
   181 	 * @return
       
   182 	 */
       
   183 	public static ArrayList<String> getFilesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix){
       
   184 		String[] files = Utils.getFileNamesFromRelativeDir(c, dir);
       
   185 		ArrayList<String> ret = new ArrayList<String>();
   164 		ArrayList<String> ret = new ArrayList<String>();
   186 		for(String s : files){
   165 		for(File file : files){
       
   166 			String s = file.getName();
   187 			if(s.endsWith(suffix)){
   167 			if(s.endsWith(suffix)){
   188 				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
   168 				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
   189 				else ret.add(s);
   169 				else ret.add(s);
   190 			}
   170 			}
   191 		}
   171 		}