Merge "Use plain user icon in UserSpinnerAdapter" into lmp-dev
diff --git a/src/com/android/settings/ChooseLockPassword.java b/src/com/android/settings/ChooseLockPassword.java
index 84758d8..d04f6ac 100644
--- a/src/com/android/settings/ChooseLockPassword.java
+++ b/src/com/android/settings/ChooseLockPassword.java
@@ -96,6 +96,7 @@
         private int mRequestedQuality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
         private ChooseLockSettingsHelper mChooseLockSettingsHelper;
         private Stage mUiStage = Stage.Introduction;
+        private boolean mDone = false;
         private TextView mHeaderText;
         private String mFirstPin;
         private KeyboardView mKeyboardView;
@@ -229,6 +230,7 @@
                     updateStage(mUiStage);
                 }
             }
+            mDone = false;
             if (activity instanceof SettingsActivity) {
                 final SettingsActivity sa = (SettingsActivity) activity;
                 int id = mIsAlphaMode ? R.string.lockpassword_choose_your_password_header
@@ -389,6 +391,8 @@
         }
 
         private void handleNext() {
+            if (mDone) return;
+
             final String pin = mPasswordEntry.getText().toString();
             if (TextUtils.isEmpty(pin)) {
                 return;
@@ -409,6 +413,7 @@
                     mLockPatternUtils.saveLockPassword(pin, mRequestedQuality, isFallback);
                     getActivity().setResult(RESULT_FINISHED);
                     getActivity().finish();
+                    mDone = true;
                     startActivity(RedactionInterstitial.createStartIntent(getActivity()));
                 } else {
                     CharSequence tmp = mPasswordEntry.getText();
diff --git a/src/com/android/settings/ChooseLockPattern.java b/src/com/android/settings/ChooseLockPattern.java
index 9b3ce0a..c218c8d 100644
--- a/src/com/android/settings/ChooseLockPattern.java
+++ b/src/com/android/settings/ChooseLockPattern.java
@@ -290,6 +290,7 @@
         }
 
         private Stage mUiStage = Stage.Introduction;
+        private boolean mDone = false;
 
         private Runnable mClearPatternRunnable = new Runnable() {
             public void run() {
@@ -363,6 +364,7 @@
                 }
                 updateStage(Stage.values()[savedInstanceState.getInt(KEY_UI_STAGE)]);
             }
+            mDone = false;
             return view;
         }
 
@@ -519,6 +521,7 @@
         }
 
         private void saveChosenPatternAndFinish() {
+            if (mDone) return;
             LockPatternUtils utils = mChooseLockSettingsHelper.utils();
             final boolean lockVirgin = !utils.isPatternEverChosen();
 
@@ -533,6 +536,7 @@
 
             getActivity().setResult(RESULT_FINISHED);
             getActivity().finish();
+            mDone = true;
             startActivity(RedactionInterstitial.createStartIntent(getActivity()));
         }
     }
diff --git a/src/com/android/settings/CryptKeeper.java b/src/com/android/settings/CryptKeeper.java
index 013fd3a..adc1b2d 100644
--- a/src/com/android/settings/CryptKeeper.java
+++ b/src/com/android/settings/CryptKeeper.java
@@ -155,6 +155,13 @@
     }
 
     private class DecryptTask extends AsyncTask<String, Void, Integer> {
+        private void hide(int id) {
+            View view = findViewById(id);
+            if (view != null) {
+                view.setVisibility(View.GONE);
+            }
+        }
+
         @Override
         protected Integer doInBackground(String... params) {
             final IMountService service = getMountService();
@@ -175,9 +182,18 @@
                     mLockPatternView.removeCallbacks(mClearPatternRunnable);
                     mLockPatternView.postDelayed(mClearPatternRunnable, RIGHT_PATTERN_CLEAR_TIMEOUT_MS);
                 }
+                hide(R.id.passwordEntry);
+                hide(R.id.switch_ime_button);
+                hide(R.id.lockPattern);
+                hide(R.id.status);
+                hide(R.id.owner_info);
+                hide(R.id.emergencyCallButton);
             } else if (failedAttempts == MAX_FAILED_ATTEMPTS) {
                 // Factory reset the device.
-                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
+                Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
+                intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
+                intent.putExtra(Intent.EXTRA_REASON, "CryptKeeper.MAX_FAILED_ATTEMPTS");
+                sendBroadcast(intent);
             } else if (failedAttempts == -1) {
                 // Right password, but decryption failed. Tell user bad news ...
                 setContentView(R.layout.crypt_keeper_progress);
@@ -536,7 +552,7 @@
      * @param corrupt true if userdata is corrupt, false if encryption failed
      *        partway through
      */
-    private void showFactoryReset(boolean corrupt) {
+    private void showFactoryReset(final boolean corrupt) {
         // Hide the encryption-bot to make room for the "factory reset" button
         findViewById(R.id.encroid).setVisibility(View.GONE);
 
@@ -547,7 +563,11 @@
                 @Override
             public void onClick(View v) {
                 // Factory reset the device.
-                sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
+                Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
+                intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
+                intent.putExtra(Intent.EXTRA_REASON,
+                        "CryptKeeper.showFactoryReset() corrupt=" + corrupt);
+                sendBroadcast(intent);
             }
         });
 
diff --git a/src/com/android/settings/MasterClearConfirm.java b/src/com/android/settings/MasterClearConfirm.java
index 40c2324..3521aa3 100644
--- a/src/com/android/settings/MasterClearConfirm.java
+++ b/src/com/android/settings/MasterClearConfirm.java
@@ -105,10 +105,14 @@
     private void doMasterClear() {
         if (mEraseSdCard) {
             Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
+            intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
             intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
             getActivity().startService(intent);
         } else {
-            getActivity().sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
+            Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
+            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
+            intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
+            getActivity().sendBroadcast(intent);
             // Intent handling is asynchronous -- assume it will happen soon.
         }
     }
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index de9fac7..08cfc58 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -613,6 +613,8 @@
             int titleResId, CharSequence title, boolean isShortcut, UserHandle userHandle) {
         Intent intent = onBuildStartFragmentIntent(context, fragmentName, args, titleResId,
                 title, isShortcut);
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
         context.startActivityAsUser(intent, userHandle);
     }
 
diff --git a/src/com/android/settings/accounts/AccountSettings.java b/src/com/android/settings/accounts/AccountSettings.java
index 3c0d9e9..8a183da 100644
--- a/src/com/android/settings/accounts/AccountSettings.java
+++ b/src/com/android/settings/accounts/AccountSettings.java
@@ -361,20 +361,9 @@
 
         for (int i = 0; i < accountTypes.length; i++) {
             final String accountType = accountTypes[i];
-            if (mAuthoritiesCount > 0) {
-                // Skip showing any account that does not have any of the requested authorities
-                final ArrayList<String> authoritiesForType = helper.getAuthoritiesForAccountType(
-                        accountType);
-                boolean show = false;
-                for (int j = 0; j < mAuthoritiesCount; j++) {
-                    if (authoritiesForType.contains(mAuthorities[j])) {
-                        show = true;
-                        break;
-                    }
-                }
-                if (!show) {
-                    continue;
-                }
+            // Skip showing any account that does not have any of the requested authorities
+            if (!accountTypeHasAnyRequestedAuthorities(helper, accountType)) {
+                continue;
             }
             final CharSequence label = helper.getLabelForType(getActivity(), accountType);
             if (label == null) {
@@ -418,6 +407,26 @@
         return accountTypePreferences;
     }
 
+    private boolean accountTypeHasAnyRequestedAuthorities(AuthenticatorHelper helper,
+            String accountType) {
+        if (mAuthoritiesCount == 0) {
+            // No authorities required
+            return true;
+        }
+        final ArrayList<String> authoritiesForType = helper.getAuthoritiesForAccountType(
+                accountType);
+        if (authoritiesForType == null) {
+            Log.d(TAG, "No sync authorities for account type: " + accountType);
+            return false;
+        }
+        for (int j = 0; j < mAuthoritiesCount; j++) {
+            if (authoritiesForType.contains(mAuthorities[j])) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private class AccountPreference extends Preference implements OnPreferenceClickListener {
         /**
          * Title of the tile that is shown to the user.
diff --git a/src/com/android/settings/accounts/AuthenticatorHelper.java b/src/com/android/settings/accounts/AuthenticatorHelper.java
index 6cebf5f..cc8a6d5 100644
--- a/src/com/android/settings/accounts/AuthenticatorHelper.java
+++ b/src/com/android/settings/accounts/AuthenticatorHelper.java
@@ -190,10 +190,10 @@
                 mEnabledAccountTypes.add(account.type);
             }
         }
+        buildAccountTypeToAuthoritiesMap();
         if (mListeningToAccountUpdates) {
             mListener.onAccountsUpdate(mUserHandle);
         }
-        buildAccountTypeToAuthoritiesMap();
     }
 
     @Override
diff --git a/src/com/android/settings/fuelgauge/PowerUsageDetail.java b/src/com/android/settings/fuelgauge/PowerUsageDetail.java
index 36a2444..9dca029 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageDetail.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageDetail.java
@@ -259,8 +259,14 @@
         args.putIntArray(PowerUsageDetail.EXTRA_DETAIL_TYPES, types);
         args.putDoubleArray(PowerUsageDetail.EXTRA_DETAIL_VALUES, values);
 
-        caller.startPreferencePanelAsUser(PowerUsageDetail.class.getName(), args,
-                R.string.details_title, null, new UserHandle(userId));
+        // This is a workaround, see b/17523189
+        if (userId == UserHandle.myUserId()) {
+            caller.startPreferencePanel(PowerUsageDetail.class.getName(), args,
+                    R.string.details_title, null, null, 0);
+        } else {
+            caller.startPreferencePanelAsUser(PowerUsageDetail.class.getName(), args,
+                    R.string.details_title, null, new UserHandle(userId));
+        }
     }
 
     public static final int ACTION_DISPLAY_SETTINGS = 1;
@@ -644,8 +650,9 @@
         if (mPackages == null) return;
         ActivityManager am = (ActivityManager)getActivity().getSystemService(
                 Context.ACTIVITY_SERVICE);
+        final int userId = UserHandle.getUserId(mUid);
         for (int i = 0; i < mPackages.length; i++) {
-            am.forceStopPackage(mPackages[i]);
+            am.forceStopPackageAsUser(mPackages[i], userId);
         }
         checkForceStop();
     }
diff --git a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
index f0f7307..bae9dbc 100644
--- a/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
+++ b/src/com/android/settings/inputmethod/InputMethodAndLanguageSettings.java
@@ -147,9 +147,10 @@
         mGameControllerCategory = (PreferenceCategory)findPreference(
                 "game_controller_settings_category");
 
+        final Intent startingIntent = activity.getIntent();
         // Filter out irrelevant features if invoked from IME settings button.
         mShowsOnlyFullImeAndKeyboardList = Settings.ACTION_INPUT_METHOD_SETTINGS.equals(
-                activity.getIntent().getAction());
+                startingIntent.getAction());
         if (mShowsOnlyFullImeAndKeyboardList) {
             getPreferenceScreen().removeAll();
             getPreferenceScreen().addPreference(mHardKeyboardCategory);
@@ -182,6 +183,14 @@
         mSettingsObserver = new SettingsObserver(mHandler, activity);
         mDpm = (DevicePolicyManager) (getActivity().
                 getSystemService(Context.DEVICE_POLICY_SERVICE));
+
+        // If we've launched from the keyboard layout notification, go ahead and just show the
+        // keyboard layout dialog.
+        final InputDeviceIdentifier identifier =
+                startingIntent.getParcelableExtra(Settings.EXTRA_INPUT_DEVICE_IDENTIFIER);
+        if (mShowsOnlyFullImeAndKeyboardList && identifier != null) {
+            showKeyboardLayoutDialog(identifier);
+        }
     }
 
     private void updateInputMethodSelectorSummary(int value) {
diff --git a/src/com/android/settings/wifi/AdvancedWifiSettings.java b/src/com/android/settings/wifi/AdvancedWifiSettings.java
index f86cc45..bda13ff 100644
--- a/src/com/android/settings/wifi/AdvancedWifiSettings.java
+++ b/src/com/android/settings/wifi/AdvancedWifiSettings.java
@@ -16,6 +16,8 @@
 
 package com.android.settings.wifi;
 
+import android.app.Dialog;
+import android.app.DialogFragment;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -151,8 +153,8 @@
         Preference wpsPushPref = findPreference(KEY_WPS_PUSH);
         wpsPushPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                 public boolean onPreferenceClick(Preference arg0) {
-                    WpsDialog wpsDialog = new WpsDialog(context, WpsInfo.PBC);
-                    wpsDialog.show();
+                    WpsFragment wpsFragment = new WpsFragment(WpsInfo.PBC);
+                    wpsFragment.show(getFragmentManager(), KEY_WPS_PUSH);
                     return true;
                 }
         });
@@ -161,8 +163,8 @@
         Preference wpsPinPref = findPreference(KEY_WPS_PIN);
         wpsPinPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){
                 public boolean onPreferenceClick(Preference arg0) {
-                    WpsDialog wpsDialog = new WpsDialog(context, WpsInfo.DISPLAY);
-                    wpsDialog.show();
+                    WpsFragment wpsFragment = new WpsFragment(WpsInfo.DISPLAY);
+                    wpsFragment.show(getFragmentManager(), KEY_WPS_PIN);
                     return true;
                 }
         });
@@ -315,4 +317,24 @@
         wifiIpAddressPref.setSelectable(false);
     }
 
+    /* Wrapper class for the WPS dialog to properly handle life cycle events like rotation. */
+    public static class WpsFragment extends DialogFragment {
+        private static int mWpsSetup;
+
+        // Public default constructor is required for rotation.
+        public WpsFragment() {
+            super();
+        }
+
+        public WpsFragment(int wpsSetup) {
+            super();
+            mWpsSetup = wpsSetup;
+        }
+
+        @Override
+        public Dialog onCreateDialog(Bundle savedInstanceState) {
+            return new WpsDialog(getActivity(), mWpsSetup);
+        }
+    }
+
 }