Merge changes from topic "uwb_api_change"

* changes:
  Pass SessionHandle to UWB Service when opening a session
  Return CancellationSignal when opening UWB session
  Address UWB API feedback
This commit is contained in:
Treehugger Robot
2021-03-12 21:50:17 +00:00
committed by Gerrit Code Review
8 changed files with 114 additions and 218 deletions

View File

@@ -38,9 +38,30 @@ public final class AngleMeasurement implements Parcelable {
private final double mErrorRadians;
private final double mConfidenceLevel;
private AngleMeasurement(double radians, double errorRadians, double confidenceLevel) {
/**
* Constructs a new {@link AngleMeasurement} object
*
* @param radians the angle in radians
* @param errorRadians the error of the angle measurement in radians
* @param confidenceLevel confidence level of the angle measurement
*
* @throws IllegalArgumentException if the radians, errorRadians, or confidenceLevel is out of
* allowed range
*/
public AngleMeasurement(double radians, double errorRadians, double confidenceLevel) {
if (radians < -Math.PI || radians > Math.PI) {
throw new IllegalArgumentException("Invalid radians: " + radians);
}
mRadians = radians;
if (errorRadians < 0.0 || errorRadians > Math.PI) {
throw new IllegalArgumentException("Invalid error radians: " + errorRadians);
}
mErrorRadians = errorRadians;
if (confidenceLevel < 0.0 || confidenceLevel > 1.0) {
throw new IllegalArgumentException("Invalid confidence level: " + confidenceLevel);
}
mConfidenceLevel = confidenceLevel;
}
@@ -122,11 +143,7 @@ public final class AngleMeasurement implements Parcelable {
new Creator<AngleMeasurement>() {
@Override
public AngleMeasurement createFromParcel(Parcel in) {
Builder builder = new Builder();
builder.setRadians(in.readDouble());
builder.setErrorRadians(in.readDouble());
builder.setConfidenceLevel(in.readDouble());
return builder.build();
return new AngleMeasurement(in.readDouble(), in.readDouble(), in.readDouble());
}
@Override
@@ -134,82 +151,4 @@ public final class AngleMeasurement implements Parcelable {
return new AngleMeasurement[size];
}
};
/**
* Builder class for {@link AngleMeasurement}.
*/
public static final class Builder {
private double mRadians = Double.NaN;
private double mErrorRadians = Double.NaN;
private double mConfidenceLevel = Double.NaN;
/**
* Set the angle in radians
*
* @param radians angle in radians
* @throws IllegalArgumentException if angle exceeds allowed limits of [-Math.PI, +Math.PI]
*/
@NonNull
public Builder setRadians(double radians) {
if (radians < -Math.PI || radians > Math.PI) {
throw new IllegalArgumentException("Invalid radians: " + radians);
}
mRadians = radians;
return this;
}
/**
* Set the angle error in radians
*
* @param errorRadians error of the angle in radians
* @throws IllegalArgumentException if the error exceeds the allowed limits of [0, +Math.PI]
*/
@NonNull
public Builder setErrorRadians(double errorRadians) {
if (errorRadians < 0.0 || errorRadians > Math.PI) {
throw new IllegalArgumentException(
"Invalid error radians: " + errorRadians);
}
mErrorRadians = errorRadians;
return this;
}
/**
* Set the angle confidence level
*
* @param confidenceLevel level of confidence of the angle measurement
* @throws IllegalArgumentException if the error exceeds the allowed limits of [0.0, 1.0]
*/
@NonNull
public Builder setConfidenceLevel(double confidenceLevel) {
if (confidenceLevel < 0.0 || confidenceLevel > 1.0) {
throw new IllegalArgumentException(
"Invalid confidence level: " + confidenceLevel);
}
mConfidenceLevel = confidenceLevel;
return this;
}
/**
* Build the {@link AngleMeasurement} object
*
* @throws IllegalStateException if angle, error, or confidence values are missing
*/
@NonNull
public AngleMeasurement build() {
if (Double.isNaN(mRadians)) {
throw new IllegalStateException("Angle is not set");
}
if (Double.isNaN(mErrorRadians)) {
throw new IllegalStateException("Angle error is not set");
}
if (Double.isNaN(mConfidenceLevel)) {
throw new IllegalStateException("Angle confidence level is not set");
}
return new AngleMeasurement(mRadians, mErrorRadians, mConfidenceLevel);
}
}
}

View File

@@ -116,9 +116,8 @@ public final class AngleOfArrivalMeasurement implements Parcelable {
new Creator<AngleOfArrivalMeasurement>() {
@Override
public AngleOfArrivalMeasurement createFromParcel(Parcel in) {
Builder builder = new Builder();
builder.setAzimuth(in.readParcelable(AngleMeasurement.class.getClassLoader()));
Builder builder =
new Builder(in.readParcelable(AngleMeasurement.class.getClassLoader()));
builder.setAltitude(in.readParcelable(AngleMeasurement.class.getClassLoader()));
@@ -135,18 +134,16 @@ public final class AngleOfArrivalMeasurement implements Parcelable {
* Builder class for {@link AngleOfArrivalMeasurement}.
*/
public static final class Builder {
private AngleMeasurement mAzimuthAngleMeasurement = null;
private final AngleMeasurement mAzimuthAngleMeasurement;
private AngleMeasurement mAltitudeAngleMeasurement = null;
/**
* Set the azimuth angle
* Constructs an {@link AngleOfArrivalMeasurement} object
*
* @param azimuthAngle azimuth angle
* @param azimuthAngle the azimuth angle of the measurement
*/
@NonNull
public Builder setAzimuth(@NonNull AngleMeasurement azimuthAngle) {
public Builder(@NonNull AngleMeasurement azimuthAngle) {
mAzimuthAngleMeasurement = azimuthAngle;
return this;
}
/**
@@ -162,15 +159,9 @@ public final class AngleOfArrivalMeasurement implements Parcelable {
/**
* Build the {@link AngleOfArrivalMeasurement} object
*
* @throws IllegalStateException if the required azimuth angle is not provided
*/
@NonNull
public AngleOfArrivalMeasurement build() {
if (mAzimuthAngleMeasurement == null) {
throw new IllegalStateException("Azimuth angle measurement is not set");
}
return new AngleOfArrivalMeasurement(mAzimuthAngleMeasurement,
mAltitudeAngleMeasurement);
}

View File

@@ -62,9 +62,6 @@ interface IUwbAdapter {
/**
* Request to open a new ranging session
*
* This function must return before calling any functions in
* IUwbAdapterCallbacks.
*
* This function does not start the ranging session, but all necessary
* components must be initialized and ready to start a new ranging
* session prior to calling IUwbAdapterCallback#onRangingOpened.
@@ -77,12 +74,16 @@ interface IUwbAdapter {
* RANGING_SESSION_OPEN_THRESHOLD_MS milliseconds of #openRanging being called
* if the ranging session fails to be opened.
*
* If the provided sessionHandle is already open for the calling client, then
* #onRangingOpenFailed must be called and the new session must not be opened.
*
* @param sessionHandle the session handle to open ranging for
* @param rangingCallbacks the callbacks used to deliver ranging information
* @param parameters the configuration to use for ranging
* @return a SessionHandle used to identify this ranging request
*/
SessionHandle openRanging(in IUwbRangingCallbacks rangingCallbacks,
in PersistableBundle parameters);
void openRanging(in SessionHandle sessionHandle,
in IUwbRangingCallbacks rangingCallbacks,
in PersistableBundle parameters);
/**
* Request to start ranging

View File

@@ -17,6 +17,7 @@
package android.uwb;
import android.annotation.NonNull;
import android.os.CancellationSignal;
import android.os.PersistableBundle;
import android.os.RemoteException;
import android.util.Log;
@@ -32,6 +33,7 @@ public class RangingManager extends android.uwb.IUwbRangingCallbacks.Stub {
private final IUwbAdapter mAdapter;
private final Hashtable<SessionHandle, RangingSession> mRangingSessionTable = new Hashtable<>();
private int mNextSessionId = 1;
public RangingManager(IUwbAdapter adapter) {
mAdapter = adapter;
@@ -44,29 +46,26 @@ public class RangingManager extends android.uwb.IUwbRangingCallbacks.Stub {
* @param executor {@link Executor} to run callbacks
* @param callbacks {@link RangingSession.Callback} to associate with the {@link RangingSession}
* that is being opened.
* @return a new {@link RangingSession}
* @return a {@link CancellationSignal} that may be used to cancel the opening of the
* {@link RangingSession}.
*/
public RangingSession openSession(@NonNull PersistableBundle params, @NonNull Executor executor,
public CancellationSignal openSession(@NonNull PersistableBundle params,
@NonNull Executor executor,
@NonNull RangingSession.Callback callbacks) {
SessionHandle sessionHandle;
try {
sessionHandle = mAdapter.openRanging(this, params);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
synchronized (this) {
if (hasSession(sessionHandle)) {
Log.w(TAG, "Newly created session unexpectedly reuses an active SessionHandle");
executor.execute(() -> callbacks.onClosed(
RangingSession.Callback.REASON_GENERIC_ERROR,
new PersistableBundle()));
}
SessionHandle sessionHandle = new SessionHandle(mNextSessionId++);
RangingSession session =
new RangingSession(executor, callbacks, mAdapter, sessionHandle);
mRangingSessionTable.put(sessionHandle, session);
return session;
try {
mAdapter.openRanging(sessionHandle, this, params);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
CancellationSignal cancellationSignal = new CancellationSignal();
cancellationSignal.setOnCancelListener(() -> session.close());
return cancellationSignal;
}
}

View File

@@ -25,6 +25,7 @@ import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.content.Context;
import android.os.CancellationSignal;
import android.os.IBinder;
import android.os.PersistableBundle;
import android.os.RemoteException;
@@ -228,14 +229,14 @@ public final class UwbManager {
* @param callbacks {@link RangingSession.Callback} to associate with the
* {@link RangingSession} that is being opened.
*
* @return an {@link AutoCloseable} that is able to be used to close or cancel the opening of a
* @return an {@link CancellationSignal} that is able to be used to cancel the opening of a
* {@link RangingSession} that has been requested through {@link #openRangingSession}
* but has not yet been made available by
* {@link RangingSession.Callback#onOpened(RangingSession)}.
*/
@NonNull
@RequiresPermission(Manifest.permission.UWB_PRIVILEGED)
public AutoCloseable openRangingSession(@NonNull PersistableBundle parameters,
public CancellationSignal openRangingSession(@NonNull PersistableBundle parameters,
@NonNull @CallbackExecutor Executor executor,
@NonNull RangingSession.Callback callbacks) {
return mRangingManager.openSession(parameters, executor, callbacks);