Merge \"Sensor: Make getId() more varied\" into nyc-dev

am: 94b61cb340

Change-Id: I8803994892af96c0defc5eda5e9a806ce7ec1bc9
This commit is contained in:
Peng Xu
2016-06-23 03:32:46 +00:00
committed by android-build-merger
2 changed files with 24 additions and 54 deletions

View File

@@ -20,8 +20,6 @@ package android.hardware;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.os.Build; import android.os.Build;
import java.util.UUID;
/** /**
* Class representing a sensor. Use {@link SensorManager#getSensorList} to get * Class representing a sensor. Use {@link SensorManager#getSensorList} to get
* the list of available Sensors. * the list of available Sensors.
@@ -806,7 +804,7 @@ public final class Sensor {
private String mRequiredPermission; private String mRequiredPermission;
private int mMaxDelay; private int mMaxDelay;
private int mFlags; private int mFlags;
private UUID mUuid; private int mId;
Sensor() { Sensor() {
} }
@@ -895,34 +893,28 @@ public final class Sensor {
} }
/** /**
* @return The UUID of the sensor. If the sensor does not support UUID, the returned value will * Do not use.
* 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. * This method throws an UnsupportedOperationException.
*
* Use getId() if you want a unique ID.
*
* @see getId
* *
* @hide * @hide
*/ */
@SystemApi @SystemApi
public UUID getUuid() { public java.util.UUID getUuid() {
return mUuid; throw new UnsupportedOperationException();
} }
/** /**
* @return The unique id of sensor. Return value of 0 means this sensor does not support UUID; * @return The sensor id that will be unique for the same app unless the device is factory
* return value of -1 means this sensor can be uniquely identified in system by combination of * reset. Return value of 0 means this sensor does not support this function; return value of -1
* its type and name. * means this sensor can be uniquely identified in system by combination of its type and name.
*/ */
public int getId() { public int getId() {
if (mUuid.equals(ALL_0_UUID)) { return mId;
return 0;
} else if (mUuid.equals(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;
}
} }
/** /**
@@ -1038,10 +1030,6 @@ public final class Sensor {
+ ", power=" + mPower + ", minDelay=" + mMinDelay + "}"; + ", 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. * Sets the Type associated with the sensor.
* NOTE: to be used only by native bindings in SensorManager. * NOTE: to be used only by native bindings in SensorManager.
@@ -1141,24 +1129,17 @@ public final class Sensor {
} }
/** /**
* Sets the UUID associated with the sensor. * Sets the ID associated with the sensor.
*
* The method name is misleading; while this ID is based on the UUID,
* we do not pass in the actual UUID.
* *
* NOTE: to be used only by native bindings in SensorManager. * NOTE: to be used only by native bindings in SensorManager.
* *
* @see #getUuid * @see #getId
* @see UUID
*/ */
private void setUuid(long msb, long lsb) { private void setUuid(long msb, long lsb) {
// reuse static object if possible // TODO(b/29547335): Rename this method to setId.
if (msb == lsb) { mId = (int)msb;
if (msb == 0L) {
mUuid = ALL_0_UUID;
return;
} else if (msb == ~0L) {
mUuid = ALL_F_UUID;
return;
}
}
mUuid = new UUID(msb, lsb);
} }
} }

View File

@@ -30,7 +30,6 @@
#include <utils/Looper.h> #include <utils/Looper.h>
#include <utils/Vector.h> #include <utils/Vector.h>
#include <endian.h> // htobe64
#include <map> #include <map>
namespace { namespace {
@@ -200,19 +199,9 @@ translateNativeSensorToJavaSensor(JNIEnv *env, jobject sensor, const Sensor& nat
env->SetObjectField(sensor, sensorOffsets.stringType, stringType); env->SetObjectField(sensor, sensorOffsets.stringType, stringType);
} }
// java.util.UUID constructor UUID(long a, long b) assumes the two long inputs a and b // TODO(b/29547335): Rename "setUuid" method to "setId".
// correspond to first half and second half of the 16-byte stream in big-endian, int64_t id = nativeSensor.getId();
// respectively, if the byte stream is serialized according to RFC 4122 (Sec. 4.1.2). env->CallVoidMethod(sensor, sensorOffsets.setUuid, id, 0);
//
// For Java UUID 12345678-90AB-CDEF-1122-334455667788, the byte stream will be
// (uint8_t) {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, ....} according RFC 4122.
//
// According to Java UUID constructor document, the long parameter a should be always
// 0x12345678980ABCDEF regardless of host machine endianess. Thus, htobe64 is used to
// convert int64_t read directly, which will be in host-endian, to the big-endian required
// by Java constructor.
const int64_t (&uuid)[2] = nativeSensor.getUuid().i64;
env->CallVoidMethod(sensor, sensorOffsets.setUuid, htobe64(uuid[0]), htobe64(uuid[1]));
} }
return sensor; return sensor;
} }