project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7584 7831c84cc644
child 10017 de822cd3df3a
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
       
     4  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     5  *
       
     6  * This program is free software; you can redistribute it and/or
       
     7  * modify it under the terms of the GNU General Public License
       
     8  * as published by the Free Software Foundation; either version 2
       
     9  * of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful,
       
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  * GNU General Public License for more details.
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License
       
    17  * along with this program; if not, write to the Free Software
       
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    19  */
       
    20 
     1 package org.hedgewars.hedgeroid.Downloader;
    21 package org.hedgewars.hedgeroid.Downloader;
     2 
    22 
     3 import java.io.BufferedInputStream;
       
     4 import java.io.BufferedOutputStream;
       
     5 import java.io.File;
    23 import java.io.File;
     6 import java.io.FileOutputStream;
    24 import java.io.FileNotFoundException;
     7 import java.io.IOException;
    25 import java.io.IOException;
     8 import java.io.InputStream;
       
     9 import java.io.OutputStream;
       
    10 
    26 
    11 import org.hedgewars.hedgeroid.MainActivity;
    27 import org.hedgewars.hedgeroid.MainActivity;
    12 import org.hedgewars.hedgeroid.R;
    28 import org.hedgewars.hedgeroid.R;
    13 import org.hedgewars.hedgeroid.Utils;
    29 import org.hedgewars.hedgeroid.Datastructures.Schemes;
    14 import org.hedgewars.hedgeroid.Datastructures.Scheme;
       
    15 import org.hedgewars.hedgeroid.Datastructures.Team;
    30 import org.hedgewars.hedgeroid.Datastructures.Team;
    16 import org.hedgewars.hedgeroid.Datastructures.Weapon;
    31 import org.hedgewars.hedgeroid.Datastructures.Weaponsets;
       
    32 import org.hedgewars.hedgeroid.util.FileUtils;
    17 
    33 
    18 import android.content.Context;
       
    19 import android.content.res.AssetManager;
    34 import android.content.res.AssetManager;
    20 import android.os.AsyncTask;
    35 import android.os.AsyncTask;
    21 import android.util.Log;
    36 import android.util.Log;
    22 
    37 
    23 public class DownloadAssets extends AsyncTask<Object, Long, Long>{
    38 public class DownloadAssets extends AsyncTask<Object, Long, Boolean> {
       
    39 	private static final String VERSION_FILENAME = "assetsversion.txt";
       
    40 	private final MainActivity act;
    24 	
    41 	
    25 	private MainActivity act;
    42 	public DownloadAssets(MainActivity act){
    26 	private static byte[] buffer = null;
    43 		this.act = act;
    27 	
       
    28 	public DownloadAssets(MainActivity _act){
       
    29 		act = _act;
       
    30 	}
    44 	}
    31 	
    45 	
    32 	public static Long copyFileOrDir(Context c, String path) {
    46 	private void copyFileOrDir(AssetManager assetManager, File target, String assetPath) throws IOException {
    33 	    AssetManager assetManager = c.getAssets();
    47 		try {
    34 	    String assets[] = null;
    48 			FileUtils.writeStreamToFile(assetManager.open(assetPath), target);
    35 	    try {
    49 		} catch(FileNotFoundException e) {
    36 	        assets = assetManager.list(path);
    50 			/*
    37 	        if (assets.length == 0) {
    51 			 * I can't find a better way to figure out whether an asset entry is
    38 	            return DownloadAssets.copyFile(c, path);
    52 			 * a file or a directory. Checking if assetManager.list(assetPath)
    39 	        } else {
    53 			 * is empty is a bit cleaner, but SLOW.
    40 	            String fullPath = Utils.getCachePath(c) + path;
    54 			 */
    41 	            File dir = new File(fullPath);
    55 			if (!target.isDirectory() && !target.mkdir()) {
    42 	            if (!dir.exists())
    56 				throw new IOException("Unable to create directory "+target);
    43 	                dir.mkdir();
    57 			}
    44 	            for (int i = 0; i < assets.length; ++i) {
    58 			for (String asset : assetManager.list(assetPath)) {
    45 	                Long result = DownloadAssets.copyFileOrDir(c, path + "/" + assets[i]);
    59 				copyFileOrDir(assetManager, new File(target, asset), assetPath + "/" + asset);
    46 	                if(result > 0) return 1l;
    60 			}
    47 	            }
    61 		}
    48 	        }
       
    49 	    } catch (IOException ex) {
       
    50 	    	ex.printStackTrace();
       
    51 	        Log.e("tag", "I/O Exception", ex);
       
    52 	        return 1l;
       
    53 	    }
       
    54 	    return 0l;
       
    55 	}
    62 	}
    56 	
    63 	
    57 	private static Long copyFile(Context c, String filename) {
    64 	@Override
    58 	    AssetManager assetManager = c.getAssets();
    65 	protected Boolean doInBackground(Object... params) {
    59 
    66 		try {
    60 	    InputStream in = null;
    67 			FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.schemes_builtin), Schemes.getBuiltinSchemesFile(act));
    61 	    OutputStream out = null;
    68 			FileUtils.writeStreamToFile(act.getResources().openRawResource(R.raw.weapons_builtin), Weaponsets.getBuiltinWeaponsetsFile(act));
    62 	    try {
    69 			FileUtils.resRawToFilesDir(act, R.array.teams, R.array.teamFilenames, Team.DIRECTORY_TEAMS);
    63 	        in = assetManager.open(filename);
    70 			copyFileOrDir(act.getAssets(), FileUtils.getDataPathFile(act), "Data");
    64 	        in = new BufferedInputStream(in, 8192);
    71 			copyFileOrDir(act.getAssets(), new File(FileUtils.getCachePath(act), VERSION_FILENAME), VERSION_FILENAME);
    65 	        
    72 			return Boolean.TRUE;
    66 	        String newFileName = Utils.getCachePath(c) + filename;
    73 		} catch(IOException e) {
    67 	        out = new FileOutputStream(newFileName);
    74 			Log.e("DownloadAssets", e.getMessage(), e);
    68 	        out = new BufferedOutputStream(out, 8192);
    75 			return Boolean.FALSE;
    69 
    76 		}
    70 	        int read;
       
    71 	        while ((read = in.read(buffer)) != -1) {
       
    72 	            out.write(buffer, 0, read);
       
    73 	        }
       
    74 	        in.close();
       
    75 	        in = null;
       
    76 	        out.flush();
       
    77 	        out.close();
       
    78 	        out = null;
       
    79 	    } catch (Exception e) {
       
    80 	    	e.printStackTrace();
       
    81 	        Log.e("tag", e.getMessage());
       
    82 	        return 1l;
       
    83 	    }
       
    84 	    return 0l;
       
    85 
       
    86 	}
       
    87 
       
    88 	protected Long doInBackground(Object... params) {
       
    89 		Utils.resRawToFilesDir(act,R.array.schemes, Scheme.DIRECTORY_SCHEME);
       
    90 		Utils.resRawToFilesDir(act, R.array.weapons, Weapon.DIRECTORY_WEAPON);
       
    91 		Utils.resRawToFilesDir(act, R.array.teams, Team.DIRECTORY_TEAMS);
       
    92 		buffer = new byte[8192];//allocate the buffer
       
    93 		return DownloadAssets.copyFileOrDir(act, "Data");
       
    94 	}
    77 	}
    95 	
    78 	
    96 	protected void onPostExecute(Long result){
    79 	@Override
    97 		act.onAssetsDownloaded(result == 0);
    80 	protected void onPostExecute(Boolean result){
    98 		buffer = null;
    81 		act.onAssetsDownloaded(result);
    99 	}
    82 	}
   100 }
    83 }