project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java
author Xeli
Fri, 17 Feb 2012 21:34:33 +0100
changeset 6701 58a43c2064ad
parent 6485 7586c266b52e
child 6724 03cd33624284
permissions -rw-r--r--
the onScreenwidgets are multitouch now, frequently (alternating) tapping left and right still causes it to bug though, but you have to try hard to duplicate it, works ok for now

package org.hedgewars.hedgeroid.Downloader;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.hedgewars.hedgeroid.MainActivity;
import org.hedgewars.hedgeroid.Utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.AsyncTask;
import android.util.Log;

public class DownloadAssets extends AsyncTask<Object, Long, Long>{
	
	private MainActivity act;
	private static byte[] buffer = null;
	
	public DownloadAssets(MainActivity _act){
		act = _act;
	}
	
	public static Long copyFileOrDir(Context c, String path) {
	    AssetManager assetManager = c.getAssets();
	    String assets[] = null;
	    try {
	        assets = assetManager.list(path);
	        if (assets.length == 0) {
	            return DownloadAssets.copyFile(c, path);
	        } else {
	            String fullPath = Utils.getCachePath(c) + path;
	            File dir = new File(fullPath);
	            if (!dir.exists())
	                dir.mkdir();
	            for (int i = 0; i < assets.length; ++i) {
	                Long result = DownloadAssets.copyFileOrDir(c, path + "/" + assets[i]);
	                if(result > 0) return 1l;
	            }
	        }
	    } catch (IOException ex) {
	    	ex.printStackTrace();
	        Log.e("tag", "I/O Exception", ex);
	        return 1l;
	    }
	    return 0l;
	}
	
	private static Long copyFile(Context c, String filename) {
	    AssetManager assetManager = c.getAssets();

	    InputStream in = null;
	    OutputStream out = null;
	    try {
	        in = assetManager.open(filename);
	        in = new BufferedInputStream(in, 8192);
	        
	        String newFileName = Utils.getCachePath(c) + filename;
	        out = new FileOutputStream(newFileName);
	        out = new BufferedOutputStream(out, 8192);

	        int read;
	        while ((read = in.read(buffer)) != -1) {
	            out.write(buffer, 0, read);
	        }
	        in.close();
	        in = null;
	        out.flush();
	        out.close();
	        out = null;
	    } catch (Exception e) {
	    	e.printStackTrace();
	        Log.e("tag", e.getMessage());
	        return 1l;
	    }
	    return 0l;

	}

	protected Long doInBackground(Object... params) {
		buffer = new byte[8192];//allocate the buffer
		return DownloadAssets.copyFileOrDir(act, "Data");
	}
	
	protected void onPostExecute(Long result){
		act.onAssetsDownloaded(result == 0);
		buffer = null;
	}
}