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

Commit be3c5dbe authored by Jason Monk's avatar Jason Monk
Browse files

Make QS use SettingsLib's BT code

A couple of changes needed to be made to SettingsLib to support this.
 - SettingsLib needed to track ACTION_CONNECTION_STATE_CHANGED
 - The summary code needed to move from Settings up into SettingsLib
 - Added a getMaxConnectionState to CachedBluetoothDevice
    - This simplifies the states of all of the profiles into
      one.

Change-Id: I7f828f0038ad0cf39274986ece6d486d545f0286
parent c94ea96c
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -76,6 +76,17 @@
    <string name="bluetooth_connecting">Connecting\u2026</string>
    <!-- Bluetooth settings.  Message when connected to a device. [CHAR LIMIT=40] -->
    <string name="bluetooth_connected">Connected</string>
    <!--Bluetooth settings screen, summary text under individual Bluetooth devices when pairing -->
    <string name="bluetooth_pairing">Pairing\u2026</string>

    <!-- Bluetooth settings.  Message when connected to a device, except for phone audio. [CHAR LIMIT=40] -->
    <string name="bluetooth_connected_no_headset">Connected (no phone)</string>
    <!-- Bluetooth settings.  Message when connected to a device, except for media audio. [CHAR LIMIT=40] -->
    <string name="bluetooth_connected_no_a2dp">Connected (no media)</string>
    <!-- Bluetooth settings.  Message when connected to a device, except for map. [CHAR LIMIT=40] -->
    <string name="bluetooth_connected_no_map">Connected (no message access)</string>
    <!-- Bluetooth settings.  Message when connected to a device, except for phone/media audio. [CHAR LIMIT=40] -->
    <string name="bluetooth_connected_no_headset_no_a2dp">Connected (no phone or media)</string>

    <!-- Bluetooth settings.  The user-visible string that is used whenever referring to the A2DP profile. -->
    <string name="bluetooth_profile_a2dp">Media audio</string>
+1 −0
Original line number Diff line number Diff line
@@ -27,4 +27,5 @@ public interface BluetoothCallback {
    void onDeviceAdded(CachedBluetoothDevice cachedDevice);
    void onDeviceDeleted(CachedBluetoothDevice cachedDevice);
    void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState);
    void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state);
}
+22 −3
Original line number Diff line number Diff line
@@ -81,6 +81,9 @@ public final class BluetoothEventManager {

        // Bluetooth on/off broadcasts
        addHandler(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedHandler());
        // Generic connected/not broadcast
        addHandler(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED,
                new ConnectionStateChangedHandler());

        // Discovery broadcasts
        addHandler(BluetoothAdapter.ACTION_DISCOVERY_STARTED, new ScanningStateChangedHandler(true));
@@ -183,8 +186,6 @@ public final class BluetoothEventManager {
                cachedDevice = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, device);
                Log.d(TAG, "DeviceFoundHandler created new CachedBluetoothDevice: "
                        + cachedDevice);
                // callback to UI to create Preference for new device
                dispatchDeviceAdded(cachedDevice);
            }
            cachedDevice.setRssi(rssi);
            cachedDevice.setBtClass(btClass);
@@ -193,7 +194,25 @@ public final class BluetoothEventManager {
        }
    }

    private void dispatchDeviceAdded(CachedBluetoothDevice cachedDevice) {
    private class ConnectionStateChangedHandler implements Handler {
        @Override
        public void onReceive(Context context, Intent intent, BluetoothDevice device) {
            CachedBluetoothDevice cachedDevice = mDeviceManager.findDevice(device);
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
                    BluetoothAdapter.ERROR);
            dispatchConnectionStateChanged(cachedDevice, state);
        }
    }

    private void dispatchConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) {
        synchronized (mCallbacks) {
            for (BluetoothCallback callback : mCallbacks) {
                callback.onConnectionStateChanged(cachedDevice, state);
            }
        }
    }

    void dispatchDeviceAdded(CachedBluetoothDevice cachedDevice) {
        synchronized (mCallbacks) {
            for (BluetoothCallback callback : mCallbacks) {
                callback.onDeviceAdded(cachedDevice);
+60 −0
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import android.text.TextUtils;
import android.util.Log;
import android.bluetooth.BluetoothAdapter;

import com.android.settingslib.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -784,4 +786,62 @@ public final class CachedBluetoothDevice implements Comparable<CachedBluetoothDe
            setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_ALLOWED);
        }
    }

    public int getMaxConnectionState() {
        int maxState = BluetoothProfile.STATE_DISCONNECTED;
        for (LocalBluetoothProfile profile : getProfiles()) {
            int connectionStatus = getProfileConnectionState(profile);
            if (connectionStatus > maxState) {
                maxState = connectionStatus;
            }
        }
        return maxState;
    }

    /**
     * @return resource for string that discribes the connection state of this device.
     */
    public int getConnectionSummary() {
        boolean profileConnected = false;       // at least one profile is connected
        boolean a2dpNotConnected = false;       // A2DP is preferred but not connected
        boolean headsetNotConnected = false;    // Headset is preferred but not connected

        for (LocalBluetoothProfile profile : getProfiles()) {
            int connectionStatus = getProfileConnectionState(profile);

            switch (connectionStatus) {
                case BluetoothProfile.STATE_CONNECTING:
                case BluetoothProfile.STATE_DISCONNECTING:
                    return Utils.getConnectionStateSummary(connectionStatus);

                case BluetoothProfile.STATE_CONNECTED:
                    profileConnected = true;
                    break;

                case BluetoothProfile.STATE_DISCONNECTED:
                    if (profile.isProfileReady()) {
                        if (profile instanceof A2dpProfile) {
                            a2dpNotConnected = true;
                        } else if (profile instanceof HeadsetProfile) {
                            headsetNotConnected = true;
                        }
                    }
                    break;
            }
        }

        if (profileConnected) {
            if (a2dpNotConnected && headsetNotConnected) {
                return R.string.bluetooth_connected_no_headset_no_a2dp;
            } else if (a2dpNotConnected) {
                return R.string.bluetooth_connected_no_a2dp;
            } else if (headsetNotConnected) {
                return R.string.bluetooth_connected_no_headset;
            } else {
                return R.string.bluetooth_connected;
            }
        }

        return getBondState() == BluetoothDevice.BOND_BONDING ? R.string.bluetooth_pairing : 0;
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -35,9 +35,11 @@ public final class CachedBluetoothDeviceManager {
    private Context mContext;
    private final List<CachedBluetoothDevice> mCachedDevices =
            new ArrayList<CachedBluetoothDevice>();
    private final LocalBluetoothManager mBtManager;

    CachedBluetoothDeviceManager(Context context) {
    CachedBluetoothDeviceManager(Context context, LocalBluetoothManager localBtManager) {
        mContext = context;
        mBtManager = localBtManager;
    }

    public synchronized Collection<CachedBluetoothDevice> getCachedDevicesCopy() {
@@ -88,6 +90,7 @@ public final class CachedBluetoothDeviceManager {
            profileManager, device);
        synchronized (mCachedDevices) {
            mCachedDevices.add(newDevice);
            mBtManager.getEventManager().dispatchDeviceAdded(newDevice);
        }
        return newDevice;
    }
Loading