1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Jonathan Gordon 2010-10-31 10:35:42 +00:00
parent 78b2711e58
commit 26f7ee13ce
4 changed files with 71 additions and 32 deletions

View file

@ -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();
}
}

View file

@ -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")

View file

@ -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,10 +95,25 @@ 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)
{
LOG("Start Service");
@ -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)
{

View file

@ -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;
}