UWB: Add getAdapterState System API

Test: Updated tests for AdapterStateListener.java
Bug: 183254940

Merged-In: I9a7472be2c7cf835cda8910261ba641bf2345b27
Change-Id: I9a7472be2c7cf835cda8910261ba641bf2345b27
This commit is contained in:
Joy Babafemi
2021-04-22 16:51:23 +00:00
parent 9ed1dac67c
commit adf4270f69
4 changed files with 42 additions and 47 deletions

View File

@@ -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();
}
}
}

View File

@@ -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.
*/

View File

@@ -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.
*
@@ -271,6 +271,21 @@ public final class UwbManager {
return mRangingManager.openSession(parameters, executor, callbacks);
}
/**
* 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
*

View File

@@ -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;
@@ -113,30 +112,6 @@ public class AdapterStateListenerTest {
verifyCallbackStateChangedInvoked(callback2, 1);
}
@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);
@@ -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);