project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Utils.java
branchhedgeroid
changeset 5473 a68f900c4e8c
parent 5428 5d0c5f7a9339
child 5621 ea796c83ea47
equal deleted inserted replaced
5471:081bc0170623 5473:a68f900c4e8c
     3 import java.io.BufferedOutputStream;
     3 import java.io.BufferedOutputStream;
     4 import java.io.File;
     4 import java.io.File;
     5 import java.io.FileOutputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     7 import java.io.InputStream;
       
     8 import java.util.ArrayList;
     8 
     9 
     9 import android.content.Context;
    10 import android.content.Context;
    10 import android.content.res.TypedArray;
    11 import android.content.res.TypedArray;
    11 import android.widget.Toast;
    12 import android.widget.Toast;
    12 
    13 
    41 		if(f.exists() && f.isDirectory()) return f.list();
    42 		if(f.exists() && f.isDirectory()) return f.list();
    42 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
    43 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
    43 	}
    44 	}
    44 	
    45 	
    45 	/**
    46 	/**
    46 	 * Return a File array if all the files from dirName
    47 	 * Return a File array with all the files from dirName
    47 	 * @param c
    48 	 * @param c
    48 	 * @param dirName
    49 	 * @param dirName
    49 	 * @return
    50 	 * @return
    50 	 */
    51 	 */
    51 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
    52 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
    55 		if(f.exists() && f.isDirectory()) return f.listFiles();
    56 		if(f.exists() && f.isDirectory()) return f.listFiles();
    56 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
    57 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
    57 	}
    58 	}
    58 	
    59 	
    59 	/**
    60 	/**
    60 	 * Checks if this directory has a lua file
    61 	 * Checks if this directory has a file with suffix suffix
    61 	 * @param f - directory
    62 	 * @param f - directory
    62 	 * @return
    63 	 * @return
    63 	 */
    64 	 */
    64 	public static boolean hasFileWithSuffix(File f, String suffix){
    65 	public static boolean hasFileWithSuffix(File f, String suffix){
    65 		if(f.isDirectory()){
    66 		if(f.isDirectory()){
    70 		}else{
    71 		}else{
    71 			return false;
    72 			return false;
    72 		}
    73 		}
    73 	}
    74 	}
    74 	
    75 	
       
    76 	/**
       
    77 	 * Gives back all dirs which contain a file with suffix fileSuffix
       
    78 	 * @param c
       
    79 	 * @param path
       
    80 	 * @param fileSuffix
       
    81 	 * @return
       
    82 	 */
    75 	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
    83 	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
    76 		File[] files = getFilesFromRelativeDir(c,path);
    84 		File[] files = getFilesFromRelativeDir(c,path);
    77 		String[] validFiles = new String[files.length];
    85 		String[] validFiles = new String[files.length];
    78 		int validCounter = 0;
    86 		int validCounter = 0;
    79 		
    87 		
    80 		for(File f : files){
    88 		for(File f : files){
    81 			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
    89 			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
    82 		}
    90 		}
    83 		String[] ret = new String[validCounter];
    91 		String[] ret = new String[validCounter];
    84 		System.arraycopy(validFiles, 0, ret, 0, validCounter);
    92 		System.arraycopy(validFiles, 0, ret, 0, validCounter);
       
    93 		return ret;
       
    94 	}
       
    95 	
       
    96 	/**
       
    97 	 * Get all files from directory dir which have the given suffix
       
    98 	 * @param c
       
    99 	 * @param dir
       
   100 	 * @param suffix
       
   101 	 * @param removeSuffix
       
   102 	 * @return
       
   103 	 */
       
   104 	public static ArrayList<String> getFilesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix){
       
   105 		String[] files = Utils.getFileNamesFromRelativeDir(c, dir);
       
   106 		ArrayList<String> ret = new ArrayList<String>();
       
   107 		for(String s : files){
       
   108 			if(s.endsWith(suffix)){
       
   109 				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
       
   110 				else ret.add(s);
       
   111 			}
       
   112 		}
    85 		return ret;
   113 		return ret;
    86 	}
   114 	}
    87 	
   115 	
    88     /**
   116     /**
    89      * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
   117      * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory