summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorDianne Hackborn <hackbod@google.com>2016-04-19 18:10:26 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2016-04-19 18:10:28 +0000
commit9c211a339689a2e54da3315ccdbf22add472c76a (patch)
tree0e0d63f2ea96bfdeb04ecde3169b4186340185ae /core/java/android
parente896090caf761afd8c443bcf4732e38e43b4baa9 (diff)
parent67324c990c4fbad238119a4667341d5618b2bea2 (diff)
Merge "Work on issue #28221912: Starting service as foreground might..." into nyc-dev
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/app/ActivityManagerNative.java8
-rw-r--r--core/java/android/app/IActivityManager.java2
-rw-r--r--core/java/android/app/Service.java83
3 files changed, 76 insertions, 17 deletions
diff --git a/core/java/android/app/ActivityManagerNative.java b/core/java/android/app/ActivityManagerNative.java
index a82b950bda87..ae2ca8441f47 100644
--- a/core/java/android/app/ActivityManagerNative.java
+++ b/core/java/android/app/ActivityManagerNative.java
@@ -1111,8 +1111,8 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM
if (data.readInt() != 0) {
notification = Notification.CREATOR.createFromParcel(data);
}
- boolean removeNotification = data.readInt() != 0;
- setServiceForeground(className, token, id, notification, removeNotification);
+ int sflags = data.readInt();
+ setServiceForeground(className, token, id, notification, sflags);
reply.writeNoException();
return true;
}
@@ -4300,7 +4300,7 @@ class ActivityManagerProxy implements IActivityManager
return res;
}
public void setServiceForeground(ComponentName className, IBinder token,
- int id, Notification notification, boolean removeNotification) throws RemoteException {
+ int id, Notification notification, int flags) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain();
data.writeInterfaceToken(IActivityManager.descriptor);
@@ -4313,7 +4313,7 @@ class ActivityManagerProxy implements IActivityManager
} else {
data.writeInt(0);
}
- data.writeInt(removeNotification ? 1 : 0);
+ data.writeInt(flags);
mRemote.transact(SET_SERVICE_FOREGROUND_TRANSACTION, data, reply, 0);
reply.readException();
data.recycle();
diff --git a/core/java/android/app/IActivityManager.java b/core/java/android/app/IActivityManager.java
index 1a4e98cc4d36..b28b5e60399a 100644
--- a/core/java/android/app/IActivityManager.java
+++ b/core/java/android/app/IActivityManager.java
@@ -235,7 +235,7 @@ public interface IActivityManager extends IInterface {
public boolean stopServiceToken(ComponentName className, IBinder token,
int startId) throws RemoteException;
public void setServiceForeground(ComponentName className, IBinder token,
- int id, Notification notification, boolean keepNotification) throws RemoteException;
+ int id, Notification notification, int flags) throws RemoteException;
public int bindService(IApplicationThread caller, IBinder token, Intent service,
String resolvedType, IServiceConnection connection, int flags,
String callingPackage, int userId) throws RemoteException;
diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java
index 21a3543e2d63..4fe4f9892305 100644
--- a/core/java/android/app/Service.java
+++ b/core/java/android/app/Service.java
@@ -16,6 +16,7 @@
package android.app;
+import android.annotation.IntDef;
import android.annotation.Nullable;
import android.content.ComponentCallbacks2;
import android.content.ComponentName;
@@ -30,6 +31,8 @@ import android.util.Log;
import java.io.FileDescriptor;
import java.io.PrintWriter;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
/**
* A Service is an application component representing either an application's desire
@@ -300,6 +303,32 @@ import java.io.PrintWriter;
public abstract class Service extends ContextWrapper implements ComponentCallbacks2 {
private static final String TAG = "Service";
+ /**
+ * Flag for {@link #stopForeground(int)}: if set, the notification previously provided
+ * to {@link #startForeground} will be removed. Otherwise it will remain
+ * until a later call (to {@link #startForeground(int, Notification)} or
+ * {@link #stopForeground(int)} removes it, or the service is destroyed.
+ */
+ public static final int STOP_FOREGROUND_REMOVE = 1<<0;
+
+ /**
+ * Flag for {@link #stopForeground(int)}: if set, the notification previously provided
+ * to {@link #startForeground} will be detached from the service. Only makes sense
+ * when {@link #STOP_FOREGROUND_REMOVE} is <b>not</b> set -- in this case, the notification
+ * will remain shown, but be completely detached from the service and so no longer changed
+ * except through direct calls to the notification manager.
+ */
+ public static final int STOP_FOREGROUND_DETACH = 1<<1;
+
+ /** @hide */
+ @IntDef(flag = true,
+ value = {
+ STOP_FOREGROUND_REMOVE,
+ STOP_FOREGROUND_DETACH
+ })
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface StopForegroundFlags {}
+
public Service() {
super(null);
}
@@ -377,7 +406,7 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
* alarm goes off.
*/
public static final int START_NOT_STICKY = 2;
-
+
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
@@ -392,7 +421,18 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
* pending events will be delivered at the point of restart).
*/
public static final int START_REDELIVER_INTENT = 3;
-
+
+ /** @hide */
+ @IntDef(flag = false,
+ value = {
+ START_STICKY_COMPATIBILITY,
+ START_STICKY,
+ START_NOT_STICKY,
+ START_REDELIVER_INTENT,
+ })
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface StartResult {}
+
/**
* Special constant for reporting that we are done processing
* {@link #onTaskRemoved(Intent)}.
@@ -414,7 +454,17 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
* {@link #onStartCommand(Intent, int, int)}.
*/
public static final int START_FLAG_RETRY = 0x0002;
-
+
+ /** @hide */
+ @IntDef(flag = true,
+ value = {
+ START_FLAG_REDELIVERY,
+ START_FLAG_RETRY,
+ })
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface StartArgFlags {}
+
+
/**
* Called by the system every time a client explicitly starts the service by calling
* {@link android.content.Context#startService}, providing the arguments it supplied and a
@@ -455,7 +505,7 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
*
* @see #stopSelfResult(int)
*/
- public int onStartCommand(Intent intent, int flags, int startId) {
+ public @StartResult int onStartCommand(Intent intent, @StartArgFlags int flags, int startId) {
onStart(intent, startId);
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}
@@ -652,28 +702,37 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
try {
mActivityManager.setServiceForeground(
new ComponentName(this, mClassName), mToken, id,
- notification, true);
+ notification, 0);
} catch (RemoteException ex) {
}
}
/**
+ * Synonym for {@link #stopForeground(int)}.
+ * @param removeNotification If true, the {@link #STOP_FOREGROUND_REMOVE} flag
+ * will be supplied.
+ * @see #stopForeground(int)
+ * @see #startForeground(int, Notification)
+ */
+ public final void stopForeground(boolean removeNotification) {
+ stopForeground(removeNotification ? STOP_FOREGROUND_REMOVE : 0);
+ }
+
+ /**
* Remove this service from foreground state, allowing it to be killed if
* more memory is needed.
- * @param removeNotification If true, the notification previously provided
- * to {@link #startForeground} will be removed. Otherwise it will remain
- * until a later call removes it (or the service is destroyed).
+ * @param flags Additional behavior options: {@link #STOP_FOREGROUND_REMOVE},
+ * {@link #STOP_FOREGROUND_DETACH}.
* @see #startForeground(int, Notification)
*/
- public final void stopForeground(boolean removeNotification) {
+ public final void stopForeground(@StopForegroundFlags int flags) {
try {
mActivityManager.setServiceForeground(
- new ComponentName(this, mClassName), mToken, 0, null,
- removeNotification);
+ new ComponentName(this, mClassName), mToken, 0, null, flags);
} catch (RemoteException ex) {
}
}
-
+
/**
* Print the Service's state into the given stream. This gets invoked if
* you run "adb shell dumpsys activity service &lt;yourservicename&gt;"