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

This commit is contained in:
Michael Chan
2010-01-14 15:17:30 -08:00
committed by Android (Google) Code Review
4 changed files with 80 additions and 20 deletions

View File

@@ -492,6 +492,7 @@
android:name=".bluetooth.DockEventReceiver"> android:name=".bluetooth.DockEventReceiver">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.DOCK_EVENT" /> <action android:name="android.intent.action.DOCK_EVENT" />
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
</receiver> </receiver>

View File

@@ -27,7 +27,7 @@ import android.util.Log;
public class DockEventReceiver extends BroadcastReceiver { public class DockEventReceiver extends BroadcastReceiver {
private static final boolean DEBUG = false; private static final boolean DEBUG = DockService.DEBUG;
private static final String TAG = "DockEventReceiver"; private static final String TAG = "DockEventReceiver";
@@ -74,6 +74,13 @@ public class DockEventReceiver extends BroadcastReceiver {
if (DEBUG) Log.e(TAG, "Unknown state"); if (DEBUG) Log.e(TAG, "Unknown state");
break; 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);
}
} }
} }

View File

@@ -19,9 +19,10 @@ package com.android.settings.bluetooth;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Notification; import android.app.Notification;
import android.app.Service; import android.app.Service;
import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver; import android.bluetooth.BluetoothHeadset;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@@ -41,13 +42,15 @@ import android.widget.CompoundButton;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile; import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
import java.util.List;
public class DockService extends Service implements AlertDialog.OnMultiChoiceClickListener, public class DockService extends Service implements AlertDialog.OnMultiChoiceClickListener,
DialogInterface.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
CompoundButton.OnCheckedChangeListener { CompoundButton.OnCheckedChangeListener {
private static final String TAG = "DockService"; 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 // Time allowed for the device to be undocked and redocked without severing
// the bluetooth connection // the bluetooth connection
@@ -87,7 +90,6 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
private BluetoothDevice mPendingDevice; private BluetoothDevice mPendingDevice;
private int mPendingStartId; private int mPendingStartId;
private boolean mRegistered;
private Object mBtSynchroObject = new Object(); private Object mBtSynchroObject = new Object();
@Override @Override
@@ -111,10 +113,6 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
mDialog.dismiss(); mDialog.dismiss();
mDialog = null; mDialog = null;
} }
if (mRegistered) {
unregisterReceiver(mReceiver);
mRegistered = false;
}
mServiceLooper.quit(); mServiceLooper.quit();
} }
@@ -138,6 +136,12 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
return START_NOT_STICKY; 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); Message msg = parseIntent(intent);
if (msg == null) { if (msg == null) {
// Bad intent // Bad intent
@@ -407,12 +411,11 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
return items; return items;
} }
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void handleBtStateChange(Intent intent) {
@Override int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
public void onReceive(Context context, Intent intent) { if (btState == BluetoothAdapter.STATE_ON) {
int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); synchronized (mBtSynchroObject) {
if (state == BluetoothAdapter.STATE_ON && mPendingDevice != null) { if (mPendingDevice != null) {
synchronized (mBtSynchroObject) {
if (mPendingDevice.equals(mDevice)) { if (mPendingDevice.equals(mDevice)) {
if(DEBUG) Log.d(TAG, "applying settings"); if(DEBUG) Log.d(TAG, "applying settings");
applyBtSettings(mPendingDevice, mPendingStartId); applyBtSettings(mPendingDevice, mPendingStartId);
@@ -423,10 +426,46 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
mPendingDevice = null; mPendingDevice = null;
DockEventReceiver.finishStartingService(mContext, mPendingStartId); 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) { private synchronized void applyBtSettings(final BluetoothDevice device, int startId) {
if (device == null || mProfiles == null || mCheckedItems == null) 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)) { if (mPendingDevice != null && mPendingDevice.equals(mDevice)) {
return; return;
} }
if (!mRegistered) {
registerReceiver(mReceiver, new IntentFilter(
BluetoothAdapter.ACTION_STATE_CHANGED));
}
mPendingDevice = device; mPendingDevice = device;
mRegistered = true;
mPendingStartId = startId; mPendingStartId = startId;
if (btState != BluetoothAdapter.STATE_TURNING_ON) { if (btState != BluetoothAdapter.STATE_TURNING_ON) {
// BT is off. Enable it // BT is off. Enable it

View File

@@ -136,6 +136,8 @@ public abstract class LocalBluetoothProfileManager {
public abstract boolean isPreferred(BluetoothDevice device); public abstract boolean isPreferred(BluetoothDevice device);
public abstract int getPreferred(BluetoothDevice device);
public abstract void setPreferred(BluetoothDevice device, boolean preferred); public abstract void setPreferred(BluetoothDevice device, boolean preferred);
public boolean isConnected(BluetoothDevice device) { public boolean isConnected(BluetoothDevice device) {
@@ -212,6 +214,11 @@ public abstract class LocalBluetoothProfileManager {
return mService.getSinkPriority(device) > BluetoothA2dp.PRIORITY_OFF; return mService.getSinkPriority(device) > BluetoothA2dp.PRIORITY_OFF;
} }
@Override
public int getPreferred(BluetoothDevice device) {
return mService.getSinkPriority(device);
}
@Override @Override
public void setPreferred(BluetoothDevice device, boolean preferred) { public void setPreferred(BluetoothDevice device, boolean preferred) {
if (preferred) { if (preferred) {
@@ -331,6 +338,11 @@ public abstract class LocalBluetoothProfileManager {
return mService.getPriority(device) > BluetoothHeadset.PRIORITY_OFF; return mService.getPriority(device) > BluetoothHeadset.PRIORITY_OFF;
} }
@Override
public int getPreferred(BluetoothDevice device) {
return mService.getPriority(device);
}
@Override @Override
public void setPreferred(BluetoothDevice device, boolean preferred) { public void setPreferred(BluetoothDevice device, boolean preferred) {
if (preferred) { if (preferred) {
@@ -402,6 +414,11 @@ public abstract class LocalBluetoothProfileManager {
return false; return false;
} }
@Override
public int getPreferred(BluetoothDevice device) {
return -1;
}
@Override @Override
public void setPreferred(BluetoothDevice device, boolean preferred) { public void setPreferred(BluetoothDevice device, boolean preferred) {
} }