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

Commit 8ecc688a authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Add utilities for creating AppOps listenerIds

Use utilities to support location listenerIds.

Bug: 153687899
Test: presubmits
Change-Id: I269661556e48218fc76e5abc2099bed4c2d667cc
parent f711fffc
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
     * prefer passing the PendingIntent to the system server, and then invoking
     * {@link #toReceiverId(PendingIntent)}.
     *
     * @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
@@ -35,6 +35,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;
@@ -2534,7 +2535,7 @@ public class LocationManager {
        }

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

        public synchronized void register(AlarmManager alarmManager,
@@ -2663,7 +2664,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
@@ -35,6 +35,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;
@@ -1833,6 +1834,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, attributionTag,
                listenerId);
@@ -2102,7 +2106,8 @@ public class LocationManagerService extends ILocationManager.Stub {
            request = DEFAULT_LOCATION_REQUEST;
        }

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

        Objects.requireNonNull(intent);