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

Commit 7357549b authored by Biswarup Pal's avatar Biswarup Pal
Browse files

Handle invalid deviceId in device-aware system services

When virtual camera, sensors, etc are being accessed from a
foreground service using an explicit device context (by calling
Context#createDeviceContext with a virtual device id), there are
situations where the camera, sensors, etc APIs fallback to the
ones corresponding to the default device if the virtual device
gets destroyed. As a result, a FGS expecting virtual camera, sensors,
etc might end up using default device camera, sensors, etc
unintentionally when the virtual device goes away. We introduce
DEVICE_POLICY_INVALID in VDM and make VDM#getDevicePolicy return
DEVICE_POLICY_INVALID for invalid deviceIds, so that system services
calling this method can detect this use case and handle the same in
a manner they see fit.

Test: atest CtsVirtualDevicesSensorTestCases
Test: atest CtsVirtualDevicesCameraTestCases
Test: atest CtsVirtualDevicesTestCases
Bug: 394613051
Flag: android.companion.virtualdevice.flags.handle_invalid_device_id
Change-Id: I7cac7e1dcb3396d8daabd72052b7f30e15261514
parent ba983ba6
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -955,6 +955,10 @@ package android.companion.virtual {
    method public void playSoundEffect(int, int);
  }

  public final class VirtualDeviceParams implements android.os.Parcelable {
    field public static final int DEVICE_POLICY_INVALID = -1; // 0xffffffff
  }

}

package android.companion.virtual.camera {
+3 −3
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SdkConstant;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.SystemService;
@@ -326,8 +325,9 @@ public final class VirtualDeviceManager {
    /**
     * Returns the device policy for the given virtual device and policy type.
     *
     * <p>In case the virtual device identifier is not valid, or there's no explicitly specified
     * policy for that device and policy type, then
     * <p>In case the virtual device identifier is not valid,
     * {@link VirtualDeviceParams#DEVICE_POLICY_INVALID} is returned. If there's no explicitly
     * specified policy for that device and policy type, then
     * {@link VirtualDeviceParams#DEVICE_POLICY_DEFAULT} is returned.
     *
     * @hide
+13 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.TestApi;
import android.companion.virtual.sensor.IVirtualSensorCallback;
import android.companion.virtual.sensor.VirtualSensor;
import android.companion.virtual.sensor.VirtualSensorCallback;
@@ -136,11 +137,22 @@ public final class VirtualDeviceParams implements Parcelable {
    public static final int NAVIGATION_POLICY_DEFAULT_BLOCKED = 1;

    /** @hide */
    @IntDef(prefix = "DEVICE_POLICY_",  value = {DEVICE_POLICY_DEFAULT, DEVICE_POLICY_CUSTOM})
    @IntDef(prefix = "DEVICE_POLICY_",  value = {DEVICE_POLICY_INVALID, DEVICE_POLICY_DEFAULT,
            DEVICE_POLICY_CUSTOM})
    @Retention(RetentionPolicy.SOURCE)
    @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
    public @interface DevicePolicy {}

    /**
     * Indicates that there is no valid virtual device and it should be treated as an error
     * scenario (or however the caller sees fit).
     *
     * @hide
     */
    @SuppressLint("UnflaggedApi") // @TestApi without associated feature.
    @TestApi
    public static final int DEVICE_POLICY_INVALID = -1;

    /**
     * Indicates that there is no special logic for this virtual device and it should be treated
     * the same way as the default device, keeping the default behavior unchanged.
+10 −0
Original line number Diff line number Diff line
@@ -243,3 +243,13 @@ flag {
    bug: "371167033"
    is_exported: true
}

flag {
    name: "handle_invalid_device_id"
    namespace: "virtual_devices"
    description: "Handle invalid device ids in various system services"
    bug: "394613051"
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ interface IVirtualDeviceManagerNative {
    /**
     * Counterpart to VirtualDeviceParams#DevicePolicy.
     */
    const int DEVICE_POLICY_INVALID = -1;
    const int DEVICE_POLICY_DEFAULT = 0;
    const int DEVICE_POLICY_CUSTOM = 1;

Loading