project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Utils.java
branchhedgeroid
changeset 6047 10011f051f9c
parent 6045 9a7cc0f29430
child 6049 7bc38086d771
equal deleted inserted replaced
6045:9a7cc0f29430 6047:10011f051f9c
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 
       
    20 package org.hedgewars.mobile;
       
    21 
       
    22 import java.io.BufferedOutputStream;
       
    23 import java.io.File;
       
    24 import java.io.FileOutputStream;
       
    25 import java.io.IOException;
       
    26 import java.io.InputStream;
       
    27 import java.util.ArrayList;
       
    28 
       
    29 import android.content.Context;
       
    30 import android.content.res.TypedArray;
       
    31 import android.os.Build;
       
    32 import android.os.Environment;
       
    33 import android.widget.Toast;
       
    34 
       
    35 public class Utils {
       
    36 
       
    37 	
       
    38 	/**
       
    39 	 * get the path to which we should download all the data files
       
    40 	 * @param c context 
       
    41 	 * @return absolute path
       
    42 	 */
       
    43 	public static String getDownloadPath(Context c){
       
    44 		if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
       
    45 			return PreFroyoSDCardDir.getDownloadPath(c);
       
    46 		}else{
       
    47 			return FroyoSDCardDir.getDownloadPath(c);
       
    48 		}
       
    49 	}
       
    50 	
       
    51 	static class FroyoSDCardDir{
       
    52 		public static String getDownloadPath(Context c){
       
    53 			File f =  c.getExternalCacheDir();
       
    54 			if(f != null){
       
    55 				return f.getAbsolutePath() + "/Data/";
       
    56 			}else{
       
    57 				Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
       
    58 				return null;
       
    59 			}	
       
    60 		}
       
    61 	}
       
    62 	
       
    63 	static class PreFroyoSDCardDir{
       
    64 		public static String getDownloadPath(Context c){
       
    65 			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
       
    66 				if(Environment.getExternalStorageDirectory() != null)
       
    67 					return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/";				
       
    68 			}
       
    69 			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
       
    70 			return null;
       
    71 		}
       
    72 	}
       
    73 	
       
    74 	/**
       
    75 	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
       
    76 	 * @param dirName
       
    77 	 * @param c context
       
    78 	 * @return string of files
       
    79 	 */
       
    80 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
       
    81 		String prefix = getDownloadPath(c);
       
    82 		File f = new File(prefix + dirName);
       
    83 		
       
    84 		if(f.exists() && f.isDirectory()) return f.list();
       
    85 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
       
    86 	}
       
    87 	
       
    88 	/**
       
    89 	 * Return a File array with all the files from dirName
       
    90 	 * @param c
       
    91 	 * @param dirName
       
    92 	 * @return
       
    93 	 */
       
    94 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
       
    95 		String prefix = getDownloadPath(c);
       
    96 		File f = new File(prefix + dirName);
       
    97 		
       
    98 		if(f.exists() && f.isDirectory()) return f.listFiles();
       
    99 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
       
   100 	}
       
   101 	
       
   102 	/**
       
   103 	 * Checks if this directory has a file with suffix suffix
       
   104 	 * @param f - directory
       
   105 	 * @return
       
   106 	 */
       
   107 	public static boolean hasFileWithSuffix(File f, String suffix){
       
   108 		if(f.isDirectory()){
       
   109 			for(String s : f.list()){
       
   110 				if(s.endsWith(suffix)) return true;
       
   111 			}
       
   112 			return false;
       
   113 		}else{
       
   114 			return false;
       
   115 		}
       
   116 	}
       
   117 	
       
   118 	/**
       
   119 	 * Gives back all dirs which contain a file with suffix fileSuffix
       
   120 	 * @param c
       
   121 	 * @param path
       
   122 	 * @param fileSuffix
       
   123 	 * @return
       
   124 	 */
       
   125 	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
       
   126 		File[] files = getFilesFromRelativeDir(c,path);
       
   127 		String[] validFiles = new String[files.length];
       
   128 		int validCounter = 0;
       
   129 		
       
   130 		for(File f : files){
       
   131 			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
       
   132 		}
       
   133 		String[] ret = new String[validCounter];
       
   134 		System.arraycopy(validFiles, 0, ret, 0, validCounter);
       
   135 		return ret;
       
   136 	}
       
   137 	
       
   138 	/**
       
   139 	 * Get all files from directory dir which have the given suffix
       
   140 	 * @param c
       
   141 	 * @param dir
       
   142 	 * @param suffix
       
   143 	 * @param removeSuffix
       
   144 	 * @return
       
   145 	 */
       
   146 	public static ArrayList<String> getFilesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix){
       
   147 		String[] files = Utils.getFileNamesFromRelativeDir(c, dir);
       
   148 		ArrayList<String> ret = new ArrayList<String>();
       
   149 		for(String s : files){
       
   150 			if(s.endsWith(suffix)){
       
   151 				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
       
   152 				else ret.add(s);
       
   153 			}
       
   154 		}
       
   155 		return ret;
       
   156 	}
       
   157 	
       
   158     /**
       
   159      * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
       
   160      * @param c
       
   161      * @param sourceResId
       
   162      * @param directory
       
   163      */
       
   164 	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
       
   165 		byte[] buffer = new byte[1024];
       
   166 		InputStream bis = null;
       
   167 		BufferedOutputStream bos = null;
       
   168 		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
       
   169 		schemesDirFile.mkdirs();
       
   170 		String schemesDirPath = schemesDirFile.getAbsolutePath() + '/';
       
   171 
       
   172 		//Get an array with the resource files ID
       
   173 		TypedArray ta = c.getResources().obtainTypedArray(sourceResId);
       
   174 		int[] resIds = new int[ta.length()];
       
   175 		for(int i = 0; i < ta.length(); i++){
       
   176 			resIds[i] = ta.getResourceId(i, 0);
       
   177 		}
       
   178 
       
   179 		for(int id : resIds){
       
   180 			String fileName = c.getResources().getResourceEntryName(id);
       
   181 			File f = new File(schemesDirPath + fileName);
       
   182 			try {
       
   183 				if(!f.createNewFile()){
       
   184 					f.delete();
       
   185 					f.createNewFile();
       
   186 				}
       
   187 
       
   188 				bis = c.getResources().openRawResource(id);
       
   189 				bos = new BufferedOutputStream(new FileOutputStream(f), 1024);
       
   190 				int read = 0;
       
   191 				while((read = bis.read(buffer)) != -1){
       
   192 					bos.write(buffer, 0, read);
       
   193 				}
       
   194 
       
   195 			} catch (IOException e) {
       
   196 				e.printStackTrace();
       
   197 			}finally{
       
   198 				if(bis != null)
       
   199 					try { 
       
   200 						bis.close();
       
   201 					} catch (IOException e) {
       
   202 						e.printStackTrace();
       
   203 					}
       
   204 				if(bos != null)
       
   205 					try {
       
   206 						bos.close();
       
   207 					} catch (IOException e) {
       
   208 						e.printStackTrace();
       
   209 					}
       
   210 			}
       
   211 		}
       
   212 	}
       
   213 }