1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Maurus Cuelenaere 2010-11-05 23:40:17 +00:00
parent 4f747c1aaa
commit fe2be33318
2 changed files with 81 additions and 87 deletions

View file

@ -26,12 +26,14 @@ import android.app.Activity;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.Toast;
public class RockboxActivity extends Activity public class RockboxActivity extends Activity
{ {
@ -58,13 +60,10 @@ public class RockboxActivity extends Activity
loadingdialog.show(); loadingdialog.show();
Intent intent = new Intent(this, RockboxService.class); Intent intent = new Intent(this, RockboxService.class);
intent.putExtra("callback", new ResultReceiver(null) { intent.putExtra("callback", new ResultReceiver(new Handler(getMainLooper())) {
@Override @Override
protected void onReceiveResult(final int resultCode, final Bundle resultData) protected void onReceiveResult(final int resultCode, final Bundle resultData)
{ {
runOnUiThread(new Runnable()
{
public void run() {
switch (resultCode) { switch (resultCode) {
case RockboxService.RESULT_LIB_LOADED: case RockboxService.RESULT_LIB_LOADED:
rbservice = RockboxService.get_instance(); rbservice = RockboxService.get_instance();
@ -79,10 +78,10 @@ public class RockboxActivity extends Activity
attachFramebuffer(); attachFramebuffer();
loadingdialog.dismiss(); loadingdialog.dismiss();
break; break;
case RockboxService.RESULT_ERROR_OCCURED:
Toast.makeText(RockboxActivity.this, resultData.getString("error"), Toast.LENGTH_LONG);
break;
} }
}
});
} }
}); });
startService(intent); startService(intent);

View file

@ -24,9 +24,7 @@ package org.rockbox;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; 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_LOADED = 0;
public static final int RESULT_LIB_LOAD_PROGRESS = 1; public static final int RESULT_LIB_LOAD_PROGRESS = 1;
public static final int RESULT_FB_INITIALIZED = 2; public static final int RESULT_FB_INITIALIZED = 2;
public static final int RESULT_ERROR_OCCURED = 3;
@Override @Override
public void onCreate() public void onCreate()
@ -152,77 +151,73 @@ public class RockboxService extends Service
{ {
public void run() public void run()
{ {
LOG("main"); File rockboxDir = new File("/data/data/org.rockbox/app_rockbox/rockbox/");
Bundle progressData = new Bundle();
/* the following block unzips libmisc.so, which contains the files /* 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 * 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 * because there's no other way to ship files and have access
* to them from native code * to them from native code
*/ */
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 try
{ {
BufferedOutputStream dest = null; Bundle progressData = new Bundle();
BufferedInputStream is = null; byte data[] = new byte[BUFFER];
ZipEntry entry; ZipFile zipfile = new ZipFile(libMisc);
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<? extends ZipEntry> e = zipfile.entries(); Enumeration<? extends ZipEntry> e = zipfile.entries();
File folder;
progressData.putInt("max", zipfile.size()); progressData.putInt("max", zipfile.size());
while(e.hasMoreElements()) while(e.hasMoreElements())
{ {
entry = (ZipEntry) e.nextElement(); ZipEntry entry = (ZipEntry) e.nextElement();
LOG("Extracting: " +entry); File file = new File(entry.getName());
if (entry.isDirectory())
if (!entry.isDirectory())
{ {
folder = new File(entry.getName()); /* Create the parent folders if necessary */
LOG("mkdir "+ entry); File folder = new File(file.getParent());
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()) if (!folder.exists())
folder.mkdirs(); folder.mkdirs();
FileOutputStream fos = new FileOutputStream(entry.getName());
dest = new BufferedOutputStream(fos, BUFFER); /* 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) while ((count = is.read(data, 0, BUFFER)) != -1)
dest.write(data, 0, count); dest.write(data, 0, count);
dest.flush(); dest.flush();
dest.close(); dest.close();
is.close(); is.close();
}
if (resultReceiver != null) { if (resultReceiver != null) {
progressData.putInt("value", progressData.getInt("value", 0) + 1); progressData.putInt("value", progressData.getInt("value", 0) + 1);
resultReceiver.send(RESULT_LIB_LOAD_PROGRESS, progressData); 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);
}
} }
} catch(FileNotFoundException e) {
LOG("FileNotFoundException when unzipping", e);
e.printStackTrace();
} catch(IOException e) {
LOG("IOException when unzipping", e);
e.printStackTrace();
} }
System.loadLibrary("rockbox"); System.loadLibrary("rockbox");
rbLibLoaded = true; rbLibLoaded = true;
if (resultReceiver != null) if (resultReceiver != null)
resultReceiver.send(RESULT_LIB_LOADED, null); resultReceiver.send(RESULT_LIB_LOADED, null);
main(); main();
throw new IllegalStateException("native main() returned!"); throw new IllegalStateException("native main() returned!");
} }