From fe2be33318dc8d83778708da468369e1e4f79cbe Mon Sep 17 00:00:00 2001 From: Maurus Cuelenaere Date: Fri, 5 Nov 2010 23:40:17 +0000 Subject: Android: clean up extracting a bit + add user-visible error-reporting Also put ResultReceiver on the RockboxActivity UI thread. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28509 a1c6a512-1295-4272-9138-f99709370657 --- android/src/org/rockbox/RockboxActivity.java | 43 +++++---- android/src/org/rockbox/RockboxService.java | 125 +++++++++++++-------------- 2 files changed, 81 insertions(+), 87 deletions(-) (limited to 'android/src/org') diff --git a/android/src/org/rockbox/RockboxActivity.java b/android/src/org/rockbox/RockboxActivity.java index 15576f4adb..ec22885c51 100644 --- a/android/src/org/rockbox/RockboxActivity.java +++ b/android/src/org/rockbox/RockboxActivity.java @@ -26,12 +26,14 @@ import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; +import android.os.Handler; import android.os.ResultReceiver; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; +import android.widget.Toast; public class RockboxActivity extends Activity { @@ -58,31 +60,28 @@ public class RockboxActivity extends Activity loadingdialog.show(); Intent intent = new Intent(this, RockboxService.class); - intent.putExtra("callback", new ResultReceiver(null) { + intent.putExtra("callback", new ResultReceiver(new Handler(getMainLooper())) { @Override protected void onReceiveResult(final int resultCode, final Bundle resultData) { - runOnUiThread(new Runnable() - { - public void run() { - switch (resultCode) { - case RockboxService.RESULT_LIB_LOADED: - rbservice = RockboxService.get_instance(); - loadingdialog.setIndeterminate(true); - break; - case RockboxService.RESULT_LIB_LOAD_PROGRESS: - loadingdialog.setIndeterminate(false); - loadingdialog.setMax(resultData.getInt("max", 100)); - loadingdialog.setProgress(resultData.getInt("value", 0)); - break; - case RockboxService.RESULT_FB_INITIALIZED: - attachFramebuffer(); - loadingdialog.dismiss(); - break; - } - - } - }); + switch (resultCode) { + case RockboxService.RESULT_LIB_LOADED: + rbservice = RockboxService.get_instance(); + loadingdialog.setIndeterminate(true); + break; + case RockboxService.RESULT_LIB_LOAD_PROGRESS: + loadingdialog.setIndeterminate(false); + loadingdialog.setMax(resultData.getInt("max", 100)); + loadingdialog.setProgress(resultData.getInt("value", 0)); + break; + case RockboxService.RESULT_FB_INITIALIZED: + attachFramebuffer(); + loadingdialog.dismiss(); + break; + case RockboxService.RESULT_ERROR_OCCURED: + Toast.makeText(RockboxActivity.this, resultData.getString("error"), Toast.LENGTH_LONG); + break; + } } }); startService(intent); diff --git a/android/src/org/rockbox/RockboxService.java b/android/src/org/rockbox/RockboxService.java index 4d61dea7ff..e788251508 100644 --- a/android/src/org/rockbox/RockboxService.java +++ b/android/src/org/rockbox/RockboxService.java @@ -24,9 +24,7 @@ package org.rockbox; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.IOException; import java.util.Enumeration; import java.util.Timer; import java.util.TimerTask; @@ -73,6 +71,7 @@ public class RockboxService extends Service public static final int RESULT_LIB_LOADED = 0; public static final int RESULT_LIB_LOAD_PROGRESS = 1; public static final int RESULT_FB_INITIALIZED = 2; + public static final int RESULT_ERROR_OCCURED = 3; @Override public void onCreate() @@ -152,81 +151,77 @@ public class RockboxService extends Service { public void run() { - LOG("main"); - Bundle progressData = new Bundle(); + File rockboxDir = new File("/data/data/org.rockbox/app_rockbox/rockbox/"); + /* the following block unzips libmisc.so, which contains the files * we ship, such as themes. It's needed to put it into a .so file * because there's no other way to ship files and have access * to them from native code */ - try - { - BufferedOutputStream dest = null; - BufferedInputStream is = null; - ZipEntry entry; - File file = new File("/data/data/org.rockbox/" + - "lib/libmisc.so"); - /* use arbitrary file to determine whether extracting is needed */ - File file2 = new File("/data/data/org.rockbox/" + - "app_rockbox/rockbox/codecs/mpa.codec"); - if (!file2.exists() || (file.lastModified() > file2.lastModified())) - { - ZipFile zipfile = new ZipFile(file); - Enumeration e = zipfile.entries(); - File folder; - progressData.putInt("max", zipfile.size()); - while(e.hasMoreElements()) - { - entry = (ZipEntry) e.nextElement(); - LOG("Extracting: " +entry); - if (entry.isDirectory()) - { - folder = new File(entry.getName()); - LOG("mkdir "+ entry); - try { - folder.mkdirs(); - } catch (SecurityException ex) { - LOG(ex.getMessage()); - } - continue; - } - is = new BufferedInputStream(zipfile.getInputStream(entry), - BUFFER); - int count; - byte data[] = new byte[BUFFER]; - folder = new File(new File(entry.getName()).getParent()); - LOG("" + folder.getAbsolutePath()); - if (!folder.exists()) - folder.mkdirs(); - FileOutputStream fos = new FileOutputStream(entry.getName()); - dest = new BufferedOutputStream(fos, BUFFER); - while ((count = is.read(data, 0, BUFFER)) != -1) - dest.write(data, 0, count); - dest.flush(); - dest.close(); - is.close(); - if (resultReceiver != null) { - progressData.putInt("value", progressData.getInt("value", 0) + 1); - resultReceiver.send(RESULT_LIB_LOAD_PROGRESS, progressData); - } - } - } - } catch(FileNotFoundException e) { - LOG("FileNotFoundException when unzipping", e); - e.printStackTrace(); - } catch(IOException e) { - LOG("IOException when unzipping", e); - e.printStackTrace(); - } - + File libMisc = new File("/data/data/org.rockbox/lib/libmisc.so"); + /* use arbitrary file to determine whether extracting is needed */ + File arbitraryFile = new File(rockboxDir, "viewers.config"); + if (!arbitraryFile.exists() || (libMisc.lastModified() > arbitraryFile.lastModified())) + { + try + { + Bundle progressData = new Bundle(); + byte data[] = new byte[BUFFER]; + ZipFile zipfile = new ZipFile(libMisc); + Enumeration e = zipfile.entries(); + progressData.putInt("max", zipfile.size()); + + while(e.hasMoreElements()) + { + ZipEntry entry = (ZipEntry) e.nextElement(); + File file = new File(entry.getName()); + + if (!entry.isDirectory()) + { + /* Create the parent folders if necessary */ + File folder = new File(file.getParent()); + if (!folder.exists()) + folder.mkdirs(); + + /* Extract file */ + BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry), BUFFER); + FileOutputStream fos = new FileOutputStream(file); + BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER); + + int count; + while ((count = is.read(data, 0, BUFFER)) != -1) + dest.write(data, 0, count); + + dest.flush(); + dest.close(); + is.close(); + } + + if (resultReceiver != null) { + progressData.putInt("value", progressData.getInt("value", 0) + 1); + resultReceiver.send(RESULT_LIB_LOAD_PROGRESS, progressData); + } + } + } catch(Exception e) { + LOG("Exception when unzipping", e); + e.printStackTrace(); + if (resultReceiver != null) { + Bundle bundle = new Bundle(); + bundle.putString("error", "Error occured during extraction!"); + resultReceiver.send(RESULT_ERROR_OCCURED, bundle); + } + } + } + System.loadLibrary("rockbox"); rbLibLoaded = true; if (resultReceiver != null) resultReceiver.send(RESULT_LIB_LOADED, null); + main(); throw new IllegalStateException("native main() returned!"); } - },"Rockbox thread"); + }, "Rockbox thread"); rb.setDaemon(false); rb.start(); } -- cgit v1.2.3