Merge changes from topic 'update mono audio' into nyc-dev

* changes:
  Add mono audio to AccessibilitySettings
  Remove mono audio from SoundSettings
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 18b541b..3f8cbb9 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5961,17 +5961,20 @@
     <!-- [CHAR LIMIT=100] Notification Importance slider: blocked importance level description -->
     <string name="notification_importance_blocked">Blocked: Never show these notifications</string>
 
+    <!-- [CHAR LIMIT=100] Notification Importance slider: min importance level description -->
+    <string name="notification_importance_min">Min: Silently show at the bottom of the notification list</string>
+
     <!-- [CHAR LIMIT=100] Notification Importance slider: low importance level description -->
-    <string name="notification_importance_low">Low: Silently show at the bottom of the notification list</string>
+    <string name="notification_importance_low">Low: Silently show these notifications</string>
 
     <!-- [CHAR LIMIT=100] Notification Importance slider: normal importance level description -->
-    <string name="notification_importance_default">Normal: Silently show these notifications</string>
+    <string name="notification_importance_default">Normal: Allow these notification to make sounds</string>
 
     <!-- [CHAR LIMIT=100] Notification Importance slider: high importance level description -->
-    <string name="notification_importance_high">High: Show at the top of the notifications list and allow sound</string>
+    <string name="notification_importance_high">High: Peek onto the screen and allow sound and allow sound</string>
 
     <!-- [CHAR LIMIT=100] Notification Importance slider: max importance level description -->
-    <string name="notification_importance_max">Urgent: Peek onto the screen and allow sound</string>
+    <string name="notification_importance_max">Urgent: Show at the top of the notifications list, peek onto the screen and allow sound</string>
 
     <!-- [CHAR LIMIT=60] Notification importance reset button -->
     <string name="importance_reset">Reset</string>
@@ -7230,6 +7233,9 @@
     <!-- [CHAR LIMIT=30] Title for dialog for setting to control the default spell checker -->
     <string name="choose_spell_checker">Choose spell checker</string>
 
+    <!-- [CHAR LIMIT=30] Label for the placeholder of the current spell checker name.  Used when no spell checker is currently selected. -->
+    <string name="spell_checker_not_selected">Not selected</string>
+
     <!-- Notification log debug tool: missing title -->
     <string name="notification_log_no_title">(none)</string>
     <!-- Notification log debug tool: delimiter between header and field data -->
diff --git a/src/com/android/settings/ChooseLockGeneric.java b/src/com/android/settings/ChooseLockGeneric.java
index 2592e83..4dde340 100644
--- a/src/com/android/settings/ChooseLockGeneric.java
+++ b/src/com/android/settings/ChooseLockGeneric.java
@@ -292,7 +292,7 @@
                 }
             } else if (requestCode == CHOOSE_LOCK_REQUEST
                     || requestCode == ENABLE_ENCRYPTION_REQUEST) {
-                if (resultCode != RESULT_CANCELED) {
+                if (resultCode != RESULT_CANCELED || mForChangeCredRequiredForBoot) {
                     getActivity().setResult(resultCode, data);
                     finish();
                 }
diff --git a/src/com/android/settings/SaveChosenLockWorkerBase.java b/src/com/android/settings/SaveChosenLockWorkerBase.java
index 128bd99..39620d5 100644
--- a/src/com/android/settings/SaveChosenLockWorkerBase.java
+++ b/src/com/android/settings/SaveChosenLockWorkerBase.java
@@ -17,6 +17,7 @@
 package com.android.settings;
 
 import android.app.Fragment;
+import android.content.Context;
 import android.content.Intent;
 import android.os.AsyncTask;
 import android.os.Bundle;
@@ -70,7 +71,10 @@
         mUtils.setSeparateProfileChallengeEnabled(mUserId, true);
         mWasSecureBefore = mUtils.isSecure(mUserId);
 
-        if (UserManager.get(getContext()).getUserInfo(mUserId).isPrimary()) {
+        Context context = getContext();
+        // If context is null, we're being invoked to change the setCredentialRequiredToDecrypt,
+        // and we made sure that this is the primary user already.
+        if (context == null || UserManager.get(context).getUserInfo(mUserId).isPrimary()) {
             mUtils.setCredentialRequiredToDecrypt(credentialRequired);
         }
 
diff --git a/src/com/android/settings/SecuritySettings.java b/src/com/android/settings/SecuritySettings.java
index 8464462..8966974 100644
--- a/src/com/android/settings/SecuritySettings.java
+++ b/src/com/android/settings/SecuritySettings.java
@@ -1054,6 +1054,7 @@
         static boolean canChangeRequireCredentialBeforeStartup(Context context) {
             DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
             return UserManager.get(context).isAdminUser()
+                    && UserManager.get(context).isPrimaryUser()
                     && LockPatternUtils.isDeviceEncryptionEnabled()
                     && !dpm.getDoNotAskCredentialsOnBoot();
         }
diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java
index 6144f72..daa2f1b 100644
--- a/src/com/android/settings/SettingsPreferenceFragment.java
+++ b/src/com/android/settings/SettingsPreferenceFragment.java
@@ -34,6 +34,7 @@
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
+import android.util.ArrayMap;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.Menu;
@@ -97,6 +98,7 @@
     private View mEmptyView;
     private LinearLayoutManager mLayoutManager;
     private HighlightablePreferenceGroupAdapter mAdapter;
+    private ArrayMap<String, Preference> mPreferenceCache;
 
     @Override
     public void onCreate(Bundle icicle) {
@@ -367,6 +369,28 @@
         return mAdapter;
     }
 
+    protected void cacheRemoveAllPrefs(PreferenceGroup group) {
+        mPreferenceCache = new ArrayMap<String, Preference>();
+        final int N = group.getPreferenceCount();
+        for (int i = 0; i < N; i++) {
+            Preference p = group.getPreference(i);
+            if (TextUtils.isEmpty(p.getKey())) {
+                continue;
+            }
+            mPreferenceCache.put(p.getKey(), p);
+        }
+    }
+
+    protected Preference getCachedPreference(String key) {
+        return mPreferenceCache != null ? mPreferenceCache.remove(key) : null;
+    }
+
+    protected void removeCachedPrefs(PreferenceGroup group) {
+        for (Preference p : mPreferenceCache.values()) {
+            group.removePreference(p);
+        }
+    }
+
     private void highlightPreference(String key) {
         final int position = canUseListViewForHighLighting(key);
         if (position >= 0) {
diff --git a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
index 4544a50..6d95351 100644
--- a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
+++ b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java
@@ -84,6 +84,10 @@
         onDeviceAttributesChanged();
     }
 
+    void rebind() {
+        notifyChanged();
+    }
+
     CachedBluetoothDevice getCachedDevice() {
         return mCachedDevice;
     }
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index 7db915f..ec0479e 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -39,7 +39,6 @@
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.TextView;
-
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.settings.LinkifyUtils;
@@ -77,6 +76,8 @@
     private static final String BTOPP_ACTION_OPEN_RECEIVED_FILES =
             "android.btopp.intent.action.OPEN_RECEIVED_FILES";
 
+    private static final String KEY_PAIRED_DEVICES = "paired_devices";
+
     private static View mSettingsDialogView = null;
 
     private BluetoothEnabler mBluetoothEnabler;
@@ -154,6 +155,21 @@
     void addPreferencesForActivity() {
         addPreferencesFromResource(R.xml.bluetooth_settings);
 
+        mPairedDevicesCategory = new PreferenceCategory(getPrefContext());
+        mPairedDevicesCategory.setKey(KEY_PAIRED_DEVICES);
+        mPairedDevicesCategory.setOrder(1);
+        getPreferenceScreen().addPreference(mPairedDevicesCategory);
+
+        mAvailableDevicesCategory = new BluetoothProgressCategory(getActivity());
+        mAvailableDevicesCategory.setSelectable(false);
+        mAvailableDevicesCategory.setOrder(2);
+        getPreferenceScreen().addPreference(mAvailableDevicesCategory);
+
+        mMyDevicePreference = new Preference(getPrefContext());
+        mMyDevicePreference.setSelectable(false);
+        mMyDevicePreference.setOrder(3);
+        getPreferenceScreen().addPreference(mMyDevicePreference);
+
         setHasOptionsMenu(true);
     }
 
@@ -275,14 +291,15 @@
 
     private void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId,
             BluetoothDeviceFilter.Filter filter, boolean addCachedDevices) {
+        cacheRemoveAllPrefs(preferenceGroup);
         preferenceGroup.setTitle(titleId);
-        getPreferenceScreen().addPreference(preferenceGroup);
         setFilter(filter);
         setDeviceListGroup(preferenceGroup);
         if (addCachedDevices) {
             addCachedDevices();
         }
         preferenceGroup.setEnabled(true);
+        removeCachedPrefs(preferenceGroup);
     }
 
     private void updateContent(int bluetoothState) {
@@ -291,8 +308,6 @@
 
         switch (bluetoothState) {
             case BluetoothAdapter.STATE_ON:
-                preferenceScreen.removeAll();
-                preferenceScreen.setOrderingAsAdded(true);
                 mDevicePreferenceMap.clear();
 
                 if (isUiRestricted()) {
@@ -301,44 +316,32 @@
                 }
 
                 // Paired devices category
-                if (mPairedDevicesCategory == null) {
-                    mPairedDevicesCategory = new PreferenceCategory(getPrefContext());
-                } else {
-                    mPairedDevicesCategory.removeAll();
-                }
                 addDeviceCategory(mPairedDevicesCategory,
                         R.string.bluetooth_preference_paired_devices,
                         BluetoothDeviceFilter.BONDED_DEVICE_FILTER, true);
                 int numberOfPairedDevices = mPairedDevicesCategory.getPreferenceCount();
 
                 if (isUiRestricted() || numberOfPairedDevices <= 0) {
-                    preferenceScreen.removePreference(mPairedDevicesCategory);
+                    if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) != null) {
+                        preferenceScreen.removePreference(mPairedDevicesCategory);
+                    }
+                } else {
+                    if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) == null) {
+                        preferenceScreen.addPreference(mPairedDevicesCategory);
+                    }
                 }
 
                 // Available devices category
-                if (mAvailableDevicesCategory == null) {
-                    mAvailableDevicesCategory = new BluetoothProgressCategory(getActivity());
-                    mAvailableDevicesCategory.setSelectable(false);
-                } else {
-                    mAvailableDevicesCategory.removeAll();
-                }
                 addDeviceCategory(mAvailableDevicesCategory,
                         R.string.bluetooth_preference_found_devices,
                         BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted);
-                int numberOfAvailableDevices = mAvailableDevicesCategory.getPreferenceCount();
 
                 if (!mInitialScanStarted) {
                     startScanning();
                 }
 
-                if (mMyDevicePreference == null) {
-                    mMyDevicePreference = new Preference(getPrefContext());
-                }
-
                 mMyDevicePreference.setSummary(getResources().getString(
                             R.string.bluetooth_is_visible_message, mLocalAdapter.getName()));
-                mMyDevicePreference.setSelectable(false);
-                preferenceScreen.addPreference(mMyDevicePreference);
 
                 getActivity().invalidateOptionsMenu();
 
diff --git a/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java b/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
index 607db59..1eea942 100644
--- a/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
+++ b/src/com/android/settings/bluetooth/DeviceListPreferenceFragment.java
@@ -176,11 +176,19 @@
             return;
         }
 
-        BluetoothDevicePreference preference = new BluetoothDevicePreference(
-                getPrefContext(), cachedDevice);
+        String key = cachedDevice.getDevice().getAddress();
+        BluetoothDevicePreference preference = (BluetoothDevicePreference) getCachedPreference(key);
+
+        if (preference == null) {
+            preference = new BluetoothDevicePreference(getPrefContext(), cachedDevice);
+            mDeviceListGroup.addPreference(preference);
+        } else {
+            // Tell the preference it is being re-used in case there is new info in the
+            // cached device.
+            preference.rebind();
+        }
 
         initDevicePreference(preference);
-        mDeviceListGroup.addPreference(preference);
         mDevicePreferenceMap.put(cachedDevice, preference);
     }
 
diff --git a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
index 1318a1b..fb0a4d5 100644
--- a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
+++ b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
@@ -261,12 +261,16 @@
         if (spellChecker != null) {
             final TextServicesManager tsm = (TextServicesManager) getSystemService(
                     Context.TEXT_SERVICES_MANAGER_SERVICE);
-            final SpellCheckerInfo sci = tsm.getCurrentSpellChecker();
-            spellChecker.setEnabled(sci != null);
-            if (tsm.isSpellCheckerEnabled() && sci != null) {
-                spellChecker.setSummary(sci.loadLabel(getPackageManager()));
-            } else {
+            if (!tsm.isSpellCheckerEnabled()) {
+                spellChecker.setEnabled(false);
                 spellChecker.setSummary(R.string.switch_off_text);
+            } else {
+                final SpellCheckerInfo sci = tsm.getCurrentSpellChecker();
+                if (sci != null) {
+                    spellChecker.setSummary(sci.loadLabel(getPackageManager()));
+                } else {
+                    spellChecker.setSummary(R.string.spell_checker_not_selected);
+                }
             }
         }
 
diff --git a/src/com/android/settings/inputmethod/SpellCheckersSettings.java b/src/com/android/settings/inputmethod/SpellCheckersSettings.java
index ad6a17b..bc2a5c0 100644
--- a/src/com/android/settings/inputmethod/SpellCheckersSettings.java
+++ b/src/com/android/settings/inputmethod/SpellCheckersSettings.java
@@ -73,19 +73,17 @@
     }
 
     private void populatePreferenceScreen() {
-        final PreferenceScreen screen = getPreferenceScreen();
-        final Context context = getActivity();
-        final int count = (mEnabledScis == null) ? 0 : mEnabledScis.length;
-
-        for (int index = 0; index < count; ++index) {
-            final SpellCheckerInfo sci = mEnabledScis[index];
-        }
         final SpellCheckerPreference pref = new SpellCheckerPreference(getPrefContext(),
                 mEnabledScis);
         pref.setTitle(R.string.default_spell_checker);
-        pref.setSummary("%s");
+        final int count = (mEnabledScis == null) ? 0 : mEnabledScis.length;
+        if (count > 0) {
+            pref.setSummary("%s");
+        } else {
+            pref.setSummary(R.string.spell_checker_not_selected);
+        }
         pref.setOnPreferenceChangeListener(this);
-        screen.addPreference(pref);
+        getPreferenceScreen().addPreference(pref);
     }
 
     @Override
@@ -114,8 +112,13 @@
         final boolean isSpellCheckerEnabled = mTsm.isSpellCheckerEnabled();
         mSwitchBar.setChecked(isSpellCheckerEnabled);
 
-        final SpellCheckerSubtype currentScs = mTsm.getCurrentSpellCheckerSubtype(
-                false /* allowImplicitlySelectedSubtype */);
+        final SpellCheckerSubtype currentScs;
+        if (mCurrentSci == null) {
+            currentScs = mTsm.getCurrentSpellCheckerSubtype(
+                    false /* allowImplicitlySelectedSubtype */);
+        } else {
+            currentScs = null;
+        }
         mSpellCheckerLanaguagePref.setSummary(getSpellCheckerSubtypeLabel(mCurrentSci, currentScs));
 
         final PreferenceScreen screen = getPreferenceScreen();
@@ -128,12 +131,13 @@
                 pref.setSelected(mCurrentSci);
             }
         }
+        mSpellCheckerLanaguagePref.setEnabled(isSpellCheckerEnabled && mCurrentSci != null);
     }
 
     private CharSequence getSpellCheckerSubtypeLabel(final SpellCheckerInfo sci,
             final SpellCheckerSubtype subtype) {
         if (sci == null) {
-            return null;
+            return getString(R.string.spell_checker_not_selected);
         }
         if (subtype == null) {
             return getString(R.string.use_system_language_to_select_input_method_subtypes);
@@ -173,6 +177,11 @@
             mDialog.dismiss();
         }
         final SpellCheckerInfo currentSci = mTsm.getCurrentSpellChecker();
+        if (currentSci == null) {
+            // This can happen in some situations.  One example is that the package that the current
+            // spell checker belongs to was uninstalled or being in background.
+            return;
+        }
         final SpellCheckerSubtype currentScs = mTsm.getCurrentSpellCheckerSubtype(
                 false /* allowImplicitlySelectedSubtype */);
         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
diff --git a/src/com/android/settings/notification/AppNotificationSettings.java b/src/com/android/settings/notification/AppNotificationSettings.java
index f04cf2f..c82ad19 100644
--- a/src/com/android/settings/notification/AppNotificationSettings.java
+++ b/src/com/android/settings/notification/AppNotificationSettings.java
@@ -106,12 +106,12 @@
         final boolean allowPrivate = getLockscreenAllowPrivateNotifications();
 
         if (getPreferenceScreen().findPreference(mBlock.getKey()) != null) {
-            setVisible(mSilent, checkCanBeVisible(Ranking.IMPORTANCE_LOW, importance));
-            mSilent.setChecked(importance == Ranking.IMPORTANCE_DEFAULT);
+            setVisible(mSilent, checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance));
+            mSilent.setChecked(importance == Ranking.IMPORTANCE_LOW);
         }
-        setVisible(mPriority, checkCanBeVisible(Ranking.IMPORTANCE_LOW, importance)
+        setVisible(mPriority, checkCanBeVisible(Ranking.IMPORTANCE_DEFAULT, importance)
                 && !mDndVisualEffectsSuppressed);
-        setVisible(mSensitive, checkCanBeVisible(Ranking.IMPORTANCE_HIGH, importance)
+        setVisible(mSensitive, checkCanBeVisible(Ranking.IMPORTANCE_MIN, importance)
                 && lockscreenSecure && lockscreenNotificationsEnabled && allowPrivate);
     }
 
diff --git a/src/com/android/settings/notification/NotificationSettingsBase.java b/src/com/android/settings/notification/NotificationSettingsBase.java
index 895d38d..ee0cac0 100644
--- a/src/com/android/settings/notification/NotificationSettingsBase.java
+++ b/src/com/android/settings/notification/NotificationSettingsBase.java
@@ -175,7 +175,7 @@
             mImportanceTitle.setSummary(getProgressSummary(importance));
             mImportance.setSystemApp(isSystemApp);
             mImportance.setMinimumProgress(
-                    isSystemApp ? Ranking.IMPORTANCE_LOW : Ranking.IMPORTANCE_NONE);
+                    isSystemApp ? Ranking.IMPORTANCE_MIN : Ranking.IMPORTANCE_NONE);
             mImportance.setMax(Ranking.IMPORTANCE_MAX);
             mImportance.setProgress(importance);
             mImportance.setCallback(new ImportanceSeekBarPreference.Callback() {
@@ -230,7 +230,7 @@
                 public boolean onPreferenceChange(Preference preference, Object newValue) {
                     final boolean silenced = (Boolean) newValue;
                     final int importance =
-                            silenced ? Ranking.IMPORTANCE_DEFAULT : Ranking.IMPORTANCE_UNSPECIFIED;
+                            silenced ? Ranking.IMPORTANCE_LOW : Ranking.IMPORTANCE_UNSPECIFIED;
                     mBackend.setImportance(mPkgInfo.packageName, mUid, importance);
                     updateDependents(importance);
                     return true;
@@ -244,6 +244,8 @@
         switch (progress) {
             case Ranking.IMPORTANCE_NONE:
                 return mContext.getString(R.string.notification_importance_blocked);
+            case Ranking.IMPORTANCE_MIN:
+                return mContext.getString(R.string.notification_importance_min);
             case Ranking.IMPORTANCE_LOW:
                 return mContext.getString(R.string.notification_importance_low);
             case Ranking.IMPORTANCE_DEFAULT:
diff --git a/src/com/android/settings/notification/NotificationStation.java b/src/com/android/settings/notification/NotificationStation.java
index 7c79e8b..b871848 100644
--- a/src/com/android/settings/notification/NotificationStation.java
+++ b/src/com/android/settings/notification/NotificationStation.java
@@ -109,6 +109,11 @@
         public void onNotificationRankingUpdate(RankingMap ranking) {
             mRanking = ranking;
         }
+
+        @Override
+        public void onListenerConnected() {
+            mRanking = getCurrentRanking();
+        }
     };
 
     private Context mContext;
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index 12e5c54..783121b 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -649,19 +649,22 @@
 
                 boolean hasAvailableAccessPoints = false;
                 int index = 0;
+                cacheRemoveAllPrefs(getPreferenceScreen());
                 for (AccessPoint accessPoint : accessPoints) {
                     // Ignore access points that are out of range.
                     if (accessPoint.getLevel() != -1) {
+                        String key = accessPoint.getBssid();
                         hasAvailableAccessPoints = true;
-                        if (accessPoint.getTag() != null) {
-                            final Preference pref = (Preference) accessPoint.getTag();
+                        LongPressAccessPointPreference pref = (LongPressAccessPointPreference)
+                                getCachedPreference(key);
+                        if (pref != null) {
                             pref.setOrder(index++);
-                            getPreferenceScreen().addPreference(pref);
                             continue;
                         }
                         LongPressAccessPointPreference
                                 preference = new LongPressAccessPointPreference(accessPoint,
                                 getPrefContext(), mUserBadgeCache, false, this);
+                        preference.setKey(key);
                         preference.setOrder(index++);
 
                         if (mOpenSsid != null && mOpenSsid.equals(accessPoint.getSsidStr())
@@ -674,6 +677,7 @@
                         accessPoint.setListener(this);
                     }
                 }
+                removeCachedPrefs(getPreferenceScreen());
                 if (!hasAvailableAccessPoints) {
                     setProgressBarVisible(true);
                     Preference pref = new Preference(getContext()) {