project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/Utils.java
author Xeli
Fri, 26 Aug 2011 21:03:23 +0200
branchhedgeroid
changeset 5663 b13d1897d06f
parent 5621 ea796c83ea47
permissions -rw-r--r--
Made getDownloadPath compatible with < Froyo devices
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5621
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     1
/*
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     3
 * Copyright (c) 2011 Richard Deurwaarder <xeli@xelification.com>
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     4
 *
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     8
 *
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    12
 * GNU General Public License for more details.
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    13
 *
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    14
 * You should have received a copy of the GNU General Public License
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    15
 * along with this program; if not, write to the Free Software
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    17
 */
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    18
ea796c83ea47 added licenses
Xeli
parents: 5473
diff changeset
    19
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    20
package org.hedgewars.mobile;
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
import java.io.BufferedOutputStream;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    23
import java.io.File;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    24
import java.io.FileOutputStream;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    25
import java.io.IOException;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    26
import java.io.InputStream;
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    27
import java.util.ArrayList;
5414
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
import android.content.Context;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    30
import android.content.res.TypedArray;
5663
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    31
import android.os.Build;
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    32
import android.os.Environment;
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    33
import android.widget.Toast;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    34
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    35
public class Utils {
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
	
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    38
	/**
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    39
	 * 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
    40
	 * @param c context 
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    41
	 * @return absolute path
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    42
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    43
	public static String getDownloadPath(Context c){
5663
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    44
		if(Build.VERSION.SDK_INT < 8){//8 == Build.VERSION_CODES.FROYO
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    45
			return PreFroyoSDCardDir.getDownloadPath(c);
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    46
		}else{
5663
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    47
			return FroyoSDCardDir.getDownloadPath(c);
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    48
		}
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    49
	}
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    50
	
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    51
	static class FroyoSDCardDir{
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    52
		public static String getDownloadPath(Context c){
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    53
			File f =  c.getExternalCacheDir();
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    54
			if(f != null){
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    55
				return f.getAbsolutePath() + "/Data/";
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    56
			}else{
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    57
				Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    58
				return null;
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    59
			}	
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    60
		}
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    61
	}
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    62
	
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    63
	static class PreFroyoSDCardDir{
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    64
		public static String getDownloadPath(Context c){
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    65
			if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    66
				if(Environment.getExternalStorageDirectory() != null)
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    67
					return Environment.getExternalStorageDirectory().getAbsolutePath() + "/Hedgewars/";				
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    68
			}
b13d1897d06f Made getDownloadPath compatible with < Froyo devices
Xeli
parents: 5621
diff changeset
    69
			Toast.makeText(c, R.string.sdcard_not_mounted, Toast.LENGTH_LONG).show();
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    70
			return null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    71
		}
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
	 * Get files from dirName, dir name is relative to {@link getDownloadPath}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    76
	 * @param dirName
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    77
	 * @param c context
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    78
	 * @return string of files
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
	public static String[] getFileNamesFromRelativeDir(Context c, String dirName){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    81
		String prefix = getDownloadPath(c);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    82
		File f = new File(prefix + dirName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    83
		
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    84
		if(f.exists() && f.isDirectory()) return f.list();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    85
		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
    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
	/**
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
    89
	 * Return a File array with all the files from dirName
5414
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 dirName
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    92
	 * @return
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 File[] getFilesFromRelativeDir(Context c, String dirName){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    95
		String prefix = getDownloadPath(c);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    96
		File f = new File(prefix + dirName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    97
		
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    98
		if(f.exists() && f.isDirectory()) return f.listFiles();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
    99
		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
   100
	}
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
	/**
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   103
	 * Checks if this directory has a file with suffix suffix
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   104
	 * @param f - directory
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   105
	 * @return
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   106
	 */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   107
	public static boolean hasFileWithSuffix(File f, String suffix){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   108
		if(f.isDirectory()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   109
			for(String s : f.list()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   110
				if(s.endsWith(suffix)) return true;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   111
			}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   112
			return false;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   113
		}else{
5428
5d0c5f7a9339 Fixed a small bug for when detecting Themes
Xeli
parents: 5414
diff changeset
   114
			return false;
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   115
		}
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
	
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   118
	/**
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   119
	 * 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
   120
	 * @param c
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   121
	 * @param path
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   122
	 * @param fileSuffix
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   123
	 * @return
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   124
	 */
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   125
	public static String[] getDirsWithFileSuffix(Context c, String path, String fileSuffix){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   126
		File[] files = getFilesFromRelativeDir(c,path);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   127
		String[] validFiles = new String[files.length];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   128
		int validCounter = 0;
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
		for(File f : files){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   131
			if(hasFileWithSuffix(f, fileSuffix)) validFiles[validCounter++] = f.getName();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   132
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   133
		String[] ret = new String[validCounter];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   134
		System.arraycopy(validFiles, 0, ret, 0, validCounter);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   135
		return ret;
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
	
5473
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   138
	/**
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   139
	 * 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
   140
	 * @param c
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   141
	 * @param dir
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   142
	 * @param suffix
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   143
	 * @param removeSuffix
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   144
	 * @return
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   145
	 */
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   146
	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
   147
		String[] files = Utils.getFileNamesFromRelativeDir(c, dir);
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   148
		ArrayList<String> ret = new ArrayList<String>();
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   149
		for(String s : files){
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   150
			if(s.endsWith(suffix)){
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   151
				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
   152
				else ret.add(s);
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   153
			}
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   154
		}
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   155
		return ret;
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   156
	}
a68f900c4e8c Utility functions which are context wide, mostly SD methods right now
Xeli
parents: 5428
diff changeset
   157
	
5414
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   158
    /**
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   159
     * 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
   160
     * @param c
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   161
     * @param sourceResId
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   162
     * @param directory
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   163
     */
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   164
	public static void resRawToFilesDir(Context c, int sourceResId, String directory){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   165
		byte[] buffer = new byte[1024];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   166
		InputStream bis = null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   167
		BufferedOutputStream bos = null;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   168
		File schemesDirFile = new File(c.getFilesDir().getAbsolutePath() + '/' + directory);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   169
		schemesDirFile.mkdirs();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   170
		String schemesDirPath = schemesDirFile.getAbsolutePath() + '/';
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   171
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   172
		//Get an array with the resource files ID
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   173
		TypedArray ta = c.getResources().obtainTypedArray(sourceResId);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   174
		int[] resIds = new int[ta.length()];
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   175
		for(int i = 0; i < ta.length(); i++){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   176
			resIds[i] = ta.getResourceId(i, 0);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   177
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   178
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   179
		for(int id : resIds){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   180
			String fileName = c.getResources().getResourceEntryName(id);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   181
			File f = new File(schemesDirPath + fileName);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   182
			try {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   183
				if(!f.createNewFile()){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   184
					f.delete();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   185
					f.createNewFile();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   186
				}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   187
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   188
				bis = c.getResources().openRawResource(id);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   189
				bos = new BufferedOutputStream(new FileOutputStream(f), 1024);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   190
				int read = 0;
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   191
				while((read = bis.read(buffer)) != -1){
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   192
					bos.write(buffer, 0, read);
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   193
				}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   194
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   195
			} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   196
				e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   197
			}finally{
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   198
				if(bis != null)
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   199
					try { 
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   200
						bis.close();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   201
					} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   202
						e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   203
					}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   204
				if(bos != null)
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   205
					try {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   206
						bos.close();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   207
					} catch (IOException e) {
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   208
						e.printStackTrace();
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   209
					}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   210
			}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   211
		}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   212
	}
34663c4743f7 Added a utility class needed by the Downloaders
Xeli
parents:
diff changeset
   213
}