From fb1c94133b0cddd5550522b476f4ab69cc0ec73b Mon Sep 17 00:00:00 2001 From: Peng Xu Date: Tue, 29 Mar 2016 21:50:43 -0700 Subject: [PATCH] Fix a few known issue in sensor framework * Resolve issues pointed out by API Consul: . Privacy concern about sensor UUID . Callback name consistency, . A few document error and typo. * New API to tell app if the sensor system support dynamic sensor discovery. * Added API to make dynamically connected sensor directly identifiable. * Explicitly indicate sensor additional information support for a sensor Bugs: b/27715564 b/27911274 Change-Id: Ie93ab4c07fea82002f3f107b7c35827a088312be --- api/current.txt | 15 ++-- api/system-current.txt | 14 +-- api/test-current.txt | 15 ++-- core/java/android/hardware/Sensor.java | 86 +++++++++++++++---- .../hardware/SensorAdditionalInfo.java | 4 +- core/java/android/hardware/SensorEvent.java | 24 +++--- core/java/android/hardware/SensorManager.java | 51 ++++++----- .../android/hardware/SystemSensorManager.java | 14 +-- 8 files changed, 150 insertions(+), 73 deletions(-) diff --git a/api/current.txt b/api/current.txt index 5ceb75974ba6e..55f8582406da2 100644 --- a/api/current.txt +++ b/api/current.txt @@ -13504,6 +13504,7 @@ package android.hardware { public final class Sensor { method public int getFifoMaxEventCount(); method public int getFifoReservedEventCount(); + method public int getId(); method public int getMaxDelay(); method public float getMaximumRange(); method public int getMinDelay(); @@ -13513,9 +13514,10 @@ package android.hardware { method public float getResolution(); method public java.lang.String getStringType(); method public int getType(); - method public java.util.UUID getUuid(); method public java.lang.String getVendor(); method public int getVersion(); + method public boolean isAdditionalInfoSupported(); + method public boolean isDynamicSensor(); method public boolean isWakeUpSensor(); field public static final int REPORTING_MODE_CONTINUOUS = 0; // 0x0 field public static final int REPORTING_MODE_ONE_SHOT = 2; // 0x2 @@ -13634,8 +13636,9 @@ package android.hardware { method public static void getRotationMatrixFromVector(float[], float[]); method public java.util.List getSensorList(int); method public deprecated int getSensors(); - method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback); - method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback, android.os.Handler); + method public boolean isDynamicSensorDiscoverySupported(); + method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback); + method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback, android.os.Handler); method public deprecated boolean registerListener(android.hardware.SensorListener, int); method public deprecated boolean registerListener(android.hardware.SensorListener, int, int); method public boolean registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int); @@ -13644,7 +13647,7 @@ package android.hardware { method public boolean registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int, int, android.os.Handler); method public static boolean remapCoordinateSystem(float[], int, int, float[]); method public boolean requestTriggerSensor(android.hardware.TriggerEventListener, android.hardware.Sensor); - method public void unregisterDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback); + method public void unregisterDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback); method public deprecated void unregisterListener(android.hardware.SensorListener); method public deprecated void unregisterListener(android.hardware.SensorListener, int); method public void unregisterListener(android.hardware.SensorEventListener, android.hardware.Sensor); @@ -13709,8 +13712,8 @@ package android.hardware { field public static final float STANDARD_GRAVITY = 9.80665f; } - public static abstract class SensorManager.DynamicSensorConnectionCallback { - ctor public SensorManager.DynamicSensorConnectionCallback(); + public static abstract class SensorManager.DynamicSensorCallback { + ctor public SensorManager.DynamicSensorCallback(); method public void onDynamicSensorConnected(android.hardware.Sensor); method public void onDynamicSensorDisconnected(android.hardware.Sensor); } diff --git a/api/system-current.txt b/api/system-current.txt index bd77046c15d37..5f2547dbcb6e3 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -13902,6 +13902,7 @@ package android.hardware { public final class Sensor { method public int getFifoMaxEventCount(); method public int getFifoReservedEventCount(); + method public int getId(); method public int getMaxDelay(); method public float getMaximumRange(); method public int getMinDelay(); @@ -13914,7 +13915,9 @@ package android.hardware { method public java.util.UUID getUuid(); method public java.lang.String getVendor(); method public int getVersion(); + method public boolean isAdditionalInfoSupported(); method public boolean isDataInjectionSupported(); + method public boolean isDynamicSensor(); method public boolean isWakeUpSensor(); field public static final int REPORTING_MODE_CONTINUOUS = 0; // 0x0 field public static final int REPORTING_MODE_ONE_SHOT = 2; // 0x2 @@ -14039,8 +14042,9 @@ package android.hardware { method public deprecated int getSensors(); method public boolean initDataInjection(boolean); method public boolean injectSensorData(android.hardware.Sensor, float[], int, long); - method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback); - method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback, android.os.Handler); + method public boolean isDynamicSensorDiscoverySupported(); + method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback); + method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback, android.os.Handler); method public deprecated boolean registerListener(android.hardware.SensorListener, int); method public deprecated boolean registerListener(android.hardware.SensorListener, int, int); method public boolean registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int); @@ -14049,7 +14053,7 @@ package android.hardware { method public boolean registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int, int, android.os.Handler); method public static boolean remapCoordinateSystem(float[], int, int, float[]); method public boolean requestTriggerSensor(android.hardware.TriggerEventListener, android.hardware.Sensor); - method public void unregisterDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback); + method public void unregisterDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback); method public deprecated void unregisterListener(android.hardware.SensorListener); method public deprecated void unregisterListener(android.hardware.SensorListener, int); method public void unregisterListener(android.hardware.SensorEventListener, android.hardware.Sensor); @@ -14114,8 +14118,8 @@ package android.hardware { field public static final float STANDARD_GRAVITY = 9.80665f; } - public static abstract class SensorManager.DynamicSensorConnectionCallback { - ctor public SensorManager.DynamicSensorConnectionCallback(); + public static abstract class SensorManager.DynamicSensorCallback { + ctor public SensorManager.DynamicSensorCallback(); method public void onDynamicSensorConnected(android.hardware.Sensor); method public void onDynamicSensorDisconnected(android.hardware.Sensor); } diff --git a/api/test-current.txt b/api/test-current.txt index 8a49756d9f6bf..5baf9675d2837 100644 --- a/api/test-current.txt +++ b/api/test-current.txt @@ -13514,6 +13514,7 @@ package android.hardware { public final class Sensor { method public int getFifoMaxEventCount(); method public int getFifoReservedEventCount(); + method public int getId(); method public int getMaxDelay(); method public float getMaximumRange(); method public int getMinDelay(); @@ -13523,9 +13524,10 @@ package android.hardware { method public float getResolution(); method public java.lang.String getStringType(); method public int getType(); - method public java.util.UUID getUuid(); method public java.lang.String getVendor(); method public int getVersion(); + method public boolean isAdditionalInfoSupported(); + method public boolean isDynamicSensor(); method public boolean isWakeUpSensor(); field public static final int REPORTING_MODE_CONTINUOUS = 0; // 0x0 field public static final int REPORTING_MODE_ONE_SHOT = 2; // 0x2 @@ -13644,8 +13646,9 @@ package android.hardware { method public static void getRotationMatrixFromVector(float[], float[]); method public java.util.List getSensorList(int); method public deprecated int getSensors(); - method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback); - method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback, android.os.Handler); + method public boolean isDynamicSensorDiscoverySupported(); + method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback); + method public void registerDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback, android.os.Handler); method public deprecated boolean registerListener(android.hardware.SensorListener, int); method public deprecated boolean registerListener(android.hardware.SensorListener, int, int); method public boolean registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int); @@ -13654,7 +13657,7 @@ package android.hardware { method public boolean registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int, int, android.os.Handler); method public static boolean remapCoordinateSystem(float[], int, int, float[]); method public boolean requestTriggerSensor(android.hardware.TriggerEventListener, android.hardware.Sensor); - method public void unregisterDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorConnectionCallback); + method public void unregisterDynamicSensorCallback(android.hardware.SensorManager.DynamicSensorCallback); method public deprecated void unregisterListener(android.hardware.SensorListener); method public deprecated void unregisterListener(android.hardware.SensorListener, int); method public void unregisterListener(android.hardware.SensorEventListener, android.hardware.Sensor); @@ -13719,8 +13722,8 @@ package android.hardware { field public static final float STANDARD_GRAVITY = 9.80665f; } - public static abstract class SensorManager.DynamicSensorConnectionCallback { - ctor public SensorManager.DynamicSensorConnectionCallback(); + public static abstract class SensorManager.DynamicSensorCallback { + ctor public SensorManager.DynamicSensorCallback(); method public void onDynamicSensorConnected(android.hardware.Sensor); method public void onDynamicSensorDisconnected(android.hardware.Sensor); } diff --git a/core/java/android/hardware/Sensor.java b/core/java/android/hardware/Sensor.java index 63ee8d2e75d3b..cc82eb67c275c 100644 --- a/core/java/android/hardware/Sensor.java +++ b/core/java/android/hardware/Sensor.java @@ -186,7 +186,7 @@ public final class Sensor { * @see #TYPE_LINEAR_ACCELERATION */ public static final String STRING_TYPE_LINEAR_ACCELERATION = - "android.sensor.linear_acceleration"; + "android.sensor.linear_acceleration"; /** * A constant describing a rotation vector sensor type. @@ -229,7 +229,7 @@ public final class Sensor { * @see #TYPE_AMBIENT_TEMPERATURE */ public static final String STRING_TYPE_AMBIENT_TEMPERATURE = - "android.sensor.ambient_temperature"; + "android.sensor.ambient_temperature"; /** * A constant describing an uncalibrated magnetic field sensor type. @@ -254,7 +254,7 @@ public final class Sensor { * @see #TYPE_MAGNETIC_FIELD_UNCALIBRATED */ public static final String STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED = - "android.sensor.magnetic_field_uncalibrated"; + "android.sensor.magnetic_field_uncalibrated"; /** * A constant describing an uncalibrated rotation vector sensor type. @@ -280,7 +280,7 @@ public final class Sensor { * @see #TYPE_GAME_ROTATION_VECTOR */ public static final String STRING_TYPE_GAME_ROTATION_VECTOR = - "android.sensor.game_rotation_vector"; + "android.sensor.game_rotation_vector"; /** * A constant describing an uncalibrated gyroscope sensor type. @@ -302,7 +302,7 @@ public final class Sensor { * @see #TYPE_GYROSCOPE_UNCALIBRATED */ public static final String STRING_TYPE_GYROSCOPE_UNCALIBRATED = - "android.sensor.gyroscope_uncalibrated"; + "android.sensor.gyroscope_uncalibrated"; /** * A constant describing a significant motion trigger sensor. @@ -324,7 +324,7 @@ public final class Sensor { * @see #TYPE_SIGNIFICANT_MOTION */ public static final String STRING_TYPE_SIGNIFICANT_MOTION = - "android.sensor.significant_motion"; + "android.sensor.significant_motion"; /** * A constant describing a step detector sensor. @@ -391,7 +391,7 @@ public final class Sensor { * @see #TYPE_GEOMAGNETIC_ROTATION_VECTOR */ public static final String STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR = - "android.sensor.geomagnetic_rotation_vector"; + "android.sensor.geomagnetic_rotation_vector"; /** * A constant describing a heart rate monitor. @@ -431,7 +431,7 @@ public final class Sensor { * A constant string describing a wake up tilt detector sensor type. * * @hide - * @see #TYPE_WAKE_UP_TILT_DETECTOR + * @see #TYPE_TILT_DETECTOR */ public static final String SENSOR_STRING_TYPE_TILT_DETECTOR = "android.sensor.tilt_detector"; @@ -495,7 +495,7 @@ public final class Sensor { */ public static final String STRING_TYPE_GLANCE_GESTURE = "android.sensor.glance_gesture"; - /** + /** * A constant describing a pick up sensor. * * A sensor of this type triggers when the device is picked up regardless of wherever it was @@ -514,7 +514,7 @@ public final class Sensor { */ public static final String STRING_TYPE_PICK_UP_GESTURE = "android.sensor.pick_up_gesture"; - /** + /** * A constant describing a wrist tilt gesture sensor. * * A sensor of this type triggers when the device face is tilted towards the user. @@ -553,7 +553,7 @@ public final class Sensor { */ public static final String STRING_TYPE_DEVICE_ORIENTATION = "android.sensor.device_orientation"; - /** + /** * A constant describing a pose sensor with 6 degrees of freedom. * * Similar to {@link #TYPE_ROTATION_VECTOR}, with additional delta @@ -578,7 +578,7 @@ public final class Sensor { */ public static final String STRING_TYPE_POSE_6DOF = "android.sensor.pose_6dof"; - /** + /** * A constant describing a stationary detect sensor. * * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details. @@ -593,7 +593,7 @@ public final class Sensor { */ public static final String STRING_TYPE_STATIONARY_DETECT = "android.sensor.stationary_detect"; - /** + /** * A constant describing a motion detect sensor. * * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details. @@ -608,7 +608,7 @@ public final class Sensor { */ public static final String STRING_TYPE_MOTION_DETECT = "android.sensor.motion_detect"; - /** + /** * A constant describing a motion detect sensor. * * See {@link android.hardware.SensorEvent#values SensorEvent.values} for more details. @@ -706,6 +706,14 @@ public final class Sensor { private static final int DATA_INJECTION_MASK = 0x10; private static final int DATA_INJECTION_SHIFT = 4; + // MASK for dynamic sensor (sensor that added during runtime), bit 6. + private static final int DYNAMIC_SENSOR_MASK = 0x20; + private static final int DYNAMIC_SENSOR_SHIFT = 5; + + // MASK for indication bit of sensor additional information support (bit 7). + private static final int ADDITIONAL_INFO_MASK = 0x40; + private static final int ADDITIONAL_INFO_SHIFT = 6; + // TODO(): The following arrays are fragile and error-prone. This needs to be refactored. // Note: This needs to be updated, whenever a new sensor is added. @@ -887,12 +895,36 @@ public final class Sensor { } /** - * @return The type of this sensor as a string. + * @return The UUID of the sensor. If the sensor does not support UUID, the returned value will + * be an all zero UUID; if the sensor's combination of type and name is guaranteed to be unique + * in system, the return value will be an all "F" UUID. + * + * @hide */ + @SystemApi public UUID getUuid() { return mUuid; } + /** + * @return The unique id of sensor. Return value of 0 means this sensor does not support UUID; + * return value of -1 means this sensor can be uniquely identified in system by combination of + * its type and name. + */ + public int getId() { + if (mUuid == ALL_0_UUID) { + return 0; + } else if (mUuid == ALL_F_UUID) { + return -1; + } else { + int id = Math.abs(mUuid.hashCode()) + 1; + if (id <= 0) { // catch corner case when hash is Integer.MIN_VALUE and Integer.MAX_VALUE + id = 1; + } + return id; + } + } + /** * @hide * @return The permission required to access this sensor. If empty, no permission is required. @@ -960,6 +992,26 @@ public final class Sensor { return (mFlags & SENSOR_FLAG_WAKE_UP_SENSOR) != 0; } + /** + * Returns true if the sensor is a dynamic sensor. + * + * @return true if the sensor is a dynamic sensor (sensor added at runtime). + * @see SensorManager.DynamicSensorCallback + */ + public boolean isDynamicSensor() { + return (mFlags & DYNAMIC_SENSOR_MASK) != 0; + } + + /** + * Returns true if the sensor supports sensor additional information API + * + * @return true if the sensor supports sensor additional information API + * @see SensorAdditionalInfo + */ + public boolean isAdditionalInfoSupported() { + return (mFlags & ADDITIONAL_INFO_MASK) != 0; + } + /** * Returns true if the sensor supports data injection when the * HAL is set to data injection mode. @@ -986,6 +1038,10 @@ public final class Sensor { + ", power=" + mPower + ", minDelay=" + mMinDelay + "}"; } + //special UUID hash constant + private final static UUID ALL_0_UUID = new UUID(0,0); + private final static UUID ALL_F_UUID = new UUID(~0L, ~0L); + /** * Sets the Type associated with the sensor. * NOTE: to be used only by native bindings in SensorManager. diff --git a/core/java/android/hardware/SensorAdditionalInfo.java b/core/java/android/hardware/SensorAdditionalInfo.java index 8e5b8a3c1a6cd..572a287f53d81 100644 --- a/core/java/android/hardware/SensorAdditionalInfo.java +++ b/core/java/android/hardware/SensorAdditionalInfo.java @@ -27,7 +27,7 @@ import java.lang.annotation.RetentionPolicy; * android.hardware.SensorEventCallback#onSensorAdditionalInfo onSensorAdditionalInfo}. * * @see SensorManager - * @see SensorEventListener3 + * @see SensorEventCallback * @see Sensor * */ @@ -106,7 +106,7 @@ public class SensorAdditionalInfo { * such as accelerometer, gyro, etc. * * Payload: - * floatValues[0..11]: First 3 rows of a homogenous matrix in row major order that captures + * floatValues[0..11]: First 3 rows of a homogeneous matrix in row major order that captures * any linear transformation, including rotation, scaling, shear, shift. */ public static final int TYPE_VEC3_CALIBRATION = 0x10002; diff --git a/core/java/android/hardware/SensorEvent.java b/core/java/android/hardware/SensorEvent.java index 35c96f724017b..0d96b8e82a5d9 100644 --- a/core/java/android/hardware/SensorEvent.java +++ b/core/java/android/hardware/SensorEvent.java @@ -18,7 +18,7 @@ package android.hardware; /** * This class represents a {@link android.hardware.Sensor Sensor} event and - * holds informations such as the sensor's type, the time-stamp, accuracy and of + * holds information such as the sensor's type, the time-stamp, accuracy and of * course the sensor's {@link SensorEvent#values data}. * *

@@ -163,7 +163,7 @@ public class SensorEvent { * *

* Typically the output of the gyroscope is integrated over time to - * calculate a rotation describing the change of angles over the timestep, + * calculate a rotation describing the change of angles over the time step, * for example: *

* @@ -173,7 +173,7 @@ public class SensorEvent { * private float timestamp; * * public void onSensorChanged(SensorEvent event) { - * // This timestep's delta rotation to be multiplied by the current rotation + * // This time step's delta rotation to be multiplied by the current rotation * // after computing it from the gyro sample data. * if (timestamp != 0) { * final float dT = (event.timestamp - timestamp) * NS2S; @@ -192,8 +192,8 @@ public class SensorEvent { * axisZ /= omegaMagnitude; * } * - * // Integrate around this axis with the angular speed by the timestep - * // in order to get a delta rotation from this sample over the timestep + * // Integrate around this axis with the angular speed by the time step + * // in order to get a delta rotation from this sample over the time step * // We will convert this axis-angle representation of the delta rotation * // into a quaternion before turning it into the rotation matrix. * float thetaOverTwo = omegaMagnitude * dT / 2.0f; @@ -433,9 +433,9 @@ public class SensorEvent { * Each field is a component of the estimated hard iron calibration. * The values are in micro-Tesla (uT). *

- *

Hard iron - These distortions arise due to the magnetized iron, steel or permanenet + *

Hard iron - These distortions arise due to the magnetized iron, steel or permanent * magnets on the device. - * Soft iron - These distortions arise due to the interaction with the earth's magentic + * Soft iron - These distortions arise due to the interaction with the earth's magnetic * field. *

*

{@link android.hardware.Sensor#TYPE_GAME_ROTATION_VECTOR}:

@@ -508,14 +508,14 @@ public class SensorEvent { * * *
    - *
  • values[0]: x*sin(θ/2)
  • - *
  • values[1]: y*sin(θ/2)
  • - *
  • values[2]: z*sin(θ/2)
  • - *
  • values[3]: cos(θ/2)
  • + *
  • values[0]: x*sin(θ/2)
  • + *
  • values[1]: y*sin(θ/2)
  • + *
  • values[2]: z*sin(θ/2)
  • + *
  • values[3]: cos(θ/2)
  • * * *
  • values[4]: Translation along x axis from an arbitrary origin.
  • - * li> values[5]: Translation along y axis from an arbitrary origin. + *
  • values[5]: Translation along y axis from an arbitrary origin.
  • *
  • values[6]: Translation along z axis from an arbitrary origin.
  • * *
  • values[7]: Delta quaternion rotation x*sin(θ/2)
  • diff --git a/core/java/android/hardware/SensorManager.java b/core/java/android/hardware/SensorManager.java index 5684aa5cb6fcf..a20307a868c7f 100644 --- a/core/java/android/hardware/SensorManager.java +++ b/core/java/android/hardware/SensorManager.java @@ -884,7 +884,7 @@ public abstract class SensorManager { * Used for receiving notifications from the SensorManager when dynamic sensors are connected or * disconnected. */ - public static abstract class DynamicSensorConnectionCallback { + public static abstract class DynamicSensorCallback { /** * Called when there is a dynamic sensor being connected to the system. * @@ -902,62 +902,73 @@ public abstract class SensorManager { /** - * Add a {@link android.hardware.SensorManager.DynamicSensorConnectionCallback - * DynamicSensorConnectionCallback} to receive dynamic sensor connection callbacks. Repeat + * Add a {@link android.hardware.SensorManager.DynamicSensorCallback + * DynamicSensorCallback} to receive dynamic sensor connection callbacks. Repeat * registration with the already registered callback object will have no additional effect. * * @param callback An object that implements the - * {@link android.hardware.SensorManager.DynamicSensorConnectionCallback - * DynamicSensorConnectionCallback} + * {@link android.hardware.SensorManager.DynamicSensorCallback + * DynamicSensorCallback} * interface for receiving callbacks. - * @see #addDynamicSensorCallback(DynamicSensorConnectionCallback, Handler) + * @see #addDynamicSensorCallback(DynamicSensorCallback, Handler) * * @throws IllegalArgumentException when callback is null. */ - public void registerDynamicSensorCallback(DynamicSensorConnectionCallback callback) { + public void registerDynamicSensorCallback(DynamicSensorCallback callback) { registerDynamicSensorCallback(callback, null); } /** - * Add a {@link android.hardware.SensorManager.DynamicSensorConnectionCallback - * DynamicSensorConnectionCallback} to receive dynamic sensor connection callbacks. Repeat + * Add a {@link android.hardware.SensorManager.DynamicSensorCallback + * DynamicSensorCallback} to receive dynamic sensor connection callbacks. Repeat * registration with the already registered callback object will have no additional effect. * * @param callback An object that implements the - * {@link android.hardware.SensorManager.DynamicSensorConnectionCallback - * DynamicSensorConnectionCallback} interface for receiving callbacks. + * {@link android.hardware.SensorManager.DynamicSensorCallback + * DynamicSensorCallback} interface for receiving callbacks. * @param handler The {@link android.os.Handler Handler} the {@link - * android.hardware.SensorManager.DynamicSensorConnectionCallback + * android.hardware.SensorManager.DynamicSensorCallback * sensor connection events} will be delivered to. * * @throws IllegalArgumentException when callback is null. */ public void registerDynamicSensorCallback( - DynamicSensorConnectionCallback callback, Handler handler) { + DynamicSensorCallback callback, Handler handler) { registerDynamicSensorCallbackImpl(callback, handler); } /** - * Remove a {@link android.hardware.SensorManager.DynamicSensorConnectionCallback - * DynamicSensorConnectionCallback} to stop sending dynamic sensor connection events to that + * Remove a {@link android.hardware.SensorManager.DynamicSensorCallback + * DynamicSensorCallback} to stop sending dynamic sensor connection events to that * callback. * * @param callback An object that implements the - * {@link android.hardware.SensorManager.DynamicSensorConnectionCallback - * DynamicSensorConnectionCallback} + * {@link android.hardware.SensorManager.DynamicSensorCallback + * DynamicSensorCallback} * interface for receiving callbacks. */ - public void unregisterDynamicSensorCallback(DynamicSensorConnectionCallback callback) { + public void unregisterDynamicSensorCallback(DynamicSensorCallback callback) { unregisterDynamicSensorCallbackImpl(callback); } + /** + * Tell if dynamic sensor discovery feature is supported by system. + * + * @return true if dynamic sensor discovery is supported, false + * otherwise. + */ + public boolean isDynamicSensorDiscoverySupported() { + List sensors = getSensorList(Sensor.TYPE_DYNAMIC_SENSOR_META); + return sensors.size() > 0; + } + /** @hide */ protected abstract void registerDynamicSensorCallbackImpl( - DynamicSensorConnectionCallback callback, Handler handler); + DynamicSensorCallback callback, Handler handler); /** @hide */ protected abstract void unregisterDynamicSensorCallbackImpl( - DynamicSensorConnectionCallback callback); + DynamicSensorCallback callback); /** *

    diff --git a/core/java/android/hardware/SystemSensorManager.java b/core/java/android/hardware/SystemSensorManager.java index 7d903bd89fe41..259ca032918b7 100644 --- a/core/java/android/hardware/SystemSensorManager.java +++ b/core/java/android/hardware/SystemSensorManager.java @@ -1,4 +1,4 @@ - /* +/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -76,7 +76,7 @@ public class SystemSensorManager extends SensorManager { new HashMap(); // Dynamic Sensor callbacks - private HashMap + private HashMap mDynamicSensorCallbacks = new HashMap<>(); private BroadcastReceiver mDynamicSensorBroadcastReceiver; @@ -120,7 +120,7 @@ public class SystemSensorManager extends SensorManager { @Override protected List getFullDynamicSensorList() { // only set up broadcast receiver if the application tries to find dynamic sensors or - // explicitly register a DynamicSensorConnectionCallback + // explicitly register a DynamicSensorCallback setupDynamicSensorBroadcastReceiver(); updateDynamicSensorList(); return mFullDynamicSensorsList; @@ -354,9 +354,9 @@ public class SystemSensorManager extends SensorManager { Handler mainHandler = new Handler(mContext.getMainLooper()); - for (Map.Entry entry : + for (Map.Entry entry : mDynamicSensorCallbacks.entrySet()) { - final DynamicSensorConnectionCallback callback = entry.getKey(); + final DynamicSensorCallback callback = entry.getKey(); Handler handler = entry.getValue() == null ? mainHandler : entry.getValue(); @@ -413,7 +413,7 @@ public class SystemSensorManager extends SensorManager { /** @hide */ protected void registerDynamicSensorCallbackImpl( - DynamicSensorConnectionCallback callback, Handler handler) { + DynamicSensorCallback callback, Handler handler) { if (DEBUG_DYNAMIC_SENSOR) { Log.i(TAG, "DYNS Register dynamic sensor callback"); } @@ -432,7 +432,7 @@ public class SystemSensorManager extends SensorManager { /** @hide */ protected void unregisterDynamicSensorCallbackImpl( - DynamicSensorConnectionCallback callback) { + DynamicSensorCallback callback) { if (DEBUG_DYNAMIC_SENSOR) { Log.i(TAG, "Removing dynamic sensor listerner"); }