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

Commit 15c3630b authored by Soonil Nagarkar's avatar Soonil Nagarkar Committed by Automerger Merge Worker
Browse files

DO NOT MERGE Add utilities for creating AppOps listenerIds am: 4523f96f am:...

DO NOT MERGE Add utilities for creating AppOps listenerIds am: 4523f96f am: c1f4205c am: eeb2112d am: 358f3bf9

Change-Id: I0b99e5244c77a8efb62e00b5db664259615764c6
parents 43774927 358f3bf9
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -2585,6 +2585,44 @@ public class AppOpsManager {
        return !sOpDisableReset[op];
    }

    /**
     * Returns a listenerId suitable for use with {@link #noteOp(int, int, String, String, String)}.
     *
     * This is intended for use client side, when the receiver id must be created before the
     * associated call is made to the system server. If using {@link PendingIntent} as the receiver,
     * avoid using this method as it will include a pointless additional x-process call. Instead to
     * prefer passing the PendingIntent to the system server, and then invoking
     * {@link #toReceiverId(PendingIntent)} instead.
     *
     * @param obj the receiver in use
     * @return a string representation of the receiver suitable for app ops use
     * @hide
     */
    // TODO: this should probably be @SystemApi as well
    public static @NonNull String toReceiverId(@NonNull Object obj) {
        if (obj instanceof PendingIntent) {
            return toReceiverId((PendingIntent) obj);
        } else {
            return obj.getClass().getName() + "@" + System.identityHashCode(obj);
        }
    }

    /**
     * Returns a listenerId suitable for use with {@link #noteOp(int, int, String, String, String)}.
     *
     * This is intended for use server side, where ActivityManagerService can be referenced without
     * an additional x-process call.
     *
     * @param pendingIntent the pendingIntent in use
     * @return a string representation of the pending intent suitable for app ops use
     * @see #toReceiverId(Object)
     * @hide
     */
    // TODO: this should probably be @SystemApi as well
    public static @NonNull String toReceiverId(@NonNull PendingIntent pendingIntent) {
        return pendingIntent.getTag("");
    }

    /**
     * When to not enforce {@link #setUserRestriction restrictions}.
     *
+3 −2
Original line number Diff line number Diff line
@@ -34,6 +34,7 @@ import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.app.AlarmManager;
import android.app.AppOpsManager;
import android.app.PendingIntent;
import android.app.PropertyInvalidatedCache;
import android.compat.Compatibility;
@@ -2561,7 +2562,7 @@ public class LocationManager {
        }

        public String getListenerId() {
            return mConsumer.getClass().getName() + "@" + System.identityHashCode(mConsumer);
            return AppOpsManager.toReceiverId(mConsumer);
        }

        public synchronized void register(AlarmManager alarmManager,
@@ -2690,7 +2691,7 @@ public class LocationManager {
        }

        public String getListenerId() {
            return mListener.getClass().getName() + "@" + System.identityHashCode(mListener);
            return AppOpsManager.toReceiverId(mListener);
        }

        public void register(@NonNull Executor executor) {
+6 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -1827,6 +1828,9 @@ public class LocationManagerService extends ILocationManager.Stub {
        if (request == null) {
            request = DEFAULT_LOCATION_REQUEST;
        }
        if (listenerId == null && intent != null) {
            listenerId = AppOpsManager.toReceiverId(intent);
        }

        CallerIdentity identity = CallerIdentity.fromBinder(mContext, packageName, featureId,
                listenerId);
@@ -2093,7 +2097,8 @@ public class LocationManagerService extends ILocationManager.Stub {
            request = DEFAULT_LOCATION_REQUEST;
        }

        CallerIdentity identity = CallerIdentity.fromBinder(mContext, packageName, featureId);
        CallerIdentity identity = CallerIdentity.fromBinder(mContext, packageName, featureId,
                AppOpsManager.toReceiverId(intent));
        identity.enforceLocationPermission();

        Objects.requireNonNull(intent);