project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/util/FileUtils.java
changeset 7586 33924ff4af50
parent 7584 7831c84cc644
child 10017 de822cd3df3a
equal deleted inserted replaced
7584:7831c84cc644 7586:33924ff4af50
    69 		} else {
    69 		} else {
    70 			return cachePath;
    70 			return cachePath;
    71 		}
    71 		}
    72 	}
    72 	}
    73 
    73 
    74 	public static File getDataPathFile(Context c) throws FileNotFoundException {
    74 	public static File getDataPathFile(Context c, String...subpath) throws FileNotFoundException {
    75 		return new File(getCachePath(c), ROOT_DIR);
    75 		File file = new File(getCachePath(c), ROOT_DIR);
    76 	}
    76 		for(String pathcomponent : subpath) {
    77 	
    77 			file = new File(file, pathcomponent);
    78 	// TODO Several callers are unaware that this may fail, so it throws an RTE now.
    78 		}
    79 	// Should be handled better though.
    79 		return file;
    80 	@Deprecated
    80 	}
    81 	public static String getDataPath(Context c) {
    81 	
    82 		try {
       
    83 			return getDataPathFile(c).getAbsolutePath()+"/";
       
    84 		} catch(FileNotFoundException e) {
       
    85 			throw new RuntimeException(e);
       
    86 		}
       
    87 	}
       
    88 
       
    89 	@TargetApi(8)
    82 	@TargetApi(8)
    90 	private static class FroyoSDCardDir{
    83 	private static class FroyoSDCardDir{
    91 		public static File getDownloadPath(Context c){
    84 		public static File getDownloadPath(Context c){
    92 			return c.getExternalCacheDir();
    85 			return c.getExternalCacheDir();
    93 		}
    86 		}
   111 	 * @param dirName
   104 	 * @param dirName
   112 	 * @return
   105 	 * @return
   113 	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dirName" does not exist
   106 	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dirName" does not exist
   114 	 */
   107 	 */
   115 	public static File[] getFilesFromRelativeDir(Context c, String dirName) throws FileNotFoundException {
   108 	public static File[] getFilesFromRelativeDir(Context c, String dirName) throws FileNotFoundException {
   116 		File f = new File(getDataPathFile(c), dirName);
   109 		File f = getDataPathFile(c, dirName);
   117 
   110 
   118 		if(f.isDirectory()) {
   111 		if(f.isDirectory()) {
   119 			return f.listFiles();
   112 			return f.listFiles();
   120 		} else {
   113 		} else {
   121 			throw new FileNotFoundException("Directory "+dirName+" does not exist.");
   114 			throw new FileNotFoundException("Directory "+dirName+" does not exist.");
   158 
   151 
   159 	/**
   152 	/**
   160 	 * Get all files from directory dir which have the given suffix
   153 	 * Get all files from directory dir which have the given suffix
   161 	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dir" does not exist
   154 	 * @throws FileNotFoundException If the sdcard is not available or the subdirectory "dir" does not exist
   162 	 */
   155 	 */
   163 	public static ArrayList<String> getFileNamesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix) throws FileNotFoundException{
   156 	public static List<String> getFileNamesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix) throws FileNotFoundException{
   164 		File[] files = FileUtils.getFilesFromRelativeDir(c, dir);
   157 		File[] files = FileUtils.getFilesFromRelativeDir(c, dir);
   165 		ArrayList<String> ret = new ArrayList<String>();
   158 		List<String> ret = new ArrayList<String>();
   166 		for(File file : files){
   159 		for(File file : files){
   167 			String s = file.getName();
   160 			String s = file.getName();
   168 			if(s.endsWith(suffix)){
   161 			if(s.endsWith(suffix)){
   169 				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
   162 				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
   170 				else ret.add(s);
   163 				else ret.add(s);