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

Commit 546c0a3b authored by William Escande's avatar William Escande Committed by Automerger Merge Worker
Browse files

Merge changes I5e85c9c5,I50c2b15c,Ie2e8c47f,I3d09891e,I51c62779, ... into main...

Merge changes I5e85c9c5,I50c2b15c,Ie2e8c47f,I3d09891e,I51c62779, ... into main am: baab8de7 am: 5342fd8f

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/3015509



Change-Id: I4470c35c34137f13e27b110b5661d8440a7780e2
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents aa3ba040 5342fd8f
Loading
Loading
Loading
Loading
+29 −36
Original line number Diff line number Diff line
@@ -250,6 +250,9 @@ public class AdapterService extends Service {

    private final DeviceConfigListener mDeviceConfigListener = new DeviceConfigListener();

    private final Looper mLooper;
    private final AdapterServiceHandler mHandler;

    private int mStackReportedState;
    private long mTxTimeTotalMs;
    private long mRxTimeTotalMs;
@@ -282,8 +285,8 @@ public class AdapterService extends Service {
    @Nullable private PhonePolicy mPhonePolicy;

    private ActiveDeviceManager mActiveDeviceManager;
    private DatabaseManager mDatabaseManager;
    private SilenceDeviceManager mSilenceDeviceManager;
    private final DatabaseManager mDatabaseManager;
    private final SilenceDeviceManager mSilenceDeviceManager;
    private CompanionManager mBtCompanionManager;
    private AppOpsManager mAppOps;

@@ -316,8 +319,6 @@ public class AdapterService extends Service {
    private volatile boolean mTestModeEnabled = false;

    private MetricsLogger mMetricsLogger;
    private Looper mLooper;
    private AdapterServiceHandler mHandler;

    /** Handlers for incoming service calls */
    private AdapterServiceBinder mBinder;
@@ -355,15 +356,21 @@ public class AdapterService extends Service {
    }

    // Keep a constructor for ActivityThread.handleCreateService
    AdapterService() {}
    AdapterService() {
        this(Looper.getMainLooper());
    }

    @VisibleForTesting
    AdapterService(Looper looper) {
        mLooper = looper;
        mLooper = requireNonNull(looper);
        mHandler = new AdapterServiceHandler(mLooper);
        mSilenceDeviceManager = new SilenceDeviceManager(this, new ServiceFactory(), mLooper);
        mDatabaseManager = new DatabaseManager(this);
    }

    @VisibleForTesting
    AdapterService(Context ctx) {
        this(Looper.getMainLooper());
        attachBaseContext(ctx);
    }

@@ -615,10 +622,7 @@ public class AdapterService extends Service {
            return;
        }
        // OnCreate must perform the minimum of infaillible and mandatory initialization
        if (mLooper == null) {
            mLooper = Looper.getMainLooper();
        }
        mHandler = new AdapterServiceHandler(mLooper);
        mRemoteDevices = new RemoteDevices(this, mLooper);
        mAdapterProperties = new AdapterProperties(this);
        mAdapterStateMachine = new AdapterState(this, mLooper);
        mBinder = new AdapterServiceBinder(this);
@@ -627,18 +631,12 @@ public class AdapterService extends Service {
        mPowerManager = getNonNullSystemService(PowerManager.class);
        mBatteryStatsManager = getNonNullSystemService(BatteryStatsManager.class);
        mCompanionDeviceManager = getNonNullSystemService(CompanionDeviceManager.class);
        setAdapterService(this);
    }

    private void init() {
        debugLog("init()");
        Config.init(this);
        if (!Flags.fastBindToApp()) {
            // Moved to OnCreate
            if (mLooper == null) {
                mLooper = Looper.getMainLooper();
            }
            mHandler = new AdapterServiceHandler(mLooper);
        }
        initMetricsLogger();
        mDeviceConfigListener.start();

@@ -649,10 +647,9 @@ public class AdapterService extends Service {
            mPowerManager = getNonNullSystemService(PowerManager.class);
            mBatteryStatsManager = getNonNullSystemService(BatteryStatsManager.class);
            mCompanionDeviceManager = getNonNullSystemService(CompanionDeviceManager.class);
            mRemoteDevices = new RemoteDevices(this, mLooper);
        }

        mRemoteDevices = new RemoteDevices(this, mLooper);
        mRemoteDevices.init();
        clearDiscoveringPackages();
        if (!Flags.fastBindToApp()) {
            mBinder = new AdapterServiceBinder(this);
@@ -700,9 +697,12 @@ public class AdapterService extends Service {
                        "BluetoothQualityReportNativeInterface cannot be null when BQR starts");
        mBluetoothQualityReportNativeInterface.init();

        if (Flags.fastBindToApp()) {
            mSdpManager = new SdpManager(this, mLooper);
        } else {
            mSdpManager = new SdpManager(this);
        }

        mDatabaseManager = new DatabaseManager(this);
        mDatabaseManager.start(MetadataDatabase.createDatabase(this));

        boolean isAutomotiveDevice =
@@ -731,14 +731,15 @@ public class AdapterService extends Service {
        }
        mActiveDeviceManager.start();

        mSilenceDeviceManager = new SilenceDeviceManager(this, new ServiceFactory(), mLooper);
        mSilenceDeviceManager.start();

        mBtCompanionManager = new CompanionManager(this, new ServiceFactory());

        mBluetoothSocketManagerBinder = new BluetoothSocketManagerBinder(this);

        if (!Flags.fastBindToApp()) {
            setAdapterService(this);
        }

        invalidateBluetoothCaches();

@@ -1355,9 +1356,7 @@ public class AdapterService extends Service {
            }
        }

        if (mDatabaseManager != null) {
        mDatabaseManager.cleanup();
        }

        if (mAdapterStateMachine != null) {
            mAdapterStateMachine.doQuit();
@@ -1368,7 +1367,7 @@ public class AdapterService extends Service {
        }

        if (mRemoteDevices != null) {
            mRemoteDevices.cleanup();
            mRemoteDevices.reset();
        }

        if (mSdpManager != null) {
@@ -1399,9 +1398,7 @@ public class AdapterService extends Service {
            mPhonePolicy.cleanup();
        }

        if (mSilenceDeviceManager != null) {
        mSilenceDeviceManager.cleanup();
        }

        if (mActiveDeviceManager != null) {
            mActiveDeviceManager.cleanup();
@@ -5684,9 +5681,7 @@ public class AdapterService extends Service {

    @VisibleForTesting
    boolean factoryReset() {
        if (mDatabaseManager != null) {
        mDatabaseManager.factoryReset();
        }

        if (mBluetoothKeystoreService != null) {
            mBluetoothKeystoreService.factoryReset();
@@ -5932,10 +5927,8 @@ public class AdapterService extends Service {
        if (mCsipSetCoordinatorService != null && mCsipSetCoordinatorService.isAvailable()) {
            mCsipSetCoordinatorService.handleBondStateChanged(device, fromState, toState);
        }
        if (mDatabaseManager != null) {
        mDatabaseManager.handleBondStateChanged(device, fromState, toState);
    }
    }

    static int convertScanModeToHal(int mode) {
        switch (mode) {
+38 −57
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ public class RemoteDevices {

    private final HashMap<String, DeviceProperties> mDevices;
    private final HashMap<String, String> mDualDevicesMap;
    private ArrayDeque<String> mDeviceQueue;
    private final ArrayDeque<String> mDeviceQueue;

    /**
     * Bluetooth HFP v1.8 specifies the Battery Charge indicator of AG can take values from
@@ -154,42 +154,26 @@ public class RemoteDevices {
    RemoteDevices(AdapterService service, Looper looper) {
        mAdapter = ((Context) service).getSystemService(BluetoothManager.class).getAdapter();
        mAdapterService = service;
        mSdpTracker = new ArrayList<BluetoothDevice>();
        mDevices = new HashMap<String, DeviceProperties>();
        mDualDevicesMap = new HashMap<String, String>();
        mSdpTracker = new ArrayList<>();
        mDevices = new HashMap<>();
        mDualDevicesMap = new HashMap<>();
        mDeviceQueue = new ArrayDeque<>();
        mHandler = new RemoteDevicesHandler(looper);
        mMainHandler = new Handler(Looper.getMainLooper());
    }

    /** Init should be called before using this RemoteDevices object */
    void init() {}

    /**
     * Clean up should be called when this object is no longer needed, must be called after init()
     */
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    void cleanup() {
        reset();
    }

    /**
     * Reset should be called when the state of this object needs to be cleared
     * RemoteDevices is still usable after reset
     */
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    void reset() {
        if (mSdpTracker != null) {
        mSdpTracker.clear();
        }

        // Unregister Handler and stop all queued messages.
        if (mMainHandler != null) {
        mMainHandler.removeCallbacksAndMessages(null);
        }

        synchronized (mDevices) {
            if (mDevices != null) {
            debugLog("reset(): Broadcasting ACL_DISCONNECTED");

            mDevices.forEach(
@@ -203,30 +187,27 @@ public class RemoteDevices {
                                        + bluetoothDevice.isConnected());

                        if (bluetoothDevice.isConnected()) {
                        int transport = deviceProperties.getConnectionHandle(
                                BluetoothDevice.TRANSPORT_BREDR) != BluetoothDevice.ERROR
                            int transport =
                                    deviceProperties.getConnectionHandle(
                                                            BluetoothDevice.TRANSPORT_BREDR)
                                                    != BluetoothDevice.ERROR
                                            ? BluetoothDevice.TRANSPORT_BREDR
                                            : BluetoothDevice.TRANSPORT_LE;
                            mAdapterService.notifyAclDisconnected(bluetoothDevice, transport);
                            Intent intent = new Intent(BluetoothDevice.ACTION_ACL_DISCONNECTED);
                            intent.putExtra(BluetoothDevice.EXTRA_DEVICE, bluetoothDevice);
                        intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                            intent.addFlags(
                                    Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
                                            | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
                            mAdapterService.sendBroadcast(intent, BLUETOOTH_CONNECT);
                        }
                    });
            mDevices.clear();
        }
        }

        if (mDualDevicesMap != null) {
        mDualDevicesMap.clear();
        }

        if (mDeviceQueue != null) {
        mDeviceQueue.clear();
    }
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
@@ -574,7 +555,7 @@ public class RemoteDevices {
        }

        /**
         * @param isBondingInitiatedLocally wether bonding is initiated locally
         * @param isBondingInitiatedLocally whether bonding is initiated locally
         */
        void setBondingInitiatedLocally(boolean isBondingInitiatedLocally) {
            synchronized (mObject) {
+29 −33
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.bluetooth.SdpRecord;
import android.bluetooth.SdpSapsRecord;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelUuid;
import android.os.Parcelable;
@@ -43,13 +44,11 @@ import java.util.Arrays;
public class SdpManager {
    private static final String TAG = SdpManager.class.getSimpleName();

    // TODO: When changing PBAP to use this new API.
    //       Move the defines to the profile (PBAP already have the feature bits)
    /* PBAP repositories */
    public static final byte PBAP_REPO_LOCAL = 0x01 << 0;
    public static final byte PBAP_REPO_SIM = 0x01 << 1;
    public static final byte PBAP_REPO_SPEED_DAIL = 0x01 << 2;
    public static final byte PBAP_REPO_FAVORITES = 0x01 << 3;
    private static final Object TRACKER_LOCK = new Object();

    /* The timeout to wait for reply from native. Should never fire. */
    private static final int SDP_INTENT_DELAY = 11000;
    private static final int MESSAGE_SDP_INTENT = 2;

    /* Variables to keep track of ongoing and queued search requests.
     * mTrackerLock must be held, when using/changing mSdpSearchTracker
@@ -57,20 +56,14 @@ public class SdpManager {
    @GuardedBy("TRACKER_LOCK")
    private final SdpSearchTracker mSdpSearchTracker = new SdpSearchTracker();

    private boolean mSearchInProgress = false;
    static final Object TRACKER_LOCK = new Object();

    /* The timeout to wait for reply from native. Should never fire. */
    private static final int SDP_INTENT_DELAY = 11000;
    private static final int MESSAGE_SDP_INTENT = 2;

    // We need a reference to the adapter service, to be able to send intents
    private final AdapterService mAdapterService;
    private boolean mNativeAvailable;

    private final Handler mHandler;
    private final SdpManagerNativeInterface mNativeInterface =
            SdpManagerNativeInterface.getInstance();

    private boolean mSearchInProgress = false;
    private boolean mNativeAvailable;

    /* Inner class used for wrapping sdp search instance data */
    private class SdpSearchInstance {
        private final BluetoothDevice mDevice;
@@ -122,7 +115,6 @@ public class SdpManager {
        }
    }


    /* We wrap the ArrayList class to decorate with functionality to
     * find an instance based on UUID AND device address.
     * As we use a mix of byte[] and object instances, this is more
@@ -177,9 +169,28 @@ public class SdpManager {
    }

    public SdpManager(AdapterService adapterService) {
        this(adapterService, Looper.myLooper());
    }

    public SdpManager(AdapterService adapterService, Looper looper) {
        mAdapterService = adapterService;
        mNativeInterface.init(this);
        mNativeAvailable = true;
        mHandler =
                new Handler(looper) {
                    @Override
                    public void handleMessage(Message msg) {
                        switch (msg.what) {
                            case MESSAGE_SDP_INTENT:
                                SdpSearchInstance msgObj = (SdpSearchInstance) msg.obj;
                                Log.w(TAG, "Search timedout for UUID " + msgObj.getUuid());
                                synchronized (TRACKER_LOCK) {
                                    sendSdpIntent(msgObj, null, false);
                                }
                                break;
                        }
                    }
                };
    }

    public void cleanup() {
@@ -438,19 +449,4 @@ public class SdpManager {
            startSearch();
        }
    }

    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_SDP_INTENT:
                    SdpSearchInstance msgObj = (SdpSearchInstance) msg.obj;
                    Log.w(TAG, "Search timedout for UUID " + msgObj.getUuid());
                    synchronized (TRACKER_LOCK) {
                        sendSdpIntent(msgObj, null, false);
                    }
                    break;
            }
        }
    };
}
+0 −369

File deleted.

Preview size limit exceeded, changes collapsed.

+33 −18
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import android.os.BatteryStatsManager;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.Process;
@@ -115,7 +116,21 @@ public class AdapterServiceTest {
    private static final int MESSAGE_PROFILE_SERVICE_REGISTERED = 2;
    private static final int MESSAGE_PROFILE_SERVICE_UNREGISTERED = 3;

    private AdapterService mAdapterService;
    private MockAdapterService mAdapterService;

    static class MockAdapterService extends AdapterService {

        int mSetProfileServiceStateCounter = 0;

        MockAdapterService(Looper looper) {
            super(looper);
        }

        @Override
        void setProfileServiceState(int profileId, int state) {
            mSetProfileServiceStateCounter++;
        }
    }

    @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();

@@ -208,12 +223,10 @@ public class AdapterServiceTest {
        ScanNativeInterface.setInstance(mScanNativeInterface);

        // Post the creation of AdapterService since it rely on Looper.myLooper()
        handler.post(() -> mAdapterService = spy(new AdapterService(mLooper.getLooper())));
        handler.post(() -> mAdapterService = new MockAdapterService(mLooper.getLooper()));
        assertThat(mLooper.dispatchAll()).isEqualTo(1);
        assertThat(mAdapterService).isNotNull();

        doNothing().when(mAdapterService).setProfileServiceState(anyInt(), anyInt());

        mMockPackageManager = mock(PackageManager.class);
        when(mMockPackageManager.getPermissionInfo(any(), anyInt()))
                .thenReturn(new PermissionInfo());
@@ -390,6 +403,9 @@ public class AdapterServiceTest {
            IBluetoothCallback callback,
            AdapterNativeInterface nativeInterface) {
        adapter.enable(false);
        if (Flags.fastBindToApp()) {
            TestUtils.syncHandler(looper, 0); // when fastBindToApp is enable init need to be run
        }
        TestUtils.syncHandler(looper, AdapterState.BLE_TURN_ON);
        verifyStateChange(callback, STATE_OFF, STATE_BLE_TURNING_ON);

@@ -406,7 +422,7 @@ public class AdapterServiceTest {

    static void onToBleOn(
            TestLooper looper,
            AdapterService adapter,
            MockAdapterService adapter,
            Context ctx,
            IBluetoothCallback callback,
            boolean onlyGatt,
@@ -417,7 +433,7 @@ public class AdapterServiceTest {

        if (!onlyGatt) {
            // Stop PBAP and PAN services
            verify(adapter, times(4)).setProfileServiceState(anyInt(), anyInt());
            assertThat(adapter.mSetProfileServiceStateCounter).isEqualTo(4);

            for (ProfileService service : services) {
                adapter.onProfileServiceStateChanged(service, STATE_OFF);
@@ -441,11 +457,12 @@ public class AdapterServiceTest {
                List.of(mMockService, mMockService2),
                mNativeInterface);
    }

    // Method is re-used in other AdapterService*Test
    static void doEnable(
            TestLooper looper,
            ProfileService gattService,
            AdapterService adapter,
            MockAdapterService adapter,
            Context ctx,
            boolean onlyGatt,
            List<ProfileService> services,
@@ -467,7 +484,7 @@ public class AdapterServiceTest {

        if (!onlyGatt) {
            // Start Mock PBAP and PAN services
            verify(adapter, times(2)).setProfileServiceState(anyInt(), anyInt());
            assertThat(adapter.mSetProfileServiceStateCounter).isEqualTo(2);

            for (ProfileService service : services) {
                adapter.addProfile(service);
@@ -502,7 +519,7 @@ public class AdapterServiceTest {
    private static void doDisable(
            TestLooper looper,
            ProfileService gattService,
            AdapterService adapter,
            MockAdapterService adapter,
            Context ctx,
            boolean onlyGatt,
            List<ProfileService> services,
@@ -603,6 +620,9 @@ public class AdapterServiceTest {
        assertThat(mAdapterService.getState()).isEqualTo(STATE_OFF);

        mAdapterService.enable(false);
        if (Flags.fastBindToApp()) {
            syncHandler(0); // when fastBindToApp is enable init need to be run
        }
        syncHandler(AdapterState.BLE_TURN_ON);
        verifyStateChange(STATE_OFF, STATE_BLE_TURNING_ON);
        assertThat(mAdapterService.getBluetoothGatt()).isNotNull();
@@ -674,8 +694,7 @@ public class AdapterServiceTest {
        mAdapterService.startBrEdr();
        syncHandler(AdapterState.USER_TURN_ON);
        verifyStateChange(STATE_BLE_ON, STATE_TURNING_ON);
        verify(mAdapterService, times(2))
                .setProfileServiceState(anyInt(), anyInt()); // Register Mock PBAP and PAN services
        assertThat(mAdapterService.mSetProfileServiceStateCounter).isEqualTo(2);

        mAdapterService.addProfile(mMockService);
        syncHandler(MESSAGE_PROFILE_SERVICE_REGISTERED);
@@ -690,8 +709,7 @@ public class AdapterServiceTest {
        syncHandler(AdapterState.BREDR_START_TIMEOUT);

        verifyStateChange(STATE_TURNING_ON, STATE_TURNING_OFF);
        verify(mAdapterService, times(4))
                .setProfileServiceState(anyInt(), anyInt()); // Stop PBAP and PAN services
        assertThat(mAdapterService.mSetProfileServiceStateCounter).isEqualTo(4);

        mAdapterService.onProfileServiceStateChanged(mMockService, STATE_OFF);
        syncHandler(MESSAGE_PROFILE_SERVICE_STATE_CHANGED);
@@ -702,10 +720,7 @@ public class AdapterServiceTest {
        assertThat(mAdapterService.getBluetoothGatt()).isNotNull();
    }

    /**
     * Test: Don't stop a classic profile
     * Check whether the AdapterService quits gracefully
     */
    /** Test: Don't stop a classic profile Check whether the AdapterService quits gracefully */
    @Test
    public void testProfileStopTimeout() {
        doEnable(false);
@@ -713,7 +728,7 @@ public class AdapterServiceTest {
        mAdapterService.disable();
        syncHandler(AdapterState.USER_TURN_OFF);
        verifyStateChange(STATE_ON, STATE_TURNING_OFF);
        verify(mAdapterService, times(4)).setProfileServiceState(anyInt(), anyInt());
        assertThat(mAdapterService.mSetProfileServiceStateCounter).isEqualTo(4);

        mAdapterService.onProfileServiceStateChanged(mMockService, STATE_OFF);
        syncHandler(MESSAGE_PROFILE_SERVICE_STATE_CHANGED);