b/2296110 New Dialog which appears when phone is place on dock.

Change-Id: I58b216ee9b4ca9ce16210309358c0512271e128e
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index fd53b95..974950d 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -488,6 +488,22 @@
             </intent-filter>
         </activity>
 
+        <receiver
+            android:name=".bluetooth.DockAudioStateChangeReceiver"
+            >
+            <intent-filter>
+                <action android:name="android.intent.action.DOCK_EVENT" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </receiver>
+
+        <activity android:name=".bluetooth.DockSettingsActivity"
+                  android:label="@string/bluetooth_dock_settings"
+                  android:launchMode="singleTask"
+                  android:excludeFromRecents="true"
+                  android:theme="@*android:style/Theme.Dialog.Alert">
+        </activity>
+
         <activity android:name=".bluetooth.RequestPermissionActivity"
                   android:label="@string/bluetooth_permission_request"
                   android:excludeFromRecents="true"
diff --git a/res/layout/remember_dock_setting.xml b/res/layout/remember_dock_setting.xml
new file mode 100644
index 0000000..a19ca7c
--- /dev/null
+++ b/res/layout/remember_dock_setting.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 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/remember"
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    android:text="@string/bluetooth_dock_settings_remember"
+    android:focusable="true"
+    android:clickable="true" />
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 85ca4d4..42905fe 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -643,6 +643,19 @@
     <!-- Bluetooth settings.  Connection options screen.  The summary for the OPP checkbox preference that describes how checking it will set the OPP profile as preferred. -->
     <string name="bluetooth_opp_profile_summary_use_for">Use for file transfer</string>
 
+    <!-- Bluetooth settings.  Dock Setting Title -->
+    <string name="bluetooth_dock_settings">Dock Settings</string>
+    <!-- Bluetooth settings.  Dock Setting Dialog Title -->
+    <string name="bluetooth_dock_settings_title">Use dock for audio</string>
+    <!-- Bluetooth settings.  Dock Setting Dialog - Checkbox selection 1: Use dock as speaker phone -->
+    <string name="bluetooth_dock_settings_headset">As speaker phone</string>
+    <!-- Bluetooth settings.  Dock Setting Dialog - Checkbox selection 2: Use dock for media audio -->
+    <string name="bluetooth_dock_settings_a2dp">For music and media</string>
+    <!-- Bluetooth settings.  Dock Setting Dialog - Remember setting and don't ask user again -->
+    <string name="bluetooth_dock_settings_remember">Remember settings</string>
+    <!-- Bluetooth settings.  Dock Setting Dialog - Hint for the user to chagne setting after checking "remember settings" -->
+    <string name="bluetooth_dock_settings_hint">"Change with Settings under Sound &amp; display &gt; Dock audio</string>
+
     <!-- Wi-Fi settings -->
     <!-- Used in the 2nd-level settings screen to turn on Wi-Fi -->
     <string name="wifi">Wi-Fi</string>
@@ -895,7 +908,9 @@
     <!-- Sound settings screen, setting option summary text -->
     <string name="media_volume_summary">Set volume for music and videos</string>
     <!-- Sound settings screen, dock settings -->
-    <string name="dock_settings_title">Dock settings</string>
+    <string name="dock_settings_title">Dock audio</string>
+    <!-- Sound settings screen, dock settings summary-->
+    <string name="dock_settings_summary">Audio settings for the attached dock</string>
     <!-- Sound settings screen, setting check box label -->
     <string name="dtmf_tone_enable_title">Audible touch tones</string>
     <!-- Sound settings screen, setting option summary text when check box is selected -->
diff --git a/res/xml/sound_and_display_settings.xml b/res/xml/sound_and_display_settings.xml
index 151b0fb..1ffbdbf 100644
--- a/res/xml/sound_and_display_settings.xml
+++ b/res/xml/sound_and_display_settings.xml
@@ -4,9 +4,9 @@
      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.
@@ -16,7 +16,7 @@
 
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
-        
+
     <PreferenceCategory android:key="sound_settings"
             android:title="@string/sound_settings">
 
@@ -50,12 +50,9 @@
         <PreferenceScreen
                 android:key="dock_settings"
                 android:order="4"
-                android:title="@string/dock_settings_title">
-            <intent
-                android:action="android.intent.action.MAIN"
-                android:targetPackage="com.android.settings"
-                android:targetClass="com.android.settings.bluetooth.DockSettingsActivity" />
-        </PreferenceScreen>
+                android:title="@string/dock_settings_title"
+                android:summary="@string/dock_settings_summary"
+                android:widgetLayout="@*android:layout/preference_dialog" />
 
         <com.android.settings.DefaultRingtonePreference
                 android:key="ringtone"
@@ -66,14 +63,14 @@
                 android:dependency="silent"
                 android:order="5"
                 android:ringtoneType="ringtone" />
-    
+
         <CheckBoxPreference
                 android:key="vibrate"
                 android:title="@string/vibrate_title"
                 android:summary="@string/vibrate_summary"
                 android:order="6"
                 android:persistent="false" />
-    
+
         <com.android.settings.DefaultRingtonePreference
                 android:key="notification_sound"
                 android:title="@string/notification_sound_title"
@@ -139,15 +136,15 @@
     <PreferenceCategory
             android:title="@string/display_settings">
 
-        <CheckBoxPreference 
-            android:key="accelerometer" 
-            android:title="@string/accelerometer_title" 
+        <CheckBoxPreference
+            android:key="accelerometer"
+            android:title="@string/accelerometer_title"
             android:summaryOn="@string/accelerometer_summary_on"
             android:summaryOff="@string/accelerometer_summary_off"/>
-    
-	    <ListPreference
-	        android:key="animations"
-	        android:title="@string/animations_title" 
+
+        <ListPreference
+            android:key="animations"
+            android:title="@string/animations_title"
             android:persistent="false"
             android:entries="@array/animations_entries"
             android:entryValues="@array/animations_values" />
@@ -158,7 +155,7 @@
                 android:summary="@string/brightness_summary"
                 android:dialogIcon="@drawable/ic_popup_brightness"
                 android:dialogTitle="@string/brightness" />
-            
+
         <ListPreference
                 android:key="screen_timeout"
                 android:title="@string/screen_timeout"
@@ -168,4 +165,4 @@
                 android:entryValues="@array/screen_timeout_values" />
     </PreferenceCategory>
 
-</PreferenceScreen>   
+</PreferenceScreen>
diff --git a/src/com/android/settings/SoundAndDisplaySettings.java b/src/com/android/settings/SoundAndDisplaySettings.java
index 67fc533..edcd4da 100644
--- a/src/com/android/settings/SoundAndDisplaySettings.java
+++ b/src/com/android/settings/SoundAndDisplaySettings.java
@@ -17,6 +17,9 @@
 package com.android.settings;
 
 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
+
+import com.android.settings.bluetooth.DockSettingsActivity;
+
 import android.content.BroadcastReceiver;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -104,6 +107,8 @@
 
     private PreferenceGroup mSoundSettings;
 
+    private Intent mDockIntent;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -217,6 +222,9 @@
             if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
                 // Show dock settings item
                 mSoundSettings.addPreference(mDockSettings);
+
+                // Save the intent to send to the activity
+                mDockIntent = intent;
             } else {
                 // Remove dock settings item
                 mSoundSettings.removePreference(mDockSettings);
@@ -346,6 +354,10 @@
             boolean value = mNotificationPulse.isChecked();
             Settings.System.putInt(getContentResolver(),
                     Settings.System.NOTIFICATION_LIGHT_PULSE, value ? 1 : 0);
+        } else if (preference == mDockSettings) {
+            Intent i = new Intent(mDockIntent);
+            i.setClass(this, DockSettingsActivity.class);
+            startActivity(i);
         }
 
         return true;
diff --git a/src/com/android/settings/bluetooth/DockAudioStateChangeReceiver.java b/src/com/android/settings/bluetooth/DockAudioStateChangeReceiver.java
new file mode 100644
index 0000000..d320742
--- /dev/null
+++ b/src/com/android/settings/bluetooth/DockAudioStateChangeReceiver.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2009 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.
+ */
+
+package com.android.settings.bluetooth;
+
+import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+public class DockAudioStateChangeReceiver extends BroadcastReceiver {
+
+    private static final boolean DBG = true;
+    private static final String TAG = "DockAudioStateChangeReceiver";
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        if (intent == null)
+            return;
+
+        if (DBG) {
+            Log.e(TAG, "Action:" + intent.getAction()
+                    + " State:" + intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
+                            Intent.EXTRA_DOCK_STATE_UNDOCKED));
+        }
+
+        if (Intent.ACTION_DOCK_EVENT.equals(intent.getAction())) {
+            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+            if (device == null) {
+                if (DBG) Log.e(TAG, "Device is missing");
+                return;
+            }
+
+            LocalBluetoothManager localManager = LocalBluetoothManager.getInstance(context);
+
+            int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
+                    Intent.EXTRA_DOCK_STATE_UNDOCKED);
+
+            switch (state) {
+                case Intent.EXTRA_DOCK_STATE_UNDOCKED:
+                    DockSettingsActivity.handleUndocked(context, localManager, device);
+                    break;
+                case Intent.EXTRA_DOCK_STATE_CAR:
+                case Intent.EXTRA_DOCK_STATE_DESK:
+                    if (DockSettingsActivity.getAutoConnectSetting(localManager)) {
+                        // Auto connect
+                        DockSettingsActivity.handleDocked(context, localManager, device, state);
+                    } else {
+                        // Don't auto connect. Show dialog.
+                        Intent i = new Intent(intent);
+                        i.setClass(context, DockSettingsActivity.class);
+                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                        context.startActivity(i);
+                    }
+                    break;
+                default:
+                    Log.e(TAG, "Unknown state");
+                    break;
+            }
+        }
+    }
+}
diff --git a/src/com/android/settings/bluetooth/DockSettingsActivity.java b/src/com/android/settings/bluetooth/DockSettingsActivity.java
new file mode 100644
index 0000000..f5e0055
--- /dev/null
+++ b/src/com/android/settings/bluetooth/DockSettingsActivity.java
@@ -0,0 +1,312 @@
+/*
+ * Copyright (C) 2009 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.
+ */
+
+package com.android.settings.bluetooth;
+
+import com.android.internal.app.AlertActivity;
+import com.android.internal.app.AlertController;
+import com.android.settings.R;
+import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
+
+import android.app.AlertDialog;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.bluetooth.BluetoothAdapter;
+import android.bluetooth.BluetoothDevice;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.WindowManager;
+import android.widget.CheckBox;
+import android.widget.CompoundButton;
+import android.widget.CompoundButton.OnCheckedChangeListener;
+
+/**
+ * RequestPermissionActivity asks the user whether to enable discovery. This is
+ * usually started by an application wanted to start bluetooth and or discovery
+ */
+public class DockSettingsActivity extends AlertActivity implements DialogInterface.OnClickListener,
+        AlertDialog.OnMultiChoiceClickListener, OnCheckedChangeListener {
+
+    private static final String TAG = "DockSettingsActivity";
+
+    private static final boolean DEBUG = true;
+
+    private static final String SHARED_PREFERENCES_KEY_AUTO_CONNECT_TO_DOCK = "auto_connect_to_dock";
+
+    private BluetoothDevice mDevice;
+
+    private int mState = Intent.EXTRA_DOCK_STATE_UNDOCKED;
+
+    private CachedBluetoothDevice mCachedDevice;
+
+    private LocalBluetoothManager mLocalManager;
+
+    private LocalBluetoothProfileManager mA2dpMgr;
+
+    private LocalBluetoothProfileManager mHeadsetMgr;
+
+    private LocalBluetoothProfileManager[] mProfileManagers;
+
+    private boolean[] mCheckedItems;
+
+    private CheckBox mRememberCheck;
+
+    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
+
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (!parseIntent(intent)) {
+                finish();
+                return;
+            }
+
+            if (DEBUG) Log.d(TAG, "Action: " + intent.getAction() + " State: " + mState);
+        }
+    };
+
+    private Profile[] mProfiles;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        if (!parseIntent(getIntent())) {
+            finish();
+            return;
+        }
+
+        if (mState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
+            handleUndocked(this, mLocalManager, mDevice);
+            dismiss();
+            return;
+        }
+        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
+        createDialog();
+        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);
+    }
+
+    @Override
+    protected void onNewIntent(Intent intent) {
+        if (!parseIntent(getIntent())) {
+            finish();
+            return;
+        }
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+
+        IntentFilter filter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
+        registerReceiver(mReceiver, filter);
+    }
+
+    @Override
+    protected void onPause() {
+        super.onPause();
+
+        unregisterReceiver(mReceiver);
+    }
+
+    private void createDialog() {
+        // TODO Avoid hardcoding dock and profiles. Read from system properties
+        int numOfProfiles;
+        switch (mState) {
+            case Intent.EXTRA_DOCK_STATE_CAR:
+                numOfProfiles = 2;
+                break;
+            case Intent.EXTRA_DOCK_STATE_DESK:
+                numOfProfiles = 1;
+                break;
+            default:
+                return;
+        }
+
+        CharSequence[] items = new CharSequence[numOfProfiles];
+        mCheckedItems = new boolean[numOfProfiles];
+        mProfileManagers = new LocalBluetoothProfileManager[numOfProfiles];
+        mProfiles = new Profile[numOfProfiles];
+
+        int i = 0;
+        switch (mState) {
+            case Intent.EXTRA_DOCK_STATE_CAR:
+                mProfileManagers[i] = mHeadsetMgr;
+                mProfiles[i] = Profile.HEADSET;
+                mCheckedItems[i] = mHeadsetMgr.isPreferred(mDevice);
+                items[i] = getString(R.string.bluetooth_dock_settings_headset);
+                ++i;
+                // fall through
+            case Intent.EXTRA_DOCK_STATE_DESK:
+                mProfileManagers[i] = mA2dpMgr;
+                mProfiles[i] = Profile.A2DP;
+                mCheckedItems[i] = mA2dpMgr.isPreferred(mDevice);
+                items[i] = getString(R.string.bluetooth_dock_settings_a2dp);
+                break;
+        }
+
+        final AlertController.AlertParams p = mAlertParams;
+        p.mTitle = getString(R.string.bluetooth_dock_settings_title);
+
+        // Profiles
+        p.mIsMultiChoice = true;
+        p.mItems = items;
+        p.mCheckedItems = mCheckedItems;
+        p.mOnCheckboxClickListener = this;
+
+        // Remember this settings
+        LayoutInflater inflater = (LayoutInflater) getSystemService(
+                Context.LAYOUT_INFLATER_SERVICE);
+        p.mView = inflater.inflate(R.layout.remember_dock_setting, null);
+        p.mViewSpacingSpecified = true;
+        float pixelScaleFactor = getResources().getDisplayMetrics().density;
+        p.mViewSpacingLeft = (int) (14 * pixelScaleFactor);
+        p.mViewSpacingRight = (int) (14 * pixelScaleFactor);
+        mRememberCheck = (CheckBox)p.mView.findViewById(R.id.remember);
+        if (DEBUG) Log.d(TAG, "Auto Check? = " + getAutoConnectSetting(mLocalManager));
+        mRememberCheck.setChecked(getAutoConnectSetting(mLocalManager));
+        mRememberCheck.setOnCheckedChangeListener(this);
+
+        // Ok Button
+        p.mPositiveButtonText = getString(android.R.string.ok);
+        p.mPositiveButtonListener = this;
+
+        setupAlert();
+    }
+
+    // Called when the individual items are clicked.
+    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
+        if (DEBUG) Log.d(TAG, "Item " + which + " changed to " + isChecked);
+        mCheckedItems[which] = isChecked;
+    }
+
+    // Called when the "Remember" Checkbox is clicked
+    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+        if (DEBUG) Log.d(TAG, "onCheckedChanged: Remember Settings = " + isChecked);
+        saveAutoConnectSetting(mLocalManager, isChecked);
+    }
+
+    // Called when clicked on the OK button
+    public void onClick(DialogInterface dialog, int which) {
+        if (which == DialogInterface.BUTTON_POSITIVE) {
+            switch (mLocalManager.getBluetoothState()) {
+                case BluetoothAdapter.STATE_OFF:
+                case BluetoothAdapter.STATE_TURNING_OFF:
+                    mLocalManager.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;
+            }
+
+            for(int i = 0; i < mProfileManagers.length; i++) {
+                mProfileManagers[i].setPreferred(mDevice, mCheckedItems[i]);
+
+                if (DEBUG) Log.d(TAG, mProfileManagers[i].toString() + " = " + mCheckedItems[i]);
+                boolean isConnected = mProfileManagers[i].isConnected(mDevice);
+                if (mCheckedItems[i] && !isConnected) {
+                    if (DEBUG) Log.d(TAG, "Connecting ");
+                    mCachedDevice.connect(mProfiles[i]);
+                } else if (isConnected){
+                    if (DEBUG) Log.d(TAG, "Disconnecting");
+                    mProfileManagers[i].disconnect(mDevice);
+                }
+            }
+        }
+    }
+
+    private boolean parseIntent(Intent intent) {
+        if (intent == null) {
+            return false;
+        }
+
+        mLocalManager = LocalBluetoothManager.getInstance(this);
+        if (mLocalManager == null) {
+            if (DEBUG) Log.d(TAG, "Error: there's a problem starting bluetooth");
+            return false;
+        }
+
+        mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+        if (mDevice == null) {
+            if (DEBUG) Log.d(TAG, "device == null");
+            return false;
+        }
+
+        mState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
+                Intent.EXTRA_DOCK_STATE_UNDOCKED);
+        if (mState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
+            handleUndocked(this, mLocalManager, mDevice);
+            return false;
+        }
+
+        mCachedDevice = getCachedBluetoothDevice(this, mLocalManager, mDevice);
+        mA2dpMgr = LocalBluetoothProfileManager.getProfileManager(mLocalManager, Profile.A2DP);
+        mHeadsetMgr = LocalBluetoothProfileManager.getProfileManager(mLocalManager,
+                Profile.HEADSET);
+
+        return true;
+    }
+
+    public static void handleUndocked(Context context, LocalBluetoothManager localManager,
+            BluetoothDevice device) {
+        CachedBluetoothDevice cachedBluetoothDevice = getCachedBluetoothDevice(context,
+                localManager, device);
+        cachedBluetoothDevice.disconnect();
+    }
+
+    public static void handleDocked(Context context, LocalBluetoothManager localManager,
+            BluetoothDevice device, int state) {
+        CachedBluetoothDevice cachedBluetoothDevice = getCachedBluetoothDevice(context,
+                localManager, device);
+        cachedBluetoothDevice.connect();
+    }
+
+    private static CachedBluetoothDevice getCachedBluetoothDevice(Context context,
+            LocalBluetoothManager localManager, BluetoothDevice device) {
+        CachedBluetoothDeviceManager cachedDeviceManager = localManager.getCachedDeviceManager();
+        CachedBluetoothDevice cachedBluetoothDevice = cachedDeviceManager.findDevice(device);
+        if (cachedBluetoothDevice == null) {
+            cachedBluetoothDevice = new CachedBluetoothDevice(context, device);
+        }
+        return cachedBluetoothDevice;
+    }
+
+    public static boolean hasAutoConnectSetting(LocalBluetoothManager localManager) {
+        return localManager.getSharedPreferences().contains(
+                SHARED_PREFERENCES_KEY_AUTO_CONNECT_TO_DOCK);
+    }
+
+    public static boolean getAutoConnectSetting(LocalBluetoothManager localManager) {
+        return localManager.getSharedPreferences().getBoolean(
+                SHARED_PREFERENCES_KEY_AUTO_CONNECT_TO_DOCK, false);
+    }
+
+    public static void saveAutoConnectSetting(LocalBluetoothManager localManager,
+            boolean autoConnect) {
+        SharedPreferences.Editor editor = localManager.getSharedPreferences().edit();
+        editor.putBoolean(SHARED_PREFERENCES_KEY_AUTO_CONNECT_TO_DOCK, autoConnect);
+        editor.commit();
+    }
+}
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
index da3c69f..4078131 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
@@ -185,7 +185,9 @@
         @Override
         public boolean disconnect(BluetoothDevice device) {
             // Downgrade priority as user is disconnecting the sink.
-            mService.setSinkPriority(device, BluetoothA2dp.PRIORITY_ON);
+            if (mService.getSinkPriority(device) > BluetoothA2dp.PRIORITY_ON) {
+                mService.setSinkPriority(device, BluetoothA2dp.PRIORITY_ON);
+            }
             return mService.disconnectSink(device);
         }
 
@@ -291,7 +293,9 @@
         public boolean disconnect(BluetoothDevice device) {
             if (mService.getCurrentHeadset().equals(device)) {
                 // Downgrade prority as user is disconnecting the headset.
-                mService.setPriority(device, BluetoothHeadset.PRIORITY_ON);
+                if (mService.getPriority(device) > BluetoothHeadset.PRIORITY_ON) {
+                    mService.setPriority(device, BluetoothHeadset.PRIORITY_ON);
+                }
                 return mService.disconnectHeadset();
             } else {
                 return false;