diff --git a/api/current.txt b/api/current.txt
index fe91ba12e28d9..75b0d682d9886 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -644,6 +644,7 @@ package android {
field public static final int forceHasOverlappingRendering = 16844065; // 0x1010521
field public static final int foreground = 16843017; // 0x1010109
field public static final int foregroundGravity = 16843264; // 0x1010200
+ field public static final int foregroundServiceType = 16844191; // 0x101059f
field public static final int foregroundTint = 16843885; // 0x101046d
field public static final int foregroundTintMode = 16843886; // 0x101046e
field public static final int format = 16843013; // 0x1010105
@@ -11755,12 +11756,20 @@ package android.content.pm {
ctor public ServiceInfo(android.content.pm.ServiceInfo);
method public int describeContents();
method public void dump(android.util.Printer, java.lang.String);
+ method public int getForegroundServiceType();
field public static final android.os.Parcelable.Creator
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 + *
+ * * @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 ac18dca749509..d0de9a1d2a762 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 ad2c72274c070..8300c0c8d472d 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 tosync 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 mediaPlay 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 phoneCall 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 location 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 deviceCompanion 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 ongoingProcess 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) {
diff --git a/core/res/res/values/attrs_manifest.xml b/core/res/res/values/attrs_manifest.xml
index 089c59f3a09f5..854582b213402 100644
--- a/core/res/res/values/attrs_manifest.xml
+++ b/core/res/res/values/attrs_manifest.xml
@@ -1394,6 +1394,29 @@