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

Commit a6706ac3 authored by Vladimir Komsiyski's avatar Vladimir Komsiyski
Browse files

A bunch of public VDM APIs for exposing VD properties.

 - a convinience method for getting a specific device in VDM
 - all displayIds for a specific device
 - whether a device has a custom sensor policy
 - a device listener for create/close events (inspired by
   DisplayManager.DisplayListener and CompanionDeviceManager
   .OnAssociationsChangedListener)

Bug: 276718212
Bug: 297253526
Test: atest VirtualDeviceTest
Change-Id: Iea011eca716b64534de693eb3d92a58ae7f67d9e
parent ba468dc2
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -9681,14 +9681,24 @@ package android.companion.virtual {
  public final class VirtualDevice implements android.os.Parcelable {
    method public int describeContents();
    method public int getDeviceId();
    method @FlaggedApi(Flags.FLAG_VDM_PUBLIC_APIS) @NonNull public int[] getDisplayIds();
    method @Nullable public String getName();
    method @Nullable public String getPersistentDeviceId();
    method @FlaggedApi(Flags.FLAG_VDM_PUBLIC_APIS) public boolean hasCustomSensorSupport();
    method public void writeToParcel(@NonNull android.os.Parcel, int);
    field @NonNull public static final android.os.Parcelable.Creator<android.companion.virtual.VirtualDevice> CREATOR;
  }
  public final class VirtualDeviceManager {
    method @FlaggedApi(Flags.FLAG_VDM_PUBLIC_APIS) @Nullable public android.companion.virtual.VirtualDevice getVirtualDevice(int);
    method @NonNull public java.util.List<android.companion.virtual.VirtualDevice> getVirtualDevices();
    method @FlaggedApi(Flags.FLAG_VDM_PUBLIC_APIS) public void registerVirtualDeviceListener(@NonNull java.util.concurrent.Executor, @NonNull android.companion.virtual.VirtualDeviceManager.VirtualDeviceListener);
    method @FlaggedApi(Flags.FLAG_VDM_PUBLIC_APIS) public void unregisterVirtualDeviceListener(@NonNull android.companion.virtual.VirtualDeviceManager.VirtualDeviceListener);
  }
  @FlaggedApi(Flags.FLAG_VDM_PUBLIC_APIS) public static interface VirtualDeviceManager.VirtualDeviceListener {
    method public default void onVirtualDeviceClosed(int);
    method public default void onVirtualDeviceCreated(int);
  }
}
+10 −0
Original line number Diff line number Diff line
@@ -63,6 +63,16 @@ interface IVirtualDevice {
     */
    String getPersistentDeviceId();

    /**
     * Returns the IDs of all virtual displays of this device.
     */
    int[] getDisplayIds();

    /**
     * Returns the device policy for the given policy type.
     */
    int getDevicePolicy(int policyType);

    /**
     * Closes the virtual device and frees all associated resources.
     */
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.companion.virtual;

/**
 * Interface to listen for changes in the available virtual devices.
 *
 * @hide
 */
oneway interface IVirtualDeviceListener {

    /**
     * Called whenever a new virtual device has been added to the system.
     */
    void onVirtualDeviceCreated(int deviceId);

    /**
     * Called whenever a virtual device has been removed from the system.
     */
    void onVirtualDeviceClosed(int deviceId);
}
+18 −2
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package android.companion.virtual;

import android.companion.virtual.IVirtualDevice;
import android.companion.virtual.IVirtualDeviceActivityListener;
import android.companion.virtual.IVirtualDeviceListener;
import android.companion.virtual.IVirtualDeviceSoundEffectListener;
import android.companion.virtual.VirtualDevice;
import android.companion.virtual.VirtualDeviceParams;
@@ -56,6 +57,21 @@ interface IVirtualDeviceManager {
     */
    List<VirtualDevice> getVirtualDevices();

    /**
     * Returns the details of the virtual device with the given ID, if any.
     */
    VirtualDevice getVirtualDevice(int deviceId);

    /**
     * Registers a virtual device listener to receive notifications for virtual device events.
     */
    void registerVirtualDeviceListener(in IVirtualDeviceListener listener);

    /**
     * Unregisters a previously registered virtual device listener.
     */
    void unregisterVirtualDeviceListener(in IVirtualDeviceListener listener);

    /**
     * Returns the ID of the device which owns the display with the given ID.
     */
+48 −22
Original line number Diff line number Diff line
@@ -16,13 +16,17 @@

package android.companion.virtual;

import static android.companion.virtual.VirtualDeviceParams.DEVICE_POLICY_CUSTOM;
import static android.companion.virtual.VirtualDeviceParams.POLICY_TYPE_SENSORS;

import android.annotation.FlaggedApi;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.companion.virtual.flags.Flags;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.Objects;
import android.os.RemoteException;

/**
 * Details of a particular virtual device.
@@ -31,9 +35,12 @@ import java.util.Objects;
 *
 * <p class="note">Not to be confused with {@link VirtualDeviceManager.VirtualDevice}, which is used
 * by the virtual device creator and allows them to manage the device.
 *
 * @see VirtualDeviceManager#registerVirtualDeviceListener
 */
public final class VirtualDevice implements Parcelable {

    private final @NonNull IVirtualDevice mVirtualDevice;
    private final int mId;
    private final @Nullable String mPersistentId;
    private final @Nullable String mName;
@@ -44,17 +51,20 @@ public final class VirtualDevice implements Parcelable {
     *
     * @hide
     */
    public VirtualDevice(int id, @Nullable String persistentId, @Nullable String name) {
    public VirtualDevice(@NonNull IVirtualDevice virtualDevice, int id,
            @Nullable String persistentId, @Nullable String name) {
        if (id <= Context.DEVICE_ID_DEFAULT) {
            throw new IllegalArgumentException("VirtualDevice ID must be greater than "
                    + Context.DEVICE_ID_DEFAULT);
        }
        mVirtualDevice = virtualDevice;
        mId = id;
        mPersistentId = persistentId;
        mName = name;
    }

    private VirtualDevice(@NonNull Parcel parcel) {
        mVirtualDevice = IVirtualDevice.Stub.asInterface(parcel.readStrongBinder());
        mId = parcel.readInt();
        mPersistentId = parcel.readString8();
        mName = parcel.readString8();
@@ -101,6 +111,40 @@ public final class VirtualDevice implements Parcelable {
        return mName;
    }

    /**
     * Returns the IDs of all virtual displays that belong to this device, if any.
     *
     * <p>The actual {@link android.view.Display} objects can be obtained by passing the returned
     * IDs to {@link android.hardware.display.DisplayManager#getDisplay(int)}.</p>
     */
    @FlaggedApi(Flags.FLAG_VDM_PUBLIC_APIS)
    public @NonNull int[] getDisplayIds() {
        try {
            return mVirtualDevice.getDisplayIds();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Returns whether this device may have custom sensors.
     *
     * <p>Returning {@code true} does not necessarily mean that this device has sensors, it only
     * means that a {@link android.hardware.SensorManager} instance created from a {@link Context}
     * associated with this device will return this device's sensors, if any.</p>
     *
     * @see Context#getDeviceId()
     * @see Context#createDeviceContext(int)
     */
    @FlaggedApi(Flags.FLAG_VDM_PUBLIC_APIS)
    public boolean hasCustomSensorSupport() {
        try {
            return mVirtualDevice.getDevicePolicy(POLICY_TYPE_SENSORS) == DEVICE_POLICY_CUSTOM;
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    @Override
    public int describeContents() {
        return 0;
@@ -108,30 +152,12 @@ public final class VirtualDevice implements Parcelable {

    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeStrongBinder(mVirtualDevice.asBinder());
        dest.writeInt(mId);
        dest.writeString8(mPersistentId);
        dest.writeString8(mName);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof VirtualDevice)) {
            return false;
        }
        VirtualDevice that = (VirtualDevice) o;
        return mId == that.mId
                && Objects.equals(mPersistentId, that.mPersistentId)
                && Objects.equals(mName, that.mName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mId, mPersistentId, mName);
    }

    @Override
    @NonNull
    public String toString() {
Loading