From f892bc856c6780187db62681d59ca538a173590f Mon Sep 17 00:00:00 2001 From: Eric Laurent Date: Mon, 29 Oct 2012 17:56:46 -0700 Subject: display audio dialog when connecting low end dock Display a dialog to enable the use of the dock audio connection when a low end dock is connected for the first time. Modify DockService to process docked and undocked messages even if the device indicated is null (meaning the dock is not a bluetooth dock) only for low end docks. Bug 7302106. Change-Id: I331d83a74fecf5f26b24bfc178342df414bd8153 --- res/layout/dock_audio_media_enable_dialog.xml | 24 +++ .../settings/bluetooth/DockEventReceiver.java | 7 +- .../android/settings/bluetooth/DockService.java | 189 +++++++++++++-------- 3 files changed, 146 insertions(+), 74 deletions(-) create mode 100644 res/layout/dock_audio_media_enable_dialog.xml diff --git a/res/layout/dock_audio_media_enable_dialog.xml b/res/layout/dock_audio_media_enable_dialog.xml new file mode 100644 index 0000000..37ecdfe --- /dev/null +++ b/res/layout/dock_audio_media_enable_dialog.xml @@ -0,0 +1,24 @@ + + + + diff --git a/src/com/android/settings/bluetooth/DockEventReceiver.java b/src/com/android/settings/bluetooth/DockEventReceiver.java index a3b6b3c..048b098 100644 --- a/src/com/android/settings/bluetooth/DockEventReceiver.java +++ b/src/com/android/settings/bluetooth/DockEventReceiver.java @@ -59,8 +59,11 @@ public final class DockEventReceiver extends BroadcastReceiver { if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction()) || ACTION_DOCK_SHOW_UI.endsWith(intent.getAction())) { - if (device == null) { - if (DEBUG) Log.d(TAG, "Device is missing"); + if ((device == null) && (ACTION_DOCK_SHOW_UI.endsWith(intent.getAction()) || + ((state != Intent.EXTRA_DOCK_STATE_UNDOCKED) && + (state != Intent.EXTRA_DOCK_STATE_LE_DESK)))) { + if (DEBUG) Log.d(TAG, + "Wrong state: "+state+" or intent: "+intent.toString()+" with null device"); return; } diff --git a/src/com/android/settings/bluetooth/DockService.java b/src/com/android/settings/bluetooth/DockService.java index 943c3f9..ab0e7c7 100644 --- a/src/com/android/settings/bluetooth/DockService.java +++ b/src/com/android/settings/bluetooth/DockService.java @@ -36,6 +36,7 @@ import android.os.HandlerThread; import android.os.IBinder; import android.os.Looper; import android.os.Message; +import android.provider.Settings; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -267,7 +268,9 @@ public final class DockService extends Service implements ServiceListener { switch (msgType) { case MSG_TYPE_SHOW_UI: - createDialog(device, state, startId); + if (device != null) { + createDialog(device, state, startId); + } break; case MSG_TYPE_DOCKED: @@ -325,28 +328,30 @@ public final class DockService extends Service implements ServiceListener { private boolean msgTypeUndockedPermanent(BluetoothDevice device, int startId) { // Grace period passed. Disconnect. handleUndocked(device); - final SharedPreferences prefs = getPrefs(); + if (device != null) { + final SharedPreferences prefs = getPrefs(); - if (DEBUG) { - Log.d(TAG, "DISABLE_BT_WHEN_UNDOCKED = " - + prefs.getBoolean(KEY_DISABLE_BT_WHEN_UNDOCKED, false)); - } + if (DEBUG) { + Log.d(TAG, "DISABLE_BT_WHEN_UNDOCKED = " + + prefs.getBoolean(KEY_DISABLE_BT_WHEN_UNDOCKED, false)); + } - if (prefs.getBoolean(KEY_DISABLE_BT_WHEN_UNDOCKED, false)) { - if (hasOtherConnectedDevices(device)) { - // Don't disable BT if something is connected - prefs.edit().remove(KEY_DISABLE_BT_WHEN_UNDOCKED).apply(); - } else { - // BT was disabled when we first docked - if (DEBUG) { - Log.d(TAG, "QUEUED BT DISABLE"); + if (prefs.getBoolean(KEY_DISABLE_BT_WHEN_UNDOCKED, false)) { + if (hasOtherConnectedDevices(device)) { + // Don't disable BT if something is connected + prefs.edit().remove(KEY_DISABLE_BT_WHEN_UNDOCKED).apply(); + } else { + // BT was disabled when we first docked + if (DEBUG) { + Log.d(TAG, "QUEUED BT DISABLE"); + } + // Queue a delayed msg to disable BT + Message newMsg = mServiceHandler.obtainMessage( + MSG_TYPE_DISABLE_BT, 0, startId, null); + mServiceHandler.sendMessageDelayed(newMsg, + DISABLE_BT_GRACE_PERIOD); + return true; } - // Queue a delayed msg to disable BT - Message newMsg = mServiceHandler.obtainMessage( - MSG_TYPE_DISABLE_BT, 0, startId, null); - mServiceHandler.sendMessageDelayed(newMsg, - DISABLE_BT_GRACE_PERIOD); - return true; } } return false; @@ -367,29 +372,41 @@ public final class DockService extends Service implements ServiceListener { mServiceHandler.removeMessages(MSG_TYPE_DISABLE_BT); getPrefs().edit().remove(KEY_DISABLE_BT).apply(); - if (device != null && !device.equals(mDevice)) { - if (mDevice != null) { - // Not expected. Cleanup/undock existing - handleUndocked(mDevice); - } + if (device != null) { + if (!device.equals(mDevice)) { + if (mDevice != null) { + // Not expected. Cleanup/undock existing + handleUndocked(mDevice); + } - mDevice = device; - - // Register first in case LocalBluetoothProfileManager - // becomes ready after isManagerReady is called and it - // would be too late to register a service listener. - mProfileManager.addServiceListener(this); - if (mProfileManager.isManagerReady()) { - handleDocked(device, state, startId); - // Not needed after all - mProfileManager.removeServiceListener(this); - } else { - final BluetoothDevice d = device; - mRunnable = new Runnable() { - public void run() { - handleDocked(d, state, startId); // FIXME: WTF runnable here? - } - }; + mDevice = device; + + // Register first in case LocalBluetoothProfileManager + // becomes ready after isManagerReady is called and it + // would be too late to register a service listener. + mProfileManager.addServiceListener(this); + if (mProfileManager.isManagerReady()) { + handleDocked(device, state, startId); + // Not needed after all + mProfileManager.removeServiceListener(this); + } else { + final BluetoothDevice d = device; + mRunnable = new Runnable() { + public void run() { + handleDocked(d, state, startId); // FIXME: WTF runnable here? + } + }; + return true; + } + } + } else { + // display dialog to enable dock for media audio only in the case of low end docks and + // if not already selected by user + int dockAudioMediaEnabled = Settings.Global.getInt(getContentResolver(), + Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, -1); + if (dockAudioMediaEnabled == -1 && + state == Intent.EXTRA_DOCK_STATE_LE_DESK) { + handleDocked(null, state, startId); return true; } } @@ -427,21 +444,25 @@ public final class DockService extends Service implements ServiceListener { + " Device: " + (device == null ? "null" : device.getAliasName())); } - if (device == null) { - Log.w(TAG, "device is null"); - return null; - } - int msgType; switch (state) { case Intent.EXTRA_DOCK_STATE_UNDOCKED: msgType = MSG_TYPE_UNDOCKED_TEMPORARY; break; case Intent.EXTRA_DOCK_STATE_DESK: - case Intent.EXTRA_DOCK_STATE_LE_DESK: case Intent.EXTRA_DOCK_STATE_HE_DESK: case Intent.EXTRA_DOCK_STATE_CAR: + if (device == null) { + Log.w(TAG, "device is null"); + return null; + } + /// Fall Through /// + case Intent.EXTRA_DOCK_STATE_LE_DESK: if (DockEventReceiver.ACTION_DOCK_SHOW_UI.equals(intent.getAction())) { + if (device == null) { + Log.w(TAG, "device is null"); + return null; + } msgType = MSG_TYPE_SHOW_UI; } else { msgType = MSG_TYPE_DOCKED; @@ -474,35 +495,53 @@ public final class DockService extends Service implements ServiceListener { startForeground(0, new Notification()); - // Device in a new dock. - boolean firstTime = !LocalBluetoothPreferences.hasDockAutoConnectSetting(this, device.getAddress()); + final AlertDialog.Builder ab = new AlertDialog.Builder(this); + View view; + LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); - CharSequence[] items = initBtSettings(device, state, firstTime); + if (device != null) { + // Device in a new dock. + boolean firstTime = + !LocalBluetoothPreferences.hasDockAutoConnectSetting(this, device.getAddress()); - final AlertDialog.Builder ab = new AlertDialog.Builder(this); - ab.setTitle(getString(R.string.bluetooth_dock_settings_title)); + CharSequence[] items = initBtSettings(device, state, firstTime); - // Profiles - ab.setMultiChoiceItems(items, mCheckedItems, mMultiClickListener); + ab.setTitle(getString(R.string.bluetooth_dock_settings_title)); - // Remember this settings - LayoutInflater inflater = (LayoutInflater) - getSystemService(LAYOUT_INFLATER_SERVICE); - float pixelScaleFactor = getResources().getDisplayMetrics().density; - View view = inflater.inflate(R.layout.remember_dock_setting, null); - CheckBox rememberCheckbox = (CheckBox) view.findViewById(R.id.remember); + // Profiles + ab.setMultiChoiceItems(items, mCheckedItems, mMultiClickListener); + + // Remember this settings + view = inflater.inflate(R.layout.remember_dock_setting, null); + CheckBox rememberCheckbox = (CheckBox) view.findViewById(R.id.remember); + + // check "Remember setting" by default if no value was saved + boolean checked = firstTime || + LocalBluetoothPreferences.getDockAutoConnectSetting(this, device.getAddress()); + rememberCheckbox.setChecked(checked); + rememberCheckbox.setOnCheckedChangeListener(mCheckedChangeListener); + if (DEBUG) { + Log.d(TAG, "Auto connect = " + + LocalBluetoothPreferences.getDockAutoConnectSetting(this, device.getAddress())); + } + } else { + ab.setTitle(getString(R.string.bluetooth_dock_settings_title)); - // check "Remember setting" by default if no value was saved - boolean checked = firstTime || LocalBluetoothPreferences.getDockAutoConnectSetting(this, device.getAddress()); - rememberCheckbox.setChecked(checked); - rememberCheckbox.setOnCheckedChangeListener(mCheckedChangeListener); + view = inflater.inflate(R.layout.dock_audio_media_enable_dialog, null); + CheckBox audioMediaCheckbox = + (CheckBox) view.findViewById(R.id.dock_audio_media_enable_cb); + + boolean checked = Settings.Global.getInt(getContentResolver(), + Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, 0) == 1; + + audioMediaCheckbox.setChecked(checked); + audioMediaCheckbox.setOnCheckedChangeListener(mCheckedChangeListener); + } + + float pixelScaleFactor = getResources().getDisplayMetrics().density; int viewSpacingLeft = (int) (14 * pixelScaleFactor); int viewSpacingRight = (int) (14 * pixelScaleFactor); ab.setView(view, viewSpacingLeft, 0 /* top */, viewSpacingRight, 0 /* bottom */); - if (DEBUG) { - Log.d(TAG, "Auto connect = " - + LocalBluetoothPreferences.getDockAutoConnectSetting(this, device.getAddress())); - } // Ok Button ab.setPositiveButton(getString(android.R.string.ok), mClickListener); @@ -536,6 +575,9 @@ public final class DockService extends Service implements ServiceListener { if (mDevice != null) { LocalBluetoothPreferences.saveDockAutoConnectSetting( DockService.this, mDevice.getAddress(), isChecked); + } else { + Settings.Global.putInt(getContentResolver(), + Settings.Global.DOCK_AUDIO_MEDIA_ENABLED, isChecked ? 1 : 0); } } }; @@ -823,7 +865,8 @@ public final class DockService extends Service implements ServiceListener { private synchronized void handleDocked(BluetoothDevice device, int state, int startId) { - if (LocalBluetoothPreferences.getDockAutoConnectSetting(this, device.getAddress())) { + if (device != null && + LocalBluetoothPreferences.getDockAutoConnectSetting(this, device.getAddress())) { // Setting == auto connect initBtSettings(device, state, false); applyBtSettings(mDevice, startId); @@ -841,8 +884,10 @@ public final class DockService extends Service implements ServiceListener { } mDevice = null; mPendingDevice = null; - CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(device); - cachedDevice.disconnect(); + if (device != null) { + CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(device); + cachedDevice.disconnect(); + } } private CachedBluetoothDevice getCachedBluetoothDevice(BluetoothDevice device) { -- cgit v1.1