project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Utils.java
author Xeli
Thu, 18 Aug 2011 15:26:41 +0200
branchhedgeroid
changeset 5597 14b3cdb23c2c
parent 5473 a68f900c4e8c
child 5621 ea796c83ea47
permissions -rw-r--r--
Volume keys now work
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     1
package org.hedgewars.mobile;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     2
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     3
import java.io.BufferedOutputStream;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     4
import java.io.File;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     5
import java.io.FileOutputStream;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     6
import java.io.IOException;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     7
import java.io.InputStream;
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
     8
import java.util.ArrayList;
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     9
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    10
import android.content.Context;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    11
import android.content.res.TypedArray;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    12
import android.widget.Toast;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    13
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    14
public class Utils {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    15
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    16
	
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    17
	/**
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    18
	 * get the path to which we should download all the data files
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    19
	 * @param c context 
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    20
	 * @return absolute path
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    21
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    22
	public static String getDownloadPath(Context c){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    23
		File f =  c.getExternalCacheDir();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    24
		if(f != null){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    25
			return f.getAbsolutePath() + "/Data/";
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    26
		}else{
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    27
			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    28
			return null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    29
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    30
	}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    31
	
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    32
	/**
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    33
	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    34
	 * @param dirName
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    35
	 * @param c context
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    36
	 * @return string of files
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    37
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    38
	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    39
		String prefix = getDownloadPath(c);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    40
		File f = new File(prefix + dirName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    41
		
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    42
		if(f.exists() && f.isDirectory()) return f.list();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    43
		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    44
	}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    45
	
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    46
	/**
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    47
	 * Return a File array with all the files from dirName
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    48
	 * @param c
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    49
	 * @param dirName
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    50
	 * @return
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    51
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    52
	public static File[] getFilesFromRelativeDir(Context c, String dirName){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    53
		String prefix = getDownloadPath(c);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    54
		File f = new File(prefix + dirName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    55
		
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    56
		if(f.exists() && f.isDirectory()) return f.listFiles();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    57
		else throw new IllegalArgumentException("File not a directory or doesn't exist dirName = " + f.getAbsolutePath());
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    58
	}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    59
	
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    60
	/**
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    61
	 * Checks if this directory has a file with suffix suffix
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    62
	 * @param f - directory
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    63
	 * @return
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    64
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    65
	public static boolean hasFileWithSuffix(File f, String suffix){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    66
		if(f.isDirectory()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    67
			for(String s : f.list()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    68
				if(s.endsWith(suffix)) return true;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    69
			}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    70
			return false;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    71
		}else{
5428
5d0c5f7a9339 Fixed a small bug for when detecting Themes
Xeli
parents: 5414
diff changeset
    72
			return false;
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    73
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    74
	}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    75
	
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    76
	/**
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    77
	 * Gives back all dirs which contain a file with suffix fileSuffix
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    78
	 * @param c
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    79
	 * @param path
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    80
	 * @param fileSuffix
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    81
	 * @return
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    82
	 */
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    83
	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    84
		File[] files = getFilesFromRelativeDir(c,path);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    85
		String[] validFiles = new String[files.length];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    86
		int validCounter = 0;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    87
		
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    88
		for(File f : files){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    89
			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    90
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    91
		String[] ret = new String[validCounter];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    92
		System.arraycopy(validFiles, 0, ret, 0, validCounter);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    93
		return ret;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    94
	}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    95
	
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    96
	/**
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    97
	 * Get all files from directory dir which have the given suffix
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    98
	 * @param c
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    99
	 * @param dir
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   100
	 * @param suffix
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   101
	 * @param removeSuffix
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   102
	 * @return
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   103
	 */
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   104
	public static ArrayList<String> getFilesFromDirWithSuffix(Context c, String dir, String suffix, boolean removeSuffix){
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   105
		String[] files = Utils.getFileNamesFromRelativeDir(c, dir);
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   106
		ArrayList<String> ret = new ArrayList<String>();
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   107
		for(String s : files){
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   108
			if(s.endsWith(suffix)){
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   109
				if(removeSuffix) ret.add(s.substring(0, s.length()-suffix.length()));
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   110
				else ret.add(s);
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   111
			}
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   112
		}
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   113
		return ret;
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   114
	}
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   115
	
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   116
    /**
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   117
     * Moves resources pointed to by sourceResId (from @res/raw/) to the app's private data directory
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   118
     * @param c
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   119
     * @param sourceResId
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   120
     * @param directory
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   121
     */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   122
	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   123
		byte[] buffer = new byte[1024];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   124
		InputStream bis = null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   125
		BufferedOutputStream bos = null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   126
		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   127
		schemesDirFile.mkdirs();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   128
		String schemesDirPath = schemesDirFile.getAbsolutePath() + '/';
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   129
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   130
		//Get an array with the resource files ID
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   131
		TypedArray ta = c.getResources().obtainTypedArray(sourceResId);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   132
		int[] resIds = new int[ta.length()];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   133
		for(int i = 0; i < ta.length(); i++){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   134
			resIds[i] = ta.getResourceId(i, 0);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   135
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   136
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   137
		for(int id : resIds){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   138
			String fileName = c.getResources().getResourceEntryName(id);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   139
			File f = new File(schemesDirPath + fileName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   140
			try {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   141
				if(!f.createNewFile()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   142
					f.delete();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   143
					f.createNewFile();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   144
				}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   145
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   146
				bis = c.getResources().openRawResource(id);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   147
				bos = new BufferedOutputStream(new FileOutputStream(f), 1024);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   148
				int read = 0;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   149
				while((read = bis.read(buffer)) != -1){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   150
					bos.write(buffer, 0, read);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   151
				}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   152
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   153
			} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   154
				e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   155
			}finally{
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   156
				if(bis != null)
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   157
					try { 
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   158
						bis.close();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   159
					} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   160
						e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   161
					}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   162
				if(bos != null)
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   163
					try {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   164
						bos.close();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   165
					} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   166
						e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   167
					}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   168
			}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   169
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   170
	}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   171
}