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

Commit 49efbf8b authored by Michael Chan's avatar Michael Chan Committed by Android Git Automerger
Browse files

am c9b9f650: Merge "b/2367861 Auto connect if the user turns on BT while docked." into eclair

Merge commit 'c9b9f650' into eclair-plus-aosp

* commit 'c9b9f650':
  b/2367861 Auto connect if the user turns on BT while docked.
parents 213c35c0 c9b9f650
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -492,6 +492,7 @@
            android:name=".bluetooth.DockEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.DOCK_EVENT" />
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
+8 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ import android.util.Log;

public class DockEventReceiver extends BroadcastReceiver {

    private static final boolean DEBUG = false;
    private static final boolean DEBUG = DockService.DEBUG;

    private static final String TAG = "DockEventReceiver";

@@ -74,6 +74,13 @@ public class DockEventReceiver extends BroadcastReceiver {
                    if (DEBUG) Log.e(TAG, "Unknown state");
                    break;
            }
        } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
            int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            if (btState == BluetoothAdapter.STATE_ON) {
                Intent i = new Intent(intent);
                i.setClass(context, DockService.class);
                beginStartingService(context, i);
            }
        }
    }

+54 −19
Original line number Diff line number Diff line
@@ -19,9 +19,10 @@ package com.android.settings.bluetooth;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.Service;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.bluetooth.BluetoothHeadset;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@@ -41,13 +42,15 @@ import android.widget.CompoundButton;
import com.android.settings.R;
import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;

import java.util.List;

public class DockService extends Service implements AlertDialog.OnMultiChoiceClickListener,
        DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
        CompoundButton.OnCheckedChangeListener {

    private static final String TAG = "DockService";

    private static final boolean DEBUG = false;
    static final boolean DEBUG = false;

    // Time allowed for the device to be undocked and redocked without severing
    // the bluetooth connection
@@ -87,7 +90,6 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
    private BluetoothDevice mPendingDevice;
    private int mPendingStartId;

    private boolean mRegistered;
    private Object mBtSynchroObject = new Object();

    @Override
@@ -111,10 +113,6 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
            mDialog.dismiss();
            mDialog = null;
        }
        if (mRegistered) {
            unregisterReceiver(mReceiver);
            mRegistered = false;
        }
        mServiceLooper.quit();
    }

@@ -138,6 +136,12 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
            return START_NOT_STICKY;
        }

        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
            handleBtStateChange(intent);
            DockEventReceiver.finishStartingService(this, startId);
            return START_NOT_STICKY;
        }

        Message msg = parseIntent(intent);
        if (msg == null) {
            // Bad intent
@@ -407,12 +411,11 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
        return items;
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            if (state == BluetoothAdapter.STATE_ON && mPendingDevice != null) {
    public void handleBtStateChange(Intent intent) {
        int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
        if (btState == BluetoothAdapter.STATE_ON) {
            synchronized (mBtSynchroObject) {
                if (mPendingDevice != null) {
                    if (mPendingDevice.equals(mDevice)) {
                        if(DEBUG) Log.d(TAG, "applying settings");
                        applyBtSettings(mPendingDevice, mPendingStartId);
@@ -423,10 +426,46 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli

                    mPendingDevice = null;
                    DockEventReceiver.finishStartingService(mContext, mPendingStartId);
                } else {
                    // Reconnect if docked and bluetooth was enabled by user.
                    Intent i = registerReceiver(null, new IntentFilter(Intent.ACTION_DOCK_EVENT));
                    if (i != null) {
                        int state = i.getIntExtra(Intent.EXTRA_DOCK_STATE,
                                Intent.EXTRA_DOCK_STATE_UNDOCKED);
                        if (state != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
                            BluetoothDevice device = i
                                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                            if (device != null) {
                                connectIfEnabled(device);
                            }
                        }
                    }
                }
            }
        }
    }

    private synchronized void connectIfEnabled(BluetoothDevice device) {
        CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(mContext, mBtManager, device);
        List<Profile> profiles = cachedDevice.getConnectableProfiles();
        for (int i = 0; i < profiles.size(); i++) {
            LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
                    .getProfileManager(mBtManager, profiles.get(i));
            int auto;
            if (Profile.A2DP == profiles.get(i)) {
                auto = BluetoothA2dp.PRIORITY_AUTO_CONNECT;
            } else if (Profile.HEADSET == profiles.get(i)) {
                auto = BluetoothHeadset.PRIORITY_AUTO_CONNECT;
            } else {
                continue;
            }

            if (profileManager.getPreferred(device) == auto) {
                cachedDevice.connect();
                break;
            }
        }
    }
    };

    private synchronized void applyBtSettings(final BluetoothDevice device, int startId) {
        if (device == null || mProfiles == null || mCheckedItems == null)
@@ -444,12 +483,8 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
                            if (mPendingDevice != null && mPendingDevice.equals(mDevice)) {
                                return;
                            }
                            if (!mRegistered) {
                                registerReceiver(mReceiver, new IntentFilter(
                                        BluetoothAdapter.ACTION_STATE_CHANGED));
                            }

                            mPendingDevice = device;
                            mRegistered = true;
                            mPendingStartId = startId;
                            if (btState != BluetoothAdapter.STATE_TURNING_ON) {
                                // BT is off. Enable it
+17 −0
Original line number Diff line number Diff line
@@ -136,6 +136,8 @@ public abstract class LocalBluetoothProfileManager {

    public abstract boolean isPreferred(BluetoothDevice device);

    public abstract int getPreferred(BluetoothDevice device);

    public abstract void setPreferred(BluetoothDevice device, boolean preferred);

    public boolean isConnected(BluetoothDevice device) {
@@ -212,6 +214,11 @@ public abstract class LocalBluetoothProfileManager {
            return mService.getSinkPriority(device) > BluetoothA2dp.PRIORITY_OFF;
        }

        @Override
        public int getPreferred(BluetoothDevice device) {
            return mService.getSinkPriority(device);
        }

        @Override
        public void setPreferred(BluetoothDevice device, boolean preferred) {
            if (preferred) {
@@ -331,6 +338,11 @@ public abstract class LocalBluetoothProfileManager {
            return mService.getPriority(device) > BluetoothHeadset.PRIORITY_OFF;
        }

        @Override
        public int getPreferred(BluetoothDevice device) {
            return mService.getPriority(device);
        }

        @Override
        public void setPreferred(BluetoothDevice device, boolean preferred) {
            if (preferred) {
@@ -402,6 +414,11 @@ public abstract class LocalBluetoothProfileManager {
            return false;
        }

        @Override
        public int getPreferred(BluetoothDevice device) {
            return -1;
        }

        @Override
        public void setPreferred(BluetoothDevice device, boolean preferred) {
        }