summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/java/android/uwb/AdapterStateListener.java28
-rw-r--r--core/java/android/uwb/IUwbAdapter.aidl12
-rw-r--r--core/java/android/uwb/UwbManager.java17
-rw-r--r--core/tests/uwbtests/src/android/uwb/AdapterStateListenerTest.java32
4 files changed, 42 insertions, 47 deletions
diff --git a/core/java/android/uwb/AdapterStateListener.java b/core/java/android/uwb/AdapterStateListener.java
index b9900951591f..91847f740953 100644
--- a/core/java/android/uwb/AdapterStateListener.java
+++ b/core/java/android/uwb/AdapterStateListener.java
@@ -68,8 +68,7 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
mIsRegistered = true;
} catch (RemoteException e) {
Log.w(TAG, "Failed to register adapter state callback");
- executor.execute(() -> callback.onStateChanged(mAdapterState,
- AdapterStateCallback.STATE_CHANGED_REASON_ERROR_UNKNOWN));
+ throw e.rethrowFromSystemServer();
}
} else {
sendCurrentState(callback);
@@ -95,6 +94,7 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
mAdapter.unregisterAdapterStateCallbacks(this);
} catch (RemoteException e) {
Log.w(TAG, "Failed to unregister AdapterStateCallback with service");
+ throw e.rethrowFromSystemServer();
}
mIsRegistered = false;
}
@@ -115,24 +115,24 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
mAdapter.setEnabled(isEnabled);
} catch (RemoteException e) {
Log.w(TAG, "Failed to set adapter state");
- sendErrorState();
+ throw e.rethrowFromSystemServer();
}
}
}
}
- private void sendErrorState() {
+ /**
+ * Gets the adapter enabled state
+ *
+ * @return integer representing adapter enabled state
+ */
+ public int getAdapterState() {
synchronized (this) {
- for (AdapterStateCallback callback: mCallbackMap.keySet()) {
- Executor executor = mCallbackMap.get(callback);
-
- final long identity = Binder.clearCallingIdentity();
- try {
- executor.execute(() -> callback.onStateChanged(
- mAdapterState, mAdapterStateChangeReason));
- } finally {
- Binder.restoreCallingIdentity(identity);
- }
+ try {
+ return mAdapter.getAdapterState();
+ } catch (RemoteException e) {
+ Log.w(TAG, "Failed to get adapter state");
+ throw e.rethrowFromSystemServer();
}
}
}
diff --git a/core/java/android/uwb/IUwbAdapter.aidl b/core/java/android/uwb/IUwbAdapter.aidl
index 5804d04bdba7..e02e5eb25990 100644
--- a/core/java/android/uwb/IUwbAdapter.aidl
+++ b/core/java/android/uwb/IUwbAdapter.aidl
@@ -158,6 +158,18 @@ interface IUwbAdapter {
*/
void setEnabled(boolean enabled);
+ /**
+ * Returns the current enabled/disabled UWB state.
+ *
+ * Possible values are:
+ * IUwbAdapterState#STATE_DISABLED
+ * IUwbAdapterState#STATE_ENABLED_ACTIVE
+ * IUwbAdapterState#STATE_ENABLED_INACTIVE
+ *
+ * @return value representing enabled/disabled UWB state.
+ */
+ int getAdapterState();
+
/**
* The maximum allowed time to open a ranging session.
*/
diff --git a/core/java/android/uwb/UwbManager.java b/core/java/android/uwb/UwbManager.java
index 9116c49d0764..4edecdaad0dd 100644
--- a/core/java/android/uwb/UwbManager.java
+++ b/core/java/android/uwb/UwbManager.java
@@ -175,7 +175,7 @@ public final class UwbManager {
* <p>The provided callback will be invoked by the given {@link Executor}.
*
* <p>When first registering a callback, the callbacks's
- * {@link AdapterStateCallback#onStateChanged(boolean, int)} is immediately invoked to indicate
+ * {@link AdapterStateCallback#onStateChanged(int, int)} is immediately invoked to indicate
* the current state of the underlying UWB adapter with the most recent
* {@link AdapterStateCallback.StateChangedReason} that caused the change.
*
@@ -272,6 +272,21 @@ public final class UwbManager {
}
/**
+ * Returns the current enabled/disabled state for UWB.
+ *
+ * Possible values are:
+ * AdapterStateCallback#STATE_DISABLED
+ * AdapterStateCallback#STATE_ENABLED_INACTIVE
+ * AdapterStateCallback#STATE_ENABLED_ACTIVE
+ *
+ * @return value representing current enabled/disabled state for UWB.
+ * @hide
+ */
+ public @AdapterStateCallback.State int getAdapterState() {
+ return mAdapterStateListener.getAdapterState();
+ }
+
+ /**
* Disables or enables UWB for a user
*
* @param enabled value representing intent to disable or enable UWB. If true any subsequent
diff --git a/core/tests/uwbtests/src/android/uwb/AdapterStateListenerTest.java b/core/tests/uwbtests/src/android/uwb/AdapterStateListenerTest.java
index bdaf63021503..4cad535a3a51 100644
--- a/core/tests/uwbtests/src/android/uwb/AdapterStateListenerTest.java
+++ b/core/tests/uwbtests/src/android/uwb/AdapterStateListenerTest.java
@@ -19,7 +19,6 @@ package android.uwb;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -114,30 +113,6 @@ public class AdapterStateListenerTest {
}
@Test
- public void testRegister_FirstRegisterFails() throws RemoteException {
- AdapterStateListener adapterStateListener = new AdapterStateListener(mUwbAdapter);
- AdapterStateCallback callback1 = mock(AdapterStateCallback.class);
- AdapterStateCallback callback2 = mock(AdapterStateCallback.class);
-
- // Throw a remote exception whenever first registering
- doThrow(mThrowRemoteException).when(mUwbAdapter).registerAdapterStateCallbacks(any());
-
- adapterStateListener.register(getExecutor(), callback1);
- verify(mUwbAdapter, times(1)).registerAdapterStateCallbacks(any());
-
- // No longer throw an exception, instead succeed
- doAnswer(mRegisterSuccessAnswer).when(mUwbAdapter).registerAdapterStateCallbacks(any());
-
- // Register a different callback
- adapterStateListener.register(getExecutor(), callback2);
- verify(mUwbAdapter, times(2)).registerAdapterStateCallbacks(any());
-
- // Ensure first callback was invoked again
- verifyCallbackStateChangedInvoked(callback1, 2);
- verifyCallbackStateChangedInvoked(callback2, 1);
- }
-
- @Test
public void testRegister_RegisterSameCallbackTwice() throws RemoteException {
AdapterStateListener adapterStateListener = new AdapterStateListener(mUwbAdapter);
AdapterStateCallback callback = mock(AdapterStateCallback.class);
@@ -162,13 +137,6 @@ public class AdapterStateListenerTest {
runViaExecutor();
}
- @Test
- public void testCallback_RunViaExecutor_Failure() throws RemoteException {
- // Verify that the callbacks are invoked on the executor when there is a remote exception
- doThrow(mThrowRemoteException).when(mUwbAdapter).registerAdapterStateCallbacks(any());
- runViaExecutor();
- }
-
private void runViaExecutor() {
AdapterStateListener adapterStateListener = new AdapterStateListener(mUwbAdapter);
AdapterStateCallback callback = mock(AdapterStateCallback.class);