Implement individual Bluetooth profile connect/disconnect.

Allow individual Bluetooth profiles to be connected/disconnected from
the device's settings pane.

Bug: 3137982
Change-Id: I21512c5bf965fc523f3dc1e83d029b16b5e22440
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 2d96ff6..27bcf19 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -837,6 +837,17 @@
     <!-- Bluetooth settings. The summary string when a device is connected to the PAN profile. [CHAR LIMIT=35]-->
     <string name="bluetooth_summary_connected_to_pan">Tethered</string>
 
+    <!-- Bluetooth settings.  Message for disconnecting from the A2DP profile. -->
+    <string name="bluetooth_disconnect_a2dp_profile"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from media audio.</string>
+    <!-- Bluetooth settings.  Message for disconnecting from the headset profile. -->
+    <string name="bluetooth_disconnect_headset_profile" product="tablet"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from tablet audio.</string>
+    <!-- Bluetooth settings.  Message for disconnecting from the headset profile. -->
+    <string name="bluetooth_disconnect_headset_profile" product="default"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from phone audio.</string>
+    <!-- Bluetooth settings.  Message for disconnecting from the HID profile. -->
+    <string name="bluetooth_disconnect_hid_profile"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from input device.</string>
+    <!-- Bluetooth settings.  Message for disconnecting from the PAN profile. -->
+    <string name="bluetooth_disconnect_pan_profile"><xliff:g id="device_name">%1$s</xliff:g> will be disconnected from tethering.</string>
+
     <!-- Bluetooth settings.  Connection options screen.  The title of the screen. -->
     <string name="bluetooth_device_advanced_title"><xliff:g id="device_name">%1$s</xliff:g> options</string>
     <!-- Bluetooth settings. Connection options screen. Title of device actions section. [CHAR LIMIT=30] -->
diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java
index 4211814..890da3d 100644
--- a/src/com/android/settings/SettingsPreferenceFragment.java
+++ b/src/com/android/settings/SettingsPreferenceFragment.java
@@ -155,7 +155,6 @@
         mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
     }
 
-    @Override
     public Dialog onCreateDialog(int dialogId) {
         return null;
     }
diff --git a/src/com/android/settings/bluetooth/BluetoothPairingDialog.java b/src/com/android/settings/bluetooth/BluetoothPairingDialog.java
index acbb99c..4b7a0e0 100644
--- a/src/com/android/settings/bluetooth/BluetoothPairingDialog.java
+++ b/src/com/android/settings/bluetooth/BluetoothPairingDialog.java
@@ -46,8 +46,8 @@
         TextWatcher {
     private static final String TAG = "BluetoothPairingDialog";
 
-    private final int BLUETOOTH_PIN_MAX_LENGTH = 16;
-    private final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6;
+    private static final int BLUETOOTH_PIN_MAX_LENGTH = 16;
+    private static final int BLUETOOTH_PASSKEY_MAX_LENGTH = 6;
     private LocalBluetoothManager mLocalManager;
     private BluetoothDevice mDevice;
     private int mType;
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index 841c9abc..e206c3d 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -251,7 +251,6 @@
         }
     }
 
-    @Override
     public void onUserLeaveHint() {
         mLocalManager.stopScanning();
     }
diff --git a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
index 22fa005..bfd2e99 100644
--- a/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
+++ b/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
@@ -104,9 +104,9 @@
      * @param profile Profile to describe
      * @return Description of the device and profile
      */
-    private String describe(CachedBluetoothDevice cachedDevice, Profile profile) {
+    private String describe(Profile profile) {
         StringBuilder sb = new StringBuilder();
-        sb.append("Address:").append(cachedDevice.mDevice);
+        sb.append("Address:").append(mDevice);
         if (profile != null) {
             sb.append(" Profile:").append(profile.name());
         }
@@ -114,10 +114,6 @@
         return sb.toString();
     }
 
-    private String describe(Profile profile) {
-        return describe(this, profile);
-    }
-
     public void onProfileStateChanged(Profile profile, int newProfileState) {
         if (D) {
             Log.d(TAG, "onProfileStateChanged: profile " + profile.toString() +
@@ -166,20 +162,13 @@
     }
 
     public void disconnect(Profile profile) {
-        disconnectInt(this, profile);
-    }
-
-    private boolean disconnectInt(CachedBluetoothDevice cachedDevice, Profile profile) {
         LocalBluetoothProfileManager profileManager =
                 LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
-        int status = profileManager.getConnectionStatus(cachedDevice.mDevice);
-        if (profileManager.disconnect(cachedDevice.mDevice)) {
+        if (profileManager.disconnect(mDevice)) {
             if (D) {
                 Log.d(TAG, "Command sent successfully:DISCONNECT " + describe(profile));
             }
-            return true;
         }
-        return false;
     }
 
     public void askDisconnect() {
@@ -204,6 +193,57 @@
             }
         };
 
+        showDisconnectDialog(context, disconnectListener, message);
+    }
+
+    public void askDisconnect(final Profile profile) {
+        Context context = mLocalManager.getForegroundActivity();
+        if (context == null) {
+            // Cannot ask, since we need an activity context
+            disconnect(profile);
+            return;
+        }
+
+        Resources res = context.getResources();
+
+        String name = getName();
+        if (TextUtils.isEmpty(name)) {
+            name = res.getString(R.string.bluetooth_device);
+        }
+        int disconnectMessage;
+        switch (profile) {
+            case A2DP:
+                disconnectMessage = R.string.bluetooth_disconnect_a2dp_profile;
+                break;
+            case HEADSET:
+                disconnectMessage = R.string.bluetooth_disconnect_headset_profile;
+                break;
+            case HID:
+                disconnectMessage = R.string.bluetooth_disconnect_hid_profile;
+                break;
+            case PAN:
+                disconnectMessage = R.string.bluetooth_disconnect_pan_profile;
+                break;
+            default:
+                Log.w(TAG, "askDisconnect: unexpected profile " + profile);
+                disconnectMessage = R.string.bluetooth_disconnect_blank;
+                break;
+        }
+        String message = res.getString(disconnectMessage, name);
+
+        DialogInterface.OnClickListener disconnectListener =
+                new DialogInterface.OnClickListener() {
+            public void onClick(DialogInterface dialog, int which) {
+                disconnect(profile);
+            }
+        };
+
+        showDisconnectDialog(context, disconnectListener, message);
+    }
+
+    private void showDisconnectDialog(Context context,
+            DialogInterface.OnClickListener disconnectListener,
+            String message) {
         if (mDialog == null) {
             mDialog = new AlertDialog.Builder(context)
                     .setPositiveButton(android.R.string.ok, disconnectListener)
@@ -213,6 +253,10 @@
             if (mDialog.isShowing()) {
                 mDialog.dismiss();
             }
+            // use disconnectListener for the correct profile(s)
+            CharSequence okText = context.getText(android.R.string.ok);
+            mDialog.setButton(DialogInterface.BUTTON_POSITIVE,
+                    okText, disconnectListener);
         }
         mDialog.setTitle(getName());
         mDialog.setMessage(message);
@@ -311,7 +355,7 @@
             CachedBluetoothDevice cachedDevice = cachedDeviceManager.findDevice(btDevice);
 
             if (cachedDevice != null && !cachedDevice.equals(device)) {
-                disconnectInt(cachedDevice, profile);
+                cachedDevice.disconnect(profile);
             }
         }
     }
@@ -321,7 +365,7 @@
 
         LocalBluetoothProfileManager profileManager =
                 LocalBluetoothProfileManager.getProfileManager(mLocalManager, profile);
-        int status = profileManager.getConnectionStatus(cachedDevice.mDevice);
+
         if (profileManager.connect(cachedDevice.mDevice)) {
             if (D) {
                 Log.d(TAG, "Command sent successfully:CONNECT " + describe(profile));
diff --git a/src/com/android/settings/bluetooth/DeviceProfilesSettings.java b/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
index f8a66f6..d6f192a 100644
--- a/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
+++ b/src/com/android/settings/bluetooth/DeviceProfilesSettings.java
@@ -21,7 +21,6 @@
 import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
 
 import android.bluetooth.BluetoothDevice;
-import android.content.Context;
 import android.os.Bundle;
 import android.preference.CheckBoxPreference;
 import android.preference.EditTextPreference;
@@ -35,8 +34,8 @@
 import java.util.HashMap;
 
 /**
- * ConnectSpecificProfilesActivity presents the user with all of the profiles
- * for a particular device, and allows him to choose which should be connected
+ * This preference fragment presents the user with all of the profiles
+ * for a particular device, and allows them to be individually connected
  * (or disconnected).
  */
 public class DeviceProfilesSettings extends SettingsPreferenceFragment
@@ -155,9 +154,6 @@
         pref.setOrder(getProfilePreferenceIndex(profile));
         pref.setOnExpandClickListener(this);
 
-        LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
-                .getProfileManager(mManager, profile);
-
         /**
          * Gray out profile while connecting and disconnecting
          */
@@ -172,7 +168,7 @@
     public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
         String key = preference.getKey();
         if (preference instanceof BluetoothProfilePreference) {
-            onProfileClicked(preference, Profile.valueOf(key));
+            onProfileClicked(Profile.valueOf(key));
             return true;
         } else if (key.equals(KEY_UNPAIR)) {
             unpairDevice();
@@ -196,18 +192,24 @@
         return true;
     }
 
-    private void onProfileClicked(Preference preference, Profile profile) {
+    private void onProfileClicked(Profile profile) {
+        BluetoothDevice device = mCachedDevice.getDevice();
         LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
                 .getProfileManager(mManager, profile);
-        // TODO: Get the current state and flip it, updating the summary for the preference
 
-//        profileManager.setPreferred(mCachedDevice.getDevice(), checked);
-//
-//        if (checked) {
-//            mCachedDevice.connect(profile);
-//        } else {
-//            mCachedDevice.disconnect(profile);
-//        }
+        int status = profileManager.getConnectionStatus(device);
+        boolean isConnected =
+                SettingsBtStatus.isConnectionStatusConnected(status);
+
+        // TODO: only change the preference on disconnect if user confirms
+        // TODO: add method to change priority of individual profiles
+        // profileManager.setPreferred(device, !isConnected);
+
+        if (isConnected) {
+            mCachedDevice.askDisconnect(profile);
+        } else {
+            mCachedDevice.connect(profile);
+        }
     }
 
     public void onDeviceAttributesChanged(CachedBluetoothDevice cachedDevice) {
diff --git a/src/com/android/settings/bluetooth/DockService.java b/src/com/android/settings/bluetooth/DockService.java
index e817e78..0641a03 100644
--- a/src/com/android/settings/bluetooth/DockService.java
+++ b/src/com/android/settings/bluetooth/DockService.java
@@ -496,7 +496,7 @@
     private CharSequence[] initBtSettings(DockService service, BluetoothDevice device, int state,
             boolean firstTime) {
         // TODO Avoid hardcoding dock and profiles. Read from system properties
-        int numOfProfiles = 0;
+        int numOfProfiles;
         switch (state) {
             case Intent.EXTRA_DOCK_STATE_DESK:
                 numOfProfiles = 1;
@@ -729,7 +729,7 @@
             profileManager.setPreferred(device, mCheckedItems[i]);
             if (DEBUG) {
                 if (mCheckedItems[i] != profileManager.isPreferred(device)) {
-                    Log.e(TAG, "Can't save prefered value");
+                    Log.e(TAG, "Can't save preferred value");
                 }
             }
         }
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothManager.java b/src/com/android/settings/bluetooth/LocalBluetoothManager.java
index b46cc96..07d3931 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothManager.java
@@ -70,7 +70,7 @@
     // If a device was picked from the device picker or was in discoverable mode
     // in the last 60 seconds, show the pairing dialogs in foreground instead
     // of raising notifications
-    private static long GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000;
+    private static final int GRACE_PERIOD_TO_SHOW_DIALOGS_IN_FOREGROUND = 60 * 1000;
 
     public static final String SHARED_PREFERENCES_KEY_DISCOVERING_TIMESTAMP =
         "last_discovering_time";
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
index f7ad3d3..a01fdec 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
@@ -225,7 +225,7 @@
     }
 
     // TODO: int instead of enum
-    public enum Profile {
+    public static enum Profile {
         HEADSET(R.string.bluetooth_profile_headset),
         A2DP(R.string.bluetooth_profile_a2dp),
         OPP(R.string.bluetooth_profile_opp),
@@ -433,7 +433,7 @@
         public boolean disconnect(BluetoothDevice device) {
             List<BluetoothDevice> deviceList = getConnectedDevices();
             if (deviceList.size() != 0 && deviceList.get(0).equals(device)) {
-                // Downgrade prority as user is disconnecting the headset.
+                // Downgrade priority as user is disconnecting the headset.
                 if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON) {
                     mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
                 }