forked from len0rd/rockbox
Factor out the start/stopForeground and notification icon management into a separate class, don't let RockboxService.java become a beast.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28416 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
b92eabd38b
commit
50a9d50fde
5 changed files with 176 additions and 129 deletions
|
@ -37,9 +37,11 @@ MANIFEST := $(ANDROID_DIR)/AndroidManifest.xml
|
||||||
R_JAVA := $(BUILDDIR)/gen/$(PACKAGE_PATH)/R.java
|
R_JAVA := $(BUILDDIR)/gen/$(PACKAGE_PATH)/R.java
|
||||||
R_OBJ := $(BUILDDIR)/bin/$(PACKAGE_PATH)/R.class
|
R_OBJ := $(BUILDDIR)/bin/$(PACKAGE_PATH)/R.class
|
||||||
|
|
||||||
JAVA_SRC := $(wildcard $(ANDROID_DIR)/src/$(PACKAGE_PATH)/*.java)
|
JAVA_SRC := $(wildcard $(ANDROID_DIR)/src/$(PACKAGE_PATH)/Helper/*.java)
|
||||||
|
JAVA_SRC += $(wildcard $(ANDROID_DIR)/src/$(PACKAGE_PATH)/*.java)
|
||||||
JAVA_OBJ := $(call java2class,$(subst $(ANDROID)/src/$(PACKAGE_PATH),$(ANDROID)/bin/$(PACKAGE_PATH),$(JAVA_SRC)))
|
JAVA_OBJ := $(call java2class,$(subst $(ANDROID)/src/$(PACKAGE_PATH),$(ANDROID)/bin/$(PACKAGE_PATH),$(JAVA_SRC)))
|
||||||
|
|
||||||
|
|
||||||
LIBS := $(BINLIB_DIR)/$(BINARY) $(BINLIB_DIR)/libmisc.so
|
LIBS := $(BINLIB_DIR)/$(BINARY) $(BINLIB_DIR)/libmisc.so
|
||||||
TEMP_APK := $(BUILDDIR)/bin/_rockbox.apk
|
TEMP_APK := $(BUILDDIR)/bin/_rockbox.apk
|
||||||
TEMP_APK2 := $(BUILDDIR)/bin/__rockbox.apk
|
TEMP_APK2 := $(BUILDDIR)/bin/__rockbox.apk
|
||||||
|
|
157
android/src/org/rockbox/Helper/RunForegroundManager.java
Normal file
157
android/src/org/rockbox/Helper/RunForegroundManager.java
Normal file
|
@ -0,0 +1,157 @@
|
||||||
|
package org.rockbox.Helper;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import org.rockbox.R;
|
||||||
|
import org.rockbox.RockboxActivity;
|
||||||
|
|
||||||
|
import android.app.Notification;
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.app.Service;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
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 NotificationManager mNM;
|
||||||
|
private IRunForeground api;
|
||||||
|
private Service mCurrentService;
|
||||||
|
|
||||||
|
public RunForegroundManager(Service service) throws Exception
|
||||||
|
{
|
||||||
|
mNM = (NotificationManager)
|
||||||
|
service.getSystemService(Service.NOTIFICATION_SERVICE);
|
||||||
|
/* For now we'll use the same text for the ticker and the
|
||||||
|
* expanded notification */
|
||||||
|
CharSequence text = service.getText(R.string.notification);
|
||||||
|
/* Set the icon, scrolling text and timestamp */
|
||||||
|
mNotification = new Notification(R.drawable.icon, text,
|
||||||
|
System.currentTimeMillis());
|
||||||
|
|
||||||
|
/* The PendingIntent to launch our activity if the user selects
|
||||||
|
* this notification */
|
||||||
|
Intent intent = new Intent(service, RockboxActivity.class);
|
||||||
|
PendingIntent contentIntent =
|
||||||
|
PendingIntent.getActivity(service, 0, intent, 0);
|
||||||
|
|
||||||
|
/* Set the info for the views that show in the notification panel. */
|
||||||
|
mNotification.setLatestEventInfo(service,
|
||||||
|
service.getText(R.string.notification), text, contentIntent);
|
||||||
|
|
||||||
|
try {
|
||||||
|
api = new newForegroundApi(R.string.notification, mNotification);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
/* Fall back on the old API */
|
||||||
|
api = new oldForegroundApi();
|
||||||
|
}
|
||||||
|
mCurrentService = service;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LOG(CharSequence text, Throwable tr)
|
||||||
|
{
|
||||||
|
Log.d("Rockbox", (String)text, tr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startForeground()
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* 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()
|
||||||
|
{
|
||||||
|
/* Note to cancel BEFORE changing the
|
||||||
|
* foreground state, since we could be killed at that point.
|
||||||
|
*/
|
||||||
|
mNM.cancel(R.string.notification);
|
||||||
|
api.stopForeground();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private interface IRunForeground
|
||||||
|
{
|
||||||
|
void startForeground();
|
||||||
|
void stopForeground();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class newForegroundApi implements IRunForeground
|
||||||
|
{
|
||||||
|
Class<?>[] mStartForegroundSignature =
|
||||||
|
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)
|
||||||
|
throws SecurityException, NoSuchMethodException
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Get the new API through reflection
|
||||||
|
*/
|
||||||
|
mStartForeground = Service.class.getMethod("startForeground",
|
||||||
|
mStartForegroundSignature);
|
||||||
|
mStopForeground = Service.class.getMethod("stopForeground",
|
||||||
|
mStopForegroundSignature);
|
||||||
|
mStartForegroundArgs[0] = id;
|
||||||
|
mStartForegroundArgs[1] = notification;
|
||||||
|
mStopForegroundArgs[0] = Boolean.TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startForeground()
|
||||||
|
{
|
||||||
|
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()
|
||||||
|
{
|
||||||
|
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
|
||||||
|
{
|
||||||
|
public void startForeground()
|
||||||
|
{
|
||||||
|
mCurrentService.setForeground(false);
|
||||||
|
}
|
||||||
|
public void stopForeground()
|
||||||
|
{
|
||||||
|
mCurrentService.setForeground(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
package org.rockbox;
|
package org.rockbox;
|
||||||
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
|
|
||||||
package org.rockbox;
|
package org.rockbox;
|
||||||
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
||||||
|
|
|
@ -21,14 +21,14 @@
|
||||||
|
|
||||||
package org.rockbox;
|
package org.rockbox;
|
||||||
|
|
||||||
|
import org.rockbox.Helper.RunForegroundManager;
|
||||||
|
|
||||||
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.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
@ -36,9 +36,6 @@ import java.util.zip.ZipEntry;
|
||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.Notification;
|
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.app.PendingIntent;
|
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
@ -63,20 +60,9 @@ public class RockboxService extends Service
|
||||||
private RockboxFramebuffer fb = null;
|
private RockboxFramebuffer fb = null;
|
||||||
private boolean mRockboxRunning = false;
|
private boolean mRockboxRunning = false;
|
||||||
private Activity current_activity = null;
|
private Activity current_activity = null;
|
||||||
|
|
||||||
private Notification notification;
|
|
||||||
private static final Class<?>[] mStartForegroundSignature =
|
|
||||||
new Class[] { int.class, Notification.class };
|
|
||||||
private static final Class<?>[] mStopForegroundSignature =
|
|
||||||
new Class[] { boolean.class };
|
|
||||||
|
|
||||||
private NotificationManager mNM;
|
|
||||||
private Method mStartForeground;
|
|
||||||
private Method mStopForeground;
|
|
||||||
private Object[] mStartForegroundArgs = new Object[2];
|
|
||||||
private Object[] mStopForegroundArgs = new Object[1];
|
|
||||||
private IntentFilter itf;
|
private IntentFilter itf;
|
||||||
private BroadcastReceiver batt_monitor;
|
private BroadcastReceiver batt_monitor;
|
||||||
|
private RunForegroundManager fg_runner;
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private int battery_level;
|
private int battery_level;
|
||||||
|
|
||||||
|
@ -84,19 +70,6 @@ public class RockboxService extends Service
|
||||||
public void onCreate()
|
public void onCreate()
|
||||||
{
|
{
|
||||||
instance = this;
|
instance = this;
|
||||||
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
mStartForeground = getClass().getMethod("startForeground",
|
|
||||||
mStartForegroundSignature);
|
|
||||||
mStopForeground = getClass().getMethod("stopForeground",
|
|
||||||
mStopForegroundSignature);
|
|
||||||
}
|
|
||||||
catch (NoSuchMethodException e)
|
|
||||||
{
|
|
||||||
/* Running on an older platform: fall back to old API */
|
|
||||||
mStartForeground = mStopForeground = null;
|
|
||||||
}
|
|
||||||
startservice();
|
startservice();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +105,11 @@ public class RockboxService extends Service
|
||||||
|
|
||||||
/* Display a notification about us starting.
|
/* Display a notification about us starting.
|
||||||
* We put an icon in the status bar. */
|
* We put an icon in the status bar. */
|
||||||
create_notification();
|
try {
|
||||||
|
fg_runner = new RunForegroundManager(this);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void LOG(CharSequence text)
|
private void LOG(CharSequence text)
|
||||||
|
@ -285,105 +262,14 @@ public class RockboxService extends Service
|
||||||
registerReceiver(batt_monitor, itf);
|
registerReceiver(batt_monitor, itf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* all below is heavily based on the examples found on
|
|
||||||
* http://developer.android.com/reference/android/app/Service.html
|
|
||||||
*/
|
|
||||||
|
|
||||||
private void create_notification()
|
|
||||||
{
|
|
||||||
/* For now we'll use the same text for the ticker and the
|
|
||||||
* expanded notification */
|
|
||||||
CharSequence text = getText(R.string.notification);
|
|
||||||
/* Set the icon, scrolling text and timestamp */
|
|
||||||
notification = new Notification(R.drawable.icon, text,
|
|
||||||
System.currentTimeMillis());
|
|
||||||
|
|
||||||
/* The PendingIntent to launch our activity if the user selects
|
|
||||||
* this notification */
|
|
||||||
Intent intent = new Intent(this, RockboxActivity.class);
|
|
||||||
PendingIntent contentIntent =
|
|
||||||
PendingIntent.getActivity(this, 0, intent, 0);
|
|
||||||
|
|
||||||
/* Set the info for the views that show in the notification panel. */
|
|
||||||
notification.setLatestEventInfo(this,
|
|
||||||
getText(R.string.notification), text, contentIntent);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void startForeground()
|
public void startForeground()
|
||||||
{
|
{
|
||||||
/*
|
fg_runner.startForeground();
|
||||||
* 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, instance.notification);
|
|
||||||
/*
|
|
||||||
* this call makes the service run as foreground, which
|
|
||||||
* provides enough cpu time to do music decoding in the
|
|
||||||
* background
|
|
||||||
*/
|
|
||||||
startForegroundCompat(R.string.notification, notification);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stopForeground()
|
public void stopForeground()
|
||||||
{
|
{
|
||||||
if (notification != null)
|
fg_runner.stopForeground();
|
||||||
{
|
|
||||||
stopForegroundCompat(R.string.notification);
|
|
||||||
mNM.cancel(R.string.notification);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a wrapper around the new startForeground method, using the older
|
|
||||||
* APIs if it is not available.
|
|
||||||
*/
|
|
||||||
void startForegroundCompat(int id, Notification notification)
|
|
||||||
{
|
|
||||||
if (mStartForeground != null) {
|
|
||||||
mStartForegroundArgs[0] = Integer.valueOf(id);
|
|
||||||
mStartForegroundArgs[1] = notification;
|
|
||||||
try {
|
|
||||||
mStartForeground.invoke(this, 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);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Fall back on the old API.*/
|
|
||||||
setForeground(true);
|
|
||||||
mNM.notify(id, notification);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is a wrapper around the new stopForeground method, using the older
|
|
||||||
* APIs if it is not available.
|
|
||||||
*/
|
|
||||||
void stopForegroundCompat(int id)
|
|
||||||
{
|
|
||||||
if (mStopForeground != null) {
|
|
||||||
mStopForegroundArgs[0] = Boolean.TRUE;
|
|
||||||
try {
|
|
||||||
mStopForeground.invoke(this, 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);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
setForeground(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -391,6 +277,6 @@ public class RockboxService extends Service
|
||||||
{
|
{
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
/* Make sure our notification is gone. */
|
/* Make sure our notification is gone. */
|
||||||
stopForegroundCompat(R.string.notification);
|
stopForeground();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue