am f79daa94: Merge "display audio dialog when connecting low end dock" into jb-mr1-dev

* commit 'f79daa942005608e56a4a0d7f414fedccd56e467':
  display audio dialog when connecting low end dock
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 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2012 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<CheckBox
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/dock_audio_media_enable_cb"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:text="@string/bluetooth_dock_settings_a2dp"
+    android:focusable="true"
+    android:clickable="true" />
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 @@
 
         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.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 @@
 
         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 @@
     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 @@
         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?
+                        }
+                    };
+                    return true;
+                }
             }
-
-            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?
-                    }
-                };
+        } 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 @@
                     + " 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 @@
 
         startForeground(0, new Notification());
 
-        // Device in a new dock.
-        boolean firstTime = !LocalBluetoothPreferences.hasDockAutoConnectSetting(this, device.getAddress());
-
-        CharSequence[] items = initBtSettings(device, state, firstTime);
-
         final AlertDialog.Builder ab = new AlertDialog.Builder(this);
-        ab.setTitle(getString(R.string.bluetooth_dock_settings_title));
+        View view;
+        LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
 
-        // Profiles
-        ab.setMultiChoiceItems(items, mCheckedItems, mMultiClickListener);
+        if (device != null) {
+            // Device in a new dock.
+            boolean firstTime =
+                    !LocalBluetoothPreferences.hasDockAutoConnectSetting(this, device.getAddress());
 
-        // Remember this settings
-        LayoutInflater inflater = (LayoutInflater)
-                getSystemService(LAYOUT_INFLATER_SERVICE);
+            CharSequence[] items = initBtSettings(device, state, firstTime);
+
+            ab.setTitle(getString(R.string.bluetooth_dock_settings_title));
+
+            // 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));
+
+            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;
-        View 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);
         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 @@
                     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 @@
 
     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 @@
         }
         mDevice = null;
         mPendingDevice = null;
-        CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(device);
-        cachedDevice.disconnect();
+        if (device != null) {
+            CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(device);
+            cachedDevice.disconnect();
+        }
     }
 
     private CachedBluetoothDevice getCachedBluetoothDevice(BluetoothDevice device) {