summaryrefslogtreecommitdiff
path: root/core/java
diff options
context:
space:
mode:
authorHui Yu <huiyu@google.com>2018-12-06 16:59:18 -0800
committerHui Yu <huiyu@google.com>2018-12-06 16:59:18 -0800
commit1ea855202fcc0006a6fa304fd686642dc2b9dc1e (patch)
tree2daa9d3aaea7a84e993cb96aba1a015bc0b1629f /core/java
parentcd504bbedd41df7402163f3bb73b270aa3e0ef61 (diff)
Add manifest service attribute foregroundServiceType
Foreground service must use attribute foregroundServiceType to specify its foreground service type in <sevice> element of manifest file, otherwise a warning message is printed when startForeground() method is called. (We will replace the warning message with a security exception when the feature is formally activiated.) The manifest attribute is: android:foregroundServiceType="<type>" Allowed types are: "sync", "mediaPlay", "phoneCall", "location", "deviceCompanion", "ongoingProcess". Bug: 111453223 Test: atest frameworks/base/tests/FrameworkPerf Change-Id: I5d2ab203d400f3c549cd153480b6252a2f9adb3c
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/app/Service.java9
-rw-r--r--core/java/android/content/pm/PackageParser.java4
-rw-r--r--core/java/android/content/pm/ServiceInfo.java88
3 files changed, 100 insertions, 1 deletions
diff --git a/core/java/android/app/Service.java b/core/java/android/app/Service.java
index 16f6bdaa4313..5fa8526b5a24 100644
--- a/core/java/android/app/Service.java
+++ b/core/java/android/app/Service.java
@@ -685,6 +685,13 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
* the permission {@link android.Manifest.permission#FOREGROUND_SERVICE} in order to use
* this API.</p>
*
+ * <p>To use this API, apps targeting API {@link android.os.Build.VERSION_CODES#Q} or later must
+ * specify the foreground service type using attribute
+ * {@link android.R.attr#foregroundServiceType} in service element of manifest file, otherwise
+ * a SecurityException is thrown when this API is called. Apps targeting API older than
+ * {@link android.os.Build.VERSION_CODES#Q} do not need to specify the foreground service type
+ * </p>
+ *
* @param id The identifier for this notification as per
* {@link NotificationManager#notify(int, Notification)
* NotificationManager.notify(int, Notification)}; must not be 0.
@@ -700,7 +707,7 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
} catch (RemoteException ex) {
}
}
-
+
/**
* Synonym for {@link #stopForeground(int)}.
* @param removeNotification If true, the {@link #STOP_FOREGROUND_REMOVE} flag
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index ac18dca74950..d0de9a1d2a76 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -5380,6 +5380,10 @@ public class PackageParser {
s.info.splitName =
sa.getNonConfigurationString(R.styleable.AndroidManifestService_splitName, 0);
+ s.info.mForegroundServiceType = sa.getInt(
+ com.android.internal.R.styleable.AndroidManifestService_foregroundServiceType,
+ ServiceInfo.FOREGROUND_SERVICE_TYPE_UNSPECIFIED);
+
s.info.flags = 0;
if (sa.getBoolean(
com.android.internal.R.styleable.AndroidManifestService_stopWithTask,
diff --git a/core/java/android/content/pm/ServiceInfo.java b/core/java/android/content/pm/ServiceInfo.java
index ad2c72274c07..8300c0c8d472 100644
--- a/core/java/android/content/pm/ServiceInfo.java
+++ b/core/java/android/content/pm/ServiceInfo.java
@@ -16,10 +16,14 @@
package android.content.pm;
+import android.annotation.IntDef;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Printer;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
/**
* Information you can retrieve about a particular application
* service. This corresponds to information collected from the
@@ -94,6 +98,81 @@ public class ServiceInfo extends ComponentInfo
*/
public int flags;
+ /**
+ * The default foreground service type if not been set in manifest file.
+ */
+ public static final int FOREGROUND_SERVICE_TYPE_UNSPECIFIED = 0;
+
+ /**
+ * Constant corresponding to <code>sync</code> in
+ * the {@link android.R.attr#foregroundServiceType} attribute.
+ * Data(photo, file, account) upload/download, backup/restore, import/export, fetch,
+ * transfer over network between device and cloud.
+ */
+ public static final int FOREGROUND_SERVICE_TYPE_SYNC = 1;
+
+ /**
+ * Constant corresponding to <code>mediaPlay</code> in
+ * the {@link android.R.attr#foregroundServiceType} attribute.
+ * Music, video, news or other media play.
+ */
+ public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAY = 2;
+
+ /**
+ * Constant corresponding to <code>phoneCall</code> in
+ * the {@link android.R.attr#foregroundServiceType} attribute.
+ * Ongoing phone call or video conference.
+ */
+ public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 3;
+
+ /**
+ * Constant corresponding to <code>location</code> in
+ * the {@link android.R.attr#foregroundServiceType} attribute.
+ * GPS, map, navigation location update.
+ */
+ public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 4;
+
+ /**
+ * Constant corresponding to <code>deviceCompanion</code> in
+ * the {@link android.R.attr#foregroundServiceType} attribute.
+ * Auto, bluetooth, TV or other devices connection, monitoring and interaction.
+ */
+ public static final int FOREGROUND_SERVICE_TYPE_DEVICE_COMPANION = 5;
+
+ /**
+ * Constant corresponding to <code>ongoingProcess</code> in
+ * the {@link android.R.attr#foregroundServiceType} attribute.
+ * Process that should not be interrupted, including installation, setup, photo
+ * compression etc.
+ */
+ public static final int FOREGROUND_SERVICE_TYPE_ONGOING_PROCESS = 6;
+
+ /**
+ * The enumeration of values for foreground service type.
+ * The foreground service type is set in {@link android.R.attr#foregroundServiceType}
+ * attribute.
+ * @hide
+ */
+ @IntDef(flag = false, prefix = { "FOREGROUND_SERVICE_TYPE_" }, value = {
+ FOREGROUND_SERVICE_TYPE_UNSPECIFIED,
+ FOREGROUND_SERVICE_TYPE_SYNC,
+ FOREGROUND_SERVICE_TYPE_MEDIA_PLAY,
+ FOREGROUND_SERVICE_TYPE_PHONE_CALL,
+ FOREGROUND_SERVICE_TYPE_LOCATION,
+ FOREGROUND_SERVICE_TYPE_DEVICE_COMPANION,
+ FOREGROUND_SERVICE_TYPE_ONGOING_PROCESS
+ })
+ @Retention(RetentionPolicy.SOURCE)
+ public @interface ForegroundServiceType {}
+
+ /**
+ * The type of foreground service, set in
+ * {@link android.R.attr#foregroundServiceType} attribute, one value in
+ * {@link ForegroundServiceType}
+ * @hide
+ */
+ public @ForegroundServiceType int mForegroundServiceType = FOREGROUND_SERVICE_TYPE_UNSPECIFIED;
+
public ServiceInfo() {
}
@@ -101,6 +180,15 @@ public class ServiceInfo extends ComponentInfo
super(orig);
permission = orig.permission;
flags = orig.flags;
+ mForegroundServiceType = orig.mForegroundServiceType;
+ }
+
+ /**
+ * Return the current foreground service type.
+ * @return the current foreground service type.
+ */
+ public int getForegroundServiceType() {
+ return mForegroundServiceType;
}
public void dump(Printer pw, String prefix) {