1
0
Fork 0
forked from len0rd/rockbox

Android: future-proof the RunForegroundManager code to Honeycomb

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@29562 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Maurus Cuelenaere 2011-03-11 15:45:38 +00:00
parent 4364111267
commit cae7560f32

View file

@ -16,12 +16,8 @@ import android.widget.RemoteViews;
public class RunForegroundManager public class RunForegroundManager
{ {
/* all below is heavily based on the examples found on
* http://developer.android.com/reference/android/app/Service.html#setForeground(boolean)
*/
private Notification mNotification; private Notification mNotification;
private NotificationManager mNM; private NotificationManager mNM;
private IRunForeground api;
private Service mCurrentService; private Service mCurrentService;
private Intent mWidgetUpdate; private Intent mWidgetUpdate;
@ -42,12 +38,7 @@ public class RunForegroundManager
mNotification.flags |= Notification.FLAG_ONGOING_EVENT; mNotification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotification.contentIntent = PendingIntent.getActivity(service, 0, intent, 0); mNotification.contentIntent = PendingIntent.getActivity(service, 0, intent, 0);
try { initForegroundCompat();
api = new newForegroundApi(R.string.notification, mNotification);
} catch (NoSuchMethodException e) {
/* Fall back on the old API */
api = new oldForegroundApi();
}
} }
private void LOG(CharSequence text) private void LOG(CharSequence text)
@ -62,27 +53,12 @@ public class RunForegroundManager
public void startForeground() public void startForeground()
{ {
/* startForegroundCompat(R.string.notification, mNotification);
* Send the notification.
* We use a layout id because it is a unique number.
* We use it later to cancel.
*/
mNM.notify(R.string.notification, mNotification);
/*
* this call makes the service run as foreground, which
* provides enough cpu time to do music decoding in the
* background
*/
api.startForeground();
} }
public void stopForeground() public void stopForeground()
{ {
/* Note to cancel BEFORE changing the stopForegroundCompat(R.string.notification);
* foreground state, since we could be killed at that point.
*/
mNM.cancel(R.string.notification);
api.stopForeground();
mWidgetUpdate = null; mWidgetUpdate = null;
} }
@ -118,74 +94,73 @@ public class RunForegroundManager
mCurrentService.sendBroadcast(widgetUpdate); mCurrentService.sendBroadcast(widgetUpdate);
} }
private interface IRunForeground /* Loosely based on http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification) */
{ private static final Class<?>[] mSetForegroundSignature = new Class[] {boolean.class};
void startForeground(); private static final Class<?>[] mStartForegroundSignature = new Class[] {int.class, Notification.class};
void stopForeground(); private static final Class<?>[] mStopForegroundSignature = new Class[] {boolean.class};
}
private class newForegroundApi implements IRunForeground private Method mSetForeground;
{
Class<?>[] mStartForegroundSignature =
new Class[] { int.class, Notification.class };
Class<?>[] mStopForegroundSignature =
new Class[] { boolean.class };
private Method mStartForeground; private Method mStartForeground;
private Method mStopForeground; private Method mStopForeground;
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];
newForegroundApi(int id, Notification notification) private void initForegroundCompat() {
throws SecurityException, NoSuchMethodException Class<?> serviceClass = mCurrentService.getClass();
{ try {
/* mStartForeground = serviceClass.getMethod("startForeground", mStartForegroundSignature);
* Get the new API through reflection mStopForeground = serviceClass.getMethod("stopForeground", mStopForegroundSignature);
} catch (NoSuchMethodException e) {
// Running on an older platform.
mStartForeground = mStopForeground = null;
try {
mSetForeground = serviceClass.getMethod("setForeground", mSetForegroundSignature);
} catch (NoSuchMethodException e2) {
throw new IllegalStateException("OS doesn't have Service.startForeground nor Service.setForeground!", e2);
}
}
}
private void invokeMethod(Method method, Object[] args) {
try {
method.invoke(mCurrentService, args);
} catch (Exception e) {
// Should not happen.
Log.w("Rockbox", "Unable to invoke method", e);
}
}
/**
* This is a wrapper around the new startForeground method, using the older
* APIs if it is not available.
*/ */
mStartForeground = Service.class.getMethod("startForeground", private void startForegroundCompat(int id, Notification notification) {
mStartForegroundSignature); // If we have the new startForeground API, then use it.
mStopForeground = Service.class.getMethod("stopForeground", if (mStartForeground != null) {
mStopForegroundSignature); Object[] startForeGroundArgs = new Object[] {Integer.valueOf(id), notification};
mStartForegroundArgs[0] = id; invokeMethod(mStartForeground, startForeGroundArgs);
mStartForegroundArgs[1] = notification; } else {
mStopForegroundArgs[0] = Boolean.TRUE; // Fall back on the old API.
} Object[] setForegroundArgs = new Object[] {Boolean.TRUE};
invokeMethod(mSetForeground, setForegroundArgs);
public void startForeground() mNM.notify(id, notification);
{
try {
mStartForeground.invoke(mCurrentService, mStartForegroundArgs);
} catch (InvocationTargetException e) {
/* Should not happen. */
LOG("Unable to invoke startForeground", e);
} catch (IllegalAccessException e) {
/* Should not happen. */
LOG("Unable to invoke startForeground", e);
} }
} }
public void stopForeground() /**
{ * This is a wrapper around the new stopForeground method, using the older
try { * APIs if it is not available.
mStopForeground.invoke(mCurrentService, mStopForegroundArgs); */
} catch (InvocationTargetException e) { private void stopForegroundCompat(int id) {
/* Should not happen. */ // If we have the new stopForeground API, then use it.
LOG("Unable to invoke stopForeground", e); if (mStopForeground != null) {
} catch (IllegalAccessException e) { Object[] stopForegroundArgs = new Object[] {Boolean.TRUE};
/* Should not happen. */ invokeMethod(mStopForeground, stopForegroundArgs);
LOG("Unable to invoke stopForeground", e); } else {
} // Fall back on the old API. Note to cancel BEFORE changing the
} // foreground state, since we could be killed at that point.
} mNM.cancel(id);
private class oldForegroundApi implements IRunForeground Object[] setForegroundArgs = new Object[] {Boolean.FALSE};
{ invokeMethod(mSetForeground, setForegroundArgs);
public void startForeground()
{
mCurrentService.setForeground(false);
}
public void stopForeground()
{
mCurrentService.setForeground(false);
} }
} }
} }