Merge "UWB: Add getAdapterState System API" am: f9169bbb09

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1685209

Change-Id: I7b9024ece370bcab9b48de188d538168867653a2
This commit is contained in:
Joy Babafemi
2021-04-26 22:27:29 +00:00
committed by Automerger Merge Worker
4 changed files with 42 additions and 47 deletions

View File

@@ -68,8 +68,7 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
mIsRegistered = true; mIsRegistered = true;
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, "Failed to register adapter state callback"); Log.w(TAG, "Failed to register adapter state callback");
executor.execute(() -> callback.onStateChanged(mAdapterState, throw e.rethrowFromSystemServer();
AdapterStateCallback.STATE_CHANGED_REASON_ERROR_UNKNOWN));
} }
} else { } else {
sendCurrentState(callback); sendCurrentState(callback);
@@ -95,6 +94,7 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
mAdapter.unregisterAdapterStateCallbacks(this); mAdapter.unregisterAdapterStateCallbacks(this);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, "Failed to unregister AdapterStateCallback with service"); Log.w(TAG, "Failed to unregister AdapterStateCallback with service");
throw e.rethrowFromSystemServer();
} }
mIsRegistered = false; mIsRegistered = false;
} }
@@ -115,24 +115,24 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
mAdapter.setEnabled(isEnabled); mAdapter.setEnabled(isEnabled);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.w(TAG, "Failed to set adapter state"); 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) { synchronized (this) {
for (AdapterStateCallback callback: mCallbackMap.keySet()) {
Executor executor = mCallbackMap.get(callback);
final long identity = Binder.clearCallingIdentity();
try { try {
executor.execute(() -> callback.onStateChanged( return mAdapter.getAdapterState();
mAdapterState, mAdapterStateChangeReason)); } catch (RemoteException e) {
} finally { Log.w(TAG, "Failed to get adapter state");
Binder.restoreCallingIdentity(identity); throw e.rethrowFromSystemServer();
}
} }
} }
} }

View File

@@ -158,6 +158,18 @@ interface IUwbAdapter {
*/ */
void setEnabled(boolean enabled); 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. * 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>The provided callback will be invoked by the given {@link Executor}.
* *
* <p>When first registering a callback, the callbacks's * <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 * the current state of the underlying UWB adapter with the most recent
* {@link AdapterStateCallback.StateChangedReason} that caused the change. * {@link AdapterStateCallback.StateChangedReason} that caused the change.
* *
@@ -271,6 +271,21 @@ public final class UwbManager {
return mRangingManager.openSession(parameters, executor, callbacks); 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 * 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.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -113,30 +112,6 @@ public class AdapterStateListenerTest {
verifyCallbackStateChangedInvoked(callback2, 1); 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 @Test
public void testRegister_RegisterSameCallbackTwice() throws RemoteException { public void testRegister_RegisterSameCallbackTwice() throws RemoteException {
AdapterStateListener adapterStateListener = new AdapterStateListener(mUwbAdapter); AdapterStateListener adapterStateListener = new AdapterStateListener(mUwbAdapter);
@@ -162,13 +137,6 @@ public class AdapterStateListenerTest {
runViaExecutor(); 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() { private void runViaExecutor() {
AdapterStateListener adapterStateListener = new AdapterStateListener(mUwbAdapter); AdapterStateListener adapterStateListener = new AdapterStateListener(mUwbAdapter);
AdapterStateCallback callback = mock(AdapterStateCallback.class); AdapterStateCallback callback = mock(AdapterStateCallback.class);