Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2d4207f8 authored by Hui Yu's avatar Hui Yu
Browse files

Change foregroundServiceType from enum to flags

1. Now the value of manifest attribute foregroundServiceType can
be multiple flags ORed together.
2. Add a overloaded version of Service.startForeground() with an
additional parameter foregroundServiceType. The flags in parameter
foregroundServiceType must be a subset of flags specified in manifest
attribute foregroundServiceType, otherwise a IllegalArgumentException is
thrown.
3. Add a field foregroundServiceType in ServiceRecord, it is the types
that have been started on this foreground sevice.

Bug: 111453223
Test: atest frameworks/base/tests/FrameworkPerf

Change-Id: I7eb68f696e6bf75720fe9c9388a6c23a529677f7
parent 28a7ebc7
Loading
Loading
Loading
Loading
+8 −7
Original line number Diff line number Diff line
@@ -6176,6 +6176,7 @@ package android.app {
    method public void onTrimMemory(int);
    method public boolean onUnbind(android.content.Intent);
    method public final void startForeground(int, android.app.Notification);
    method public final void startForeground(int, @NonNull android.app.Notification, int);
    method public final void stopForeground(boolean);
    method public final void stopForeground(int);
    method public final void stopSelf();
@@ -11919,13 +11920,13 @@ package android.content.pm {
    field public static final int FLAG_SINGLE_USER = 1073741824; // 0x40000000
    field public static final int FLAG_STOP_WITH_TASK = 1; // 0x1
    field public static final int FLAG_USE_APP_ZYGOTE = 8; // 0x8
    field public static final int FOREGROUND_SERVICE_TYPE_DEVICE_COMPANION = 5; // 0x5
    field public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 4; // 0x4
    field public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAY = 2; // 0x2
    field public static final int FOREGROUND_SERVICE_TYPE_ONGOING_PROCESS = 6; // 0x6
    field public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 3; // 0x3
    field public static final int FOREGROUND_SERVICE_TYPE_SYNC = 1; // 0x1
    field public static final int FOREGROUND_SERVICE_TYPE_UNSPECIFIED = 0; // 0x0
    field public static final int FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE = 16; // 0x10
    field public static final int FOREGROUND_SERVICE_TYPE_DATA_SYNC = 1; // 0x1
    field public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 8; // 0x8
    field public static final int FOREGROUND_SERVICE_TYPE_MANIFEST = -1; // 0xffffffff
    field public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK = 2; // 0x2
    field public static final int FOREGROUND_SERVICE_TYPE_NONE = 0; // 0x0
    field public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 4; // 0x4
    field public int flags;
    field public String permission;
  }
+1 −1
Original line number Diff line number Diff line
@@ -203,7 +203,7 @@ interface IActivityManager {
    void unbindFinished(in IBinder token, in Intent service, boolean doRebind);
    void setProcessImportant(in IBinder token, int pid, boolean isForeground, String reason);
    void setServiceForeground(in ComponentName className, in IBinder token,
            int id, in Notification notification, int flags);
            int id, in Notification notification, int flags, int foregroundServiceType);
    boolean moveActivityTaskToBack(in IBinder token, boolean nonRoot);
    void getMemoryInfo(out ActivityManager.MemoryInfo outInfo);
    List<ActivityManager.ProcessErrorStateInfo> getProcessesInErrorState();
+45 −8
Original line number Diff line number Diff line
@@ -16,7 +16,10 @@

package android.app;

import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST;

import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UnsupportedAppUsage;
import android.content.ComponentCallbacks2;
@@ -685,12 +688,10 @@ 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>
     * <p>Apps built with SDK version {@link android.os.Build.VERSION_CODES#Q} or later can specify
     * the foreground service types using attribute {@link android.R.attr#foregroundServiceType} in
     * service element of manifest file. The value of attribute
     * {@link android.R.attr#foregroundServiceType} can be multiple flags ORed together.</p>
     *
     * @param id The identifier for this notification as per
     * {@link NotificationManager#notify(int, Notification)
@@ -703,7 +704,42 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
        try {
            mActivityManager.setServiceForeground(
                    new ComponentName(this, mClassName), mToken, id,
                    notification, 0);
                    notification, 0, FOREGROUND_SERVICE_TYPE_MANIFEST);
        } catch (RemoteException ex) {
        }
    }

  /**
   * An overloaded version of {@link #startForeground(int, Notification)} with additional
   * foregroundServiceType parameter.
   *
   * <p>Apps built with SDK version {@link android.os.Build.VERSION_CODES#Q} or later can specify
   * the foreground service types using attribute {@link android.R.attr#foregroundServiceType} in
   * service element of manifest file. The value of attribute
   * {@link android.R.attr#foregroundServiceType} can be multiple flags ORed together.</p>
   *
   * <p>The foregroundServiceType parameter must be a subset flags of what is specified in manifest
   * attribute {@link android.R.attr#foregroundServiceType}, if not, an IllegalArgumentException is
   * thrown. Specify foregroundServiceType parameter as
   * {@link android.content.pm.ServiceInfo#FOREGROUND_SERVICE_TYPE_MANIFEST} to use all flags that
   * is specified in manifest attribute foregroundServiceType.</p>
   *
   * @param id The identifier for this notification as per
   * {@link NotificationManager#notify(int, Notification)
   * NotificationManager.notify(int, Notification)}; must not be 0.
   * @param notification The Notification to be displayed.
   * @param foregroundServiceType must be a subset flags of manifest attribute
   * {@link android.R.attr#foregroundServiceType} flags.
   * @throws IllegalArgumentException if param foregroundServiceType is not subset of manifest
   *     attribute {@link android.R.attr#foregroundServiceType}.
   * @see {@link android.content.pm.ServiceInfo} for the set of FOREGROUND_SERVICE_TYPE flags.
   */
    public final void startForeground(int id, @NonNull Notification notification,
            int foregroundServiceType) {
        try {
            mActivityManager.setServiceForeground(
                    new ComponentName(this, mClassName), mToken, id,
                    notification, 0, foregroundServiceType);
        } catch (RemoteException ex) {
        }
    }
@@ -731,7 +767,8 @@ public abstract class Service extends ContextWrapper implements ComponentCallbac
    public final void stopForeground(@StopForegroundFlags int flags) {
        try {
            mActivityManager.setServiceForeground(
                    new ComponentName(this, mClassName), mToken, 0, null, flags);
                    new ComponentName(this, mClassName), mToken, 0, null,
                    flags, 0);
        } catch (RemoteException ex) {
        }
    }
+1 −1
Original line number Diff line number Diff line
@@ -5573,7 +5573,7 @@ public class PackageParser {

        s.info.mForegroundServiceType = sa.getInt(
                com.android.internal.R.styleable.AndroidManifestService_foregroundServiceType,
                ServiceInfo.FOREGROUND_SERVICE_TYPE_UNSPECIFIED);
                ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE);

        s.info.flags = 0;
        if (sa.getBoolean(
+22 −24
Original line number Diff line number Diff line
@@ -101,77 +101,73 @@ public class ServiceInfo extends ComponentInfo
    /**
     * The default foreground service type if not been set in manifest file.
     */
    public static final int FOREGROUND_SERVICE_TYPE_UNSPECIFIED = 0;
    public static final int FOREGROUND_SERVICE_TYPE_NONE = 0;

    /**
     * Constant corresponding to <code>sync</code> in
     * Constant corresponding to <code>dataSync</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;
    public static final int FOREGROUND_SERVICE_TYPE_DATA_SYNC = 1 << 0;

    /**
     * Constant corresponding to <code>mediaPlay</code> in
     * Constant corresponding to <code>mediaPlayback</code> in
     * the {@link android.R.attr#foregroundServiceType} attribute.
     * Music, video, news or other media play.
     * Music, video, news or other media playback.
     */
    public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAY = 2;
    public static final int FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK = 1 << 1;

    /**
     * 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;
    public static final int FOREGROUND_SERVICE_TYPE_PHONE_CALL = 1 << 2;

    /**
     * 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;
    public static final int FOREGROUND_SERVICE_TYPE_LOCATION = 1 << 3;

    /**
     * Constant corresponding to <code>deviceCompanion</code> in
     * Constant corresponding to <code>connectedDevice</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;
    public static final int FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE = 1 << 4;

    /**
     * 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.
     * A special value indicates to use all types set in manifest file.
     */
    public static final int FOREGROUND_SERVICE_TYPE_ONGOING_PROCESS = 6;
    public static final int FOREGROUND_SERVICE_TYPE_MANIFEST = -1;

    /**
     * The enumeration of values for foreground service type.
     * The set of flags 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,
    @IntDef(flag = true, prefix = { "FOREGROUND_SERVICE_TYPE_" }, value = {
            FOREGROUND_SERVICE_TYPE_NONE,
            FOREGROUND_SERVICE_TYPE_DATA_SYNC,
            FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK,
            FOREGROUND_SERVICE_TYPE_PHONE_CALL,
            FOREGROUND_SERVICE_TYPE_LOCATION,
            FOREGROUND_SERVICE_TYPE_DEVICE_COMPANION,
            FOREGROUND_SERVICE_TYPE_ONGOING_PROCESS
            FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ForegroundServiceType {}

    /**
     * The type of foreground service, set in
     * {@link android.R.attr#foregroundServiceType} attribute, one value in
     * {@link android.R.attr#foregroundServiceType} attribute by ORing flags in
     * {@link ForegroundServiceType}
     * @hide
     */
    public @ForegroundServiceType int mForegroundServiceType = FOREGROUND_SERVICE_TYPE_UNSPECIFIED;
    public @ForegroundServiceType int mForegroundServiceType = FOREGROUND_SERVICE_TYPE_NONE;

    public ServiceInfo() {
    }
@@ -217,6 +213,7 @@ public class ServiceInfo extends ComponentInfo
        super.writeToParcel(dest, parcelableFlags);
        dest.writeString(permission);
        dest.writeInt(flags);
        dest.writeInt(mForegroundServiceType);
    }

    public static final Creator<ServiceInfo> CREATOR =
@@ -233,5 +230,6 @@ public class ServiceInfo extends ComponentInfo
        super(source);
        permission = source.readString();
        flags = source.readInt();
        mForegroundServiceType = source.readInt();
    }
}
Loading