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

Commit 49511145 authored by Joy Babafemi's avatar Joy Babafemi
Browse files

UWB: Add setUwbEnabled System API.

Test: Test updated for AdapterStateListener.java
Bug: 183254940

Change-Id: I269d24c995d8064f8e62c80a1566fa0646e96580
parent 1027fb5c
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -14311,12 +14311,15 @@ package android.uwb {
  }
  public static interface UwbManager.AdapterStateCallback {
    method public void onStateChanged(boolean, int);
    method public void onStateChanged(int, int);
    field public static final int STATE_CHANGED_REASON_ALL_SESSIONS_CLOSED = 1; // 0x1
    field public static final int STATE_CHANGED_REASON_ERROR_UNKNOWN = 4; // 0x4
    field public static final int STATE_CHANGED_REASON_SESSION_STARTED = 0; // 0x0
    field public static final int STATE_CHANGED_REASON_SYSTEM_BOOT = 3; // 0x3
    field public static final int STATE_CHANGED_REASON_SYSTEM_POLICY = 2; // 0x2
    field public static final int STATE_DISABLED = 0; // 0x0
    field public static final int STATE_ENABLED_ACTIVE = 2; // 0x2
    field public static final int STATE_ENABLED_INACTIVE = 1; // 0x1
  }
}
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.uwb;

/**
 * @hide
 */
@Backing(type="int")
enum AdapterState {
 /**
   * The state when UWB is disabled.
   */
  STATE_DISABLED,

  /**
   * The state when UWB is enabled but has no active sessions.
   */
  STATE_ENABLED_INACTIVE,

  /**
   * The state when UWB is enabled and has active sessions.
   */
  STATE_ENABLED_ACTIVE,
}
 No newline at end of file
+58 −5
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.os.Binder;
import android.os.RemoteException;
import android.util.Log;
import android.uwb.UwbManager.AdapterStateCallback;
import android.uwb.UwbManager.AdapterStateCallback.State;
import android.uwb.UwbManager.AdapterStateCallback.StateChangedReason;

import java.util.HashMap;
@@ -40,7 +41,8 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {

    @StateChangedReason
    private int mAdapterStateChangeReason = AdapterStateCallback.STATE_CHANGED_REASON_ERROR_UNKNOWN;
    private boolean mAdapterEnabledState = false;
    @State
    private int mAdapterState = AdapterStateCallback.STATE_DISABLED;

    public AdapterStateListener(@NonNull IUwbAdapter adapter) {
        mAdapter = adapter;
@@ -66,7 +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(false,
                    executor.execute(() -> callback.onStateChanged(mAdapterState,
                            AdapterStateCallback.STATE_CHANGED_REASON_ERROR_UNKNOWN));
                }
            } else {
@@ -99,6 +101,42 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
        }
    }

    /**
     * Sets the adapter enabled state
     *
     * @param isEnabled value of new adapter state
     */
    public void setEnabled(boolean isEnabled) {
        synchronized (this) {
            if (!mIsRegistered) {
                return;
            } else {
                try {
                    mAdapter.setEnabled(isEnabled);
                } catch (RemoteException e) {
                    Log.w(TAG, "Failed to set adapter state");
                    sendErrorState();
                }
            }
        }
    }

    private void sendErrorState() {
        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);
                }
            }
        }
    }

    private void sendCurrentState(@NonNull AdapterStateCallback callback) {
        synchronized (this) {
            Executor executor = mCallbackMap.get(callback);
@@ -106,7 +144,7 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
            final long identity = Binder.clearCallingIdentity();
            try {
                executor.execute(() -> callback.onStateChanged(
                        mAdapterEnabledState, mAdapterStateChangeReason));
                        mAdapterState, mAdapterStateChangeReason));
            } finally {
                Binder.restoreCallingIdentity(identity);
            }
@@ -114,12 +152,13 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
    }

    @Override
    public void onAdapterStateChanged(boolean isEnabled, int reason) {
    public void onAdapterStateChanged(int state, int reason) {
        synchronized (this) {
            @StateChangedReason int localReason =
                    convertToStateChangedReason(reason);
            mAdapterEnabledState = isEnabled;
            @State int localState = convertToState(state);
            mAdapterStateChangeReason = localReason;
            mAdapterState = localState;
            for (AdapterStateCallback cb : mCallbackMap.keySet()) {
                sendCurrentState(cb);
            }
@@ -146,4 +185,18 @@ public class AdapterStateListener extends IUwbAdapterStateCallbacks.Stub {
                return AdapterStateCallback.STATE_CHANGED_REASON_ERROR_UNKNOWN;
        }
    }

    private static @State int convertToState(@AdapterState int state) {
        switch (state) {
            case AdapterState.STATE_ENABLED_INACTIVE:
                return AdapterStateCallback.STATE_ENABLED_INACTIVE;

            case AdapterState.STATE_ENABLED_ACTIVE:
                return AdapterStateCallback.STATE_ENABLED_ACTIVE;

            case AdapterState.STATE_DISABLED:
            default:
                return AdapterStateCallback.STATE_DISABLED;
        }
    }
}
+13 −0
Original line number Diff line number Diff line
@@ -148,6 +148,19 @@ interface IUwbAdapter {
   */
  void closeRanging(in SessionHandle sessionHandle);

   /**
     * Disables or enables UWB for a user
     *
     * The provided callback's IUwbAdapterStateCallbacks#onAdapterStateChanged
     * function must be called immediately following state change.
     *
     * @param enabled value representing intent to disable or enable UWB. If
     * true, any subsequent calls to #openRanging will be allowed. If false,
     * all active ranging sessions will be closed and subsequent calls to
     * #openRanging will be disallowed.
     */
    void setEnabled(boolean enabled);

  /**
   * The maximum allowed time to open a ranging session.
   */
+7 −6
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.uwb;

import android.uwb.StateChangeReason;
import android.uwb.AdapterState;

/**
 * @hide
@@ -25,8 +26,8 @@ interface IUwbAdapterStateCallbacks {
  /**
     * Called whenever the adapter state changes
     *
   * @param isEnabled true if the adapter is enabled, false otherwise
     * @param state UWB state; enabled_active, enabled_inactive, or disabled.
     * @param reason the reason that the state has changed
     */
  void onAdapterStateChanged(boolean isEnabled, StateChangeReason reason);
    void onAdapterStateChanged(AdapterState state, StateChangeReason reason);
}
 No newline at end of file
Loading