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

Commit 6ebedc96 authored by Karishma Vakil's avatar Karishma Vakil
Browse files

[DeviceAware] Use AttributionSourceState in AppOpsService checkOp, noteOp,

startOp, finishOp methods

This allows us to reduce the number of parameters being passed around
and also enables easy addition of new parameters such as device id.

Note that this change is unflagged and is meant to be a pure refactor.
This is preparatory CL and no new methods are being added to AppOpsManager yet, but will be needed
to allow clients to pass in device id.

Bug: 299160174
Test: atest CtsAppOpsTestCases CtsAppOps2TestCases
Change-Id: I591b1dc7d7e4af0371c2d1d80b06d858e7213a94
parent b48f0d09
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.app.AppOpsManager;
import android.app.AppOpsManager.PackageOps;
import android.app.IActivityManager;
import android.app.usage.UsageStatsManager;
import android.content.AttributionSource;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -746,8 +747,10 @@ public class AppStateTrackerImpl implements AppStateTracker {
        public void opChanged(int op, int uid, String packageName) throws RemoteException {
            boolean restricted = false;
            try {
                restricted = mAppOpsService.checkOperation(TARGET_OP,
                        uid, packageName) != AppOpsManager.MODE_ALLOWED;
                final AttributionSource attributionSource =
                        new AttributionSource.Builder(uid).setPackageName(packageName).build();
                restricted = mAppOpsService.checkOperationWithState(TARGET_OP,
                        attributionSource.asState()) != AppOpsManager.MODE_ALLOWED;
            } catch (RemoteException e) {
                // Shouldn't happen
            }
+6 −2
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import android.app.tare.EconomyManager;
import android.app.tare.IEconomyManager;
import android.app.usage.UsageEvents;
import android.app.usage.UsageStatsManagerInternal;
import android.content.AttributionSource;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
@@ -230,8 +231,11 @@ public class InternalResourceService extends SystemService {
        public void opChanged(int op, int uid, String packageName) {
            boolean restricted = false;
            try {
                restricted = mAppOpsService.checkOperation(
                        AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, uid, packageName)
                final AttributionSource attributionSource = new AttributionSource.Builder(uid)
                        .setPackageName(packageName)
                        .build();
                restricted = mAppOpsService.checkOperationWithState(
                        AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, attributionSource.asState())
                        != AppOpsManager.MODE_ALLOWED;
            } catch (RemoteException e) {
                // Shouldn't happen
+29 −7
Original line number Diff line number Diff line
@@ -8305,7 +8305,9 @@ public class AppOpsManager {
     */
    public int unsafeCheckOpRawNoThrow(int op, int uid, @NonNull String packageName) {
        try {
            return mService.checkOperationRaw(op, uid, packageName, null);
            final AttributionSource attributionSource =
                    new AttributionSource.Builder(uid).setPackageName(packageName).build();
            return mService.checkOperationWithStateRaw(op, attributionSource.asState());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
@@ -8468,7 +8470,12 @@ public class AppOpsManager {
                }
            }

            SyncNotedAppOp syncOp = mService.noteOperation(op, uid, packageName, attributionTag,
            final AttributionSource attributionSource =
                    new AttributionSource.Builder(uid)
                            .setPackageName(packageName)
                            .setAttributionTag(attributionTag)
                            .build();
            SyncNotedAppOp syncOp = mService.noteOperationWithState(op, attributionSource.asState(),
                    collectionMode == COLLECT_ASYNC, message, shouldCollectMessage);

            if (syncOp.getOpMode() == MODE_ALLOWED) {
@@ -8708,7 +8715,9 @@ public class AppOpsManager {
    @UnsupportedAppUsage
    public int checkOp(int op, int uid, String packageName) {
        try {
            int mode = mService.checkOperation(op, uid, packageName);
            final AttributionSource attributionSource =
                    new AttributionSource.Builder(uid).setPackageName(packageName).build();
            int mode = mService.checkOperationWithState(op, attributionSource.asState());
            if (mode == MODE_ERRORED) {
                throw new SecurityException(buildSecurityExceptionMsg(op, uid, packageName));
            }
@@ -8729,7 +8738,9 @@ public class AppOpsManager {
    @UnsupportedAppUsage
    public int checkOpNoThrow(int op, int uid, String packageName) {
        try {
            int mode = mService.checkOperation(op, uid, packageName);
            final AttributionSource attributionSource =
                    new AttributionSource.Builder(uid).setPackageName(packageName).build();
            int mode = mService.checkOperationWithState(op, attributionSource.asState());
            return mode == AppOpsManager.MODE_FOREGROUND ? AppOpsManager.MODE_ALLOWED : mode;
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
@@ -8974,8 +8985,14 @@ public class AppOpsManager {
                }
            }

            SyncNotedAppOp syncOp = mService.startOperation(token, op, uid, packageName,
                    attributionTag, startIfModeDefault, collectionMode == COLLECT_ASYNC, message,
            final AttributionSource attributionSource =
                    new AttributionSource.Builder(uid)
                            .setPackageName(packageName)
                            .setAttributionTag(attributionTag)
                            .build();
            SyncNotedAppOp syncOp = mService.startOperationWithState(token, op,
                    attributionSource.asState(), startIfModeDefault,
                    collectionMode == COLLECT_ASYNC, message,
                    shouldCollectMessage, attributionFlags, attributionChainId);

            if (syncOp.getOpMode() == MODE_ALLOWED) {
@@ -9188,7 +9205,12 @@ public class AppOpsManager {
    public void finishOp(IBinder token, int op, int uid, @NonNull String packageName,
            @Nullable String attributionTag) {
        try {
            mService.finishOperation(token, op, uid, packageName, attributionTag);
            final AttributionSource attributionSource =
                    new AttributionSource.Builder(uid)
                            .setPackageName(packageName)
                            .setAttributionTag(attributionTag)
                            .build();
            mService.finishOperationWithState(token, op, attributionSource.asState());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+22 −29
Original line number Diff line number Diff line
@@ -26,11 +26,12 @@ import android.util.SparseArray;
import android.util.SparseIntArray;

import com.android.internal.app.IAppOpsCallback;
import com.android.internal.util.function.HeptFunction;
import com.android.internal.util.function.HexFunction;
import com.android.internal.util.function.NonaFunction;
import com.android.internal.util.function.QuadFunction;
import com.android.internal.util.function.QuintConsumer;
import com.android.internal.util.function.QuintFunction;
import com.android.internal.util.function.TriConsumer;
import com.android.internal.util.function.TriFunction;
import com.android.internal.util.function.UndecFunction;

/**
@@ -45,15 +46,13 @@ public abstract class AppOpsManagerInternal {
         * Allows overriding check operation behavior.
         *
         * @param code The op code to check.
         * @param uid The UID for which to check.
         * @param packageName The package for which to check.
         * @param attributionTag The attribution tag for which to check.
         * @param attributionSource the {@link AttributionSource} responsible for data access
         * @param raw Whether to check the raw op i.e. not interpret the mode based on UID state.
         * @param superImpl The super implementation.
         * @return The app op check result.
         */
        int checkOperation(int code, int uid, String packageName, @Nullable String attributionTag,
                boolean raw, QuintFunction<Integer, Integer, String, String, Boolean, Integer>
        int checkOperation(int code, AttributionSource attributionSource,
                boolean raw, TriFunction<Integer, AttributionSource, Boolean, Integer>
                superImpl);

        /**
@@ -73,25 +72,23 @@ public abstract class AppOpsManagerInternal {
         * Allows overriding note operation behavior.
         *
         * @param code The op code to note.
         * @param uid The UID for which to note.
         * @param packageName The package for which to note. {@code null} for system package.
         * @param featureId Id of the feature in the package
         * @param attributionSource the {@link AttributionSource} responsible for data access
         * @param shouldCollectAsyncNotedOp If an {@link AsyncNotedAppOp} should be collected
         * @param message The message in the async noted op
         * @param superImpl The super implementation.
         * @return The app op note result.
         */
        SyncNotedAppOp noteOperation(int code, int uid, @Nullable String packageName,
                @Nullable String featureId, boolean shouldCollectAsyncNotedOp,
        SyncNotedAppOp noteOperation(int code, AttributionSource attributionSource,
                boolean shouldCollectAsyncNotedOp,
                @Nullable String message, boolean shouldCollectMessage,
                @NonNull HeptFunction<Integer, Integer, String, String, Boolean, String, Boolean,
                @NonNull QuintFunction<Integer, AttributionSource, Boolean, String, Boolean,
                        SyncNotedAppOp> superImpl);

        /**
         * Allows overriding note proxy operation behavior.
         *
         * @param code The op code to note.
         * @param attributionSource The permission identity of the caller.
         * @param attributionSource the {@link AttributionSource} responsible for data access
         * @param shouldCollectAsyncNotedOp If an {@link AsyncNotedAppOp} should be collected
         * @param message The message in the async noted op
         * @param shouldCollectMessage whether to collect messages
@@ -110,9 +107,7 @@ public abstract class AppOpsManagerInternal {
         *
         * @param token The client state.
         * @param code The op code to start.
         * @param uid The UID for which to note.
         * @param packageName The package for which to note. {@code null} for system package.
         * @param attributionTag the attribution tag.
         * @param attributionSource the {@link AttributionSource} responsible for data access
         * @param startIfModeDefault Whether to start the op of the mode is default.
         * @param shouldCollectAsyncNotedOp If an {@link AsyncNotedAppOp} should be collected
         * @param message The message in the async noted op
@@ -122,12 +117,12 @@ public abstract class AppOpsManagerInternal {
         * @param superImpl The super implementation.
         * @return The app op note result.
         */
        SyncNotedAppOp startOperation(IBinder token, int code, int uid,
                @Nullable String packageName, @Nullable String attributionTag,
        SyncNotedAppOp startOperation(IBinder token, int code,
                AttributionSource attributionSource,
                boolean startIfModeDefault, boolean shouldCollectAsyncNotedOp,
                @Nullable String message, boolean shouldCollectMessage,
                @AttributionFlags int attributionFlags, int attributionChainId,
                @NonNull UndecFunction<IBinder, Integer, Integer, String, String, Boolean,
                @NonNull NonaFunction<IBinder, Integer, AttributionSource, Boolean,
                        Boolean, String, Boolean, Integer, Integer, SyncNotedAppOp> superImpl);

        /**
@@ -135,7 +130,7 @@ public abstract class AppOpsManagerInternal {
         *
         * @param clientId The client calling start, represented by an IBinder
         * @param code The op code to start.
         * @param attributionSource The permission identity of the caller.
         * @param attributionSource the {@link AttributionSource} responsible for data access
         * @param startIfModeDefault Whether to start the op of the mode is default.
         * @param shouldCollectAsyncNotedOp If an {@link AsyncNotedAppOp} should be collected
         * @param message The message in the async noted op
@@ -161,21 +156,19 @@ public abstract class AppOpsManagerInternal {
         *
         * @param clientId The client state.
         * @param code The op code to finish.
         * @param uid The UID for which the op was noted.
         * @param packageName The package for which it was noted. {@code null} for system package.
         * @param attributionTag the attribution tag.
         */
        default void finishOperation(IBinder clientId, int code, int uid, String packageName,
                String attributionTag,
                @NonNull QuintConsumer<IBinder, Integer, Integer, String, String> superImpl) {
            superImpl.accept(clientId, code, uid, packageName, attributionTag);
         * @param attributionSource the {@link AttributionSource} responsible for data access
         */
        default void finishOperation(IBinder clientId, int code,
                AttributionSource attributionSource,
                @NonNull TriConsumer<IBinder, Integer, AttributionSource> superImpl) {
            superImpl.accept(clientId, code, attributionSource);
        }

        /**
         * Allows overriding finish proxy op.
         *
         * @param code The op code to finish.
         * @param attributionSource The permission identity of the caller.
         * @param attributionSource the {@link AttributionSource} responsible for data access
         * @param skipProxyOperation Whether to skip the proxy in the proxy/proxied operation
         * @param clientId The client calling finishProxyOperation
         * @param superImpl The "standard" implementation to potentially call
+6 −0
Original line number Diff line number Diff line
@@ -234,6 +234,12 @@ public final class AttributionSource implements Parcelable {
        return withToken(sDefaultToken);
    }

    /** @hide */
    public AttributionSource withUid(int uid) {
        return new AttributionSource(uid, getPid(), getPackageName(), getAttributionTag(),
                getToken(), mAttributionSourceState.renouncedPermissions, getDeviceId(), getNext());
    }

    /** @hide */
    public AttributionSource withPid(int pid) {
        return new AttributionSource(getUid(), pid, getPackageName(), getAttributionTag(),
Loading