diff -r 162fec525764 -r 41b0a9955c47 project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/Downloader/DownloadAssets.java Thu Nov 24 13:44:30 2011 +0100 @@ -0,0 +1,95 @@ +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{ + + 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; + } +}