project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java
branchhedgeroid
changeset 7855 ddcdedd3330b
parent 6700 e04da46ee43c
child 6839 2dd2c0f2c9d0
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java	Thu Nov 24 13:44:30 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Utils.java	Sun Oct 28 13:28:23 2012 +0100
@@ -1,6 +1,6 @@
 /*
  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
- * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
+ * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -25,18 +25,18 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.List;
 
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.os.Build;
 import android.os.Environment;
 import android.util.Log;
-import android.widget.Toast;
 
 public class Utils {
 
 	private static final String ROOT_DIR = "Data/";
-	
+
 	/**
 	 * get the path to which we should download all the data files
 	 * @param c context 
@@ -49,34 +49,32 @@
 			return FroyoSDCardDir.getDownloadPath(c) + '/';
 		}
 	}
-	
+
 	public static String getDataPath(Context c){
 		return getCachePath(c) + ROOT_DIR;
 	}
-	
+
 	static class FroyoSDCardDir{
 		public static String getDownloadPath(Context c){
 			File f =  c.getExternalCacheDir();
 			if(f != null){
 				return f.getAbsolutePath();
 			}else{
-				Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
 				return null;
 			}	
 		}
 	}
-	
+
 	static class PreFroyoSDCardDir{
 		public static String getDownloadPath(Context c){
 			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
 				if(Environment.getExternalStorageDirectory() != null)
 					return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/";				
 			}
-			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
 			return null;
 		}
 	}
-	
+
 	/**
 	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
 	 * @param dirName
@@ -86,15 +84,15 @@
 	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
 		String prefix = getDataPath(c);
 		File f = new File(prefix + dirName);
-		
+
 		if(f.exists() && f.isDirectory()) return f.list();
 		else{
-			
+
 			Log.e("Utils::", "Couldn't find dir: " + dirName);
 			return new String[0];
 		}
 	}
-	
+
 	/**
 	 * Return a File array with all the files from dirName
 	 * @param c
@@ -104,14 +102,14 @@
 	public static File[] getFilesFromRelativeDir(Context c, String dirName){
 		String prefix = getDataPath(c);
 		File f = new File(prefix + dirName);
-		
+
 		if(f.exists() && f.isDirectory()) return f.listFiles();
 		else {
 			Log.e("Utils::", "Dir not found: " + dirName);
 			return new File[0];
 		}
 	}
-	
+
 	/**
 	 * Checks if this directory has a file with suffix suffix
 	 * @param f - directory
@@ -127,7 +125,7 @@
 			return false;
 		}
 	}
-	
+
 	/**
 	 * Gives back all dirs which contain a file with suffix fileSuffix
 	 * @param c
@@ -135,19 +133,16 @@
 	 * @param fileSuffix
 	 * @return
 	 */
-	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
+	public static List<String> getDirsWithFileSuffix(Context c, String path, String fileSuffix){
 		File[] files = getFilesFromRelativeDir(c,path);
-		String[] validFiles = new String[files.length];
-		int validCounter = 0;
-		
+		ArrayList<String> ret = new ArrayList<String>();
+
 		for(File f : files){
-			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
+			if(hasFileWithSuffix(f, fileSuffix)) ret.add(f.getName());
 		}
-		String[] ret = new String[validCounter];
-		System.arraycopy(validFiles, 0, ret, 0, validCounter);
 		return ret;
 	}
-	
+
 	/**
 	 * Get all files from directory dir which have the given suffix
 	 * @param c
@@ -167,13 +162,13 @@
 		}
 		return ret;
 	}
-	
-    /**
-     * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
-     * @param c
-     * @param sourceResId
-     * @param directory
-     */
+
+	/**
+	 * 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;
@@ -214,12 +209,12 @@
 					} catch (IOException e) {
 						e.printStackTrace();
 					}
-				if(bos != null)
-					try {
-						bos.close();
-					} catch (IOException e) {
-						e.printStackTrace();
-					}
+					if(bos != null)
+						try {
+							bos.close();
+						} catch (IOException e) {
+							e.printStackTrace();
+						}
 			}
 		}
 	}