project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Utils.java
author Xeli
Thu, 14 Jul 2011 15:42:01 +0200
branchhedgeroid
changeset 5414 34663c4743f7
child 5428 5d0c5f7a9339
permissions -rw-r--r--
Added a utility class needed by the Downloaders
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;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     8
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
     9
import android.content.Context;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    10
import android.content.res.TypedArray;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    11
import android.widget.Toast;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    12
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    13
public class Utils {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    14
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
	 * 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
    18
	 * @param c context 
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    19
	 * @return absolute path
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    20
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    21
	public static String getDownloadPath(Context c){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    22
		File f =  c.getExternalCacheDir();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    23
		if(f != null){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    24
			return f.getAbsolutePath() + "/Data/";
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    25
		}else{
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    26
			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    27
			return null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    28
		}
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
	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    33
	 * @param dirName
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    34
	 * @param c context
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    35
	 * @return string of files
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    36
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    37
	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    38
		String prefix = getDownloadPath(c);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    39
		File f = new File(prefix + dirName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    40
		
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    41
		if(f.exists() && f.isDirectory()) return f.list();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    42
		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
    43
	}
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
	 * Return a File array if all the files from dirName
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    47
	 * @param c
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    48
	 * @param dirName
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    49
	 * @return
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    50
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    51
	public static File[] getFilesFromRelativeDir(Context c, String dirName){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    52
		String prefix = getDownloadPath(c);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    53
		File f = new File(prefix + dirName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    54
		
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    55
		if(f.exists() && f.isDirectory()) return f.listFiles();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    56
		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
    57
	}
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
	 * Checks if this directory has a lua file
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    61
	 * @param f - directory
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    62
	 * @return
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    63
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    64
	public static boolean hasFileWithSuffix(File f, String suffix){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    65
		if(f.isDirectory()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    66
			for(String s : f.list()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    67
				if(s.endsWith(suffix)) return true;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    68
			}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    69
			return false;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    70
		}else{
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    71
			throw new IllegalArgumentException("Not a directory.. f = " + f.getAbsolutePath());
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    72
		}
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
	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    76
		File[] files = getFilesFromRelativeDir(c,path);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    77
		String[] validFiles = new String[files.length];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    78
		int validCounter = 0;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    79
		
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    80
		for(File f : files){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    81
			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    82
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    83
		String[] ret = new String[validCounter];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    84
		System.arraycopy(validFiles, 0, ret, 0, validCounter);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    85
		return ret;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    86
	}
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
    /**
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    89
     * 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
    90
     * @param c
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    91
     * @param sourceResId
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    92
     * @param directory
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    93
     */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    94
	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    95
		byte[] buffer = new byte[1024];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    96
		InputStream bis = null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    97
		BufferedOutputStream bos = null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    98
		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    99
		schemesDirFile.mkdirs();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   100
		String schemesDirPath = schemesDirFile.getAbsolutePath() + '/';
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   101
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   102
		//Get an array with the resource files ID
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   103
		TypedArray ta = c.getResources().obtainTypedArray(sourceResId);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   104
		int[] resIds = new int[ta.length()];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   105
		for(int i = 0; i < ta.length(); i++){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   106
			resIds[i] = ta.getResourceId(i, 0);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   107
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   108
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   109
		for(int id : resIds){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   110
			String fileName = c.getResources().getResourceEntryName(id);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   111
			File f = new File(schemesDirPath + fileName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   112
			try {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   113
				if(!f.createNewFile()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   114
					f.delete();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   115
					f.createNewFile();
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
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   118
				bis = c.getResources().openRawResource(id);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   119
				bos = new BufferedOutputStream(new FileOutputStream(f), 1024);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   120
				int read = 0;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   121
				while((read = bis.read(buffer)) != -1){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   122
					bos.write(buffer, 0, read);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   123
				}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   124
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   125
			} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   126
				e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   127
			}finally{
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   128
				if(bis != null)
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   129
					try { 
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   130
						bis.close();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   131
					} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   132
						e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   133
					}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   134
				if(bos != null)
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   135
					try {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   136
						bos.close();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   137
					} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   138
						e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   139
					}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   140
			}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   141
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   142
	}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   143
}