Merge "Add support for more VirtualSensor properties." into udc-dev

This commit is contained in:
Vladimir Komsiyski
2023-03-24 14:32:17 +00:00
committed by Android (Google) Code Review
8 changed files with 163 additions and 17 deletions

View File

@@ -3333,7 +3333,12 @@ package android.companion.virtual.sensor {
method public int describeContents();
method public int getDirectChannelTypesSupported();
method public int getHighestDirectReportRateLevel();
method public int getMaxDelay();
method public float getMaximumRange();
method public int getMinDelay();
method @NonNull public String getName();
method public float getPower();
method public float getResolution();
method public int getType();
method @Nullable public String getVendor();
method public void writeToParcel(@NonNull android.os.Parcel, int);
@@ -3345,6 +3350,11 @@ package android.companion.virtual.sensor {
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig build();
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setDirectChannelTypesSupported(int);
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setHighestDirectReportRateLevel(int);
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setMaxDelay(int);
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setMaximumRange(float);
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setMinDelay(int);
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setPower(float);
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setResolution(float);
method @NonNull public android.companion.virtual.sensor.VirtualSensorConfig.Builder setVendor(@Nullable String);
}

View File

@@ -50,14 +50,25 @@ public final class VirtualSensorConfig implements Parcelable {
private final String mName;
@Nullable
private final String mVendor;
private final float mMaximumRange;
private final float mResolution;
private final float mPower;
private final int mMinDelay;
private final int mMaxDelay;
private final int mFlags;
private VirtualSensorConfig(int type, @NonNull String name, @Nullable String vendor,
float maximumRange, float resolution, float power, int minDelay, int maxDelay,
int flags) {
mType = type;
mName = name;
mVendor = vendor;
mMaximumRange = maximumRange;
mResolution = resolution;
mPower = power;
mMinDelay = minDelay;
mMaxDelay = maxDelay;
mFlags = flags;
}
@@ -65,6 +76,11 @@ public final class VirtualSensorConfig implements Parcelable {
mType = parcel.readInt();
mName = parcel.readString8();
mVendor = parcel.readString8();
mMaximumRange = parcel.readFloat();
mResolution = parcel.readFloat();
mPower = parcel.readFloat();
mMinDelay = parcel.readInt();
mMaxDelay = parcel.readInt();
mFlags = parcel.readInt();
}
@@ -78,6 +94,11 @@ public final class VirtualSensorConfig implements Parcelable {
parcel.writeInt(mType);
parcel.writeString8(mName);
parcel.writeString8(mVendor);
parcel.writeFloat(mMaximumRange);
parcel.writeFloat(mResolution);
parcel.writeFloat(mPower);
parcel.writeInt(mMinDelay);
parcel.writeInt(mMaxDelay);
parcel.writeInt(mFlags);
}
@@ -108,6 +129,47 @@ public final class VirtualSensorConfig implements Parcelable {
return mVendor;
}
/**
* Returns maximum range of the sensor in the sensor's unit.
* @see Sensor#getMaximumRange
*/
public float getMaximumRange() {
return mMaximumRange;
}
/**
* Returns The resolution of the sensor in the sensor's unit.
* @see Sensor#getResolution
*/
public float getResolution() {
return mResolution;
}
/**
* Returns The power in mA used by this sensor while in use.
* @see Sensor#getPower
*/
public float getPower() {
return mPower;
}
/**
* Returns The minimum delay allowed between two events in microseconds, or zero depending on
* the sensor type.
* @see Sensor#getMinDelay
*/
public int getMinDelay() {
return mMinDelay;
}
/**
* Returns The maximum delay between two sensor events in microseconds.
* @see Sensor#getMaxDelay
*/
public int getMaxDelay() {
return mMaxDelay;
}
/**
* Returns the highest supported direct report mode rate level of the sensor.
*
@@ -157,6 +219,11 @@ public final class VirtualSensorConfig implements Parcelable {
private final String mName;
@Nullable
private String mVendor;
private float mMaximumRange;
private float mResolution;
private float mPower;
private int mMinDelay;
private int mMaxDelay;
private int mFlags;
@SensorDirectChannel.RateLevel
int mHighestDirectReportRateLevel;
@@ -193,7 +260,8 @@ public final class VirtualSensorConfig implements Parcelable {
throw new IllegalArgumentException("Highest direct report rate level is "
+ "required for sensors with direct channel support.");
}
return new VirtualSensorConfig(mType, mName, mVendor, mFlags);
return new VirtualSensorConfig(mType, mName, mVendor, mMaximumRange, mResolution,
mPower, mMinDelay, mMaxDelay, mFlags);
}
/**
@@ -205,6 +273,56 @@ public final class VirtualSensorConfig implements Parcelable {
return this;
}
/**
* Sets the maximum range of the sensor in the sensor's unit.
* @see Sensor#getMaximumRange
*/
@NonNull
public VirtualSensorConfig.Builder setMaximumRange(float maximumRange) {
mMaximumRange = maximumRange;
return this;
}
/**
* Sets the resolution of the sensor in the sensor's unit.
* @see Sensor#getResolution
*/
@NonNull
public VirtualSensorConfig.Builder setResolution(float resolution) {
mResolution = resolution;
return this;
}
/**
* Sets the power in mA used by this sensor while in use.
* @see Sensor#getPower
*/
@NonNull
public VirtualSensorConfig.Builder setPower(float power) {
mPower = power;
return this;
}
/**
* Sets the minimum delay allowed between two events in microseconds.
* @see Sensor#getMinDelay
*/
@NonNull
public VirtualSensorConfig.Builder setMinDelay(int minDelay) {
mMinDelay = minDelay;
return this;
}
/**
* Sets the maximum delay between two sensor events in microseconds.
* @see Sensor#getMaxDelay
*/
@NonNull
public VirtualSensorConfig.Builder setMaxDelay(int maxDelay) {
mMaxDelay = maxDelay;
return this;
}
/**
* Sets the highest supported rate level for direct sensor report.
*

View File

@@ -104,8 +104,9 @@ public class SensorController {
}
final int handle = mSensorManagerInternal.createRuntimeSensor(mVirtualDeviceId,
config.getType(), config.getName(),
config.getVendor() == null ? "" : config.getVendor(), config.getFlags(),
mRuntimeSensorCallback);
config.getVendor() == null ? "" : config.getVendor(), config.getMaximumRange(),
config.getResolution(), config.getPower(), config.getMinDelay(),
config.getMaxDelay(), config.getFlags(), mRuntimeSensorCallback);
if (handle <= 0) {
throw new SensorCreationException("Received an invalid virtual sensor handle.");
}

View File

@@ -60,7 +60,8 @@ public abstract class SensorManagerInternal {
* @return The sensor handle.
*/
public abstract int createRuntimeSensor(int deviceId, int type, @NonNull String name,
@NonNull String vendor, int flags, @NonNull RuntimeSensorCallback callback);
@NonNull String vendor, float maximumRange, float resolution, float power,
int minDelay, int maxDelay, int flags, @NonNull RuntimeSensorCallback callback);
/**
* Unregisters the sensor with the given handle from the framework.

View File

@@ -56,7 +56,8 @@ public class SensorService extends SystemService {
private static native void unregisterProximityActiveListenerNative(long ptr);
private static native int registerRuntimeSensorNative(long ptr, int deviceId, int type,
String name, String vendor, int flags,
String name, String vendor, float maximumRange, float resolution, float power,
int minDelay, int maxDelay, int flags,
SensorManagerInternal.RuntimeSensorCallback callback);
private static native void unregisterRuntimeSensorNative(long ptr, int handle);
private static native boolean sendRuntimeSensorEventNative(long ptr, int handle, int type,
@@ -96,10 +97,11 @@ public class SensorService extends SystemService {
class LocalService extends SensorManagerInternal {
@Override
public int createRuntimeSensor(int deviceId, int type, @NonNull String name,
@NonNull String vendor, int flags, @NonNull RuntimeSensorCallback callback) {
@NonNull String vendor, float maximumRange, float resolution, float power,
int minDelay, int maxDelay, int flags, @NonNull RuntimeSensorCallback callback) {
synchronized (mLock) {
int handle = registerRuntimeSensorNative(mPtr, deviceId, type, name, vendor, flags,
callback);
int handle = registerRuntimeSensorNative(mPtr, deviceId, type, name, vendor,
maximumRange, resolution, power, minDelay, maxDelay, flags, callback);
mRuntimeSensorHandles.add(handle);
return handle;
}

View File

@@ -55,7 +55,8 @@ public:
void registerProximityActiveListener();
void unregisterProximityActiveListener();
jint registerRuntimeSensor(JNIEnv* env, jint deviceId, jint type, jstring name, jstring vendor,
jint flags, jobject callback);
jfloat maximumRange, jfloat resolution, jfloat power, jint minDelay,
jint maxDelay, jint flags, jobject callback);
void unregisterRuntimeSensor(jint handle);
jboolean sendRuntimeSensorEvent(JNIEnv* env, jint handle, jint type, jlong timestamp,
jfloatArray values);
@@ -119,7 +120,9 @@ void NativeSensorService::unregisterProximityActiveListener() {
}
jint NativeSensorService::registerRuntimeSensor(JNIEnv* env, jint deviceId, jint type, jstring name,
jstring vendor, jint flags, jobject callback) {
jstring vendor, jfloat maximumRange,
jfloat resolution, jfloat power, jint minDelay,
jint maxDelay, jint flags, jobject callback) {
if (mService == nullptr) {
ALOGD("Dropping registerRuntimeSensor, sensor service not available.");
return -1;
@@ -130,6 +133,11 @@ jint NativeSensorService::registerRuntimeSensor(JNIEnv* env, jint deviceId, jint
.vendor = env->GetStringUTFChars(vendor, 0),
.version = sizeof(sensor_t),
.type = type,
.maxRange = maximumRange,
.resolution = resolution,
.power = power,
.minDelay = minDelay,
.maxDelay = maxDelay,
#ifdef __LP64__
.flags = static_cast<uint64_t>(flags),
#else
@@ -299,10 +307,12 @@ static void unregisterProximityActiveListenerNative(JNIEnv* env, jclass, jlong p
}
static jint registerRuntimeSensorNative(JNIEnv* env, jclass, jlong ptr, jint deviceId, jint type,
jstring name, jstring vendor, jint flags,
jobject callback) {
jstring name, jstring vendor, jfloat maximumRange,
jfloat resolution, jfloat power, jint minDelay,
jint maxDelay, jint flags, jobject callback) {
auto* service = reinterpret_cast<NativeSensorService*>(ptr);
return service->registerRuntimeSensor(env, deviceId, type, name, vendor, flags, callback);
return service->registerRuntimeSensor(env, deviceId, type, name, vendor, maximumRange,
resolution, power, minDelay, maxDelay, flags, callback);
}
static void unregisterRuntimeSensorNative(JNIEnv* env, jclass, jlong ptr, jint handle) {
@@ -324,7 +334,7 @@ static const JNINativeMethod methods[] = {
{"unregisterProximityActiveListenerNative", "(J)V",
reinterpret_cast<void*>(unregisterProximityActiveListenerNative)},
{"registerRuntimeSensorNative",
"(JIILjava/lang/String;Ljava/lang/String;IL" RUNTIME_SENSOR_CALLBACK_CLASS ";)I",
"(JIILjava/lang/String;Ljava/lang/String;FFFIIIL" RUNTIME_SENSOR_CALLBACK_CLASS ";)I",
reinterpret_cast<void*>(registerRuntimeSensorNative)},
{"unregisterRuntimeSensorNative", "(JI)V",
reinterpret_cast<void*>(unregisterRuntimeSensorNative)},

View File

@@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
@@ -81,7 +82,8 @@ public class SensorControllerTest {
@Test
public void createSensor_invalidHandle_throwsException() {
doReturn(/* handle= */0).when(mSensorManagerInternalMock).createRuntimeSensor(
anyInt(), anyInt(), anyString(), anyString(), anyInt(), any());
anyInt(), anyInt(), anyString(), anyString(), anyFloat(), anyFloat(), anyFloat(),
anyInt(), anyInt(), anyInt(), any());
Throwable thrown = assertThrows(
RuntimeException.class,
@@ -138,7 +140,8 @@ public class SensorControllerTest {
private void doCreateSensorSuccessfully() {
doReturn(SENSOR_HANDLE).when(mSensorManagerInternalMock).createRuntimeSensor(
anyInt(), anyInt(), anyString(), anyString(), anyInt(), any());
anyInt(), anyInt(), anyString(), anyString(), anyFloat(), anyFloat(), anyFloat(),
anyInt(), anyInt(), anyInt(), any());
assertThat(mSensorController.createSensor(mSensorToken, mVirtualSensorConfig))
.isEqualTo(SENSOR_HANDLE);
}

View File

@@ -536,7 +536,8 @@ public class VirtualDeviceManagerServiceTest {
.build();
doReturn(SENSOR_HANDLE).when(mSensorManagerInternalMock).createRuntimeSensor(
anyInt(), anyInt(), anyString(), anyString(), anyInt(), any());
anyInt(), anyInt(), anyString(), anyString(), anyFloat(), anyFloat(), anyFloat(),
anyInt(), anyInt(), anyInt(), any());
mDeviceImpl.close();
mDeviceImpl = createVirtualDevice(VIRTUAL_DEVICE_ID_1, DEVICE_OWNER_UID_1, params);