Added a utility class needed by the Downloaders hedgeroid
authorXeli
Thu, 14 Jul 2011 15:42:01 +0200
branchhedgeroid
changeset 5414 34663c4743f7
parent 5412 ab055114c788
child 5416 26a36326d199
Added a utility class needed by the Downloaders
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Utils.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Utils.java	Thu Jul 14 15:42:01 2011 +0200
@@ -0,0 +1,143 @@
+package org.hedgewars.mobile;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.widget.Toast;
+
+public class Utils {
+
+	
+	/**
+	 * get the path to which we should download all the data files
+	 * @param c context 
+	 * @return absolute path
+	 */
+	public static String getDownloadPath(Context c){
+		File f =  c.getExternalCacheDir();
+		if(f != null){
+			return f.getAbsolutePath() + "/Data/";
+		}else{
+			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG);
+			return null;
+		}
+	}
+	
+	/**
+	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
+	 * @param dirName
+	 * @param c context
+	 * @return string of files
+	 */
+	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
+		String prefix = getDownloadPath(c);
+		File f = new File(prefix + dirName);
+		
+		if(f.exists() && f.isDirectory()) return f.list();
+		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
+	}
+	
+	/**
+	 * Return a File array if all the files from dirName
+	 * @param c
+	 * @param dirName
+	 * @return
+	 */
+	public static File[] getFilesFromRelativeDir(Context c, String dirName){
+		String prefix = getDownloadPath(c);
+		File f = new File(prefix + dirName);
+		
+		if(f.exists() && f.isDirectory()) return f.listFiles();
+		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
+	}
+	
+	/**
+	 * Checks if this directory has a lua file
+	 * @param f - directory
+	 * @return
+	 */
+	public static boolean hasFileWithSuffix(File f, String suffix){
+		if(f.isDirectory()){
+			for(String s : f.list()){
+				if(s.endsWith(suffix)) return true;
+			}
+			return false;
+		}else{
+			throw new IllegalArgumentException("Not a directory.. f = " + f.getAbsolutePath());
+		}
+	}
+	
+	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
+		File[] files = getFilesFromRelativeDir(c,path);
+		String[] validFiles = new String[files.length];
+		int validCounter = 0;
+		
+		for(File f : files){
+			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
+		}
+		String[] ret = new String[validCounter];
+		System.arraycopy(validFiles, 0, ret, 0, validCounter);
+		return ret;
+	}
+	
+    /**
+     * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
+     * @param c
+     * @param sourceResId
+     * @param directory
+     */
+	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
+		byte[] buffer = new byte[1024];
+		InputStream bis = null;
+		BufferedOutputStream bos = null;
+		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
+		schemesDirFile.mkdirs();
+		String schemesDirPath = schemesDirFile.getAbsolutePath() + '/';
+
+		//Get an array with the resource files ID
+		TypedArray ta = c.getResources().obtainTypedArray(sourceResId);
+		int[] resIds = new int[ta.length()];
+		for(int i = 0; i < ta.length(); i++){
+			resIds[i] = ta.getResourceId(i, 0);
+		}
+
+		for(int id : resIds){
+			String fileName = c.getResources().getResourceEntryName(id);
+			File f = new File(schemesDirPath + fileName);
+			try {
+				if(!f.createNewFile()){
+					f.delete();
+					f.createNewFile();
+				}
+
+				bis = c.getResources().openRawResource(id);
+				bos = new BufferedOutputStream(new FileOutputStream(f), 1024);
+				int read = 0;
+				while((read = bis.read(buffer)) != -1){
+					bos.write(buffer, 0, read);
+				}
+
+			} catch (IOException e) {
+				e.printStackTrace();
+			}finally{
+				if(bis != null)
+					try { 
+						bis.close();
+					} catch (IOException e) {
+						e.printStackTrace();
+					}
+				if(bos != null)
+					try {
+						bos.close();
+					} catch (IOException e) {
+						e.printStackTrace();
+					}
+			}
+		}
+	}
+}