Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 79744866 authored by Joy Babafemi's avatar Joy Babafemi Committed by Automerger Merge Worker
Browse files

Merge "UWB: Add getAdapterState System API" am: f9169bbb am: ab258666

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

Change-Id: I9ee831e08fa9391867cb2e501624cb3c328cff38
parents b3527b07 ab258666
Loading
Loading
Loading
Loading
+14 −14
Original line number Diff line number Diff line
@@ -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);
                }
                return mAdapter.getAdapterState();
            } catch (RemoteException e) {
                Log.w(TAG, "Failed to get adapter state");
                throw e.rethrowFromSystemServer();
            }
        }
    }
+12 −0
Original line number Diff line number Diff line
@@ -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.
   */
+16 −1
Original line number Diff line number Diff line
@@ -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
     *
+0 −32
Original line number Diff line number Diff line
@@ -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);