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

Commit d8a43f61 authored by Dianne Hackborn's avatar Dianne Hackborn
Browse files

Fix issue #2047139: Remove Service.setForeground()

This API is becoming seriously abused, so now it is deprecated and has
become a no-op.

As an alternative, there is now a new API that allows you to make a service
be in the foreground but requires providing a persistent notification to
go along with this state, allowing the user to know about and control it.
parent 30c0b834
Loading
Loading
Loading
Loading
+40 −1
Original line number Diff line number Diff line
@@ -22200,6 +22200,17 @@
 visibility="public"
>
</field>
<field name="FLAG_FOREGROUND_SERVICE"
 type="int"
 transient="false"
 volatile="false"
 value="64"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="FLAG_INSISTENT"
 type="int"
 transient="false"
@@ -23760,12 +23771,40 @@
 synchronized="false"
 static="false"
 final="true"
 deprecated="not deprecated"
 deprecated="deprecated"
 visibility="public"
>
<parameter name="isForeground" type="boolean">
</parameter>
</method>
<method name="startForeground"
 return="void"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="id" type="int">
</parameter>
<parameter name="notification" type="android.app.Notification">
</parameter>
</method>
<method name="stopForeground"
 return="void"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="removeNotification" type="boolean">
</parameter>
</method>
<method name="stopSelf"
 return="void"
 abstract="false"
+16 −7
Original line number Diff line number Diff line
@@ -39,9 +39,6 @@ import android.text.TextUtils;
import android.util.Config;
import android.util.Log;

import java.io.FileNotFoundException;
import java.io.FileDescriptor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@@ -551,8 +548,13 @@ public abstract class ActivityManagerNative extends Binder implements IActivityM
            data.enforceInterface(IActivityManager.descriptor);
            ComponentName className = ComponentName.readFromParcel(data);
            IBinder token = data.readStrongBinder();
            boolean isForeground = data.readInt() != 0;
            setServiceForeground(className, token, isForeground);
            int id = data.readInt();
            Notification notification = null;
            if (data.readInt() != 0) {
                notification = Notification.CREATOR.createFromParcel(data);
            }
            boolean removeNotification = data.readInt() != 0;
            setServiceForeground(className, token, id, notification, removeNotification);
            reply.writeNoException();
            return true;
        }
@@ -1664,13 +1666,20 @@ class ActivityManagerProxy implements IActivityManager
        return res;
    }
    public void setServiceForeground(ComponentName className, IBinder token,
            boolean isForeground) throws RemoteException {
            int id, Notification notification, boolean removeNotification) throws RemoteException {
        Parcel data = Parcel.obtain();
        Parcel reply = Parcel.obtain();
        data.writeInterfaceToken(IActivityManager.descriptor);
        ComponentName.writeToParcel(className, data);
        data.writeStrongBinder(token);
        data.writeInt(isForeground ? 1 : 0);
        data.writeInt(id);
        if (notification != null) {
            data.writeInt(1);
            notification.writeToParcel(data, 0);
        } else {
            data.writeInt(0);
        }
        data.writeInt(removeNotification ? 1 : 0);
        mRemote.transact(SET_SERVICE_FOREGROUND_TRANSACTION, data, reply, 0);
        reply.readException();
        data.recycle();
+1 −1
Original line number Diff line number Diff line
@@ -139,7 +139,7 @@ public interface IActivityManager extends IInterface {
    public boolean stopServiceToken(ComponentName className, IBinder token,
            int startId) throws RemoteException;
    public void setServiceForeground(ComponentName className, IBinder token,
            boolean isForeground) throws RemoteException;
            int id, Notification notification, boolean keepNotification) throws RemoteException;
    public int bindService(IApplicationThread caller, IBinder token,
            Intent service, String resolvedType,
            IServiceConnection connection, int flags) throws RemoteException;
+7 −0
Original line number Diff line number Diff line
@@ -257,6 +257,13 @@ public class Notification implements Parcelable
     */
    public static final int FLAG_NO_CLEAR           = 0x00000020;

    /**
     * Bit to be bitwise-ored into the {@link #flags} field that should be
     * set if this notification represents a currently running service.  This
     * will normally be set for you by {@link Service#startForeground}.
     */
    public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;

    public int flags;

    /**
+2 −1
Original line number Diff line number Diff line
@@ -61,7 +61,8 @@ public class NotificationManager

    private static INotificationManager sService;

    static private INotificationManager getService()
    /** @hide */
    static public INotificationManager getService()
    {
        if (sService != null) {
            return sService;
Loading