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;
{ private Method mStartForeground;
Class<?>[] mStartForegroundSignature = private Method mStopForeground;
new Class[] { int.class, Notification.class };
Class<?>[] mStopForegroundSignature =
new Class[] { boolean.class };
private Method mStartForeground;
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) {
mStartForeground = Service.class.getMethod("startForeground", // Running on an older platform.
mStartForegroundSignature); mStartForeground = mStopForeground = null;
mStopForeground = Service.class.getMethod("stopForeground",
mStopForegroundSignature);
mStartForegroundArgs[0] = id;
mStartForegroundArgs[1] = notification;
mStopForegroundArgs[0] = Boolean.TRUE;
}
public void startForeground()
{
try { try {
mStartForeground.invoke(mCurrentService, mStartForegroundArgs); mSetForeground = serviceClass.getMethod("setForeground", mSetForegroundSignature);
} catch (InvocationTargetException e) { } catch (NoSuchMethodException e2) {
/* Should not happen. */ throw new IllegalStateException("OS doesn't have Service.startForeground nor Service.setForeground!", e2);
LOG("Unable to invoke startForeground", e);
} catch (IllegalAccessException e) {
/* Should not happen. */
LOG("Unable to invoke startForeground", e);
}
}
public void stopForeground()
{
try {
mStopForeground.invoke(mCurrentService, mStopForegroundArgs);
} catch (InvocationTargetException e) {
/* Should not happen. */
LOG("Unable to invoke stopForeground", e);
} catch (IllegalAccessException e) {
/* Should not happen. */
LOG("Unable to invoke stopForeground", e);
} }
} }
} }
private class oldForegroundApi implements IRunForeground private void invokeMethod(Method method, Object[] args) {
{ try {
public void startForeground() method.invoke(mCurrentService, args);
{ } catch (Exception e) {
mCurrentService.setForeground(false); // Should not happen.
Log.w("Rockbox", "Unable to invoke method", e);
} }
public void stopForeground() }
{
mCurrentService.setForeground(false); /**
* This is a wrapper around the new startForeground method, using the older
* APIs if it is not available.
*/
private void startForegroundCompat(int id, Notification notification) {
// If we have the new startForeground API, then use it.
if (mStartForeground != null) {
Object[] startForeGroundArgs = new Object[] {Integer.valueOf(id), notification};
invokeMethod(mStartForeground, startForeGroundArgs);
} else {
// Fall back on the old API.
Object[] setForegroundArgs = new Object[] {Boolean.TRUE};
invokeMethod(mSetForeground, setForegroundArgs);
mNM.notify(id, notification);
}
}
/**
* This is a wrapper around the new stopForeground method, using the older
* APIs if it is not available.
*/
private void stopForegroundCompat(int id) {
// If we have the new stopForeground API, then use it.
if (mStopForeground != null) {
Object[] stopForegroundArgs = new Object[] {Boolean.TRUE};
invokeMethod(mStopForeground, stopForegroundArgs);
} 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);
Object[] setForegroundArgs = new Object[] {Boolean.FALSE};
invokeMethod(mSetForeground, setForegroundArgs);
} }
} }
} }