am 14638566
: b/2296110 Car Dock - Enable BT if not already enabled.
Merge commit '146385663bb55d9f55ad179d3c35ccf5cac13fc8' into eclair-mr2 * commit '146385663bb55d9f55ad179d3c35ccf5cac13fc8': b/2296110 Car Dock - Enable BT if not already enabled.
This commit is contained in:
@@ -103,6 +103,7 @@ public class DockEventReceiver extends BroadcastReceiver {
|
|||||||
public static void finishStartingService(Service service, int startId) {
|
public static void finishStartingService(Service service, int startId) {
|
||||||
synchronized (mStartingServiceSync) {
|
synchronized (mStartingServiceSync) {
|
||||||
if (mStartingService != null) {
|
if (mStartingService != null) {
|
||||||
|
if (DEBUG) Log.d(TAG, "stopSelf id = "+ startId);
|
||||||
if (service.stopSelfResult(startId)) {
|
if (service.stopSelfResult(startId)) {
|
||||||
mStartingService.release();
|
mStartingService.release();
|
||||||
}
|
}
|
||||||
|
@@ -25,9 +25,11 @@ import android.app.PendingIntent;
|
|||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.HandlerThread;
|
import android.os.HandlerThread;
|
||||||
@@ -64,10 +66,13 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
|
|
||||||
// Msg for user wanting the UI to setup the dock
|
// Msg for user wanting the UI to setup the dock
|
||||||
private static final int MSG_TYPE_SHOW_UI = 111;
|
private static final int MSG_TYPE_SHOW_UI = 111;
|
||||||
|
|
||||||
// Msg for device docked event
|
// Msg for device docked event
|
||||||
private static final int MSG_TYPE_DOCKED = 222;
|
private static final int MSG_TYPE_DOCKED = 222;
|
||||||
|
|
||||||
// Msg for device undocked event
|
// Msg for device undocked event
|
||||||
private static final int MSG_TYPE_UNDOCKED_TEMPORARY = 333;
|
private static final int MSG_TYPE_UNDOCKED_TEMPORARY = 333;
|
||||||
|
|
||||||
// Msg for undocked command to be process after UNDOCKED_GRACE_PERIOD millis
|
// Msg for undocked command to be process after UNDOCKED_GRACE_PERIOD millis
|
||||||
// since MSG_TYPE_UNDOCKED_TEMPORARY
|
// since MSG_TYPE_UNDOCKED_TEMPORARY
|
||||||
private static final int MSG_TYPE_UNDOCKED_PERMANENT = 444;
|
private static final int MSG_TYPE_UNDOCKED_PERMANENT = 444;
|
||||||
@@ -89,6 +94,13 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
private boolean[] mCheckedItems;
|
private boolean[] mCheckedItems;
|
||||||
private int mStartIdAssociatedWithDialog;
|
private int mStartIdAssociatedWithDialog;
|
||||||
|
|
||||||
|
// Set while BT is being enabled.
|
||||||
|
private BluetoothDevice mPendingDevice;
|
||||||
|
private int mPendingStartId;
|
||||||
|
|
||||||
|
private boolean mRegistered;
|
||||||
|
private Object mBtSynchroObject = new Object();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
if (DEBUG) Log.d(TAG, "onCreate");
|
if (DEBUG) Log.d(TAG, "onCreate");
|
||||||
@@ -110,6 +122,10 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,7 +186,11 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
|
|
||||||
switch (msgType) {
|
switch (msgType) {
|
||||||
case MSG_TYPE_SHOW_UI:
|
case MSG_TYPE_SHOW_UI:
|
||||||
//TODO dismiss mDialog if exist? Shouldn't normally happen
|
if (mDialog != null) {
|
||||||
|
// Shouldn't normally happen
|
||||||
|
mDialog.dismiss();
|
||||||
|
mDialog = null;
|
||||||
|
}
|
||||||
mDevice = device;
|
mDevice = device;
|
||||||
createDialog(mContext, mDevice, state, startId);
|
createDialog(mContext, mDevice, state, startId);
|
||||||
break;
|
break;
|
||||||
@@ -197,7 +217,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
if (getAutoConnectSetting(mBtManager, device.getAddress())) {
|
if (getAutoConnectSetting(mBtManager, device.getAddress())) {
|
||||||
// Setting == auto connect
|
// Setting == auto connect
|
||||||
initBtSettings(mContext, device, state, false);
|
initBtSettings(mContext, device, state, false);
|
||||||
applyBtSettings();
|
applyBtSettings(mDevice, startId);
|
||||||
} else {
|
} else {
|
||||||
createDialog(mContext, mDevice, state, startId);
|
createDialog(mContext, mDevice, state, startId);
|
||||||
}
|
}
|
||||||
@@ -217,7 +237,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mDialog == null) {
|
if (mDialog == null && mPendingDevice == null) {
|
||||||
// NOTE: We MUST not call stopSelf() directly, since we need to
|
// NOTE: We MUST not call stopSelf() directly, since we need to
|
||||||
// make sure the wake lock acquired by the Receiver is released.
|
// make sure the wake lock acquired by the Receiver is released.
|
||||||
DockEventReceiver.finishStartingService(DockService.this, msg.arg1);
|
DockEventReceiver.finishStartingService(DockService.this, msg.arg1);
|
||||||
@@ -326,7 +346,9 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
public void onDismiss(DialogInterface dialog) {
|
public void onDismiss(DialogInterface dialog) {
|
||||||
// NOTE: We MUST not call stopSelf() directly, since we need to
|
// NOTE: We MUST not call stopSelf() directly, since we need to
|
||||||
// make sure the wake lock acquired by the Receiver is released.
|
// make sure the wake lock acquired by the Receiver is released.
|
||||||
|
if (mPendingDevice == null) {
|
||||||
DockEventReceiver.finishStartingService(mContext, mStartIdAssociatedWithDialog);
|
DockEventReceiver.finishStartingService(mContext, mStartIdAssociatedWithDialog);
|
||||||
|
}
|
||||||
mContext.stopForeground(true);
|
mContext.stopForeground(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,21 +359,7 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
saveAutoConnectSetting(mBtManager, mDevice.getAddress(), true);
|
saveAutoConnectSetting(mBtManager, mDevice.getAddress(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO move this to a background thread
|
applyBtSettings(mDevice, mStartIdAssociatedWithDialog);
|
||||||
switch (mBtManager.getBluetoothState()) {
|
|
||||||
case BluetoothAdapter.STATE_OFF:
|
|
||||||
case BluetoothAdapter.STATE_TURNING_OFF:
|
|
||||||
mBtManager.getBluetoothAdapter().enable();
|
|
||||||
// TODO can I call connect right away? probably not.
|
|
||||||
break;
|
|
||||||
case BluetoothAdapter.STATE_TURNING_ON:
|
|
||||||
// TODO wait? probably
|
|
||||||
break;
|
|
||||||
case BluetoothAdapter.STATE_ON:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
applyBtSettings();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -401,30 +409,82 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applyBtSettings() {
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
if (mProfiles == null) return;
|
@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) {
|
||||||
|
if (mPendingDevice.equals(mDevice)) {
|
||||||
|
if(DEBUG) Log.d(TAG, "applying settings");
|
||||||
|
applyBtSettings(mPendingDevice, mPendingStartId);
|
||||||
|
} if(DEBUG) {
|
||||||
|
Log.d(TAG, "mPendingDevice != mDevice");
|
||||||
|
}
|
||||||
|
|
||||||
|
mPendingDevice = null;
|
||||||
|
DockEventReceiver.finishStartingService(mContext, mPendingStartId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private void applyBtSettings(final BluetoothDevice device, int startId) {
|
||||||
|
if (device == null || mProfiles == null || mCheckedItems == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Turn on BT if something is enabled
|
||||||
|
synchronized (mBtSynchroObject) {
|
||||||
|
for (boolean enable : mCheckedItems) {
|
||||||
|
if (enable) {
|
||||||
|
int btState = mBtManager.getBluetoothState();
|
||||||
|
switch (btState) {
|
||||||
|
case BluetoothAdapter.STATE_OFF:
|
||||||
|
case BluetoothAdapter.STATE_TURNING_OFF:
|
||||||
|
case BluetoothAdapter.STATE_TURNING_ON:
|
||||||
|
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
|
||||||
|
mBtManager.getBluetoothAdapter().enable();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mPendingDevice = null;
|
||||||
|
|
||||||
for (int i = 0; i < mProfiles.length; i++) {
|
for (int i = 0; i < mProfiles.length; i++) {
|
||||||
LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
|
LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
|
||||||
.getProfileManager(mBtManager, mProfiles[i]);
|
.getProfileManager(mBtManager, mProfiles[i]);
|
||||||
boolean isConnected = profileManager.isConnected(mDevice);
|
boolean isConnected = profileManager.isConnected(device);
|
||||||
CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(mContext, mBtManager,
|
CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(mContext, mBtManager,
|
||||||
mDevice);
|
device);
|
||||||
|
|
||||||
if (DEBUG) Log.d(TAG, mProfiles[i].toString() + " = " + mCheckedItems[i]);
|
if (DEBUG) Log.d(TAG, mProfiles[i].toString() + " = " + mCheckedItems[i]);
|
||||||
|
|
||||||
if (mCheckedItems[i] && !isConnected) {
|
if (mCheckedItems[i] && !isConnected) {
|
||||||
// Checked but not connected
|
// Checked but not connected
|
||||||
if (DEBUG) Log.d(TAG, "Connecting ");
|
if (DEBUG) Log.d(TAG, "applyBtSettings - Connecting");
|
||||||
cachedDevice.connect(mProfiles[i]);
|
cachedDevice.connect(mProfiles[i]);
|
||||||
} else if (!mCheckedItems[i] && isConnected) {
|
} else if (!mCheckedItems[i] && isConnected) {
|
||||||
// Unchecked but connected
|
// Unchecked but connected
|
||||||
if (DEBUG) Log.d(TAG, "Disconnecting");
|
if (DEBUG) Log.d(TAG, "applyBtSettings - Disconnecting");
|
||||||
cachedDevice.disconnect(mProfiles[i]);
|
cachedDevice.disconnect(mProfiles[i]);
|
||||||
}
|
}
|
||||||
profileManager.setPreferred(mDevice, mCheckedItems[i]);
|
profileManager.setPreferred(device, mCheckedItems[i]);
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
if (mCheckedItems[i] != LocalBluetoothProfileManager.getProfileManager(
|
if (mCheckedItems[i] != profileManager.isPreferred(device)) {
|
||||||
mBtManager, Profile.HEADSET).isPreferred(mDevice)) {
|
|
||||||
Log.e(TAG, "Can't save prefered value");
|
Log.e(TAG, "Can't save prefered value");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -438,18 +498,12 @@ public class DockService extends Service implements AlertDialog.OnMultiChoiceCli
|
|||||||
mDialog = null;
|
mDialog = null;
|
||||||
}
|
}
|
||||||
mDevice = null;
|
mDevice = null;
|
||||||
|
mPendingDevice = null;
|
||||||
CachedBluetoothDevice cachedBluetoothDevice = getCachedBluetoothDevice(context,
|
CachedBluetoothDevice cachedBluetoothDevice = getCachedBluetoothDevice(context,
|
||||||
localManager, device);
|
localManager, device);
|
||||||
cachedBluetoothDevice.disconnect();
|
cachedBluetoothDevice.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleDocked(Context context, LocalBluetoothManager localManager,
|
|
||||||
BluetoothDevice device, int state) {
|
|
||||||
CachedBluetoothDevice cachedBluetoothDevice = getCachedBluetoothDevice(context,
|
|
||||||
localManager, device);
|
|
||||||
cachedBluetoothDevice.connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CachedBluetoothDevice getCachedBluetoothDevice(Context context,
|
private static CachedBluetoothDevice getCachedBluetoothDevice(Context context,
|
||||||
LocalBluetoothManager localManager, BluetoothDevice device) {
|
LocalBluetoothManager localManager, BluetoothDevice device) {
|
||||||
CachedBluetoothDeviceManager cachedDeviceManager = localManager.getCachedDeviceManager();
|
CachedBluetoothDeviceManager cachedDeviceManager = localManager.getCachedDeviceManager();
|
||||||
|
Reference in New Issue
Block a user