project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java
changeset 7929 6e01c5134eb5
parent 7926 550083f61a0e
parent 7861 bc7b6aa5d67a
child 7932 ebe1d112e439
equal deleted inserted replaced
7926:550083f61a0e 7929:6e01c5134eb5
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011-2012 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.hedgeroid;
       
    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 import java.util.List;
       
    29 
       
    30 import android.content.Context;
       
    31 import android.content.res.TypedArray;
       
    32 import android.os.Build;
       
    33 import android.os.Environment;
       
    34 import android.util.Log;
       
    35 
       
    36 public class Utils {
       
    37 
       
    38 	private static final String ROOT_DIR = "Data/";
       
    39 
       
    40 	/**
       
    41 	 * get the path to which we should download all the data files
       
    42 	 * @param c context 
       
    43 	 * @return absolute path
       
    44 	 */
       
    45 	public static String getCachePath(Context c){
       
    46 		if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
       
    47 			return PreFroyoSDCardDir.getDownloadPath(c) + '/';
       
    48 		}else{
       
    49 			return FroyoSDCardDir.getDownloadPath(c) + '/';
       
    50 		}
       
    51 	}
       
    52 
       
    53 	public static String getDataPath(Context c){
       
    54 		return getCachePath(c) + ROOT_DIR;
       
    55 	}
       
    56 
       
    57 	static class FroyoSDCardDir{
       
    58 		public static String getDownloadPath(Context c){
       
    59 			File f =  c.getExternalCacheDir();
       
    60 			if(f != null){
       
    61 				return f.getAbsolutePath();
       
    62 			}else{
       
    63 				return null;
       
    64 			}	
       
    65 		}
       
    66 	}
       
    67 
       
    68 	static class PreFroyoSDCardDir{
       
    69 		public static String getDownloadPath(Context c){
       
    70 			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
       
    71 				if(Environment.getExternalStorageDirectory() != null)
       
    72 					return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/";				
       
    73 			}
       
    74 			return null;
       
    75 		}
       
    76 	}
       
    77 
       
    78 	/**
       
    79 	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
       
    80 	 * @param dirName
       
    81 	 * @param c context
       
    82 	 * @return string of files
       
    83 	 */
       
    84 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
       
    85 		String prefix = getDataPath(c);
       
    86 		File f = new File(prefix + dirName);
       
    87 
       
    88 		if(f.exists() && f.isDirectory()) return f.list();
       
    89 		else{
       
    90 
       
    91 			Log.e("Utils::", "Couldn't find dir: " + dirName);
       
    92 			return new String[0];
       
    93 		}
       
    94 	}
       
    95 
       
    96 	/**
       
    97 	 * Return a File array with all the files from dirName
       
    98 	 * @param c
       
    99 	 * @param dirName
       
   100 	 * @return
       
   101 	 */
       
   102 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
       
   103 		String prefix = getDataPath(c);
       
   104 		File f = new File(prefix + dirName);
       
   105 
       
   106 		if(f.exists() && f.isDirectory()) return f.listFiles();
       
   107 		else {
       
   108 			Log.e("Utils::", "Dir not found: " + dirName);
       
   109 			return new File[0];
       
   110 		}
       
   111 	}
       
   112 
       
   113 	/**
       
   114 	 * Checks if this directory has a file with suffix suffix
       
   115 	 * @param f - directory
       
   116 	 * @return
       
   117 	 */
       
   118 	public static boolean hasFileWithSuffix(File f, String suffix){
       
   119 		if(f.isDirectory()){
       
   120 			for(String s : f.list()){
       
   121 				if(s.endsWith(suffix)) return true;
       
   122 			}
       
   123 			return false;
       
   124 		}else{
       
   125 			return false;
       
   126 		}
       
   127 	}
       
   128 
       
   129 	/**
       
   130 	 * Gives back all dirs which contain a file with suffix fileSuffix
       
   131 	 * @param c
       
   132 	 * @param path
       
   133 	 * @param fileSuffix
       
   134 	 * @return
       
   135 	 */
       
   136 	public static List<String> getDirsWithFileSuffix(Context c, String path, String fileSuffix){
       
   137 		File[] files = getFilesFromRelativeDir(c,path);
       
   138 		ArrayList<String> ret = new ArrayList<String>();
       
   139 
       
   140 		for(File f : files){
       
   141 			if(hasFileWithSuffix(f, fileSuffix)) ret.add(f.getName());
       
   142 		}
       
   143 		return ret;
       
   144 	}
       
   145 
       
   146 	/**
       
   147 	 * Get all files from directory dir which have the given suffix
       
   148 	 * @param c
       
   149 	 * @param dir
       
   150 	 * @param suffix
       
   151 	 * @param removeSuffix
       
   152 	 * @return
       
   153 	 */
       
   154 	public static ArrayList<String> getFilesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix){
       
   155 		String[] files = Utils.getFileNamesFromRelativeDir(c, dir);
       
   156 		ArrayList<String> ret = new ArrayList<String>();
       
   157 		for(String s : files){
       
   158 			if(s.endsWith(suffix)){
       
   159 				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
       
   160 				else ret.add(s);
       
   161 			}
       
   162 		}
       
   163 		return ret;
       
   164 	}
       
   165 
       
   166 	/**
       
   167 	 * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
       
   168 	 * @param c
       
   169 	 * @param sourceResId
       
   170 	 * @param directory
       
   171 	 */
       
   172 	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
       
   173 		byte[] buffer = new byte[1024];
       
   174 		InputStream bis = null;
       
   175 		BufferedOutputStream bos = null;
       
   176 		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
       
   177 		schemesDirFile.mkdirs();
       
   178 		String schemesDirPath = schemesDirFile.getAbsolutePath() + '/';
       
   179 
       
   180 		//Get an array with the resource files ID
       
   181 		TypedArray ta = c.getResources().obtainTypedArray(sourceResId);
       
   182 		int[] resIds = new int[ta.length()];
       
   183 		for(int i = 0; i < ta.length(); i++){
       
   184 			resIds[i] = ta.getResourceId(i, 0);
       
   185 		}
       
   186 
       
   187 		for(int id : resIds){
       
   188 			String fileName = c.getResources().getResourceEntryName(id);
       
   189 			File f = new File(schemesDirPath + fileName);
       
   190 			try {
       
   191 				if(!f.createNewFile()){
       
   192 					f.delete();
       
   193 					f.createNewFile();
       
   194 				}
       
   195 
       
   196 				bis = c.getResources().openRawResource(id);
       
   197 				bos = new BufferedOutputStream(new FileOutputStream(f), 1024);
       
   198 				int read = 0;
       
   199 				while((read = bis.read(buffer)) != -1){
       
   200 					bos.write(buffer, 0, read);
       
   201 				}
       
   202 
       
   203 			} catch (IOException e) {
       
   204 				e.printStackTrace();
       
   205 			}finally{
       
   206 				if(bis != null)
       
   207 					try { 
       
   208 						bis.close();
       
   209 					} catch (IOException e) {
       
   210 						e.printStackTrace();
       
   211 					}
       
   212 					if(bos != null)
       
   213 						try {
       
   214 							bos.close();
       
   215 						} catch (IOException e) {
       
   216 							e.printStackTrace();
       
   217 						}
       
   218 			}
       
   219 		}
       
   220 	}
       
   221 }