Merge "Changing TextToSpeechSettings to query for the sample text strings provided by plugins rather than using hardcoded sample text."
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 2cb7772..0f6dae3 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -265,6 +265,19 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="DockSettings"
+                android:label="@string/dock_settings_title"
+                android:clearTaskOnLaunch="true"
+                >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <action android:name="com.android.settings.DOCK_SETTINGS" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.VOICE_LAUNCH" />
+                <category android:name="com.android.settings.SHORTCUT" />
+            </intent-filter>
+        </activity>
+
         <activity android:name="DeviceInfoSettings" android:label="@string/device_info_settings"
                 >
             <intent-filter>
@@ -374,6 +387,19 @@
             </intent-filter>
         </activity>
 
+        <activity android:name="DeviceAdminSettings"
+                android:label="@string/device_admin_settings_title"
+                android:clearTaskOnLaunch="true"
+                >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <action android:name="android.app.action.ADD_DEVICE_ADMIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.VOICE_LAUNCH" />
+                <category android:name="com.android.settings.SHORTCUT" />
+            </intent-filter>
+        </activity>
+
         <activity android:name="IccLockSettings" android:label="@string/sim_lock_settings"
                 android:process="com.android.phone">
             <intent-filter>
@@ -410,31 +436,29 @@
             android:theme="@android:style/Theme.NoTitleBar">
         </activity>
 
-        <activity android:name="ChooseLockPattern" android:label="@string/lockpattern_change_lock_pattern_label">
+        <activity android:name="ChooseLockGeneric"
+                android:theme="@android:style/Theme.NoDisplay">
             <intent-filter>
-                <action android:name="android.intent.action.DEFAULT" />
+                <action android:name="android.app.action.SET_NEW_PASSWORD" />
+                <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </activity>
 
-        <activity android:name="ChooseLockPassword" android:label="@string/lockpattern_change_lock_pin_label"
+        <activity android:name="ChooseLockPattern"
+                android:label="@string/lockpattern_change_lock_pattern_label">
+        </activity>
+
+        <activity android:name="ChooseLockPassword"
+                android:label="@string/lockpattern_change_lock_pin_label"
             android:theme="@android:style/Theme.NoTitleBar">
-            <intent-filter>
-                <action android:name="android.intent.action.DEFAULT" />
-            </intent-filter>
         </activity>
 
         <activity android:name="ChooseLockPatternTutorial"
             android:label="@string/lockpattern_change_lock_pattern_label">
-            <intent-filter>
-                <action android:name="android.intent.action.DEFAULT" />
-            </intent-filter>
         </activity>
 
         <activity android:name="ChooseLockPatternExample"
             android:label="@string/lockpattern_change_lock_pattern_label">
-            <intent-filter>
-                <action android:name="android.intent.action.DEFAULT" />
-            </intent-filter>
         </activity>
 
         <activity android:name="ZoneList" android:label="@string/choose_timezone" />
@@ -534,6 +558,7 @@
             android:name=".bluetooth.DockEventReceiver">
             <intent-filter>
                 <action android:name="android.intent.action.DOCK_EVENT" />
+                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
                 <category android:name="android.intent.category.DEFAULT" />
             </intent-filter>
         </receiver>
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/layout/device_admin_item.xml b/res/layout/device_admin_item.xml
new file mode 100644
index 0000000..d17ff24
--- /dev/null
+++ b/res/layout/device_admin_item.xml
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2010, 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.
+*/
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:orientation="horizontal"
+    android:paddingRight="6dip"
+    android:paddingLeft="6dip"
+    android:gravity="fill" >
+
+    <ImageView android:id="@+id/icon"
+        android:layout_width="@android:dimen/app_icon_size"
+        android:layout_height="@android:dimen/app_icon_size"
+        android:layout_marginLeft="5dip"
+        android:layout_marginRight="11dip"
+        android:layout_gravity="center_vertical"
+        android:scaleType="fitCenter"/>
+
+    <TextView android:id="@+id/name"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:layout_gravity="center_vertical"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:textStyle="bold"
+        android:singleLine="true"
+        android:ellipsize="marquee"
+        android:layout_marginBottom="2dip" />
+</LinearLayout>
diff --git a/res/layout/device_admin_settings.xml b/res/layout/device_admin_settings.xml
new file mode 100644
index 0000000..221e45f
--- /dev/null
+++ b/res/layout/device_admin_settings.xml
@@ -0,0 +1,88 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 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.
+-->
+
+<FrameLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+    <LinearLayout
+        android:id="@+id/active_layout"
+	    android:layout_width="match_parent"
+	    android:layout_height="match_parent"
+	    android:orientation="vertical"
+	    android:visibility="gone">
+	    <TextView
+	        android:layout_width="match_parent"
+	        android:layout_height="wrap_content"
+	        android:paddingLeft="20dip"
+	        android:paddingTop="5dip"
+	        android:text="@string/active_device_admin_msg"
+	        android:gravity="center"
+	        android:textAppearance="?android:attr/textAppearanceMedium" />
+        <LinearLayout
+	        android:layout_width="match_parent"
+	        android:layout_height="wrap_content"
+	        android:orientation="horizontal">
+	        <ImageView android:id="@+id/active_icon"
+	            android:layout_width="@android:dimen/app_icon_size"
+	            android:layout_height="@android:dimen/app_icon_size"
+	            android:layout_marginLeft="5dip"
+	            android:layout_marginRight="11dip"
+	            android:layout_gravity="center_vertical"
+	            android:scaleType="fitCenter"/>
+            <TextView android:id="@+id/active_name"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_weight="1"
+                android:layout_marginBottom="2dip"
+                android:layout_gravity="center_vertical"
+                android:textAppearance="?android:attr/textAppearanceMedium"
+                android:textStyle="bold"
+                android:singleLine="true"
+                android:ellipsize="marquee" />
+        </LinearLayout>
+        <TextView android:id="@+id/active_description"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content" />
+        <Button android:id="@+id/remove_button"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android_layout_gravity="center_vertical|east"
+            android:text="@string/remove_device_admin"
+        />
+    </LinearLayout>
+    
+    <LinearLayout
+        android:id="@+id/select_layout"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical"
+        android:visibility="gone">
+        <TextView
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:paddingLeft="20dip"
+            android:paddingTop="5dip"
+            android:text="@string/select_device_admin_msg"
+            android:gravity="center"
+            android:textAppearance="?android:attr/textAppearanceMedium" />
+        <ListView android:id="@android:id/list"
+                android:layout_width="match_parent" 
+                android:layout_height="match_parent"
+                android:drawSelectorOnTop="false"
+                android:fastScrollEnabled="true" />
+    </LinearLayout>
+</FrameLayout>
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index eec1ffc..0e5c920 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -17,7 +17,7 @@
 ** limitations under the License.
 */
 -->
-<resources>
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <!-- Choices for timezone picker first level.
          These values will be used as search terms for TimeZone displayName
          strings. --> <skip />
@@ -180,6 +180,56 @@
 
     <!-- Wi-Fi settings -->
 
+    <!-- Match this with the order of NetworkInfo.DetailedState. --> <skip />
+    <!-- Wi-Fi settings. The status messages when the network is unknown. -->
+    <string-array name="wifi_status">
+        <!-- Status message of Wi-Fi when it is idle. -->
+        <item></item>
+        <!-- Status message of Wi-Fi when it is scanning. -->
+        <item>Scanning\u2026</item>
+        <!-- Status message of Wi-Fi when it is connecting. -->
+        <item>Connecting\u2026</item>
+        <!-- Status message of Wi-Fi when it is authenticating. -->
+        <item>Authenticating\u2026</item>
+        <!-- Status message of Wi-Fi when it is obtaining IP address. -->
+        <item>Obtaining IP address\u2026</item>
+        <!-- Status message of Wi-Fi when it is connected. -->
+        <item>Connected</item>
+        <!-- Status message of Wi-Fi when it is suspended. -->
+        <item>Suspended</item>
+        <!-- Status message of Wi-Fi when it is disconnecting. -->
+        <item>Disconnecting\u2026</item>
+        <!-- Status message of Wi-Fi when it is disconnected. -->
+        <item>Disconnected</item>
+        <!-- Status message of Wi-Fi when it is a failure. -->
+        <item>Unsuccessful</item>
+    </string-array>
+
+    <!-- Match this with the order of NetworkInfo.DetailedState. --> <skip />
+    <!-- Wi-Fi settings. The status messages when the network is known. -->
+    <string-array name="wifi_status_with_ssid">
+        <!-- Status message of Wi-Fi when it is idle. -->
+        <item></item>
+        <!-- Status message of Wi-Fi when it is scanning. -->
+        <item>Scanning\u2026</item>
+        <!-- Status message of Wi-Fi when it is connecting to a network. -->
+        <item>Connecting to <xliff:g id="network_name">%1$s</xliff:g>\u2026</item>
+        <!-- Status message of Wi-Fi when it is authenticating with a network. -->
+        <item>Authenticating with <xliff:g id="network_name">%1$s</xliff:g>\u2026</item>
+        <!-- Status message of Wi-Fi when it is obtaining IP address from a network. -->
+        <item>Obtaining IP address from <xliff:g id="network_name">%1$s</xliff:g>\u2026</item>
+        <!-- Status message of Wi-Fi when it is connected to a network. -->
+        <item>Connected to <xliff:g id="network_name">%1$s</xliff:g></item>
+        <!-- Status message of Wi-Fi when it is suspended. -->
+        <item>Suspended</item>
+        <!-- Status message of Wi-Fi when it is disconnecting from a network. -->
+        <item>Disconnecting from <xliff:g id="network_name">%1$s</xliff:g>\u2026</item>
+        <!-- Status message of Wi-Fi when it is disconnected. -->
+        <item>Disconnected</item>
+        <!-- Status message of Wi-Fi when it is a failure. -->
+        <item>Unsuccessful</item>
+    </string-array>
+
     <!-- Match this with code. --> <skip />
     <!-- Wi-Fi settings.  The type of security a Wi-Fi network has.  The user can choose this when he adds a manual network, or configures an existing network. -->
     <string-array name="wifi_security_entries">
diff --git a/res/values/strings.xml b/res/values/strings.xml
index c289038..86cd087 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -528,6 +528,12 @@
     <string name="pin_password_illegal_character">PIN must contain only digits 0-9</string>
     <!-- Error shown when in PASSWORD mode and user enters an invalid character -->
     <string name="pin_password_contains_non_digits">Password contains an illegal character</string>
+    <!-- In the security screen, the header title for settings related to device admins -->
+    <string name="device_admin_title">Device administration</string>
+    <!-- Title of preference to manage device admins -->
+    <string name="manage_device_admin">Select device administrators</string>
+    <!-- Summary of preference to manage device policies -->
+    <string name="manage_device_admin_summary">Add or remove device administrators</string>
 
     <!-- Bluetooth settings -->
     <!-- Bluetooth settings check box title on Main Settings screen -->
@@ -707,12 +713,14 @@
     <string name="ip_address">IP address</string>
     <!-- Label for the signal strength -->
     <string name="signal">Signal strength</string>
-    <!--Wireless controls setting screen, Wi-Fi check box summary text when turning Wi-Fi on -->
+    <!-- Summary text when turning Wi-Fi or bluetooth on -->
     <string name="wifi_starting">Turning on\u2026</string>
-    <!--Wireless controls setting screen, Wi-Fi check box summary text when turning Wi-Fi off -->
+    <!-- Summary text when turning Wi-Fi or bluetooth off -->
     <string name="wifi_stopping">Turning off\u2026</string>
-    <!-- Generic error message , probably not used-->
+    <!-- Summary text when Wi-Fi or bluetooth has error -->
     <string name="wifi_error">Error</string>
+    <!-- Toast message when Wi-Fi or bluetooth is disallowed in airplane mode -->
+    <string name="wifi_in_airplane_mode">In airplane mode</string>
     <!-- Error message when Wi-Fi can't start -->
     <string name="error_starting">Unable to start Wi-Fi</string>
     <!-- Error message when Wi-Fi can't stop -->
@@ -991,8 +999,6 @@
     <string name="media_volume_title">Media volume</string>
     <!-- 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 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 -->
@@ -1024,6 +1030,25 @@
     <!-- Sound settings screen, setting option summary text -->
     <string name="audio_record_proc_summary">Suppress background noise when speaking or recording.</string>
 
+    <!-- Dock settings title, top level -->
+    <string name="dock_settings">Dock</string>
+    <!-- Dock settings title -->
+    <string name="dock_settings_title">Dock settings</string>
+    <!-- Dock audio settings -->
+    <string name="dock_audio_settings_title">Audio</string>
+    <!-- Dock audio summary for docked to desk dock -->
+    <string name="dock_audio_summary_desk">Settings for the attached desktop dock</string>
+    <!-- Dock audio summary for docked to car dock -->
+    <string name="dock_audio_summary_car">Settings for the attached car dock</string>
+    <!-- Dock audio summary for undocked -->
+    <string name="dock_audio_summary_none">Phone not docked</string>
+    <!-- Dock audio summary for docked to unknown -->
+    <string name="dock_audio_summary_unknown">Settings for the attached dock</string>
+    <!-- Dock not found dialog title -->
+    <string name="dock_not_found_title">Dock not found</string>
+    <!-- Dock not found dialog text -->
+    <string name="dock_not_found_text">The phone must be docked to configure dock audio</string>
+    
     <!-- Acounts & Sync settings screen setting option name to go into the screen for data sync settings-->
     <string name="sync_settings">Accounts &amp; sync</string>
     <!-- Main Settings screen setting option summary text for the itme to go into the screen with data sync settings-->
@@ -2302,6 +2327,16 @@
     <!-- Dialog title for confirmation to erase backup data from server -->
     <string name="backup_erase_dialog_message">Are you sure you want to stop backing up your settings and erase all copies on Google servers?</string>
 
+    <!-- Device admin settings screen --><skip/>
+    <!-- Device admin settings activity title -->
+    <string name="device_admin_settings_title">Device administration settings</string>
+    <!-- Label for screen showing the active device policy -->
+    <string name="active_device_admin_msg">Active device administrator</string>
+    <!-- Label for button to remove the active device admin -->
+    <string name="remove_device_admin">Remove</string>
+    <!-- Label for screen showing to select device policy -->
+    <string name="select_device_admin_msg">Select device administrator</string>
+    
     <!-- Name to assign to a Network Access Point that was saved without a name -->
     <string name="untitled_apn">Untitled</string>
 </resources>
diff --git a/res/xml/dock_settings.xml b/res/xml/dock_settings.xml
new file mode 100644
index 0000000..43e9fa5
--- /dev/null
+++ b/res/xml/dock_settings.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2010 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.
+-->
+
+<PreferenceScreen
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
+    android:title="@string/dock_settings_title"
+    android:key="parent">
+
+    <PreferenceScreen
+        android:key="dock_audio"
+        android:title="@string/dock_audio_settings_title"
+        android:summary="@string/dock_settings_summary"
+        android:widgetLayout="@*android:layout/preference_dialog" />
+
+</PreferenceScreen>
\ No newline at end of file
diff --git a/res/xml/settings.xml b/res/xml/settings.xml
index 79a777d..22f2523 100644
--- a/res/xml/settings.xml
+++ b/res/xml/settings.xml
@@ -133,6 +133,18 @@
                 android:targetClass="com.android.settings.LanguageSettings" />
         </com.android.settings.IconPreferenceScreen>
 
+        <!-- Dock -->
+
+        <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"
+                android:targetPackage="com.android.settings"
+                android:targetClass="com.android.settings.DockSettings" />
+        </com.android.settings.IconPreferenceScreen>
+
         <!-- Accessibility feedback -->
 
         <com.android.settings.IconPreferenceScreen
diff --git a/res/xml/sound_and_display_settings.xml b/res/xml/sound_and_display_settings.xml
index e3e3a8e..9ac3692 100644
--- a/res/xml/sound_and_display_settings.xml
+++ b/res/xml/sound_and_display_settings.xml
@@ -47,13 +47,6 @@
                 android:order="3"
                 android:streamType="music" />
 
-        <PreferenceScreen
-                android:key="dock_settings"
-                android:order="4"
-                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"
                 android:title="@string/ringtone_title"
diff --git a/src/com/android/settings/ChooseLockGeneric.java b/src/com/android/settings/ChooseLockGeneric.java
new file mode 100644
index 0000000..a3c8d8a
--- /dev/null
+++ b/src/com/android/settings/ChooseLockGeneric.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2010 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;
+
+import com.android.internal.widget.LockPatternUtils;
+
+import android.app.Activity;
+import android.app.DevicePolicyManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+
+public class ChooseLockGeneric extends Activity {
+    private ChooseLockSettingsHelper mChooseLockSettingsHelper;
+    DevicePolicyManager mDPM;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        
+        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
+        mChooseLockSettingsHelper = new ChooseLockSettingsHelper(this);
+        
+        final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
+        
+        int mode = getIntent().getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY, -1);
+        if (mode == -1) {
+            mode = lockPatternUtils.getPasswordMode();
+        }
+        int minMode = mDPM.getPasswordMode();
+        if (mode < minMode) {
+            mode = minMode;
+        }
+        if (mode >= DevicePolicyManager.PASSWORD_MODE_NUMERIC) {
+            int minLength = mDPM.getMinimumPasswordLength();
+            if (minLength < 4) {
+                minLength = 4;
+            }
+            final int maxLength = 16;
+            Intent intent = new Intent().setClass(this, ChooseLockPassword.class);
+            intent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, mode);
+            intent.putExtra(ChooseLockPassword.PASSWORD_MIN_KEY, minLength);
+            intent.putExtra(ChooseLockPassword.PASSWORD_MAX_KEY, maxLength);
+            startActivity(intent);
+        } else {
+            boolean showTutorial = !lockPatternUtils.isPatternEverChosen();
+            Intent intent = new Intent();
+            intent.setClass(this, showTutorial
+                    ? ChooseLockPatternTutorial.class
+                    : ChooseLockPattern.class);
+            intent.putExtra("key_lock_method", "pattern");
+            startActivity(intent);
+        }
+        finish();
+    }
+}
diff --git a/src/com/android/settings/ChooseLockPassword.java b/src/com/android/settings/ChooseLockPassword.java
index 4a8b543..6f9cefd 100644
--- a/src/com/android/settings/ChooseLockPassword.java
+++ b/src/com/android/settings/ChooseLockPassword.java
@@ -78,10 +78,18 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        mLockPatternUtils = new LockPatternUtils(getContentResolver());
+        mLockPatternUtils = new LockPatternUtils(this);
         mRequestedMode = getIntent().getIntExtra("password_mode", mRequestedMode);
         mPasswordMinLength = getIntent().getIntExtra("password_min_length", mPasswordMinLength);
         mPasswordMaxLength = getIntent().getIntExtra("password_max_length", mPasswordMaxLength);
+        int minMode = mLockPatternUtils.getRequestedPasswordMode();
+        if (mRequestedMode < minMode) {
+            mRequestedMode = minMode;
+        }
+        int minLength = mLockPatternUtils.getRequestedMinimumPasswordLength();
+        if (mPasswordMinLength < minLength) {
+            mPasswordMinLength = minLength;
+        }
         initViews();
         mChooseLockSettingsHelper = new ChooseLockSettingsHelper(this);
         if (savedInstanceState == null) {
@@ -91,19 +99,22 @@
     }
 
     private void initViews() {
-        if (LockPatternUtils.MODE_PIN == mRequestedMode
-                || LockPatternUtils.MODE_PASSWORD == mRequestedMode) {
-            setContentView(R.layout.choose_lock_pin);
-            // TODO: alphanumeric layout
-            // setContentView(R.layout.choose_lock_password);
-            for (int i = 0; i < digitIds.length; i++) {
-                Button button = (Button) findViewById(digitIds[i]);
-                button.setOnClickListener(this);
-                button.setText(Integer.toString(i));
-            }
-            findViewById(R.id.ok).setOnClickListener(this);
-            findViewById(R.id.cancel).setOnClickListener(this);
+        switch(mRequestedMode) {
+            case LockPatternUtils.MODE_PIN:
+            case LockPatternUtils.MODE_PASSWORD:
+            case LockPatternUtils.MODE_PATTERN:
+                setContentView(R.layout.choose_lock_pin);
+                // TODO: alphanumeric layout
+                // setContentView(R.layout.choose_lock_password);
+                for (int i = 0; i < digitIds.length; i++) {
+                    Button button = (Button) findViewById(digitIds[i]);
+                    button.setOnClickListener(this);
+                    button.setText(Integer.toString(i));
+                }
+                break;
         }
+        findViewById(R.id.ok).setOnClickListener(this);
+        findViewById(R.id.cancel).setOnClickListener(this);
         findViewById(R.id.backspace).setOnClickListener(this);
         mPasswordTextView = (TextView) findViewById(R.id.pinDisplay);
         mHeaderText = (TextView) findViewById(R.id.headerText);
@@ -179,9 +190,7 @@
                             // TODO: move these to LockPatternUtils
                             mLockPatternUtils.setLockPatternEnabled(false);
                             mLockPatternUtils.saveLockPattern(null);
-
-
-                            mLockPatternUtils.saveLockPassword(pin);
+                            mLockPatternUtils.saveLockPassword(pin, mRequestedMode);
                             finish();
                         } else {
                             int msg = R.string.lockpassword_confirm_passwords_dont_match;
diff --git a/src/com/android/settings/ChooseLockPatternTutorial.java b/src/com/android/settings/ChooseLockPatternTutorial.java
index 6e92ca8..aa6213e 100644
--- a/src/com/android/settings/ChooseLockPatternTutorial.java
+++ b/src/com/android/settings/ChooseLockPatternTutorial.java
@@ -25,25 +25,25 @@
 
 public class ChooseLockPatternTutorial extends Activity implements View.OnClickListener {
     private static final int REQUESTCODE_EXAMPLE = 1;
-    
+
     private View mNextButton;
     private View mSkipButton;
-    
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         // Don't show the tutorial if the user has seen it before.
-        LockPatternUtils lockPatternUtils = new LockPatternUtils(getContentResolver());
+        LockPatternUtils lockPatternUtils = new LockPatternUtils(this);
         if (savedInstanceState == null && lockPatternUtils.isPatternEverChosen()) {
             Intent intent = new Intent();
-            intent.setClassName("com.android.settings", "com.android.settings.ChooseLockPattern");
+            intent.setClass(this, ChooseLockPattern.class);
             startActivity(intent);
             finish();
         } else {
             initViews();
         }
     }
-    
+
     private void initViews() {
         setContentView(R.layout.choose_lock_pattern_tutorial);
         mNextButton = findViewById(R.id.next_button);
@@ -70,6 +70,6 @@
             finish();
         }
     }
-    
+
 }
 
diff --git a/src/com/android/settings/ChooseLockSettingsHelper.java b/src/com/android/settings/ChooseLockSettingsHelper.java
index ab2f8c0..3697b28e 100644
--- a/src/com/android/settings/ChooseLockSettingsHelper.java
+++ b/src/com/android/settings/ChooseLockSettingsHelper.java
@@ -27,7 +27,7 @@
 
     public ChooseLockSettingsHelper(Activity activity) {
         mActivity = activity;
-        mLockPatternUtils = new LockPatternUtils(activity.getContentResolver());
+        mLockPatternUtils = new LockPatternUtils(activity);
     }
 
     public LockPatternUtils utils() {
diff --git a/src/com/android/settings/ConfirmLockPassword.java b/src/com/android/settings/ConfirmLockPassword.java
index 0cbc158..5308f88 100644
--- a/src/com/android/settings/ConfirmLockPassword.java
+++ b/src/com/android/settings/ConfirmLockPassword.java
@@ -39,7 +39,7 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        mLockPatternUtils = new LockPatternUtils(getContentResolver());
+        mLockPatternUtils = new LockPatternUtils(this);
         initViews();
     }
 
diff --git a/src/com/android/settings/ConfirmLockPattern.java b/src/com/android/settings/ConfirmLockPattern.java
index a91b45f..eb9a4d8 100644
--- a/src/com/android/settings/ConfirmLockPattern.java
+++ b/src/com/android/settings/ConfirmLockPattern.java
@@ -80,7 +80,7 @@
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        mLockPatternUtils = new LockPatternUtils(getContentResolver());
+        mLockPatternUtils = new LockPatternUtils(this);
 
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setContentView(R.layout.confirm_lock_pattern);
@@ -94,7 +94,7 @@
         final LinearLayoutWithDefaultTouchRecepient topLayout
                 = (LinearLayoutWithDefaultTouchRecepient) findViewById(
                 R.id.topLayout);
-        topLayout.setDefaultTouchRecepient(mLockPatternView);        
+        topLayout.setDefaultTouchRecepient(mLockPatternView);
 
         Intent intent = getIntent();
         if (intent != null) {
@@ -161,7 +161,7 @@
                 } else {
                     mFooterTextView.setText(R.string.lockpattern_need_to_unlock_footer);
                 }
-                
+
                 mLockPatternView.setEnabled(true);
                 mLockPatternView.enableInput();
                 break;
@@ -176,7 +176,7 @@
                 } else {
                     mFooterTextView.setText(R.string.lockpattern_need_to_unlock_wrong_footer);
                 }
-                
+
                 mLockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
                 mLockPatternView.setEnabled(true);
                 mLockPatternView.enableInput();
diff --git a/src/com/android/settings/DeviceAdminSettings.java b/src/com/android/settings/DeviceAdminSettings.java
new file mode 100644
index 0000000..e3d733e
--- /dev/null
+++ b/src/com/android/settings/DeviceAdminSettings.java
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2010 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;
+
+import org.xmlpull.v1.XmlPullParserException;
+
+import android.app.Activity;
+import android.app.DeviceAdmin;
+import android.app.DeviceAdminInfo;
+import android.app.DevicePolicyManager;
+import android.app.ListActivity;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ImageView;
+import android.widget.ListView;
+import android.widget.TextView;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class DeviceAdminSettings extends ListActivity {
+    static final String TAG = "DeviceAdminSettings";
+    
+    DevicePolicyManager mDPM;
+    DeviceAdminInfo mCurrentAdmin;
+    
+    View mActiveLayout;
+    ImageView mActiveIcon;
+    TextView mActiveName;
+    TextView mActiveDescription;
+    
+    View mSelectLayout;
+    ArrayList<DeviceAdminInfo> mAvailablePolicies
+            = new ArrayList<DeviceAdminInfo>();
+    
+    @Override
+    protected void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+
+        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
+        mCurrentAdmin = mDPM.getActiveAdminInfo();
+        
+        setContentView(R.layout.device_admin_settings);
+        
+        mActiveLayout = findViewById(R.id.active_layout);
+        mActiveIcon = (ImageView)findViewById(R.id.active_icon);
+        mActiveName = (TextView)findViewById(R.id.active_name);
+        mActiveDescription = (TextView)findViewById(R.id.active_description);
+        findViewById(R.id.remove_button).setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                if (mCurrentAdmin != null) {
+                    mDPM.removeActiveAdmin(mCurrentAdmin.getComponent());
+                    finish();
+                }
+            }
+        });
+        
+        mSelectLayout = findViewById(R.id.select_layout);
+        getListView().setDivider(null);
+
+        updateLayout();
+        
+        if (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN.equals(getIntent().getAction())) {
+            ComponentName cn = (ComponentName)getIntent().getParcelableExtra(
+                    DevicePolicyManager.EXTRA_DEVICE_ADMIN);
+            if (cn == null) {
+                Log.w(TAG, "No component specified in " + getIntent().getAction());
+                finish();
+                return;
+            }
+            if (cn.equals(mCurrentAdmin)) {
+                setResult(Activity.RESULT_OK);
+                finish();
+                return;
+            }
+            if (mCurrentAdmin != null) {
+                Log.w(TAG, "Admin already set, can't do " + getIntent().getAction());
+                finish();
+                return;
+            }
+            
+            try {
+                mDPM.setActiveAdmin(cn);
+                setResult(Activity.RESULT_OK);
+            } catch (RuntimeException e) {
+                Log.w(TAG, "Unable to set admin " + cn, e);
+                setResult(Activity.RESULT_CANCELED);
+            }
+            finish();
+        }
+    }
+    
+    void updateLayout() {
+        if (mCurrentAdmin != null) {
+            mActiveLayout.setVisibility(View.VISIBLE);
+            mSelectLayout.setVisibility(View.GONE);
+            mActiveIcon.setImageDrawable(mCurrentAdmin.loadIcon(getPackageManager()));
+            mActiveName.setText(mCurrentAdmin.loadLabel(getPackageManager()));
+        } else {
+            mActiveLayout.setVisibility(View.GONE);
+            mSelectLayout.setVisibility(View.VISIBLE);
+            mAvailablePolicies.clear();
+            List<ResolveInfo> avail = getPackageManager().queryBroadcastReceivers(
+                    new Intent(DeviceAdmin.ACTION_DEVICE_ADMIN_ENABLED),
+                    PackageManager.GET_META_DATA);
+            for (int i=0; i<avail.size(); i++) {
+                ResolveInfo ri = avail.get(i);
+                try {
+                    DeviceAdminInfo dpi = new DeviceAdminInfo(this, ri);
+                    mAvailablePolicies.add(dpi);
+                } catch (XmlPullParserException e) {
+                    Log.w(TAG, "Skipping " + ri.activityInfo, e);
+                } catch (IOException e) {
+                    Log.w(TAG, "Skipping " + ri.activityInfo, e);
+                }
+            }
+            getListView().setAdapter(new PolicyListAdapter());
+        }
+    }
+    
+    @Override
+    protected void onListItemClick(ListView l, View v, int position, long id) {
+        DeviceAdminInfo dpi = (DeviceAdminInfo)l.getAdapter().getItem(position);
+        mDPM.setActiveAdmin(dpi.getComponent());
+        finish();
+    }
+    
+    static class ViewHolder {
+        ImageView icon;
+        TextView name;
+    }
+    
+    class PolicyListAdapter extends BaseAdapter {
+        final LayoutInflater mInflater;
+        
+        PolicyListAdapter() {
+            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+        }
+
+        public boolean hasStableIds() {
+            return true;
+        }
+        
+        public int getCount() {
+            return mAvailablePolicies.size();
+        }
+
+        public Object getItem(int position) {
+            return mAvailablePolicies.get(position);
+        }
+
+        public long getItemId(int position) {
+            return position;
+        }
+
+        public boolean areAllItemsEnabled() {
+            return false;
+        }
+
+        public boolean isEnabled(int position) {
+            return true;
+        }
+
+        public View getView(int position, View convertView, ViewGroup parent) {
+            View v;
+            if (convertView == null) {
+                v = newView(parent);
+            } else {
+                v = convertView;
+            }
+            bindView(v, position);
+            return v;
+        }
+        
+        public View newView(ViewGroup parent) {
+            View v = mInflater.inflate(R.layout.device_admin_item, parent, false);
+            ViewHolder h = new ViewHolder();
+            h.icon = (ImageView)v.findViewById(R.id.icon);
+            h.name = (TextView)v.findViewById(R.id.name);
+            v.setTag(h);
+            return v;
+        }
+        
+        public void bindView(View view, int position) {
+            ViewHolder vh = (ViewHolder) view.getTag();
+            DeviceAdminInfo item = mAvailablePolicies.get(position);
+            vh.icon.setImageDrawable(item.loadIcon(getPackageManager()));
+            vh.name.setText(item.loadLabel(getPackageManager()));
+        }
+    }
+}
diff --git a/src/com/android/settings/DockSettings.java b/src/com/android/settings/DockSettings.java
new file mode 100644
index 0000000..fe9aeb7
--- /dev/null
+++ b/src/com/android/settings/DockSettings.java
@@ -0,0 +1,141 @@
+/*
+ * Copyright (C) 2010 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;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceScreen;
+
+import com.android.settings.bluetooth.DockEventReceiver;
+
+public class DockSettings extends PreferenceActivity {
+
+    private static final int DIALOG_NOT_DOCKED = 1;
+    private static final String KEY_AUDIO_SETTINGS = "dock_audio";
+    private Preference mAudioSettings;
+    private Intent mDockIntent;
+
+    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (intent.getAction().equals(Intent.ACTION_DOCK_EVENT)) {
+                handleDockChange(intent);
+            }
+        }
+    };
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        ContentResolver resolver = getContentResolver();
+        addPreferencesFromResource(R.xml.dock_settings);
+
+        initDockSettings();
+    }
+
+    @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 initDockSettings() {
+        mAudioSettings = findPreference(KEY_AUDIO_SETTINGS);
+        if (mAudioSettings != null) {
+            mAudioSettings.setSummary(R.string.dock_audio_summary_none);
+        }
+    }
+
+    private void handleDockChange(Intent intent) {
+        if (mAudioSettings != null) {
+            int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
+            mDockIntent = intent;
+            int resId = R.string.dock_audio_summary_unknown;
+            switch (dockState) {
+            case Intent.EXTRA_DOCK_STATE_CAR:
+                resId = R.string.dock_audio_summary_car;
+                break;
+            case Intent.EXTRA_DOCK_STATE_DESK:
+                resId = R.string.dock_audio_summary_desk;
+                break;
+            case Intent.EXTRA_DOCK_STATE_UNDOCKED:
+                resId = R.string.dock_audio_summary_none;
+            }
+            mAudioSettings.setSummary(resId);
+            if (dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
+                // remove undocked dialog if currently showing.
+                try {
+                    dismissDialog(DIALOG_NOT_DOCKED);
+                } catch (IllegalArgumentException iae) {
+                    // Maybe it was already dismissed
+                }
+            }
+        }
+    }
+
+    @Override
+    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
+        if (preference == mAudioSettings) {
+            int dockState = mDockIntent != null
+                    ? mDockIntent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0)
+                    : Intent.EXTRA_DOCK_STATE_UNDOCKED;
+            if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
+                showDialog(DIALOG_NOT_DOCKED);
+            } else {
+                Intent i = new Intent(mDockIntent);
+                i.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
+                i.setClass(this, DockEventReceiver.class);
+                sendBroadcast(i);
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public Dialog onCreateDialog(int id) {
+        if (id == DIALOG_NOT_DOCKED) {
+            return createUndockedMessage();
+        }
+        return null;
+    }
+
+    private Dialog createUndockedMessage() {
+        final AlertDialog.Builder ab = new AlertDialog.Builder(this);
+        ab.setTitle(R.string.dock_not_found_title);
+        ab.setMessage(R.string.dock_not_found_text);
+        ab.setPositiveButton(android.R.string.ok, null);
+        return ab.create();
+    }
+}
diff --git a/src/com/android/settings/MasterClear.java b/src/com/android/settings/MasterClear.java
index fd4a411..d33e1e8 100644
--- a/src/com/android/settings/MasterClear.java
+++ b/src/com/android/settings/MasterClear.java
@@ -53,7 +53,7 @@
     private View mFinalView;
     private Button mFinalButton;
 
-    /** 
+    /**
      * The user has gone through the multiple confirmation, so now we go ahead
      * and invoke the Checkin Service to reset the device to its factory-default
      * state (rebooting in the process).
@@ -65,7 +65,7 @@
                     return;
                 }
 
-                ICheckinService service = 
+                ICheckinService service =
                         ICheckinService.Stub.asInterface(ServiceManager.getService("checkin"));
                 if (service != null) {
                     try {
@@ -159,7 +159,7 @@
      * click in order to initiate a confirmation sequence.  This method is
      * called from various other points in the code to reset the activity to
      * this base state.
-     * 
+     *
      * <p>Reinflating views from resources is expensive and prevents us from
      * caching widget pointers, so we use a single-inflate pattern:  we lazy-
      * inflate each view, caching all of the widget pointers we'll need at the
@@ -184,7 +184,7 @@
         mInitialView = null;
         mFinalView = null;
         mInflater = LayoutInflater.from(this);
-        mLockUtils = new LockPatternUtils(getContentResolver());
+        mLockUtils = new LockPatternUtils(this);
 
         establishInitialState();
     }
diff --git a/src/com/android/settings/MediaFormat.java b/src/com/android/settings/MediaFormat.java
index e414569..40ae0d7 100644
--- a/src/com/android/settings/MediaFormat.java
+++ b/src/com/android/settings/MediaFormat.java
@@ -147,7 +147,7 @@
      * click in order to initiate a confirmation sequence.  This method is
      * called from various other points in the code to reset the activity to
      * this base state.
-     * 
+     *
      * <p>Reinflating views from resources is expensive and prevents us from
      * caching widget pointers, so we use a single-inflate pattern:  we lazy-
      * inflate each view, caching all of the widget pointers we'll need at the
@@ -172,7 +172,7 @@
         mInitialView = null;
         mFinalView = null;
         mInflater = LayoutInflater.from(this);
-        mLockUtils = new LockPatternUtils(getContentResolver());
+        mLockUtils = new LockPatternUtils(this);
 
         establishInitialState();
     }
diff --git a/src/com/android/settings/PrivacySettings.java b/src/com/android/settings/PrivacySettings.java
index 2edb328..611af04 100644
--- a/src/com/android/settings/PrivacySettings.java
+++ b/src/com/android/settings/PrivacySettings.java
@@ -45,7 +45,7 @@
     private static final String PREFS_USE_LOCATION = "use_location";
 
     // Vendor specific
-    private static final String GSETTINGS_PROVIDER = "com.google.android.providers.settings";
+    private static final String GSETTINGS_PROVIDER = "com.google.settings";
     private static final String LOCATION_CATEGORY = "location_category";
     private static final String SETTINGS_CATEGORY = "settings_category";
     private static final String USE_LOCATION = "use_location";
@@ -69,11 +69,7 @@
         mBackup = (CheckBoxPreference) getPreferenceScreen().findPreference(BACKUP_SETTINGS);
 
         // Vendor specific
-        try {
-            if (mUseLocation != null) {
-                getPackageManager().getPackageInfo(GSETTINGS_PROVIDER, 0);
-            }
-        } catch (NameNotFoundException nnfe) {
+        if (getPackageManager().resolveContentProvider(GSETTINGS_PROVIDER, 0) == null) {
             getPreferenceScreen().removePreference(findPreference(LOCATION_CATEGORY));
             getPreferenceScreen().removePreference(findPreference(SETTINGS_CATEGORY));
         }
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index fb284fd..5c44f27 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -22,9 +22,11 @@
 
 import android.app.Activity;
 import android.app.AlertDialog;
+import android.app.DevicePolicyManager;
 import android.app.Dialog;
 import android.content.ContentQueryMap;
 import android.content.ContentResolver;
+import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.database.Cursor;
@@ -89,6 +91,10 @@
     private static final String LOCATION_GPS = "location_gps";
     private static final String ASSISTED_GPS = "assisted_gps";
 
+    // Default password lengths if device policy isn't in effect. Ignored otherwise.
+    private static final int PASSWORD_MIN_LENGTH = 4;
+    private static final int PASSWORD_MAX_LENGTH = 16;
+
     // Credential storage
     private CredentialStorage mCredentialStorage = new CredentialStorage();
 
@@ -99,6 +105,8 @@
     private CheckBoxPreference mGps;
     private CheckBoxPreference mAssistedGps;
 
+    DevicePolicyManager mDPM;
+    
     // These provide support for receiving notification when Location Manager settings change.
     // This is necessary because the Network Location Provider can change settings
     // if the user does not confirm enabling the provider.
@@ -116,6 +124,8 @@
         super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.security_settings);
 
+        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
+        
         mChooseLockSettingsHelper = new ChooseLockSettingsHelper(this);
 
         createPreferenceHierarchy();
@@ -184,6 +194,19 @@
         showPassword.setPersistent(false);
         passwordsCat.addPreference(showPassword);
 
+        // Device policies
+        PreferenceCategory devicePoliciesCat = new PreferenceCategory(this);
+        devicePoliciesCat.setTitle(R.string.device_admin_title);
+        root.addPreference(devicePoliciesCat);
+
+        Preference deviceAdminButton = new Preference(this);
+        deviceAdminButton.setTitle(R.string.manage_device_admin);
+        deviceAdminButton.setSummary(R.string.manage_device_admin_summary);
+        Intent deviceAdminIntent = new Intent();
+        deviceAdminIntent.setClass(this, DeviceAdminSettings.class);
+        deviceAdminButton.setIntent(deviceAdminIntent);
+        devicePoliciesCat.addPreference(deviceAdminButton);
+
         // Credential storage
         PreferenceCategory credentialsCat = new PreferenceCategory(this);
         credentialsCat.setTitle(R.string.credentials_category);
@@ -200,25 +223,26 @@
     }
 
     protected void handleUpdateUnlockMethod(final String value) {
-        final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
         if ("none".equals(value)) {
-            mChooseLockSettingsHelper.launchConfirmationActivity(CONFIRM_EXISTING_REQUEST);
-        } else if ("password".equals(value) || "pin".equals(value)) {
-            final int minLength = 4; // TODO: get from policy store.
-            final int maxLength = 16;
-            final int mode = "password".equals(value)
-                    ? LockPatternUtils.MODE_PASSWORD : LockPatternUtils.MODE_PIN;
-            Intent intent = new Intent().setClassName(PACKAGE, CHOOSE_LOCK_PIN);
-            intent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, mode);
-            intent.putExtra(ChooseLockPassword.PASSWORD_MIN_KEY, minLength);
-            intent.putExtra(ChooseLockPassword.PASSWORD_MAX_KEY, maxLength);
-            startActivityForResult(intent, UPDATE_PASSWORD_REQUEST);
-        } else if ("pattern".equals(value)) {
-            boolean showTutorial = !lockPatternUtils.isPatternEverChosen();
+            if (mDPM.getPasswordMode() == DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED) {
+                mChooseLockSettingsHelper.launchConfirmationActivity(CONFIRM_EXISTING_REQUEST);
+            }
+        } else {
+            int reqMode;
+            if ("password".equals(value)) {
+                reqMode = LockPatternUtils.MODE_PASSWORD;
+            } else if ( "pin".equals(value)) {
+                reqMode = LockPatternUtils.MODE_PIN;
+            } else {
+                reqMode = LockPatternUtils.MODE_PATTERN;
+            }
+            int minMode = mDPM.getPasswordMode();
+            if (reqMode < minMode) {
+                reqMode = minMode;
+            }
             Intent intent = new Intent();
-            intent.setClassName(PACKAGE, showTutorial ?
-                    LOCK_PATTERN_TUTORIAL : CHOOSE_LOCK_PATTERN);
-            intent.putExtra("key_lock_method", value);
+            intent.setClass(this, ChooseLockGeneric.class);
+            intent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, reqMode);
             startActivityForResult(intent, UPDATE_PASSWORD_REQUEST);
         }
     }
@@ -306,9 +330,7 @@
 
         LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
         if ((requestCode == CONFIRM_EXISTING_REQUEST) && resultOk) {
-            lockPatternUtils.saveLockPassword(null);
-            lockPatternUtils.setLockPatternEnabled(false);
-            lockPatternUtils.saveLockPattern(null);
+            lockPatternUtils.clearLock();
         }
     }
 
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index c0a8613..5309cf5 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -17,9 +17,9 @@
 package com.android.settings;
 
 import android.os.Bundle;
+import android.preference.Preference;
 import android.preference.PreferenceActivity;
 import android.preference.PreferenceGroup;
-import android.provider.Settings.System;
 import android.telephony.TelephonyManager;
 
 public class Settings extends PreferenceActivity {
@@ -28,6 +28,7 @@
     private static final String KEY_CALL_SETTINGS = "call_settings";
     private static final String KEY_SYNC_SETTINGS = "sync_settings";
     private static final String KEY_SEARCH_SETTINGS = "search_settings";
+    private static final String KEY_DOCK_SETTINGS = "dock_settings";
     
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -40,6 +41,11 @@
         PreferenceGroup parent = (PreferenceGroup) findPreference(KEY_PARENT);
         Utils.updatePreferenceToSpecificActivityOrRemove(this, parent, KEY_SYNC_SETTINGS, 0);
         Utils.updatePreferenceToSpecificActivityOrRemove(this, parent, KEY_SEARCH_SETTINGS, 0);
+
+        Preference dockSettings = parent.findPreference(KEY_DOCK_SETTINGS);
+        if (getResources().getBoolean(R.bool.has_dock_settings) == false && dockSettings != null) {
+            parent.removePreference(dockSettings);
+        }
     }
     
     @Override
diff --git a/src/com/android/settings/SoundAndDisplaySettings.java b/src/com/android/settings/SoundAndDisplaySettings.java
index e01f7c3..29eb878 100644
--- a/src/com/android/settings/SoundAndDisplaySettings.java
+++ b/src/com/android/settings/SoundAndDisplaySettings.java
@@ -18,8 +18,6 @@
 
 import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
 
-import com.android.settings.bluetooth.DockEventReceiver;
-
 import android.content.BroadcastReceiver;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -63,15 +61,11 @@
     private static final String KEY_EMERGENCY_TONE = "emergency_tone";
     private static final String KEY_SOUND_SETTINGS = "sound_settings";
     private static final String KEY_NOTIFICATION_PULSE = "notification_pulse";
-    private static final String KEY_DOCK_SETTINGS = "dock_settings";
 
     private CheckBoxPreference mSilent;
 
     private CheckBoxPreference mPlayMediaNotificationSounds;
 
-    private Preference mDockSettings;
-    private boolean mHasDockSettings;
-
     private IMountService mMountService = null;
 
     /*
@@ -99,16 +93,12 @@
         public void onReceive(Context context, Intent intent) {
             if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
                 updateState(false);
-            } else if (intent.getAction().equals(Intent.ACTION_DOCK_EVENT)) {
-                handleDockChange(intent);
             }
         }
     };
 
     private PreferenceGroup mSoundSettings;
 
-    private Intent mDockIntent;
-
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -178,7 +168,6 @@
             }
         }
 
-        initDockSettings();
     }
 
     @Override
@@ -188,12 +177,6 @@
         updateState(true);
 
         IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
-        if (mHasDockSettings) {
-            if (mDockSettings != null) {
-                mSoundSettings.removePreference(mDockSettings);
-            }
-            filter.addAction(Intent.ACTION_DOCK_EVENT);
-        }
         registerReceiver(mReceiver, filter);
     }
 
@@ -204,34 +187,6 @@
         unregisterReceiver(mReceiver);
     }
 
-    private void initDockSettings() {
-        mDockSettings = mSoundSettings.findPreference(KEY_DOCK_SETTINGS);
-        mHasDockSettings = getResources().getBoolean(R.bool.has_dock_settings);
-        if (mDockSettings != null) {
-            mSoundSettings.removePreference(mDockSettings);
-            // Don't care even if we dock
-            if (getResources().getBoolean(R.bool.has_dock_settings) == false) {
-                mDockSettings = null;
-            }
-        }
-    }
-
-    private void handleDockChange(Intent intent) {
-        if (mHasDockSettings && mDockSettings != null) {
-            int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, 0);
-            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);
-            }
-        }
-    }
-
     private void updateState(boolean force) {
         final int ringerMode = mAudioManager.getRingerMode();
         final boolean silentOrVibrateMode =
@@ -354,11 +309,6 @@
             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.setAction(DockEventReceiver.ACTION_DOCK_SHOW_UI);
-            i.setClass(this, DockEventReceiver.class);
-            sendBroadcast(i);
         }
 
         return true;
diff --git a/src/com/android/settings/WirelessSettings.java b/src/com/android/settings/WirelessSettings.java
index 1d0b2d8..bf75e27 100644
--- a/src/com/android/settings/WirelessSettings.java
+++ b/src/com/android/settings/WirelessSettings.java
@@ -17,6 +17,7 @@
 package com.android.settings;
 
 import android.bluetooth.BluetoothAdapter;
+import android.content.Context;
 import android.content.Intent;
 import android.net.wifi.WifiManager;
 import android.os.Bundle;
@@ -67,7 +68,17 @@
         // Let the intents be launched by the Preference manager
         return false;
     }
-    
+
+    public static boolean isRadioAllowed(Context context, String type) {
+        if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
+            return true;
+        }
+        // Here we use the same logic in onCreate().
+        String toggleable = Settings.System.getString(context.getContentResolver(),
+                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
+        return toggleable != null && toggleable.contains(type);
+    }
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -86,7 +97,7 @@
         String toggleable = Settings.System.getString(getContentResolver(),
                 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
 
-        // Manually set up dependencies for Wifi when not toggleable.
+        // Manually set dependencies for Wifi when not toggleable.
         if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
             wifi.setDependency(KEY_TOGGLE_AIRPLANE);
             findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
@@ -99,7 +110,7 @@
             findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
         }
 
-        // Disable BT Settings if BT service is not available.
+        // Disable Bluetooth Settings if Bluetooth service is not available.
         if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
             findPreference(KEY_BT_SETTINGS).setEnabled(false);
         }
diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java
index 46793ce..426a4d3 100644
--- a/src/com/android/settings/bluetooth/BluetoothEnabler.java
+++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2010 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.
@@ -16,8 +16,8 @@
 
 package com.android.settings.bluetooth;
 
-import com.android.settings.AirplaneModeEnabler;
 import com.android.settings.R;
+import com.android.settings.WirelessSettings;
 
 import android.bluetooth.BluetoothAdapter;
 import android.content.BroadcastReceiver;
@@ -28,7 +28,7 @@
 import android.preference.CheckBoxPreference;
 import android.provider.Settings;
 import android.text.TextUtils;
-import android.util.Config;
+import android.widget.Toast;
 
 /**
  * BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
@@ -36,16 +36,12 @@
  * preference reflects the current state.
  */
 public class BluetoothEnabler implements Preference.OnPreferenceChangeListener {
-
-    private static final boolean LOCAL_LOGD = Config.LOGD || false;
-    private static final String TAG = "BluetoothEnabler";
-
     private final Context mContext;
-    private final CheckBoxPreference mCheckBoxPreference;
+    private final CheckBoxPreference mCheckBox;
     private final CharSequence mOriginalSummary;
 
     private final LocalBluetoothManager mLocalManager;
-
+    private final IntentFilter mIntentFilter;
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -54,18 +50,18 @@
         }
     };
 
-    public BluetoothEnabler(Context context, CheckBoxPreference checkBoxPreference) {
+    public BluetoothEnabler(Context context, CheckBoxPreference checkBox) {
         mContext = context;
-        mCheckBoxPreference = checkBoxPreference;
-
-        mOriginalSummary = checkBoxPreference.getSummary();
-        checkBoxPreference.setPersistent(false);
+        mCheckBox = checkBox;
+        mOriginalSummary = checkBox.getSummary();
+        checkBox.setPersistent(false);
 
         mLocalManager = LocalBluetoothManager.getInstance(context);
         if (mLocalManager == null) {
-            // Bluetooth not supported
-            checkBoxPreference.setEnabled(false);
+            // Bluetooth is not supported
+            checkBox.setEnabled(false);
         }
+        mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
     }
 
     public void resume() {
@@ -73,16 +69,11 @@
             return;
         }
 
-        int state = mLocalManager.getBluetoothState();
-        // This is the widget enabled state, not the preference toggled state
-        mCheckBoxPreference.setEnabled(state == BluetoothAdapter.STATE_ON ||
-                state == BluetoothAdapter.STATE_OFF);
-        // BT state is not a sticky broadcast, so set it manually
-        handleStateChanged(state);
+        // Bluetooth state is not sticky, so set it manually
+        handleStateChanged(mLocalManager.getBluetoothState());
 
-        mContext.registerReceiver(mReceiver,
-                new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
-        mCheckBoxPreference.setOnPreferenceChangeListener(this);
+        mContext.registerReceiver(mReceiver, mIntentFilter);
+        mCheckBox.setOnPreferenceChangeListener(this);
     }
 
     public void pause() {
@@ -91,72 +82,51 @@
         }
 
         mContext.unregisterReceiver(mReceiver);
-        mCheckBoxPreference.setOnPreferenceChangeListener(null);
+        mCheckBox.setOnPreferenceChangeListener(null);
     }
 
     public boolean onPreferenceChange(Preference preference, Object value) {
-        // Turn on/off BT
-        setEnabled((Boolean) value);
+        boolean enable = (Boolean) value;
+
+        // Show toast message if Bluetooth is not allowed in airplane mode
+        if (enable && !WirelessSettings
+                .isRadioAllowed(mContext, Settings.System.RADIO_BLUETOOTH)) {
+            Toast.makeText(mContext, R.string.wifi_in_airplane_mode,
+                    Toast.LENGTH_SHORT).show();
+            return false;
+        }
+
+        mLocalManager.setBluetoothEnabled(enable);
+        mCheckBox.setEnabled(false);
 
         // Don't update UI to opposite state until we're sure
         return false;
     }
 
-    private void setEnabled(final boolean enable) {
-        // Disable preference
-        mCheckBoxPreference.setEnabled(false);
-
-        mLocalManager.setBluetoothEnabled(enable);
-    }
-
     private void handleStateChanged(int state) {
-
-        if (state == BluetoothAdapter.STATE_OFF ||
-                state == BluetoothAdapter.STATE_ON) {
-            mCheckBoxPreference.setChecked(state == BluetoothAdapter.STATE_ON);
-            mCheckBoxPreference.setSummary(state == BluetoothAdapter.STATE_OFF ?
-                                           mOriginalSummary :
-                                           null);
-
-            final boolean hasDependency = !TextUtils.isEmpty(mCheckBoxPreference.getDependency());
-            final boolean bluetoothAllowed = isBluetoothAllowed(mContext);
-
-            // Avoid disabling when dependencies have been manually set,
-            // workaround for framework bug http://b/2053751
-            if (bluetoothAllowed) {
-                mCheckBoxPreference.setEnabled(true);
-            } else if (!hasDependency) {
-                mCheckBoxPreference.setEnabled(false);
-            }
-
-        } else if (state == BluetoothAdapter.STATE_TURNING_ON ||
-                state == BluetoothAdapter.STATE_TURNING_OFF) {
-            mCheckBoxPreference.setSummary(state == BluetoothAdapter.STATE_TURNING_ON
-                    ? R.string.wifi_starting
-                    : R.string.wifi_stopping);
-
-        } else {
-            mCheckBoxPreference.setChecked(false);
-            mCheckBoxPreference.setSummary(R.string.wifi_error);
-            mCheckBoxPreference.setEnabled(true);
+        switch (state) {
+            case BluetoothAdapter.STATE_TURNING_ON:
+                mCheckBox.setSummary(R.string.wifi_starting);
+                mCheckBox.setEnabled(false);
+                break;
+            case BluetoothAdapter.STATE_ON:
+                mCheckBox.setChecked(true);
+                mCheckBox.setSummary(null);
+                mCheckBox.setEnabled(true);
+                break;
+            case BluetoothAdapter.STATE_TURNING_OFF:
+                mCheckBox.setSummary(R.string.wifi_stopping);
+                mCheckBox.setEnabled(false);
+                break;
+            case BluetoothAdapter.STATE_OFF:
+                mCheckBox.setChecked(false);
+                mCheckBox.setSummary(mOriginalSummary);
+                mCheckBox.setEnabled(true);
+                break;
+            default:
+                mCheckBox.setChecked(false);
+                mCheckBox.setSummary(R.string.wifi_error);
+                mCheckBox.setEnabled(true);
         }
     }
-
-    private static boolean isBluetoothAllowed(Context context) {
-        // allowed if we are not in airplane mode
-        if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
-            return true;
-        }
-        // allowed if bluetooth is not in AIRPLANE_MODE_RADIOS
-        String radios = Settings.System.getString(context.getContentResolver(),
-                Settings.System.AIRPLANE_MODE_RADIOS);
-        if (radios == null || !radios.contains(Settings.System.RADIO_BLUETOOTH)) {
-            return true;
-        }
-        // allowed if bluetooth is in AIRPLANE_MODE_TOGGLEABLE_RADIOS
-        radios = Settings.System.getString(context.getContentResolver(),
-                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
-        return (radios != null && radios.contains(Settings.System.RADIO_BLUETOOTH));
-    }
-
 }
diff --git a/src/com/android/settings/bluetooth/DockEventReceiver.java b/src/com/android/settings/bluetooth/DockEventReceiver.java
index 73f90e5..2d634b2 100644
--- a/src/com/android/settings/bluetooth/DockEventReceiver.java
+++ b/src/com/android/settings/bluetooth/DockEventReceiver.java
@@ -27,7 +27,7 @@
 
 public class DockEventReceiver extends BroadcastReceiver {
 
-    private static final boolean DEBUG = false;
+    private static final boolean DEBUG = DockService.DEBUG;
 
     private static final String TAG = "DockEventReceiver";
 
@@ -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) {
@@ -74,6 +75,13 @@
                     if (DEBUG) Log.e(TAG, "Unknown state");
                     break;
             }
+        } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
+            int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
+            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 4545b4e..db5bcb8 100644
--- a/src/com/android/settings/bluetooth/DockService.java
+++ b/src/com/android/settings/bluetooth/DockService.java
@@ -16,16 +16,21 @@
 
 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;
+import android.bluetooth.BluetoothA2dp;
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
-import android.content.BroadcastReceiver;
+import android.bluetooth.BluetoothHeadset;
 import android.content.Context;
 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;
@@ -38,8 +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,
@@ -47,12 +52,16 @@
 
     private static final String TAG = "DockService";
 
-    private static final boolean DEBUG = false;
+    static final boolean DEBUG = false;
 
     // Time allowed for the device to be undocked and redocked without severing
     // 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;
 
@@ -66,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;
@@ -86,9 +109,8 @@
     // Set while BT is being enabled.
     private BluetoothDevice mPendingDevice;
     private int mPendingStartId;
-
-    private boolean mRegistered;
-    private Object mBtSynchroObject = new Object();
+    private int mPendingTurnOnStartId = INVALID_STARTID;
+    private int mPendingTurnOffStartId = INVALID_STARTID;
 
     @Override
     public void onCreate() {
@@ -111,10 +133,6 @@
             mDialog.dismiss();
             mDialog = null;
         }
-        if (mRegistered) {
-            unregisterReceiver(mReceiver);
-            mRegistered = false;
-        }
         mServiceLooper.quit();
     }
 
@@ -138,6 +156,11 @@
             return START_NOT_STICKY;
         }
 
+        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
+            handleBtStateChange(intent, startId);
+            return START_NOT_STICKY;
+        }
+
         Message msg = parseIntent(intent);
         if (msg == null) {
             // Bad intent
@@ -168,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:
@@ -195,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) {
@@ -216,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:
@@ -224,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);
@@ -407,12 +490,12 @@
         return items;
     }
 
-    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
-            if (state == BluetoothAdapter.STATE_ON && mPendingDevice != null) {
-                synchronized (mBtSynchroObject) {
+    private void handleBtStateChange(Intent intent, int startId) {
+        int btState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
+        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");
                         applyBtSettings(mPendingDevice, mPendingStartId);
@@ -423,20 +506,101 @@
 
                     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) {
+                        int state = i.getIntExtra(Intent.EXTRA_DOCK_STATE,
+                                Intent.EXTRA_DOCK_STATE_UNDOCKED);
+                        if (state != Intent.EXTRA_DOCK_STATE_UNDOCKED) {
+                            BluetoothDevice device = i
+                                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
+                            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);
                 }
             }
         }
-    };
+    }
+
+    private synchronized void connectIfEnabled(BluetoothDevice device) {
+        CachedBluetoothDevice cachedDevice = getCachedBluetoothDevice(mContext, mBtManager, device);
+        List<Profile> profiles = cachedDevice.getConnectableProfiles();
+        for (int i = 0; i < profiles.size(); i++) {
+            LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
+                    .getProfileManager(mBtManager, profiles.get(i));
+            int auto;
+            if (Profile.A2DP == profiles.get(i)) {
+                auto = BluetoothA2dp.PRIORITY_AUTO_CONNECT;
+            } else if (Profile.HEADSET == profiles.get(i)) {
+                auto = BluetoothHeadset.PRIORITY_AUTO_CONNECT;
+            } else {
+                continue;
+            }
+
+            if (profileManager.getPreferred(device) == auto) {
+                cachedDevice.connect();
+                break;
+            }
+        }
+    }
 
     private synchronized void applyBtSettings(final BluetoothDevice device, int startId) {
         if (device == null || mProfiles == null || mCheckedItems == null)
             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:
@@ -444,16 +608,11 @@
                             if (mPendingDevice != null && mPendingDevice.equals(mDevice)) {
                                 return;
                             }
-                            if (!mRegistered) {
-                                registerReceiver(mReceiver, new IntentFilter(
-                                        BluetoothAdapter.ACTION_STATE_CHANGED));
-                            }
+
                             mPendingDevice = device;
-                            mRegistered = true;
                             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;
                     }
@@ -516,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;
+    }
 }
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
index 24ba045..f3aaade 100644
--- a/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothProfileManager.java
@@ -136,6 +136,8 @@
 
     public abstract boolean isPreferred(BluetoothDevice device);
 
+    public abstract int getPreferred(BluetoothDevice device);
+
     public abstract void setPreferred(BluetoothDevice device, boolean preferred);
 
     public boolean isConnected(BluetoothDevice device) {
@@ -213,6 +215,11 @@
         }
 
         @Override
+        public int getPreferred(BluetoothDevice device) {
+            return mService.getSinkPriority(device);
+        }
+
+        @Override
         public void setPreferred(BluetoothDevice device, boolean preferred) {
             if (preferred) {
                 if (mService.getSinkPriority(device) < BluetoothA2dp.PRIORITY_ON) {
@@ -332,6 +339,11 @@
         }
 
         @Override
+        public int getPreferred(BluetoothDevice device) {
+            return mService.getPriority(device);
+        }
+
+        @Override
         public void setPreferred(BluetoothDevice device, boolean preferred) {
             if (preferred) {
                 if (mService.getPriority(device) < BluetoothHeadset.PRIORITY_ON) {
@@ -403,6 +415,11 @@
         }
 
         @Override
+        public int getPreferred(BluetoothDevice device) {
+            return -1;
+        }
+
+        @Override
         public void setPreferred(BluetoothDevice device, boolean preferred) {
         }
 
diff --git a/src/com/android/settings/wifi/Summary.java b/src/com/android/settings/wifi/Summary.java
new file mode 100644
index 0000000..6da2fa5
--- /dev/null
+++ b/src/com/android/settings/wifi/Summary.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2010 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.wifi;
+
+import com.android.settings.R;
+
+import android.content.Context;
+import android.net.NetworkInfo.DetailedState;
+import android.text.TextUtils;
+
+class Summary {
+    static String get(Context context, String ssid, DetailedState state) {
+        String[] formats = context.getResources().getStringArray((ssid == null)
+                ? R.array.wifi_status : R.array.wifi_status_with_ssid);
+        int index = state.ordinal();
+
+        if (index >= formats.length || formats[index].length() == 0) {
+            return null;
+        }
+        return String.format(formats[index], ssid);
+    }
+
+    static String get(Context context, DetailedState state) {
+        return get(context, null, state);
+    }
+}
diff --git a/src/com/android/settings/wifi/WifiEnabler.java b/src/com/android/settings/wifi/WifiEnabler.java
index b6e758d..7ede80d 100644
--- a/src/com/android/settings/wifi/WifiEnabler.java
+++ b/src/com/android/settings/wifi/WifiEnabler.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007 The Android Open Source Project
+ * Copyright (C) 2010 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.
@@ -16,188 +16,133 @@
 
 package com.android.settings.wifi;
 
-import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED;
-import static android.net.wifi.WifiManager.WIFI_STATE_DISABLING;
-import static android.net.wifi.WifiManager.WIFI_STATE_ENABLED;
-import static android.net.wifi.WifiManager.WIFI_STATE_ENABLING;
-import static android.net.wifi.WifiManager.WIFI_STATE_UNKNOWN;
-
 import com.android.settings.R;
-import com.android.settings.AirplaneModeEnabler;
+import com.android.settings.WirelessSettings;
 
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.NetworkInfo;
+import android.net.wifi.SupplicantState;
+import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
 import android.preference.Preference;
 import android.preference.CheckBoxPreference;
 import android.provider.Settings;
 import android.text.TextUtils;
-import android.util.Config;
-import android.util.Log;
+import android.widget.Toast;
 
 public class WifiEnabler implements Preference.OnPreferenceChangeListener {
-    
-    private static final boolean LOCAL_LOGD = Config.LOGD || WifiLayer.LOGV;
-    private static final String TAG = "SettingsWifiEnabler";
-    
     private final Context mContext; 
-    private final WifiManager mWifiManager;
-    private final CheckBoxPreference mWifiCheckBoxPref;
+    private final CheckBoxPreference mCheckBox;
     private final CharSequence mOriginalSummary;
-    
-    private final IntentFilter mWifiStateFilter;
-    private final BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() {
 
+    private final WifiManager mWifiManager;
+    private final IntentFilter mIntentFilter;
+    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
-            if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
-                handleWifiStateChanged(
-                        intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WIFI_STATE_UNKNOWN),
-                        intent.getIntExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE,
-                                WIFI_STATE_UNKNOWN));
-            } else if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
-                handleNetworkStateChanged(
-                        (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
+            String action = intent.getAction();
+            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
+                handleWifiStateChanged(intent.getIntExtra(
+                        WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
+            } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
+                handleStateChanged(WifiInfo.getDetailedStateOf((SupplicantState)
+                        intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
+            } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
+                handleStateChanged(((NetworkInfo) intent.getParcelableExtra(
+                        WifiManager.EXTRA_NETWORK_INFO)).getDetailedState());
             }
         }
     };
 
-    public WifiEnabler(Context context, CheckBoxPreference wifiCheckBoxPreference) {
+    public WifiEnabler(Context context, CheckBoxPreference checkBox) {
         this(context, (WifiManager) context.getSystemService(Context.WIFI_SERVICE),
-                wifiCheckBoxPreference);
+                checkBox);
     }
     
     public WifiEnabler(Context context, WifiManager wifiManager,
-            CheckBoxPreference wifiCheckBoxPreference) {
+            CheckBoxPreference checkBox) {
         mContext = context;
-        mWifiCheckBoxPref = wifiCheckBoxPreference;
+        mCheckBox = checkBox;
         mWifiManager = wifiManager;
+        mOriginalSummary = checkBox.getSummary();
+        checkBox.setPersistent(false);
         
-        mOriginalSummary = wifiCheckBoxPreference.getSummary();
-        wifiCheckBoxPreference.setPersistent(false);
-        
-        mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
-        mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
+        mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
+        // The order matters! We really should not depend on this. :(
+        mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
+        mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
     }
 
     public void resume() {
-        int state = mWifiManager.getWifiState();
-        // This is the widget enabled state, not the preference toggled state
-        mWifiCheckBoxPref.setEnabled(state == WIFI_STATE_ENABLED || state == WIFI_STATE_DISABLED
-                || state == WIFI_STATE_UNKNOWN);
-
-        mContext.registerReceiver(mWifiStateReceiver, mWifiStateFilter);
-        mWifiCheckBoxPref.setOnPreferenceChangeListener(this);
+        // Wi-Fi state is sticky, so just let the receiver update UI
+        mContext.registerReceiver(mReceiver, mIntentFilter);
+        mCheckBox.setOnPreferenceChangeListener(this);
     }
     
     public void pause() {
-        mContext.unregisterReceiver(mWifiStateReceiver);
-        mWifiCheckBoxPref.setOnPreferenceChangeListener(null);
+        mContext.unregisterReceiver(mReceiver);
+        mCheckBox.setOnPreferenceChangeListener(null);
     }
     
     public boolean onPreferenceChange(Preference preference, Object value) {
-        // Turn on/off Wi-Fi
-        setWifiEnabled((Boolean) value);
-        
+        boolean enable = (Boolean) value;
+    
+        // Show toast message if Wi-Fi is not allowed in airplane mode
+        if (enable && !WirelessSettings
+                .isRadioAllowed(mContext, Settings.System.RADIO_WIFI)) {
+            Toast.makeText(mContext, R.string.wifi_in_airplane_mode,
+                    Toast.LENGTH_SHORT).show();
+            return false;
+        }
+
+        if (mWifiManager.setWifiEnabled(enable)) {
+            mCheckBox.setEnabled(false);
+        } else {
+            mCheckBox.setSummary(R.string.wifi_error);
+        }
+
         // Don't update UI to opposite state until we're sure
         return false;
     }
     
-    private void setWifiEnabled(final boolean enable) {
-        // Disable button
-        mWifiCheckBoxPref.setEnabled(false);
-        
-        if (!mWifiManager.setWifiEnabled(enable)) {
-            mWifiCheckBoxPref.setSummary(enable ? R.string.error_starting : R.string.error_stopping);
-        }
-    }
-    
-    private void handleWifiStateChanged(int wifiState, int previousWifiState) {
-
-        if (LOCAL_LOGD) {
-            Log.d(TAG, "Received wifi state changed from "
-                    + getHumanReadableWifiState(previousWifiState) + " to "
-                    + getHumanReadableWifiState(wifiState));
-        }
-        
-        if (wifiState == WIFI_STATE_DISABLED || wifiState == WIFI_STATE_ENABLED) {
-            mWifiCheckBoxPref.setChecked(wifiState == WIFI_STATE_ENABLED);
-            mWifiCheckBoxPref
-                    .setSummary(wifiState == WIFI_STATE_DISABLED ? mOriginalSummary : null);
-
-            final boolean hasDependency = !TextUtils.isEmpty(mWifiCheckBoxPref.getDependency());
-            final boolean wifiAllowed = isWifiAllowed(mContext);
-
-            // Avoid disabling when dependencies have been manually set,
-            // workaround for framework bug http://b/2053751
-            if (wifiAllowed) {
-                mWifiCheckBoxPref.setEnabled(true);
-            } else if (!hasDependency) {
-                mWifiCheckBoxPref.setEnabled(false);
-            }
-
-        } else if (wifiState == WIFI_STATE_DISABLING || wifiState == WIFI_STATE_ENABLING) {
-            mWifiCheckBoxPref.setSummary(wifiState == WIFI_STATE_ENABLING ? R.string.wifi_starting
-                    : R.string.wifi_stopping);
-            
-        } else if (wifiState == WIFI_STATE_UNKNOWN) {
-            int message = R.string.wifi_error;
-            if (previousWifiState == WIFI_STATE_ENABLING) message = R.string.error_starting;
-            else if (previousWifiState == WIFI_STATE_DISABLING) message = R.string.error_stopping;
-            
-            mWifiCheckBoxPref.setChecked(false);
-            mWifiCheckBoxPref.setSummary(message);
-            mWifiCheckBoxPref.setEnabled(true);
-        }
-    }
-
-    private void handleNetworkStateChanged(NetworkInfo networkInfo) {
-
-        if (LOCAL_LOGD) {
-            Log.d(TAG, "Received network state changed to " + networkInfo);
-        }
-        
-        if (mWifiManager.isWifiEnabled()) {
-            String summary = WifiStatus.getStatus(mContext, 
-                    mWifiManager.getConnectionInfo().getSSID(), networkInfo.getDetailedState());
-            mWifiCheckBoxPref.setSummary(summary);
-        }
-    }
-
-    private static boolean isWifiAllowed(Context context) {
-        // allowed if we are not in airplane mode
-        if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
-            return true;
-        }
-        // allowed if wifi is not in AIRPLANE_MODE_RADIOS
-        String radios = Settings.System.getString(context.getContentResolver(),
-                Settings.System.AIRPLANE_MODE_RADIOS);
-        if (radios == null || !radios.contains(Settings.System.RADIO_WIFI)) {
-            return true;
-        }
-        // allowed if wifi is in AIRPLANE_MODE_TOGGLEABLE_RADIOS
-        radios = Settings.System.getString(context.getContentResolver(),
-                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
-        return (radios != null && radios.contains(Settings.System.RADIO_WIFI));
-    }
-
-    private static String getHumanReadableWifiState(int wifiState) {
-        switch (wifiState) {
-            case WIFI_STATE_DISABLED:
-                return "Disabled";
-            case WIFI_STATE_DISABLING:
-                return "Disabling";
-            case WIFI_STATE_ENABLED:
-                return "Enabled";
-            case WIFI_STATE_ENABLING:
-                return "Enabling";
-            case WIFI_STATE_UNKNOWN:
-                return "Unknown";
+    private void handleWifiStateChanged(int state) {
+        switch (state) {
+            case WifiManager.WIFI_STATE_ENABLING:
+                mCheckBox.setSummary(R.string.wifi_starting);
+                mCheckBox.setEnabled(false);
+                break;
+            case WifiManager.WIFI_STATE_ENABLED:
+                mCheckBox.setChecked(true);
+                mCheckBox.setSummary(null);
+                mCheckBox.setEnabled(true);
+                break;
+            case WifiManager.WIFI_STATE_DISABLING:
+                mCheckBox.setSummary(R.string.wifi_stopping);
+                mCheckBox.setEnabled(false);
+                break;
+            case WifiManager.WIFI_STATE_DISABLED:
+                mCheckBox.setChecked(false);
+                mCheckBox.setSummary(mOriginalSummary);
+                mCheckBox.setEnabled(true);
+                break;
             default:
-                return "Some other state!";    
+                mCheckBox.setChecked(false);
+                mCheckBox.setSummary(R.string.wifi_error);
+                mCheckBox.setEnabled(true);
+        }
+    }
+
+    private void handleStateChanged(NetworkInfo.DetailedState state) {
+        // WifiInfo is valid if and only if Wi-Fi is enabled.
+        // Here we use the state of the check box as an optimization.
+        if (state != null && mCheckBox.isChecked()) {
+            WifiInfo info = mWifiManager.getConnectionInfo();
+            if (info != null) {
+                mCheckBox.setSummary(Summary.get(mContext, info.getSSID(), state));
+            }
         }
     }
 }