project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java
branchhedgeroid
changeset 6047 10011f051f9c
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.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 
       
    29 import org.hedgewars.mobile.R;
       
    30 
       
    31 import android.content.Context;
       
    32 import android.content.res.TypedArray;
       
    33 import android.os.Build;
       
    34 import android.os.Environment;
       
    35 import android.widget.Toast;
       
    36 
       
    37 public class Utils {
       
    38 
       
    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 getDownloadPath(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 	static class FroyoSDCardDir{
       
    54 		public static String getDownloadPath(Context c){
       
    55 			File f =  c.getExternalCacheDir();
       
    56 			if(f != null){
       
    57 				return f.getAbsolutePath() + "/Data/";
       
    58 			}else{
       
    59 				Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
       
    60 				return null;
       
    61 			}	
       
    62 		}
       
    63 	}
       
    64 	
       
    65 	static class PreFroyoSDCardDir{
       
    66 		public static String getDownloadPath(Context c){
       
    67 			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
       
    68 				if(Environment.getExternalStorageDirectory() != null)
       
    69 					return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/";				
       
    70 			}
       
    71 			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
       
    72 			return null;
       
    73 		}
       
    74 	}
       
    75 	
       
    76 	/**
       
    77 	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
       
    78 	 * @param dirName
       
    79 	 * @param c context
       
    80 	 * @return string of files
       
    81 	 */
       
    82 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
       
    83 		String prefix = getDownloadPath(c);
       
    84 		File f = new File(prefix + dirName);
       
    85 		
       
    86 		if(f.exists() && f.isDirectory()) return f.list();
       
    87 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
       
    88 	}
       
    89 	
       
    90 	/**
       
    91 	 * Return a File array with all the files from dirName
       
    92 	 * @param c
       
    93 	 * @param dirName
       
    94 	 * @return
       
    95 	 */
       
    96 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
       
    97 		String prefix = getDownloadPath(c);
       
    98 		File f = new File(prefix + dirName);
       
    99 		
       
   100 		if(f.exists() && f.isDirectory()) return f.listFiles();
       
   101 		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
       
   102 	}
       
   103 	
       
   104 	/**
       
   105 	 * Checks if this directory has a file with suffix suffix
       
   106 	 * @param f - directory
       
   107 	 * @return
       
   108 	 */
       
   109 	public static boolean hasFileWithSuffix(File f, String suffix){
       
   110 		if(f.isDirectory()){
       
   111 			for(String s : f.list()){
       
   112 				if(s.endsWith(suffix)) return true;
       
   113 			}
       
   114 			return false;
       
   115 		}else{
       
   116 			return false;
       
   117 		}
       
   118 	}
       
   119 	
       
   120 	/**
       
   121 	 * Gives back all dirs which contain a file with suffix fileSuffix
       
   122 	 * @param c
       
   123 	 * @param path
       
   124 	 * @param fileSuffix
       
   125 	 * @return
       
   126 	 */
       
   127 	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
       
   128 		File[] files = getFilesFromRelativeDir(c,path);
       
   129 		String[] validFiles = new String[files.length];
       
   130 		int validCounter = 0;
       
   131 		
       
   132 		for(File f : files){
       
   133 			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
       
   134 		}
       
   135 		String[] ret = new String[validCounter];
       
   136 		System.arraycopy(validFiles, 0, ret, 0, validCounter);
       
   137 		return ret;
       
   138 	}
       
   139 	
       
   140 	/**
       
   141 	 * Get all files from directory dir which have the given suffix
       
   142 	 * @param c
       
   143 	 * @param dir
       
   144 	 * @param suffix
       
   145 	 * @param removeSuffix
       
   146 	 * @return
       
   147 	 */
       
   148 	public static ArrayList<String> getFilesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix){
       
   149 		String[] files = Utils.getFileNamesFromRelativeDir(c, dir);
       
   150 		ArrayList<String> ret = new ArrayList<String>();
       
   151 		for(String s : files){
       
   152 			if(s.endsWith(suffix)){
       
   153 				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
       
   154 				else ret.add(s);
       
   155 			}
       
   156 		}
       
   157 		return ret;
       
   158 	}
       
   159 	
       
   160     /**
       
   161      * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
       
   162      * @param c
       
   163      * @param sourceResId
       
   164      * @param directory
       
   165      */
       
   166 	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
       
   167 		byte[] buffer = new byte[1024];
       
   168 		InputStream bis = null;
       
   169 		BufferedOutputStream bos = null;
       
   170 		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
       
   171 		schemesDirFile.mkdirs();
       
   172 		String schemesDirPath = schemesDirFile.getAbsolutePath() + '/';
       
   173 
       
   174 		//Get an array with the resource files ID
       
   175 		TypedArray ta = c.getResources().obtainTypedArray(sourceResId);
       
   176 		int[] resIds = new int[ta.length()];
       
   177 		for(int i = 0; i < ta.length(); i++){
       
   178 			resIds[i] = ta.getResourceId(i, 0);
       
   179 		}
       
   180 
       
   181 		for(int id : resIds){
       
   182 			String fileName = c.getResources().getResourceEntryName(id);
       
   183 			File f = new File(schemesDirPath + fileName);
       
   184 			try {
       
   185 				if(!f.createNewFile()){
       
   186 					f.delete();
       
   187 					f.createNewFile();
       
   188 				}
       
   189 
       
   190 				bis = c.getResources().openRawResource(id);
       
   191 				bos = new BufferedOutputStream(new FileOutputStream(f), 1024);
       
   192 				int read = 0;
       
   193 				while((read = bis.read(buffer)) != -1){
       
   194 					bos.write(buffer, 0, read);
       
   195 				}
       
   196 
       
   197 			} catch (IOException e) {
       
   198 				e.printStackTrace();
       
   199 			}finally{
       
   200 				if(bis != null)
       
   201 					try { 
       
   202 						bis.close();
       
   203 					} catch (IOException e) {
       
   204 						e.printStackTrace();
       
   205 					}
       
   206 				if(bos != null)
       
   207 					try {
       
   208 						bos.close();
       
   209 					} catch (IOException e) {
       
   210 						e.printStackTrace();
       
   211 					}
       
   212 			}
       
   213 		}
       
   214 	}
       
   215 }