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

Commit 6911baa0 authored by Eric Cochran's avatar Eric Cochran Committed by Danny Baumann
Browse files

show connected devices or name of single device on Bluetooth Tile

Patchset 4: Add ACL listener to trace back connected/disconnected bt devices
            CleanUp
Patchset 5: Rebased
Patchset 6: Move ACL listener to BluetoothController

Change-Id: Ic9dda4f9123bc541753332df759cbd8534ab2e10
parent f9732c06
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@
    <string name="quick_settings_camera_label">Camera</string>
    <string name="quick_settings_network_adb_disabled_label">Disabled</string>
    <string name="quick_settings_network_adb_enabled_label">Enabled</string>
    <string name="quick_settings_bluetooth_multi_label">%1$d connected</string>

    <!-- [CHAR LIMIT=30] Location settings screen, high accuracy location mode -->
    <string name="location_mode_high_accuracy_title">High accuracy</string>
+58 −52
Original line number Diff line number Diff line
package com.android.systemui.quicksettings;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter.BluetoothStateChangeCallback;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;

import com.android.systemui.R;
import com.android.systemui.statusbar.phone.QuickSettingsContainerView;
import com.android.systemui.statusbar.phone.QuickSettingsController;
import com.android.systemui.statusbar.policy.BluetoothController;

public class BluetoothTile extends QuickSettingsTile implements BluetoothStateChangeCallback{
import java.util.Set;

    private boolean enabled = false;
    private boolean connected = false;
public class BluetoothTile extends QuickSettingsTile implements
        BluetoothAdapter.BluetoothStateChangeCallback,
        BluetoothController.BluetoothDeviceConnectionStateChangeCallback {

    private boolean mEnabled = false;
    private boolean mConnected = false;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothController mController;

    public BluetoothTile(Context context, QuickSettingsController qsc, BluetoothController controller) {
        super(context, qsc);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mController = controller;
        enabled = mBluetoothAdapter.isEnabled();
        connected = mBluetoothAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED;

        mOnClick = new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(enabled){
                if (mEnabled) {
                    mBluetoothAdapter.disable();
                } else {
                    mBluetoothAdapter.enable();
@@ -41,42 +42,28 @@ public class BluetoothTile extends QuickSettingsTile implements BluetoothStateCh
        };

        mOnLongClick = new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                startSettingsActivity(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS);
                return true;
            }
        };
        qsc.registerAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED, this);
        qsc.registerAction(BluetoothAdapter.ACTION_STATE_CHANGED, this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        boolean update = false;
        if(intent.getAction().equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                    BluetoothAdapter.ERROR);
            enabled = (state == BluetoothAdapter.STATE_ON);
            update = true;
        }

        if(intent.getAction().equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)){
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
                    BluetoothAdapter.STATE_DISCONNECTED);
            connected = (state == BluetoothAdapter.STATE_CONNECTED);
            update = true;
        }

        if (update) {
            updateResources();
        }
    void onPostCreate() {
        checkBluetoothState();
        updateTile();
        mController.addStateChangedCallback(this);
        mController.addConnectionStateChangedCallback(this);
        super.onPostCreate();
    }

    void checkBluetoothState() {
        enabled = mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
        connected = mBluetoothAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED;
    @Override
    public void onDestroy() {
        mController.removeStateChangedCallback(this);
        mController.removeConnectionStateChangedCallback(this);
        super.onDestroy();
    }

    @Override
@@ -85,14 +72,36 @@ public class BluetoothTile extends QuickSettingsTile implements BluetoothStateCh
        super.updateResources();
    }

    void checkBluetoothState() {
        mEnabled = mBluetoothAdapter.isEnabled() &&
                mBluetoothAdapter.getState() == BluetoothAdapter.STATE_ON;
        mConnected = mEnabled &&
                mBluetoothAdapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED;
    }

    private synchronized void updateTile() {
        if(enabled){
            if(connected){
        if (mEnabled) {
            if (mConnected) {
                final Set<BluetoothDevice> connected = mController.getConnectedBluetoothDevices();

                mDrawable = R.drawable.ic_qs_bluetooth_on;
                if (connected.isEmpty()) {
                    // shouldn't happen, but provide a sane fallback nevertheless
                    mLabel = mContext.getString(R.string.quick_settings_bluetooth_label);
                } else if (connected.size() == 1) {
                    BluetoothDevice device = connected.iterator().next();
                    mLabel = device.getAlias();
                    if (mLabel == null) {
                        mLabel = device.getName();
                    }
                } else {
                mDrawable = R.drawable.ic_qs_bluetooth_not_connected;
                    mLabel = mContext.getString(R.string.quick_settings_bluetooth_multi_label,
                            connected.size());
                }
            } else {
                mDrawable = R.drawable.ic_qs_bluetooth_not_connected;
                mLabel = mContext.getString(R.string.quick_settings_bluetooth_label);
            }
        } else {
            mDrawable = R.drawable.ic_qs_bluetooth_off;
            mLabel = mContext.getString(R.string.quick_settings_bluetooth_off_label);
@@ -100,23 +109,20 @@ public class BluetoothTile extends QuickSettingsTile implements BluetoothStateCh
    }

    @Override
    void onPostCreate() {
    public void onBluetoothStateChange(boolean on) {
        checkBluetoothState();
        updateTile();
        mController.addStateChangedCallback(this);
        super.onPostCreate();
        updateResources();
    }

    @Override
    public void onDestroy() {
        mController.removeStateChangedCallback(this);
        super.onDestroy();
    public void onDeviceConnectionStateChange(BluetoothDevice device) {
        updateResources();
    }

    @Override
    public void onBluetoothStateChange(boolean on) {
        this.enabled = on;
    public void onDeviceNameChange(BluetoothDevice device) {
        if (mController.getConnectedBluetoothDevices().size() == 1) {
            updateResources();
        }

    }
}
+61 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.HashSet;
@@ -31,12 +32,20 @@ import java.util.Set;
public class BluetoothController extends BroadcastReceiver {
    private static final String TAG = "StatusBar.BluetoothController";

    public interface BluetoothDeviceConnectionStateChangeCallback {
        void onDeviceConnectionStateChange(BluetoothDevice device);
        void onDeviceNameChange(BluetoothDevice device);
    }

    private boolean mEnabled = false;

    private Set<BluetoothDevice> mBondedDevices = new HashSet<BluetoothDevice>();
    private Set<BluetoothDevice> mConnectedDevices = new HashSet<BluetoothDevice>();

    private ArrayList<BluetoothStateChangeCallback> mChangeCallbacks =
            new ArrayList<BluetoothStateChangeCallback>();
    private ArrayList<BluetoothDeviceConnectionStateChangeCallback> mConnectionChangeCallbacks =
            new ArrayList<BluetoothDeviceConnectionStateChangeCallback>();

    public BluetoothController(Context context) {

@@ -44,6 +53,10 @@ public class BluetoothController extends BroadcastReceiver {
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ALIAS_CHANGED);
        filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED);
        context.registerReceiver(this, filter);

        final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
@@ -62,22 +75,56 @@ public class BluetoothController extends BroadcastReceiver {
        mChangeCallbacks.remove(cb);
    }

    public void addConnectionStateChangedCallback(
            BluetoothDeviceConnectionStateChangeCallback cb) {
        mConnectionChangeCallbacks.add(cb);
    }

    public void removeConnectionStateChangedCallback(
            BluetoothDeviceConnectionStateChangeCallback cb) {
        mConnectionChangeCallbacks.remove(cb);
    }

    public Set<BluetoothDevice> getBondedBluetoothDevices() {
        return mBondedDevices;
    }

    public Set<BluetoothDevice> getConnectedBluetoothDevices() {
        return mConnectedDevices;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        final Bundle extras = intent.getExtras();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            handleAdapterStateChange(
                    intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR));
        }

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)
                || action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)
                || action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
            fireCallbacks();
            updateBondedBluetoothDevices();
        }

        if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
            BluetoothDevice device = extras.getParcelable(BluetoothDevice.EXTRA_DEVICE);
            mConnectedDevices.add(device);
            fireConnectionStateChanged(device);
        } else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
            BluetoothDevice device = extras.getParcelable(BluetoothDevice.EXTRA_DEVICE);
            mConnectedDevices.remove(device);
            fireConnectionStateChanged(device);
        } else if (BluetoothDevice.ACTION_ALIAS_CHANGED.equals(action) ||
                BluetoothDevice.ACTION_NAME_CHANGED.equals(action)) {
            BluetoothDevice device = extras.getParcelable(BluetoothDevice.EXTRA_DEVICE);
            fireDeviceNameChanged(device);
        }
    }

    private void updateBondedBluetoothDevices() {
        mBondedDevices.clear();

@@ -103,4 +150,16 @@ public class BluetoothController extends BroadcastReceiver {
            cb.onBluetoothStateChange(mEnabled);
        }
    }

    private void fireConnectionStateChanged(BluetoothDevice device) {
        for (BluetoothDeviceConnectionStateChangeCallback cb : mConnectionChangeCallbacks) {
            cb.onDeviceConnectionStateChange(device);
        }
    }

    private void fireDeviceNameChanged(BluetoothDevice device) {
        for (BluetoothDeviceConnectionStateChangeCallback cb : mConnectionChangeCallbacks) {
            cb.onDeviceNameChange(device);
        }
    }
}