project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
branchhedgeroid
changeset 6350 41b0a9955c47
child 6485 7586c266b52e
equal deleted inserted replaced
6348:162fec525764 6350:41b0a9955c47
       
     1 package org.hedgewars.hedgeroid.Downloader;
       
     2 
       
     3 import java.io.BufferedInputStream;
       
     4 import java.io.BufferedOutputStream;
       
     5 import java.io.File;
       
     6 import java.io.FileOutputStream;
       
     7 import java.io.IOException;
       
     8 import java.io.InputStream;
       
     9 import java.io.OutputStream;
       
    10 
       
    11 import org.hedgewars.hedgeroid.MainActivity;
       
    12 import org.hedgewars.hedgeroid.Utils;
       
    13 
       
    14 import android.content.Context;
       
    15 import android.content.res.AssetManager;
       
    16 import android.os.AsyncTask;
       
    17 import android.util.Log;
       
    18 
       
    19 public class DownloadAssets extends AsyncTask<Object, Long, Long>{
       
    20 	
       
    21 	private MainActivity act;
       
    22 	private static byte[] buffer = null;
       
    23 	
       
    24 	public DownloadAssets(MainActivity _act){
       
    25 		act = _act;
       
    26 	}
       
    27 	
       
    28 	
       
    29 	
       
    30 	public static Long copyFileOrDir(Context c, String path) {
       
    31 	    AssetManager assetManager = c.getAssets();
       
    32 	    String assets[] = null;
       
    33 	    try {
       
    34 	        assets = assetManager.list(path);
       
    35 	        if (assets.length == 0) {
       
    36 	            return DownloadAssets.copyFile(c, path);
       
    37 	        } else {
       
    38 	            String fullPath = Utils.getCachePath(c) + path;
       
    39 	            File dir = new File(fullPath);
       
    40 	            if (!dir.exists())
       
    41 	                dir.mkdir();
       
    42 	            for (int i = 0; i < assets.length; ++i) {
       
    43 	                Long result = DownloadAssets.copyFileOrDir(c, path + "/" + assets[i]);
       
    44 	                if(result > 0) return 1l;
       
    45 	            }
       
    46 	        }
       
    47 	    } catch (IOException ex) {
       
    48 	    	ex.printStackTrace();
       
    49 	        Log.e("tag", "I/O Exception", ex);
       
    50 	        return 1l;
       
    51 	    }
       
    52 	    return 0l;
       
    53 	}
       
    54 	
       
    55 	private static Long copyFile(Context c, String filename) {
       
    56 	    AssetManager assetManager = c.getAssets();
       
    57 
       
    58 	    InputStream in = null;
       
    59 	    OutputStream out = null;
       
    60 	    try {
       
    61 	        in = assetManager.open(filename);
       
    62 	        in = new BufferedInputStream(in, 8192);
       
    63 	        
       
    64 	        String newFileName = Utils.getCachePath(c) + filename;
       
    65 	        out = new FileOutputStream(newFileName);
       
    66 	        out = new BufferedOutputStream(out, 8192);
       
    67 
       
    68 	        int read;
       
    69 	        while ((read = in.read(buffer)) != -1) {
       
    70 	            out.write(buffer, 0, read);
       
    71 	        }
       
    72 	        in.close();
       
    73 	        in = null;
       
    74 	        out.flush();
       
    75 	        out.close();
       
    76 	        out = null;
       
    77 	    } catch (Exception e) {
       
    78 	    	e.printStackTrace();
       
    79 	        Log.e("tag", e.getMessage());
       
    80 	        return 1l;
       
    81 	    }
       
    82 	    return 0l;
       
    83 
       
    84 	}
       
    85 
       
    86 	protected Long doInBackground(Object... params) {
       
    87 		buffer = new byte[8192];//allocate the buffer
       
    88 		return DownloadAssets.copyFileOrDir(act, "Data");
       
    89 	}
       
    90 	
       
    91 	protected void onPostExecute(Long result){
       
    92 		act.onAssetsDownloaded(result == 0);
       
    93 		buffer = null;
       
    94 	}
       
    95 }