From 26f7ee13ce024b21c83cf68ca7fd27306a0bf5be Mon Sep 17 00:00:00 2001 From: Jonathan Gordon Date: Sun, 31 Oct 2010 10:35:42 +0000 Subject: Clean up usage of RockboxService. Add a proper way to check if rockbox is actually running (checking RockboxService.fb != null was very very bad) git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28406 a1c6a512-1295-4272-9138-f99709370657 --- android/src/org/rockbox/RockboxActivity.java | 42 ++++++++++++++---------- android/src/org/rockbox/RockboxPCM.java | 4 +-- android/src/org/rockbox/RockboxService.java | 49 ++++++++++++++++++++++++---- firmware/target/hosted/android/lcd-android.c | 8 ++--- 4 files changed, 71 insertions(+), 32 deletions(-) diff --git a/android/src/org/rockbox/RockboxActivity.java b/android/src/org/rockbox/RockboxActivity.java index 6226455deb..f775597d30 100644 --- a/android/src/org/rockbox/RockboxActivity.java +++ b/android/src/org/rockbox/RockboxActivity.java @@ -32,6 +32,7 @@ import android.view.WindowManager; public class RockboxActivity extends Activity { private ProgressDialog loadingdialog; + private RockboxService rbservice; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) @@ -40,8 +41,7 @@ public class RockboxActivity extends Activity requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,WindowManager.LayoutParams.FLAG_FULLSCREEN); - final Intent intent = new Intent(this, - RockboxService.class); + final Intent intent = new Intent(this, RockboxService.class); /* prepare a please wait dialog in case we need * to wait for unzipping libmisc.so */ @@ -50,6 +50,7 @@ public class RockboxActivity extends Activity loadingdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); loadingdialog.setCancelable(false); startService(intent); + rbservice = RockboxService.get_instance(); /* Now it gets a bit tricky: * The service is started in the same thread as we are now, * but the service also initializes the framebuffer @@ -68,8 +69,8 @@ public class RockboxActivity extends Activity while (true) { Thread.sleep(250); - if (RockboxService.fb != null) - break; + if (isRockboxRunning()) + break; /* if it's still null show the please wait dialog * but not before 0.5s are over */ if (!loadingdialog.isShowing() && i > 0) @@ -92,32 +93,37 @@ public class RockboxActivity extends Activity { public void run() { loadingdialog.dismiss(); - if (RockboxService.fb == null) + if (rbservice.get_fb() == null) throw new IllegalStateException("FB NULL"); - setContentView(RockboxService.fb); - RockboxService.fb.invalidate(); + setContentView(rbservice.get_fb()); + rbservice.get_fb().invalidate(); } }); } }).start(); } + private boolean isRockboxRunning() + { + if (rbservice == null) + rbservice = RockboxService.get_instance(); + return (rbservice!= null && rbservice.isRockboxRunning() == true); + } public void onResume() { super.onResume(); - - if (RockboxService.fb != null) + if (isRockboxRunning()) { try { - setContentView(RockboxService.fb); + setContentView(rbservice.get_fb()); } catch (IllegalStateException e) { /* we are already using the View, * need to remove it and re-attach it */ - ViewGroup g = (ViewGroup)RockboxService.fb.getParent(); - g.removeView(RockboxService.fb); - setContentView(RockboxService.fb); + ViewGroup g = (ViewGroup)rbservice.get_fb().getParent(); + g.removeView(rbservice.get_fb()); + setContentView(rbservice.get_fb()); } finally { - RockboxService.fb.resume(); + rbservice.get_fb().resume(); } } } @@ -129,20 +135,20 @@ public class RockboxActivity extends Activity protected void onPause() { super.onPause(); - RockboxService.fb.suspend(); + rbservice.get_fb().suspend(); } @Override protected void onStop() { super.onStop(); - RockboxService.fb.suspend(); + rbservice.get_fb().suspend(); } @Override protected void onDestroy() { super.onDestroy(); - RockboxService.fb.suspend(); + rbservice.get_fb().suspend(); } -} \ No newline at end of file +} diff --git a/android/src/org/rockbox/RockboxPCM.java b/android/src/org/rockbox/RockboxPCM.java index 46bdd121f0..146e639a08 100644 --- a/android/src/org/rockbox/RockboxPCM.java +++ b/android/src/org/rockbox/RockboxPCM.java @@ -86,7 +86,7 @@ public class RockboxPCM extends AudioTrack { if (getPlayState() == AudioTrack.PLAYSTATE_STOPPED) { - RockboxService.startForeground(); + RockboxService.get_instance().startForeground(); if (getState() == AudioTrack.STATE_INITIALIZED) { if (h == null) @@ -113,7 +113,7 @@ public class RockboxPCM extends AudioTrack } catch (IllegalStateException e) { throw new IllegalStateException(e); } - RockboxService.stopForeground(); + RockboxService.get_instance().stopForeground(); } @SuppressWarnings("unused") diff --git a/android/src/org/rockbox/RockboxService.java b/android/src/org/rockbox/RockboxService.java index be02342768..c403736c72 100644 --- a/android/src/org/rockbox/RockboxService.java +++ b/android/src/org/rockbox/RockboxService.java @@ -46,11 +46,22 @@ import android.content.IntentFilter; import android.os.IBinder; import android.util.Log; +/* This class is used as the main glue between java and c. + * All access should be done through RockboxService.get_instance() for safety. + */ + public class RockboxService extends Service { - /* this Service is really a singleton class */ - public static RockboxFramebuffer fb = null; - private static RockboxService instance; + /* this Service is really a singleton class - well almost. + * To do it properly this line should be instance = new RockboxService() + * but apparently that doesnt work with the way android Services are created. + */ + private static RockboxService instance = null; + + /* locals needed for the c code and rockbox state */ + private RockboxFramebuffer fb = null; + private boolean mRockboxRunning = false; + private Notification notification; private static final Class[] mStartForegroundSignature = new Class[] { int.class, Notification.class }; @@ -70,6 +81,7 @@ public class RockboxService extends Service @Override public void onCreate() { + instance = this; mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); try { @@ -83,9 +95,24 @@ public class RockboxService extends Service /* Running on an older platform: fall back to old API */ mStartForeground = mStopForeground = null; } - instance = this; startservice(); } + + public static RockboxService get_instance() + { + return instance; + } + + public RockboxFramebuffer get_fb() + { + return fb; + } + /* framebuffer is initialised by the native code(!) so this is needed */ + public void set_fb(RockboxFramebuffer newfb) + { + fb = newfb; + mRockboxRunning = true; + } private void do_start(Intent intent) { @@ -190,8 +217,16 @@ public class RockboxService extends Service rb.setDaemon(false); rb.start(); } - private native void main(); + + /* returns true once rockbox is up and running. + * This is considered done once the framebuffer is initialised + */ + public boolean isRockboxRunning() + { + return mRockboxRunning; + } + @Override public IBinder onBind(Intent intent) { @@ -262,7 +297,7 @@ public class RockboxService extends Service getText(R.string.notification), text, contentIntent); } - public static void startForeground() + public void startForeground() { if (instance != null) { @@ -282,7 +317,7 @@ public class RockboxService extends Service } } - public static void stopForeground() + public void stopForeground() { if (instance.notification != null) { diff --git a/firmware/target/hosted/android/lcd-android.c b/firmware/target/hosted/android/lcd-android.c index fc9e22a921..75bf7a6d1d 100644 --- a/firmware/target/hosted/android/lcd-android.c +++ b/firmware/target/hosted/android/lcd-android.c @@ -78,11 +78,9 @@ void lcd_init_device(void) "java_lcd_update_rect", "(IIII)V"); - /* at last, give RockboxService the Framebuffer instance */ - jfieldID id = e->GetStaticFieldID(env_ptr, RockboxService_class, - "fb", "Lorg/rockbox/RockboxFramebuffer;"); - e->SetStaticObjectField(env_ptr, RockboxService_class, - id, RockboxFramebuffer_instance); + jmethodID fbsetter = e->GetMethodID(env_ptr,RockboxService_class, + "set_fb", "(Lorg/rockbox/RockboxFramebuffer;)V"); + e->CallVoidMethod(env_ptr, RockboxService_instance, fbsetter, RockboxFramebuffer_instance); display_on = true; } -- cgit v1.2.3