Merge "Fix GoogleSettings check in PrivacySettings"
diff --git a/res/drawable-hdpi/ic_settings_dock.png b/res/drawable-hdpi/ic_settings_dock.png
new file mode 100644
index 0000000..ee594be
--- /dev/null
+++ b/res/drawable-hdpi/ic_settings_dock.png
Binary files differ
diff --git a/res/drawable-mdpi/ic_settings_dock.png b/res/drawable-mdpi/ic_settings_dock.png
new file mode 100644
index 0000000..fb5d576
--- /dev/null
+++ b/res/drawable-mdpi/ic_settings_dock.png
Binary files differ
diff --git a/res/xml/settings.xml b/res/xml/settings.xml
index 0fe502e..22f2523 100644
--- a/res/xml/settings.xml
+++ b/res/xml/settings.xml
@@ -137,6 +137,7 @@
 
         <com.android.settings.IconPreferenceScreen
             android:key="dock_settings"
+            settings:icon="@drawable/ic_settings_dock"
             android:title="@string/dock_settings">
             <intent
                 android:action="android.intent.action.MAIN"
diff --git a/src/com/android/settings/bluetooth/DockEventReceiver.java b/src/com/android/settings/bluetooth/DockEventReceiver.java
index 3324be4..2d634b2 100644
--- a/src/com/android/settings/bluetooth/DockEventReceiver.java
+++ b/src/com/android/settings/bluetooth/DockEventReceiver.java
@@ -47,7 +47,8 @@
         if (intent == null)
             return;
 
-        int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, EXTRA_INVALID);
+        int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, intent.getIntExtra(
+                BluetoothAdapter.EXTRA_STATE, EXTRA_INVALID));
         BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
 
         if (DEBUG) {
@@ -76,7 +77,7 @@
             }
         } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
             int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
-            if (btState == BluetoothAdapter.STATE_ON) {
+            if (btState != BluetoothAdapter.STATE_TURNING_ON) {
                 Intent i = new Intent(intent);
                 i.setClass(context, DockService.class);
                 beginStartingService(context, i);
diff --git a/src/com/android/settings/bluetooth/DockService.java b/src/com/android/settings/bluetooth/DockService.java
index 1365b52..db5bcb8 100644
--- a/src/com/android/settings/bluetooth/DockService.java
+++ b/src/com/android/settings/bluetooth/DockService.java
@@ -16,6 +16,9 @@
 
 package com.android.settings.bluetooth;
 
+import com.android.settings.R;
+import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
+
 import android.app.AlertDialog;
 import android.app.Notification;
 import android.app.Service;
@@ -27,6 +30,7 @@
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.SharedPreferences;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.IBinder;
@@ -39,10 +43,8 @@
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
 
-import com.android.settings.R;
-import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
-
 import java.util.List;
+import java.util.Set;
 
 public class DockService extends Service implements AlertDialog.OnMultiChoiceClickListener,
         DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
@@ -56,6 +58,10 @@
     // the bluetooth connection
     private static final long UNDOCKED_GRACE_PERIOD = 1000;
 
+    // Time allowed for the device to be undocked and redocked without turning
+    // off Bluetooth
+    private static final long DISABLE_BT_GRACE_PERIOD = 2000;
+
     // Msg for user wanting the UI to setup the dock
     private static final int MSG_TYPE_SHOW_UI = 111;
 
@@ -69,6 +75,20 @@
     // since MSG_TYPE_UNDOCKED_TEMPORARY
     private static final int MSG_TYPE_UNDOCKED_PERMANENT = 444;
 
+    // Msg for disabling bt after DISABLE_BT_GRACE_PERIOD millis since
+    // MSG_TYPE_UNDOCKED_PERMANENT
+    private static final int MSG_TYPE_DISABLE_BT = 555;
+
+    private static final String SHARED_PREFERENCES_NAME = "dock_settings";
+
+    private static final String SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED =
+        "disable_bt_when_undock";
+
+    private static final String SHARED_PREFERENCES_KEY_DISABLE_BT =
+        "disable_bt";
+
+    private static final int INVALID_STARTID = -100;
+
     // Created in OnCreate()
     private volatile Looper mServiceLooper;
     private volatile ServiceHandler mServiceHandler;
@@ -89,8 +109,8 @@
     // Set while BT is being enabled.
     private BluetoothDevice mPendingDevice;
     private int mPendingStartId;
-
-    private Object mBtSynchroObject = new Object();
+    private int mPendingTurnOnStartId = INVALID_STARTID;
+    private int mPendingTurnOffStartId = INVALID_STARTID;
 
     @Override
     public void onCreate() {
@@ -137,8 +157,7 @@
         }
 
         if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
-            handleBtStateChange(intent);
-            DockEventReceiver.finishStartingService(this, startId);
+            handleBtStateChange(intent, startId);
             return START_NOT_STICKY;
         }
 
@@ -172,10 +191,14 @@
         int msgType = msg.what;
         int state = msg.arg1;
         int startId = msg.arg2;
-        BluetoothDevice device = (BluetoothDevice) msg.obj;
+        boolean deferFinishCall = false;
+        BluetoothDevice device = null;
+        if (msg.obj != null) {
+            device = (BluetoothDevice) msg.obj;
+        }
 
         if(DEBUG) Log.d(TAG, "processMessage: " + msgType + " state: " + state + " device = "
-                + (msg.obj == null ? "null" : device.toString()));
+                + (device == null ? "null" : device.toString()));
 
         switch (msgType) {
             case MSG_TYPE_SHOW_UI:
@@ -199,6 +222,8 @@
                 }
 
                 mServiceHandler.removeMessages(MSG_TYPE_UNDOCKED_PERMANENT);
+                mServiceHandler.removeMessages(MSG_TYPE_DISABLE_BT);
+                removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT);
 
                 if (!device.equals(mDevice)) {
                     if (mDevice != null) {
@@ -220,6 +245,26 @@
             case MSG_TYPE_UNDOCKED_PERMANENT:
                 // Grace period passed. Disconnect.
                 handleUndocked(mContext, mBtManager, device);
+
+                if (DEBUG) {
+                    Log.d(TAG, "DISABLE_BT_WHEN_UNDOCKED = "
+                            + getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED));
+                }
+
+                if (getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED)) {
+                    // BT was disabled when we first docked
+                    if (!hasOtherConnectedDevices(device)) {
+                        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);
+                        deferFinishCall = true;
+                    } else {
+                        // Don't disable BT if something is connected
+                        removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
+                    }
+                }
                 break;
 
             case MSG_TYPE_UNDOCKED_TEMPORARY:
@@ -228,15 +273,49 @@
                         startId, device);
                 mServiceHandler.sendMessageDelayed(newMsg, UNDOCKED_GRACE_PERIOD);
                 break;
+
+            case MSG_TYPE_DISABLE_BT:
+                if(DEBUG) Log.d(TAG, "BT DISABLE");
+                if (mBtManager.getBluetoothAdapter().disable()) {
+                    removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
+                } else {
+                    // disable() returned an error. Persist a flag to disable BT later
+                    setSetting(SHARED_PREFERENCES_KEY_DISABLE_BT, true);
+                    mPendingTurnOffStartId = startId;
+                    deferFinishCall = true;
+                    if(DEBUG) Log.d(TAG, "disable failed. try again later " + startId);
+                }
+                break;
         }
 
-        if (mDialog == null && mPendingDevice == null && msgType != MSG_TYPE_UNDOCKED_TEMPORARY) {
+        if (mDialog == null && mPendingDevice == null && msgType != MSG_TYPE_UNDOCKED_TEMPORARY
+                && !deferFinishCall) {
             // NOTE: We MUST not call stopSelf() directly, since we need to
             // make sure the wake lock acquired by the Receiver is released.
             DockEventReceiver.finishStartingService(DockService.this, startId);
         }
     }
 
+    public synchronized boolean hasOtherConnectedDevices(BluetoothDevice dock) {
+        List<CachedBluetoothDevice> cachedDevices = mBtManager.getCachedDeviceManager()
+                .getCachedDevicesCopy();
+        Set<BluetoothDevice> btDevices = mBtManager.getBluetoothAdapter().getBondedDevices();
+        if (btDevices == null || cachedDevices == null || btDevices.size() == 0) {
+            return false;
+        }
+        if(DEBUG) Log.d(TAG, "btDevices = " + btDevices.size());
+        if(DEBUG) Log.d(TAG, "cachedDevices = " + cachedDevices.size());
+
+        for (CachedBluetoothDevice device : cachedDevices) {
+            BluetoothDevice btDevice = device.getDevice();
+            if (!btDevice.equals(dock) && btDevices.contains(btDevice) && device.isConnected()) {
+                if(DEBUG) Log.d(TAG, "connected device = " + device.getName());
+                return true;
+            }
+        }
+        return false;
+    }
+
     private Message parseIntent(Intent intent) {
         BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
         int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, -1234);
@@ -411,10 +490,11 @@
         return items;
     }
 
-    public void handleBtStateChange(Intent intent) {
+    private void handleBtStateChange(Intent intent, int startId) {
         int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
-        if (btState == BluetoothAdapter.STATE_ON) {
-            synchronized (mBtSynchroObject) {
+        synchronized (this) {
+            if(DEBUG) Log.d(TAG, "BtState = " + btState + " mPendingDevice = " + mPendingDevice);
+            if (btState == BluetoothAdapter.STATE_ON) {
                 if (mPendingDevice != null) {
                     if (mPendingDevice.equals(mDevice)) {
                         if(DEBUG) Log.d(TAG, "applying settings");
@@ -427,6 +507,10 @@
                     mPendingDevice = null;
                     DockEventReceiver.finishStartingService(mContext, mPendingStartId);
                 } else {
+                    if (DEBUG) {
+                        Log.d(TAG, "A DISABLE_BT_WHEN_UNDOCKED = "
+                                + getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED));
+                    }
                     // Reconnect if docked and bluetooth was enabled by user.
                     Intent i = registerReceiver(null, new IntentFilter(Intent.ACTION_DOCK_EVENT));
                     if (i != null) {
@@ -438,9 +522,45 @@
                             if (device != null) {
                                 connectIfEnabled(device);
                             }
+                        } else if (getSetting(SHARED_PREFERENCES_KEY_DISABLE_BT)
+                                && mBtManager.getBluetoothAdapter().disable()) {
+                            mPendingTurnOffStartId = startId;
+                            removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT);
+                            return;
                         }
                     }
                 }
+
+                if (mPendingTurnOnStartId != INVALID_STARTID) {
+                    DockEventReceiver.finishStartingService(this, mPendingTurnOnStartId);
+                    mPendingTurnOnStartId = INVALID_STARTID;
+                }
+
+                DockEventReceiver.finishStartingService(this, startId);
+            } else if (btState == BluetoothAdapter.STATE_TURNING_OFF) {
+                // Remove the flag to disable BT if someone is turning off bt.
+                // The rational is that:
+                // a) if BT is off at undock time, no work needs to be done
+                // b) if BT is on at undock time, the user wants it on.
+                removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED);
+                DockEventReceiver.finishStartingService(this, startId);
+            } else if (btState == BluetoothAdapter.STATE_OFF) {
+                // Bluetooth was turning off as we were trying to turn it on.
+                // Let's try again
+                if(DEBUG) Log.d(TAG, "Bluetooth = OFF mPendingDevice = " + mPendingDevice);
+
+                if (mPendingTurnOffStartId != INVALID_STARTID) {
+                    DockEventReceiver.finishStartingService(this, mPendingTurnOffStartId);
+                    removeSetting(SHARED_PREFERENCES_KEY_DISABLE_BT);
+                    mPendingTurnOffStartId = INVALID_STARTID;
+                }
+
+                if (mPendingDevice != null) {
+                    mBtManager.getBluetoothAdapter().enable();
+                    mPendingTurnOnStartId = startId;
+                } else {
+                    DockEventReceiver.finishStartingService(this, startId);
+                }
             }
         }
     }
@@ -472,10 +592,15 @@
             return;
 
         // Turn on BT if something is enabled
-        synchronized (mBtSynchroObject) {
+        synchronized (this) {
             for (boolean enable : mCheckedItems) {
                 if (enable) {
                     int btState = mBtManager.getBluetoothState();
+                    if(DEBUG) Log.d(TAG, "BtState = " + btState);
+                    // May have race condition as the phone comes in and out and in the dock.
+                    // Always turn on BT
+                    mBtManager.getBluetoothAdapter().enable();
+
                     switch (btState) {
                         case BluetoothAdapter.STATE_OFF:
                         case BluetoothAdapter.STATE_TURNING_OFF:
@@ -487,8 +612,7 @@
                             mPendingDevice = device;
                             mPendingStartId = startId;
                             if (btState != BluetoothAdapter.STATE_TURNING_ON) {
-                                // BT is off. Enable it
-                                mBtManager.getBluetoothAdapter().enable();
+                                setSetting(SHARED_PREFERENCES_KEY_DISABLE_BT_WHEN_UNDOCKED, true);
                             }
                             return;
                     }
@@ -551,4 +675,26 @@
         }
         return cachedBluetoothDevice;
     }
+
+    private boolean getSetting(String key) {
+        SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFERENCES_NAME,
+                Context.MODE_PRIVATE);
+        return sharedPref.getBoolean(key, false);
+    }
+
+    private void setSetting(String key, boolean disableBt) {
+        SharedPreferences.Editor editor = getSharedPreferences(SHARED_PREFERENCES_NAME,
+                Context.MODE_PRIVATE).edit();
+        editor.putBoolean(key, disableBt);
+        editor.commit();
+    }
+
+    private void removeSetting(String key) {
+        SharedPreferences sharedPref = getSharedPreferences(SHARED_PREFERENCES_NAME,
+                Context.MODE_PRIVATE);
+        SharedPreferences.Editor editor = sharedPref.edit();
+        editor.remove(key);
+        editor.commit();
+        return;
+    }
 }