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

Commit 896ff059 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Split VirtualSensor callback." into udc-dev am: 96bc9ff0

parents 066bbd54 96bc9ff0
Loading
Loading
Loading
Loading
+16 −3
Original line number Diff line number Diff line
@@ -3263,6 +3263,7 @@ package android.companion.virtual {
    method @NonNull public android.companion.virtual.VirtualDeviceParams.Builder setName(@NonNull String);
    method @NonNull public android.companion.virtual.VirtualDeviceParams.Builder setUsersWithMatchingAccounts(@NonNull java.util.Set<android.os.UserHandle>);
    method @NonNull public android.companion.virtual.VirtualDeviceParams.Builder setVirtualSensorCallback(@NonNull java.util.concurrent.Executor, @NonNull android.companion.virtual.sensor.VirtualSensorCallback);
    method @NonNull public android.companion.virtual.VirtualDeviceParams.Builder setVirtualSensorDirectChannelCallback(@NonNull java.util.concurrent.Executor, @NonNull android.companion.virtual.sensor.VirtualSensorDirectChannelCallback);
  }
}
@@ -3326,9 +3327,6 @@ package android.companion.virtual.sensor {
  public interface VirtualSensorCallback {
    method public void onConfigurationChanged(@NonNull android.companion.virtual.sensor.VirtualSensor, boolean, @NonNull java.time.Duration, @NonNull java.time.Duration);
    method public default void onDirectChannelConfigured(@IntRange(from=1) int, @NonNull android.companion.virtual.sensor.VirtualSensor, int, @IntRange(from=1) int);
    method public default void onDirectChannelCreated(@IntRange(from=1) int, @NonNull android.os.SharedMemory);
    method public default void onDirectChannelDestroyed(@IntRange(from=1) int);
  }
  public final class VirtualSensorConfig implements android.os.Parcelable {
@@ -3350,6 +3348,21 @@ package android.companion.virtual.sensor {
    method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setVendor(@Nullable String);
  }
  public interface VirtualSensorDirectChannelCallback {
    method public void onDirectChannelConfigured(@IntRange(from=1) int, @NonNull android.companion.virtual.sensor.VirtualSensor, int, @IntRange(from=1) int);
    method public void onDirectChannelCreated(@IntRange(from=1) int, @NonNull android.os.SharedMemory);
    method public void onDirectChannelDestroyed(@IntRange(from=1) int);
  }
  public final class VirtualSensorDirectChannelWriter implements java.lang.AutoCloseable {
    ctor public VirtualSensorDirectChannelWriter();
    method public void addChannel(@IntRange(from=1) int, @NonNull android.os.SharedMemory) throws android.system.ErrnoException;
    method public void close();
    method public boolean configureChannel(@IntRange(from=1) int, @NonNull android.companion.virtual.sensor.VirtualSensor, int, @IntRange(from=1) int);
    method public void removeChannel(@IntRange(from=1) int);
    method public boolean writeSensorEvent(@NonNull android.companion.virtual.sensor.VirtualSensor, @NonNull android.companion.virtual.sensor.VirtualSensorEvent);
  }
  public final class VirtualSensorEvent implements android.os.Parcelable {
    method public int describeContents();
    method public long getTimestampNanos();
+71 −16
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.companion.virtual.sensor.IVirtualSensorCallback;
import android.companion.virtual.sensor.VirtualSensor;
import android.companion.virtual.sensor.VirtualSensorCallback;
import android.companion.virtual.sensor.VirtualSensorConfig;
import android.companion.virtual.sensor.VirtualSensorDirectChannelCallback;
import android.content.ComponentName;
import android.os.Parcel;
import android.os.Parcelable;
@@ -381,7 +382,8 @@ public final class VirtualDeviceParams implements Parcelable {
    }

    /**
     * Returns the callback to get notified about changes in the sensor listeners.
     * Returns the callback to get notified about changes in the sensor listeners or sensor direct
     * channel configuration.
     * @hide
     */
    @Nullable
@@ -533,19 +535,29 @@ public final class VirtualDeviceParams implements Parcelable {
        private int mAudioRecordingSessionId = AUDIO_SESSION_ID_GENERATE;

        @NonNull private List<VirtualSensorConfig> mVirtualSensorConfigs = new ArrayList<>();
        @Nullable
        private IVirtualSensorCallback mVirtualSensorCallback;
        @Nullable private Executor mVirtualSensorCallbackExecutor;
        @Nullable private VirtualSensorCallback mVirtualSensorCallback;
        @Nullable private Executor mVirtualSensorDirectChannelCallbackExecutor;
        @Nullable private VirtualSensorDirectChannelCallback mVirtualSensorDirectChannelCallback;

        private static class VirtualSensorCallbackDelegate extends IVirtualSensorCallback.Stub {
            @NonNull
            private final Executor mExecutor;
            @NonNull
            private final VirtualSensorCallback mCallback;
            @Nullable
            private final Executor mDirectChannelExecutor;
            @Nullable
            private final VirtualSensorDirectChannelCallback mDirectChannelCallback;

            VirtualSensorCallbackDelegate(@NonNull @CallbackExecutor Executor executor,
                    @NonNull VirtualSensorCallback callback) {
                mCallback = callback;
                    @NonNull VirtualSensorCallback callback,
                    @Nullable @CallbackExecutor Executor directChannelExecutor,
                    @Nullable VirtualSensorDirectChannelCallback directChannelCallback) {
                mExecutor = executor;
                mCallback = callback;
                mDirectChannelExecutor = directChannelExecutor;
                mDirectChannelCallback = directChannelCallback;
            }

            @Override
@@ -562,22 +574,31 @@ public final class VirtualDeviceParams implements Parcelable {
            @Override
            public void onDirectChannelCreated(int channelHandle,
                    @NonNull SharedMemory sharedMemory) {
                mExecutor.execute(
                        () -> mCallback.onDirectChannelCreated(channelHandle, sharedMemory));
                if (mDirectChannelCallback != null && mDirectChannelExecutor != null) {
                    mDirectChannelExecutor.execute(
                            () -> mDirectChannelCallback.onDirectChannelCreated(channelHandle,
                                    sharedMemory));
                }
            }

            @Override
            public void onDirectChannelDestroyed(int channelHandle) {
                mExecutor.execute(() -> mCallback.onDirectChannelDestroyed(channelHandle));
                if (mDirectChannelCallback != null && mDirectChannelExecutor != null) {
                    mDirectChannelExecutor.execute(
                            () -> mDirectChannelCallback.onDirectChannelDestroyed(channelHandle));
                }
            }

            @Override
            public void onDirectChannelConfigured(int channelHandle, @NonNull VirtualSensor sensor,
                    int rateLevel, int reportToken) {
                mExecutor.execute(() -> mCallback.onDirectChannelConfigured(
                if (mDirectChannelCallback != null && mDirectChannelExecutor != null) {
                    mDirectChannelExecutor.execute(
                            () -> mDirectChannelCallback.onDirectChannelConfigured(
                                    channelHandle, sensor, rateLevel, reportToken));
                }
            }
        }

        /**
         * Sets the lock state of the device. The permission {@code ADD_ALWAYS_UNLOCKED_DISPLAY}
@@ -783,20 +804,37 @@ public final class VirtualDeviceParams implements Parcelable {
        }

        /**
         * Sets the callback to get notified about changes in the sensor listeners.
         * Sets the callback to get notified about changes in the sensor configuration.
         *
         * @param executor The executor where the callback is executed on.
         * @param callback The callback to get notified when the state of the sensor
         * listeners has changed, see {@link VirtualSensorCallback}
         * configuration has changed, see {@link VirtualSensorCallback}
         */
        @SuppressLint("MissingGetterMatchingBuilder")
        @NonNull
        public Builder setVirtualSensorCallback(
                @NonNull @CallbackExecutor Executor executor,
                @NonNull VirtualSensorCallback callback) {
            mVirtualSensorCallback = new VirtualSensorCallbackDelegate(
                    Objects.requireNonNull(executor),
                    Objects.requireNonNull(callback));
            mVirtualSensorCallbackExecutor = Objects.requireNonNull(executor);
            mVirtualSensorCallback = Objects.requireNonNull(callback);
            return this;
        }

        /**
         * Sets the callback to get notified about changes in
         * {@link android.hardware.SensorDirectChannel} configuration.
         *
         * @param executor The executor where the callback is executed on.
         * @param callback The callback to get notified when the state of the sensor
         * configuration has changed, see {@link VirtualSensorDirectChannelCallback}
         */
        @SuppressLint("MissingGetterMatchingBuilder")
        @NonNull
        public Builder setVirtualSensorDirectChannelCallback(
                @NonNull @CallbackExecutor Executor executor,
                @NonNull VirtualSensorDirectChannelCallback callback) {
            mVirtualSensorDirectChannelCallbackExecutor = Objects.requireNonNull(executor);
            mVirtualSensorDirectChannelCallback = Objects.requireNonNull(callback);
            return this;
        }

@@ -857,6 +895,7 @@ public final class VirtualDeviceParams implements Parcelable {
         */
        @NonNull
        public VirtualDeviceParams build() {
            VirtualSensorCallbackDelegate virtualSensorCallbackDelegate = null;
            if (!mVirtualSensorConfigs.isEmpty()) {
                if (mDevicePolicies.get(POLICY_TYPE_SENSORS, DEVICE_POLICY_DEFAULT)
                        != DEVICE_POLICY_CUSTOM) {
@@ -868,6 +907,22 @@ public final class VirtualDeviceParams implements Parcelable {
                    throw new IllegalArgumentException(
                            "VirtualSensorCallback is required for creating virtual sensors.");
                }

                for (int i = 0; i < mVirtualSensorConfigs.size(); ++i) {
                    if (mVirtualSensorConfigs.get(i).getDirectChannelTypesSupported() > 0) {
                        if (mVirtualSensorDirectChannelCallback == null) {
                            throw new IllegalArgumentException(
                                    "VirtualSensorDirectChannelCallback is required for creating "
                                            + "virtual sensors that support direct channel.");
                        }
                        break;
                    }
                }
                virtualSensorCallbackDelegate = new VirtualSensorCallbackDelegate(
                        mVirtualSensorCallbackExecutor,
                        mVirtualSensorCallback,
                        mVirtualSensorDirectChannelCallbackExecutor,
                        mVirtualSensorDirectChannelCallback);
            }

            if ((mAudioPlaybackSessionId != AUDIO_SESSION_ID_GENERATE
@@ -901,7 +956,7 @@ public final class VirtualDeviceParams implements Parcelable {
                    mName,
                    mDevicePolicies,
                    mVirtualSensorConfigs,
                    mVirtualSensorCallback,
                    virtualSensorCallbackDelegate,
                    mAudioPlaybackSessionId,
                    mAudioRecordingSessionId);
        }
+3 −76
Original line number Diff line number Diff line
@@ -17,18 +17,14 @@
package android.companion.virtual.sensor;


import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.hardware.Sensor;
import android.hardware.SensorDirectChannel;
import android.os.MemoryFile;
import android.os.SharedMemory;

import java.time.Duration;

/**
 * Interface for notifying the sensor owner about whether and how sensor events should be injected.
 * Interface for notifying the virtual device owner about whether and how sensor events should be
 * injected.
 *
 * <p>This callback can be used for controlling the sensor event injection - e.g. if the sensor is
 * not enabled, then no events should be injected. Similarly, the rate and delay of the injected
@@ -45,6 +41,7 @@ public interface VirtualSensorCallback {
     * Called when the requested sensor event injection parameters have changed.
     *
     * <p>This is effectively called when the registered listeners to a virtual sensor have changed.
     * The events for the corresponding sensor should be sent via {@link VirtualSensor#sendEvent}.
     *
     * @param sensor The sensor whose requested injection parameters have changed.
     * @param enabled Whether the sensor is enabled. True if any listeners are currently registered,
@@ -55,74 +52,4 @@ public interface VirtualSensorCallback {
     */
    void onConfigurationChanged(@NonNull VirtualSensor sensor, boolean enabled,
            @NonNull Duration samplingPeriod, @NonNull Duration batchReportLatency);

    /**
     * Called when a {@link android.hardware.SensorDirectChannel} is created.
     *
     * <p>The {@link android.hardware.SensorManager} instance used to create the direct channel must
     * be associated with the virtual device.
     *
     * <p>A typical order of callback invocations is:
     * <ul>
     *     <li>{@code onDirectChannelCreated} - the channel handle and the associated shared memory
     *     should be stored by the virtual device</li>
     *     <li>{@code onDirectChannelConfigured} with a positive {@code rateLevel} - the virtual
     *     device should start writing to the shared memory for the associated channel with the
     *     requested parameters.</li>
     *     <li>{@code onDirectChannelConfigured} with a {@code rateLevel = RATE_STOP} - the virtual
     *     device should stop writing to the shared memory for the associated channel.</li>
     *     <li>{@code onDirectChannelDestroyed} - the shared memory associated with the channel
     *     handle should be closed.</li>
     * </ul>
     *
     * @param channelHandle Identifier of the newly created channel.
     * @param sharedMemory writable shared memory region.
     *
     * @see android.hardware.SensorManager#createDirectChannel(MemoryFile)
     * @see #onDirectChannelConfigured
     * @see #onDirectChannelDestroyed
     */
    default void onDirectChannelCreated(@IntRange(from = 1) int channelHandle,
            @NonNull SharedMemory sharedMemory) {}

    /**
     * Called when a {@link android.hardware.SensorDirectChannel} is destroyed.
     *
     * <p>The virtual device must perform any clean-up and close the shared memory that was
     * received with the {@link #onDirectChannelCreated} callback and the corresponding
     * {@code channelHandle}.
     *
     * @param channelHandle Identifier of the channel that was destroyed.
     *
     * @see SensorDirectChannel#close()
     */
    default void onDirectChannelDestroyed(@IntRange(from = 1) int channelHandle) {}

    /**
     * Called when a {@link android.hardware.SensorDirectChannel} is configured.
     *
     * <p>Sensor events for the corresponding sensor should be written at the indicated rate to the
     * shared memory region that was received with the {@link #onDirectChannelCreated} callback and
     * the corresponding {@code channelHandle}. The events should be written in the correct format
     * and with the provided {@code reportToken} until the channel is reconfigured with
     * {@link SensorDirectChannel#RATE_STOP}.
     *
     * <p>The sensor must support direct channel in order for this callback to be invoked. Only
     * {@link MemoryFile} sensor direct channels are supported for virtual sensors.
     *
     * @param channelHandle Identifier of the channel that was configured.
     * @param sensor The sensor, for which the channel was configured.
     * @param rateLevel The rate level used to configure the direct sensor channel.
     * @param reportToken A positive sensor report token, used to differentiate between events from
     * different sensors within the same channel.
     *
     * @see VirtualSensorConfig.Builder#setHighestDirectReportRateLevel(int)
     * @see VirtualSensorConfig.Builder#setDirectChannelTypesSupported(int)
     * @see android.hardware.SensorManager#createDirectChannel(MemoryFile)
     * @see #onDirectChannelCreated
     * @see SensorDirectChannel#configure(Sensor, int)
     */
    default void onDirectChannelConfigured(@IntRange(from = 1) int channelHandle,
            @NonNull VirtualSensor sensor, @SensorDirectChannel.RateLevel int rateLevel,
            @IntRange(from = 1) int reportToken) {}
}
+108 −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.sensor;


import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.hardware.Sensor;
import android.hardware.SensorDirectChannel;
import android.os.MemoryFile;
import android.os.SharedMemory;

/**
 * Interface for notifying the virtual device owner about any {@link SensorDirectChannel} events.
 *
 * <p>This callback can be used for controlling the sensor event injection to direct channels. A
 * typical order of callback invocations is:
 * <ul>
 *     <li>{@code onDirectChannelCreated} - the channel handle and the associated shared memory
 *     should be stored by the virtual device</li>
 *     <li>{@code onDirectChannelConfigured} with a positive {@code rateLevel} - the virtual
 *     device should start writing to the shared memory for the associated channel with the
 *     requested parameters.</li>
 *     <li>{@code onDirectChannelConfigured} with a {@code rateLevel = RATE_STOP} - the virtual
 *     device should stop writing to the shared memory for the associated channel.</li>
 *     <li>{@code onDirectChannelDestroyed} - the shared memory associated with the channel
 *     handle should be closed.</li>
 * </ul>
 *
 * <p>The callback is tied to the VirtualDevice's lifetime as the virtual sensors are created when
 * the device is created and destroyed when the device is destroyed.
 *
 * @hide
 */
@SystemApi
public interface VirtualSensorDirectChannelCallback {
    /**
     * Called when a {@link android.hardware.SensorDirectChannel} is created.
     *
     * <p>The {@link android.hardware.SensorManager} instance used to create the direct channel must
     * be associated with the virtual device.
     *
     * @param channelHandle Identifier of the newly created channel.
     * @param sharedMemory writable shared memory region.
     *
     * @see android.hardware.SensorManager#createDirectChannel(MemoryFile)
     * @see #onDirectChannelConfigured
     * @see #onDirectChannelDestroyed
     */
    void onDirectChannelCreated(@IntRange(from = 1) int channelHandle,
            @NonNull SharedMemory sharedMemory);

    /**
     * Called when a {@link android.hardware.SensorDirectChannel} is destroyed.
     *
     * <p>The virtual device must perform any clean-up and close the shared memory that was
     * received with the {@link #onDirectChannelCreated} callback and the corresponding
     * {@code channelHandle}.
     *
     * @param channelHandle Identifier of the channel that was destroyed.
     *
     * @see SensorDirectChannel#close()
     */
    void onDirectChannelDestroyed(@IntRange(from = 1) int channelHandle);

    /**
     * Called when a {@link android.hardware.SensorDirectChannel} is configured.
     *
     * <p>Sensor events for the corresponding sensor should be written at the indicated rate to the
     * shared memory region that was received with the {@link #onDirectChannelCreated} callback and
     * the corresponding {@code channelHandle}. The events should be written in the correct format
     * and with the provided {@code reportToken} until the channel is reconfigured with
     * {@link SensorDirectChannel#RATE_STOP}.
     *
     * <p>The sensor must support direct channel in order for this callback to be invoked. Only
     * {@link MemoryFile} sensor direct channels are supported for virtual sensors.
     *
     * @param channelHandle Identifier of the channel that was configured.
     * @param sensor The sensor, for which the channel was configured.
     * @param rateLevel The rate level used to configure the direct sensor channel.
     * @param reportToken A positive sensor report token, used to differentiate between events from
     * different sensors within the same channel.
     *
     * @see VirtualSensorConfig.Builder#setHighestDirectReportRateLevel(int)
     * @see VirtualSensorConfig.Builder#setDirectChannelTypesSupported(int)
     * @see android.hardware.SensorManager#createDirectChannel(MemoryFile)
     * @see #onDirectChannelCreated
     * @see SensorDirectChannel#configure(Sensor, int)
     */
    void onDirectChannelConfigured(@IntRange(from = 1) int channelHandle,
            @NonNull VirtualSensor sensor, @SensorDirectChannel.RateLevel int rateLevel,
            @IntRange(from = 1) int reportToken);
}
+261 −0

File added.

Preview size limit exceeded, changes collapsed.