Merge "Don't allow monkey to play with SIM lock settings. Bug # 2358489. DO NOT MERGE" into eclair
diff --git a/src/com/android/settings/bluetooth/DockEventReceiver.java b/src/com/android/settings/bluetooth/DockEventReceiver.java
index 261ec1d..73f90e5 100644
--- a/src/com/android/settings/bluetooth/DockEventReceiver.java
+++ b/src/com/android/settings/bluetooth/DockEventReceiver.java
@@ -17,6 +17,7 @@
 package com.android.settings.bluetooth;
 
 import android.app.Service;
+import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -35,9 +36,11 @@
 
     private static final int EXTRA_INVALID = -1234;
 
-    static final Object mStartingServiceSync = new Object();
+    private static final Object mStartingServiceSync = new Object();
 
-    static PowerManager.WakeLock mStartingService;
+    private static final long WAKELOCK_TIMEOUT = 5000;
+
+    private static PowerManager.WakeLock mStartingService;
 
     @Override
     public void onReceive(Context context, Intent intent) {
@@ -84,14 +87,12 @@
                 PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                 mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                         "StartingDockService");
-                mStartingService.setReferenceCounted(false);
             }
 
-            mStartingService.acquire();
+            mStartingService.acquire(WAKELOCK_TIMEOUT);
 
             if (context.startService(intent) == null) {
                 Log.e(TAG, "Can't start DockService");
-                mStartingService.release();
             }
         }
     }
@@ -104,9 +105,7 @@
         synchronized (mStartingServiceSync) {
             if (mStartingService != null) {
                 if (DEBUG) Log.d(TAG, "stopSelf id = "+ startId);
-                if (service.stopSelfResult(startId)) {
-                    mStartingService.release();
-                }
+                service.stopSelfResult(startId);
             }
         }
     }
diff --git a/src/com/android/settings/bluetooth/DockService.java b/src/com/android/settings/bluetooth/DockService.java
index 1ad2987..4545b4e 100644
--- a/src/com/android/settings/bluetooth/DockService.java
+++ b/src/com/android/settings/bluetooth/DockService.java
@@ -16,12 +16,8 @@
 
 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.PendingIntent;
 import android.app.Service;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
@@ -42,13 +38,15 @@
 import android.widget.CheckBox;
 import android.widget.CompoundButton;
 
+import com.android.settings.R;
+import com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile;
+
 public class DockService extends Service implements AlertDialog.OnMultiChoiceClickListener,
         DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
         CompoundButton.OnCheckedChangeListener {
 
     private static final String TAG = "DockService";
 
-    // TODO clean up logs. Disable DEBUG flag for this file and receiver's too
     private static final boolean DEBUG = false;
 
     // Time allowed for the device to be undocked and redocked without severing
@@ -166,7 +164,7 @@
     }
 
     // This method gets messages from both onStartCommand and mServiceHandler/mServiceLooper
-    void processMessage(Message msg) {
+    private synchronized void processMessage(Message msg) {
         int msgType = msg.what;
         int state = msg.arg1;
         int startId = msg.arg2;
@@ -376,26 +374,32 @@
         mCheckedItems = new boolean[numOfProfiles];
         CharSequence[] items = new CharSequence[numOfProfiles];
 
-        int i = 0;
         switch (state) {
             case Intent.EXTRA_DOCK_STATE_CAR:
-                items[i] = service.getString(R.string.bluetooth_dock_settings_headset);
-                mProfiles[i] = Profile.HEADSET;
+                items[0] = service.getString(R.string.bluetooth_dock_settings_headset);
+                items[1] = service.getString(R.string.bluetooth_dock_settings_a2dp);
+                mProfiles[0] = Profile.HEADSET;
+                mProfiles[1] = Profile.A2DP;
                 if (firstTime) {
-                    mCheckedItems[i] = false;
+                    // Enable by default for car dock
+                    mCheckedItems[0] = true;
+                    mCheckedItems[1] = true;
                 } else {
-                    mCheckedItems[i] = LocalBluetoothProfileManager.getProfileManager(mBtManager,
+                    mCheckedItems[0] = LocalBluetoothProfileManager.getProfileManager(mBtManager,
                             Profile.HEADSET).isPreferred(device);
+                    mCheckedItems[1] = LocalBluetoothProfileManager.getProfileManager(mBtManager,
+                            Profile.A2DP).isPreferred(device);
                 }
-                ++i;
-                // fall through
+                break;
+
             case Intent.EXTRA_DOCK_STATE_DESK:
-                items[i] = service.getString(R.string.bluetooth_dock_settings_a2dp);
-                mProfiles[i] = Profile.A2DP;
+                items[0] = service.getString(R.string.bluetooth_dock_settings_a2dp);
+                mProfiles[0] = Profile.A2DP;
                 if (firstTime) {
-                    mCheckedItems[i] = false;
+                    // Disable by default for desk dock
+                    mCheckedItems[0] = false;
                 } else {
-                    mCheckedItems[i] = LocalBluetoothProfileManager.getProfileManager(mBtManager,
+                    mCheckedItems[0] = LocalBluetoothProfileManager.getProfileManager(mBtManager,
                             Profile.A2DP).isPreferred(device);
                 }
                 break;
@@ -424,7 +428,7 @@
         }
     };
 
-    private void applyBtSettings(final BluetoothDevice device, int startId) {
+    private synchronized void applyBtSettings(final BluetoothDevice device, int startId) {
         if (device == null || mProfiles == null || mCheckedItems == null)
             return;
 
@@ -465,14 +469,13 @@
         for (int i = 0; i < mProfiles.length; i++) {
             LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
                     .getProfileManager(mBtManager, mProfiles[i]);
-            boolean isConnected = profileManager.isConnected(device);
 
             if (DEBUG) Log.d(TAG, mProfiles[i].toString() + " = " + mCheckedItems[i]);
 
-            if (mCheckedItems[i] && !isConnected) {
+            if (mCheckedItems[i]) {
                 // Checked but not connected
                 callConnect = true;
-            } else if (!mCheckedItems[i] && isConnected) {
+            } else if (!mCheckedItems[i]) {
                 // Unchecked but connected
                 if (DEBUG) Log.d(TAG, "applyBtSettings - Disconnecting");
                 cachedDevice.disconnect(mProfiles[i]);
@@ -491,7 +494,7 @@
         }
     }
 
-    void handleUndocked(Context context, LocalBluetoothManager localManager,
+    private synchronized void handleUndocked(Context context, LocalBluetoothManager localManager,
             BluetoothDevice device) {
         if (mDialog != null) {
             mDialog.dismiss();
@@ -513,20 +516,4 @@
         }
         return cachedBluetoothDevice;
     }
-
-    // TODO Delete this method if not needed.
-    private Notification getNotification(Service service) {
-        CharSequence title = service.getString(R.string.dock_settings_title);
-
-        Notification n = new Notification(R.drawable.ic_bt_headphones_a2dp, title, System
-                .currentTimeMillis());
-
-        CharSequence contentText = service.getString(R.string.dock_settings_summary);
-        Intent notificationIntent = new Intent(service, DockEventReceiver.class);
-        notificationIntent.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
-        PendingIntent pendingIntent = PendingIntent.getActivity(service, 0, notificationIntent, 0);
-
-        n.setLatestEventInfo(service, title, contentText, pendingIntent);
-        return n;
-    }
 }