diff options
author | Michael Chan <mchan@android.com> | 2010-01-13 16:31:39 -0800 |
---|---|---|
committer | Michael Chan <mchan@android.com> | 2010-01-14 11:26:18 -0800 |
commit | 845e740fc63657438b9085376c8e7d60d8334a72 (patch) | |
tree | c2ccb32d6e51a982fc287f1c0b7e12c896caaadc | |
parent | c5aa68f3f2201227e85e52ee33937e70e7741c3c (diff) | |
download | packages_apps_settings-845e740fc63657438b9085376c8e7d60d8334a72.zip packages_apps_settings-845e740fc63657438b9085376c8e7d60d8334a72.tar.gz packages_apps_settings-845e740fc63657438b9085376c8e7d60d8334a72.tar.bz2 |
b/2367861 Auto connect if the user turns on BT while docked.
Change-Id: I855d0448af9c9c665fb8288c83cd13ae9c3d6050
-rw-r--r-- | AndroidManifest.xml | 1 | ||||
-rw-r--r-- | src/com/android/settings/bluetooth/DockEventReceiver.java | 9 | ||||
-rw-r--r-- | src/com/android/settings/bluetooth/DockService.java | 73 | ||||
-rw-r--r-- | src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java | 17 |
4 files changed, 80 insertions, 20 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index b9945c5..9f55fe2 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -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> diff --git a/src/com/android/settings/bluetooth/DockEventReceiver.java b/src/com/android/settings/bluetooth/DockEventReceiver.java index 73f90e5..3324be4 100644 --- a/src/com/android/settings/bluetooth/DockEventReceiver.java +++ b/src/com/android/settings/bluetooth/DockEventReceiver.java @@ -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); + } } } diff --git a/src/com/android/settings/bluetooth/DockService.java b/src/com/android/settings/bluetooth/DockService.java index 4545b4e..1365b52 100644 --- a/src/com/android/settings/bluetooth/DockService.java +++ b/src/com/android/settings/bluetooth/DockService.java @@ -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) { - synchronized (mBtSynchroObject) { + 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 diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java index 24ba045..f3aaade 100644 --- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java @@ -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) { @@ -213,6 +215,11 @@ public abstract class LocalBluetoothProfileManager { } @Override + public int getPreferred(BluetoothDevice device) { + return mService.getSinkPriority(device); + } + + @Override public void setPreferred(BluetoothDevice device, boolean preferred) { if (preferred) { if (mService.getSinkPriority(device) < BluetoothA2dp.PRIORITY_ON) { @@ -332,6 +339,11 @@ public abstract class LocalBluetoothProfileManager { } @Override + public int getPreferred(BluetoothDevice device) { + return mService.getPriority(device); + } + + @Override public void setPreferred(BluetoothDevice device, boolean preferred) { if (preferred) { if (mService.getPriority(device) < BluetoothHeadset.PRIORITY_ON) { @@ -403,6 +415,11 @@ public abstract class LocalBluetoothProfileManager { } @Override + public int getPreferred(BluetoothDevice device) { + return -1; + } + + @Override public void setPreferred(BluetoothDevice device, boolean preferred) { } |