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

Commit 61dbf045 authored by Kenneth Ford's avatar Kenneth Ford Committed by Android Build Coastguard Worker
Browse files

Allows an app to cancel a device state request they made at any point if they're in the foreground.

Merges cl 28368952 & 28785068 that allows the updated behavior
for cancelling a device state request for a request that the caller
made themselves with a requirement that the caller must still be
in the foreground, to be cherry-picked to 24Q3-release.

Bug: 350930205
Bug: 354772125
Test: DeviceStateManagerTests
Test: ExtensionRearDisplayPresentationTest
Flag: android.hardware.devicestate.feature.flags.device_state_requester_cancel_state
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:ffb9832df3de57a8e29aa8eee8b85ae4e3d8ea32)
Merged-In: I864716078d377b2c65cfccf518b2f81f2d043efc
Change-Id: I864716078d377b2c65cfccf518b2f81f2d043efc
parent f5f5eb69
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -9,3 +9,15 @@ flag {
    bug: "293636629"
    is_fixed_read_only: true
}

flag {
    name: "device_state_requester_cancel_state"
    is_exported: true
    namespace: "windowing_sdk"
    description: "Removes foreground requirement if process attempting to cancel a state request is the requester"
    bug: "354772125"
    is_fixed_read_only: true
    metadata {
      purpose: PURPOSE_BUGFIX
    }
}
+31 −10
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import android.hardware.devicestate.DeviceStateManager;
import android.hardware.devicestate.DeviceStateManagerInternal;
import android.hardware.devicestate.IDeviceStateManager;
import android.hardware.devicestate.IDeviceStateManagerCallback;
import android.hardware.devicestate.feature.flags.Flags;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
@@ -968,16 +969,16 @@ public final class DeviceStateManagerService extends SystemService {
     * @param callingPid Process ID that is requesting this state change
     * @param state state that is being requested.
     */
    private void assertCanRequestDeviceState(int callingPid, int callingUid, int state) {
    private void enforceRequestDeviceStatePermitted(int callingPid, int callingUid, int state) {
        final boolean isTopApp = isTopApp(callingPid);
        final boolean isForegroundApp = isForegroundApp(callingPid, callingUid);
        final boolean isStateAvailableForAppRequests = isStateAvailableForAppRequests(state);

        final boolean canRequestState = isTopApp
        final boolean isAllowedToRequestState = isTopApp
                && isForegroundApp
                && isStateAvailableForAppRequests;

        if (!canRequestState) {
        if (!isAllowedToRequestState) {
            getContext().enforceCallingOrSelfPermission(CONTROL_DEVICE_STATE,
                    "Permission required to request device state, "
                            + "or the call must come from the top app "
@@ -986,19 +987,29 @@ public final class DeviceStateManagerService extends SystemService {
    }

    /**
     * Checks if the process can control the device state. If the calling process ID is
     * not the top app, then check if this process holds the CONTROL_DEVICE_STATE permission.
     * Checks if the process can cancel a device state request. If the calling process ID is not
     * both the top app and foregrounded, verify that the calling process is in the foreground and
     * that it matches the process ID and user ID that made the device state request. If neither are
     * true, then check if this process holds the CONTROL_DEVICE_STATE permission.
     *
     * @param callingPid Process ID that is requesting this state change
     * @param callingUid UID that is requesting this state change
     */
    private void assertCanControlDeviceState(int callingPid, int callingUid) {
    private void enforceCancelDeviceStatePermitted(int callingPid, int callingUid) {
        final boolean isTopApp = isTopApp(callingPid);
        final boolean isForegroundApp = isForegroundApp(callingPid, callingUid);

        final boolean canControlState = isTopApp && isForegroundApp;
        boolean isAllowedToControlState = isTopApp && isForegroundApp;

        if (!canControlState) {
        if (Flags.deviceStateRequesterCancelState()) {
            synchronized (mLock) {
                isAllowedToControlState =
                        isTopApp || (isForegroundApp && doCallingIdsMatchOverrideRequestIdsLocked(
                                callingPid, callingUid));
            }
        }

        if (!isAllowedToControlState) {
            getContext().enforceCallingOrSelfPermission(CONTROL_DEVICE_STATE,
                    "Permission required to request device state, "
                            + "or the call must come from the top app.");
@@ -1032,6 +1043,16 @@ public final class DeviceStateManagerService extends SystemService {
        return topApp != null && topApp.getPid() == callingPid;
    }

    /**
     * Returns if the provided {@code callingPid} and {@code callingUid} match the same id's that
     * requested the current device state override.
     */
    @GuardedBy("mLock")
    private boolean doCallingIdsMatchOverrideRequestIdsLocked(int callingPid, int callingUid) {
        OverrideRequest request = mActiveOverride.orElse(null);
        return request != null && request.getPid() == callingPid && request.getUid() == callingUid;
    }

    private boolean isStateAvailableForAppRequests(int state) {
        synchronized (mLock) {
            return mDeviceStatesAvailableForAppRequests.contains(state);
@@ -1256,7 +1277,7 @@ public final class DeviceStateManagerService extends SystemService {
            // Allow top processes to request a device state change
            // If the calling process ID is not the top app, then we check if this process
            // holds a permission to CONTROL_DEVICE_STATE
            assertCanRequestDeviceState(callingPid, callingUid, state);
            enforceRequestDeviceStatePermitted(callingPid, callingUid, state);

            if (token == null) {
                throw new IllegalArgumentException("Request token must not be null.");
@@ -1281,7 +1302,7 @@ public final class DeviceStateManagerService extends SystemService {
            // Allow top processes to cancel a device state change
            // If the calling process ID is not the top app, then we check if this process
            // holds a permission to CONTROL_DEVICE_STATE
            assertCanControlDeviceState(callingPid, callingUid);
            enforceCancelDeviceStatePermitted(callingPid, callingUid);

            final long callingIdentity = Binder.clearCallingIdentity();
            try {