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

Commit 5c391309 authored by William Escande's avatar William Escande
Browse files

Prepare database for mostRecentlyActiveHfpDevice

Bug: 290553699
Test: atest PhonePolicyTest
Test: atest DatabaseManagerTest
Test: This is supposed to be a no-op change for the logic
Change-Id: I366796e4144d9bc0f2cc6b678789f4b5b037973c
parent 2bae10e8
Loading
Loading
Loading
Loading
+122 −84
Original line number Diff line number Diff line
@@ -23,9 +23,6 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
@@ -206,9 +203,10 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {

        // Set profile priorities only for the profiles discovered on the remote device.
        // This avoids needless auto-connect attempts to profiles non-existent on the remote device
        if ((hidService != null) && (Utils.arrayContains(uuids, BluetoothUuid.HID)
                || Utils.arrayContains(uuids, BluetoothUuid.HOGP)) && (
                hidService.getConnectionPolicy(device)
        if ((hidService != null)
                && (Utils.arrayContains(uuids, BluetoothUuid.HID)
                        || Utils.arrayContains(uuids, BluetoothUuid.HOGP))
                && (hidService.getConnectionPolicy(device)
                        == BluetoothProfile.CONNECTION_POLICY_UNKNOWN)) {
            if (mAutoConnectProfilesSupported) {
                hidService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
@@ -230,8 +228,8 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                        BluetoothProfile.HEADSET, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            } else {
                if (mAutoConnectProfilesSupported) {
                    headsetService.setConnectionPolicy(device,
                            BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                    headsetService.setConnectionPolicy(
                            device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                } else {
                    mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                            BluetoothProfile.HEADSET, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
@@ -245,8 +243,7 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                && (a2dpService.getConnectionPolicy(device)
                        == BluetoothProfile.CONNECTION_POLICY_UNKNOWN)) {
            if (!isDualModeAudioEnabled() && isLeAudioProfileAllowed) {
                debugLog("clear a2dp profile priority for the le audio dual mode device "
                        + device);
                debugLog("clear a2dp profile priority for the le audio dual mode device " + device);
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                        BluetoothProfile.A2DP, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            } else {
@@ -254,8 +251,12 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                    a2dpService.setConnectionPolicy(device,
                            BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                } else {
                    mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                            BluetoothProfile.A2DP, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                    mAdapterService
                            .getDatabase()
                            .setProfileConnectionPolicy(
                                    device,
                                    BluetoothProfile.A2DP,
                                    BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                }
            }
        }
@@ -267,10 +268,13 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                        == BluetoothProfile.CONNECTION_POLICY_UNKNOWN)) {
            // Always allow CSIP during pairing process regardless of LE audio preference
            if (mAutoConnectProfilesSupported) {
                csipSetCoordinatorService.setConnectionPolicy(device,
                        BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                csipSetCoordinatorService.setConnectionPolicy(
                        device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            } else {
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                mAdapterService
                        .getDatabase()
                        .setProfileConnectionPolicy(
                                device,
                                BluetoothProfile.CSIP_SET_COORDINATOR,
                                BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            }
@@ -281,13 +285,18 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                && (Utils.arrayContains(uuids, BluetoothUuid.PANU)
                        && (panService.getConnectionPolicy(device)
                                == BluetoothProfile.CONNECTION_POLICY_UNKNOWN)
                && mAdapterService.getResources()
                        && mAdapterService
                                .getResources()
                                .getBoolean(R.bool.config_bluetooth_pan_enable_autoconnect))) {
            if (mAutoConnectProfilesSupported) {
                panService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            } else {
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                        BluetoothProfile.PAN, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                mAdapterService
                        .getDatabase()
                        .setProfileConnectionPolicy(
                                device,
                                BluetoothProfile.PAN,
                                BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            }
        }

@@ -298,16 +307,24 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
            if (isLeAudioProfileAllowed) {
                debugLog("setting le audio profile priority for device " + device);
                if (mAutoConnectProfilesSupported) {
                    leAudioService.setConnectionPolicy(device,
                            BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                    leAudioService.setConnectionPolicy(
                            device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                } else {
                    mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                            BluetoothProfile.LE_AUDIO, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                    mAdapterService
                            .getDatabase()
                            .setProfileConnectionPolicy(
                                    device,
                                    BluetoothProfile.LE_AUDIO,
                                    BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                }
            } else {
                debugLog("clear LEA profile priority because LE audio is not allowed");
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                        BluetoothProfile.LE_AUDIO, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
                mAdapterService
                        .getDatabase()
                        .setProfileConnectionPolicy(
                                device,
                                BluetoothProfile.LE_AUDIO,
                                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            }
        }

@@ -317,15 +334,22 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                        == BluetoothProfile.CONNECTION_POLICY_UNKNOWN)) {
            if (isLeAudioProfileAllowed) {
                debugLog("LE Audio preferred over ASHA for device " + device);
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                        BluetoothProfile.HEARING_AID, BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
                mAdapterService
                        .getDatabase()
                        .setProfileConnectionPolicy(
                                device,
                                BluetoothProfile.HEARING_AID,
                                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            } else {
                debugLog("setting hearing aid profile priority for device " + device);
                if (mAutoConnectProfilesSupported) {
                    hearingAidService.setConnectionPolicy(device,
                            BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                    hearingAidService.setConnectionPolicy(
                            device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                } else {
                    mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                    mAdapterService
                            .getDatabase()
                            .setProfileConnectionPolicy(
                                    device,
                                    BluetoothProfile.HEARING_AID,
                                    BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                }
@@ -339,16 +363,22 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
            if (isLeAudioProfileAllowed) {
                debugLog("setting volume control profile priority for device " + device);
                if (mAutoConnectProfilesSupported) {
                    volumeControlService.setConnectionPolicy(device,
                            BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                    volumeControlService.setConnectionPolicy(
                            device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                } else {
                    mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                    mAdapterService
                            .getDatabase()
                            .setProfileConnectionPolicy(
                                    device,
                                    BluetoothProfile.VOLUME_CONTROL,
                                    BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                }
            } else {
                debugLog("clear VCP priority because dual mode is disabled by default");
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                mAdapterService
                        .getDatabase()
                        .setProfileConnectionPolicy(
                                device,
                                BluetoothProfile.VOLUME_CONTROL,
                                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            }
@@ -361,15 +391,18 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
            debugLog("setting hearing access profile priority for device " + device);
            if (isLeAudioProfileAllowed) {
                if (mAutoConnectProfilesSupported) {
                    hapClientService.setConnectionPolicy(device,
                            BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                    hapClientService.setConnectionPolicy(
                            device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                } else {
                    mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                            BluetoothProfile.HAP_CLIENT,
                            BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                }
            } else {
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                mAdapterService
                        .getDatabase()
                        .setProfileConnectionPolicy(
                                device,
                                BluetoothProfile.HAP_CLIENT,
                                BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
            }
@@ -383,7 +416,10 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
            if (mAutoConnectProfilesSupported) {
                bcService.setConnectionPolicy(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            } else {
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                mAdapterService
                        .getDatabase()
                        .setProfileConnectionPolicy(
                                device,
                                BluetoothProfile.LE_AUDIO_BROADCAST_ASSISTANT,
                                BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            }
@@ -398,8 +434,12 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                batteryService.setConnectionPolicy(device,
                        BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            } else {
                mAdapterService.getDatabase().setProfileConnectionPolicy(device,
                        BluetoothProfile.BATTERY, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
                mAdapterService
                        .getDatabase()
                        .setProfileConnectionPolicy(
                                device,
                                BluetoothProfile.BATTERY,
                                BluetoothProfile.CONNECTION_POLICY_ALLOWED);
            }
        }
    }
@@ -410,7 +450,8 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
        debugLog("processProfileStateChanged, device=" + device + ", profile="
                + BluetoothProfile.getProfileName(profileId) + ", " + prevState + " -> "
                + nextState);
        if (((profileId == BluetoothProfile.A2DP) || (profileId == BluetoothProfile.HEADSET)
        if (((profileId == BluetoothProfile.A2DP)
                || (profileId == BluetoothProfile.HEADSET)
                || (profileId == BluetoothProfile.LE_AUDIO)
                || (profileId == BluetoothProfile.CSIP_SET_COORDINATOR)
                || (profileId == BluetoothProfile.VOLUME_CONTROL)
@@ -427,10 +468,9 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                connectOtherProfile(device);
            }
            if (nextState == BluetoothProfile.STATE_DISCONNECTED) {
                if (profileId == BluetoothProfile.A2DP
                        && (prevState == BluetoothProfile.STATE_CONNECTING
                        || prevState == BluetoothProfile.STATE_DISCONNECTING)) {
                    mDatabaseManager.setDisconnection(device);
                if (prevState == BluetoothProfile.STATE_CONNECTING
                        || prevState == BluetoothProfile.STATE_DISCONNECTING) {
                    mDatabaseManager.setDisconnection(device, profileId);
                }
                handleAllProfilesDisconnected(device);
            }
@@ -440,8 +480,8 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
    /**
     * Updates the last connection date in the connection order database for the newly active device
     * if connected to the A2DP profile. If this is a dual mode audio device (supports classic and
     * LE Audio), LE Audio is made active, and {@link Utils#isDualModeAudioEnabled()} is false,
     * A2DP and HFP will be disconnected.
     * LE Audio), LE Audio is made active, and {@link Utils#isDualModeAudioEnabled()} is false, A2DP
     * and HFP will be disconnected.
     *
     * @param device is the device we just made the active device
     */
@@ -451,7 +491,7 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
                + isDualModeAudioEnabled());

        if (device != null) {
            mDatabaseManager.setConnection(device, profileId == BluetoothProfile.A2DP);
            mDatabaseManager.setConnection(device, profileId);

            if (isDualModeAudioEnabled()) return;
            if (profileId == BluetoothProfile.LE_AUDIO) {
@@ -486,7 +526,7 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {

    private void processDeviceConnected(BluetoothDevice device) {
        debugLog("processDeviceConnected, device=" + device);
        mDatabaseManager.setConnection(device, false);
        mDatabaseManager.setConnection(device);
    }

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
@@ -553,22 +593,20 @@ class PhonePolicy implements AdapterService.BluetoothStateCallback {
            errorLog("autoConnect: BT is not ON. Exiting autoConnect");
            return;
        }
        if (mAdapterService.isQuietModeEnabled()) {
            Log.i(TAG, "autoConnect() - BT is in quiet mode. Not initiating autoConnect");
            return;
        }

        if (!mAdapterService.isQuietModeEnabled()) {
            debugLog("autoConnect: Initiate auto connection on BT on...");
        Log.i(TAG, "autoConnect: Initiate auto connection on BT on...");
        final BluetoothDevice mostRecentlyActiveA2dpDevice =
                mDatabaseManager.getMostRecentlyConnectedA2dpDevice();
            if (mostRecentlyActiveA2dpDevice == null) {
                errorLog("autoConnect: most recently active a2dp device is null");
                return;
            }
        if (mostRecentlyActiveA2dpDevice != null) {
            debugLog("autoConnect: Device " + mostRecentlyActiveA2dpDevice
                    + " attempting auto connection");
            autoConnectHeadset(mostRecentlyActiveA2dpDevice);
            autoConnectA2dp(mostRecentlyActiveA2dpDevice);
            autoConnectHidHost(mostRecentlyActiveA2dpDevice);
        } else {
            debugLog("autoConnect() - BT is in quiet mode. Not initiating auto connections");
        }
    }

+92 −53
Original line number Diff line number Diff line
@@ -585,29 +585,16 @@ public class DatabaseManager {
        }
    }

    /**
     * Updates the time this device was last connected
     *
     * @param device is the remote bluetooth device for which we are setting the connection time
     */
    public void setConnection(BluetoothDevice device, boolean isA2dpDevice) {
        synchronized (mMetadataCache) {
            Log.d(TAG, "setConnection: device " + device.getAnonymizedAddress()
                    + " and isA2dpDevice=" + isA2dpDevice);
    @GuardedBy("mMetadataCache")
    private void setConnection(BluetoothDevice device, boolean isActiveA2dp) {
        if (device == null) {
            Log.e(TAG, "setConnection: device is null");
            return;
        }

            if (isA2dpDevice) {
                resetActiveA2dpDevice();
            }

        String address = device.getAddress();

        if (!mMetadataCache.containsKey(address)) {
                Log.d(TAG, "setConnection: Creating new metadata entry for device: " + device);
                createMetadata(address, isA2dpDevice);
            createMetadata(address, isActiveA2dp);
            return;
        }
        // Updates last_active_time to the current counter value and increments the counter
@@ -617,49 +604,97 @@ public class DatabaseManager {
        }

        // Only update is_active_a2dp_device if an a2dp device is connected
            if (isA2dpDevice) {
        if (isActiveA2dp) {
            metadata.is_active_a2dp_device = true;
        }

            Log.d(TAG, "Updating last connected time for device: " + device.getAnonymizedAddress()
                    + " to " + metadata.last_active_time);
        Log.d(
                TAG,
                "Updating last connected time for device: "
                        + device
                        + " to "
                        + metadata.last_active_time);
        updateDatabase(metadata);
    }

    /**
     * Updates the time this device was last connected
     *
     * @param device is the remote bluetooth device for which we are setting the connection time
     */
    public void setConnection(BluetoothDevice device) {
        synchronized (mMetadataCache) {
            setConnection(device, false);
        }
    }

    /**
     * Sets is_active_device to false if currently true for device
     * Updates the time this device was last connected with its profile information
     *
     * @param device is the remote bluetooth device with which we have disconnected a2dp
     * @param device is the remote bluetooth device for which we are setting the connection time
     * @param profileId see {@link BluetoothProfile}
     */
    public void setDisconnection(BluetoothDevice device) {
    public void setConnection(BluetoothDevice device, int profileId) {
        boolean isA2dpDevice = profileId == BluetoothProfile.A2DP;

        synchronized (mMetadataCache) {
            if (isA2dpDevice) {
                resetActiveA2dpDevice();
            }

            setConnection(device, isA2dpDevice);
        }
    }

    /**
     * Sets device profileId's active status to false if currently true
     *
     * @param device is the remote bluetooth device with which we have disconnected
     * @param profileId see {@link BluetoothProfile}
     */
    public void setDisconnection(BluetoothDevice device, int profileId) {
        if (device == null) {
                Log.e(TAG, "setDisconnection: device is null");
            Log.e(
                    TAG,
                    "setDisconnection: device is null, "
                            + "profileId: "
                            + BluetoothProfile.getProfileName(profileId));
            return;
        }
        Log.d(
                TAG,
                "setDisconnection: device "
                        + device
                        + "profileId: "
                        + BluetoothProfile.getProfileName(profileId));

        if (profileId != BluetoothProfile.A2DP) {
            // there is no change on metadata when profile is not A2DP
            return;
        }

        String address = device.getAddress();

        synchronized (mMetadataCache) {
            if (!mMetadataCache.containsKey(address)) {
                return;
            }
            // Updates last connected time to either current time if connected or -1 if disconnected
            Metadata metadata = mMetadataCache.get(address);
            if (metadata.is_active_a2dp_device) {

            if (profileId == BluetoothProfile.A2DP && metadata.is_active_a2dp_device) {
                metadata.is_active_a2dp_device = false;
                Log.d(TAG, "setDisconnection: Updating is_active_device to false for device: "
                Log.d(
                        TAG,
                        "setDisconnection: Updating is_active_device to false for device: "
                                + device);
                updateDatabase(metadata);
            }
        }
    }

    /**
     * Remove a2dpActiveDevice from the current active device in the connection order table
     */
    /** Remove a2dpActiveDevice from the current active device in the connection order table */
    @GuardedBy("mMetadataCache")
    private void resetActiveA2dpDevice() {
        synchronized (mMetadataCache) {
        Log.d(TAG, "resetActiveA2dpDevice()");
        for (Map.Entry<String, Metadata> entry : mMetadataCache.entrySet()) {
            Metadata metadata = entry.getValue();
@@ -670,7 +705,6 @@ public class DatabaseManager {
            }
        }
    }
    }

    /**
     * Gets the most recently connected bluetooth devices in order with most recently connected
@@ -988,6 +1022,11 @@ public class DatabaseManager {
        }

        Metadata data = dataBuilder.build();
        Log.d(
                TAG,
                "createMetadata: "
                        + (" address=" + data.getAnonymizedAddress())
                        + (" isActiveA2dpDevice=" + isActiveA2dpDevice));
        mMetadataCache.put(address, data);
        updateDatabase(data);
        logMetadataChange(data, "Metadata created");
+38 −32

File changed.

Preview size limit exceeded, changes collapsed.

+10 −10

File changed.

Preview size limit exceeded, changes collapsed.

+12 −12

File changed.

Contains only whitespace changes.