[ChooseAccount/AccountManager.newChooseAccountIntent Bug Fixes]

Fixing lots of bugs related to the ChooseAccount Activities.
1. Fix jank which is seen when no accounts are present on the device.
2. After addition of the account, return to the user.
3. Don't crash when the user provides null to allowableAccountTypes.
4. Updated documentation of AccountManager#newChooseAccountIntent.
5. Fix NPE.

Bug: 13104800
Bug: 17926560
Bug: 9626001

Change-Id: I0d1913e46560cfb458526a7c930a38049602d8f1
diff --git a/core/java/android/accounts/AccountManager.java b/core/java/android/accounts/AccountManager.java
index fd40d99..31e129b 100644
--- a/core/java/android/accounts/AccountManager.java
+++ b/core/java/android/accounts/AccountManager.java
@@ -2326,7 +2326,8 @@
      * shown. If not specified then this field will not limit the displayed accounts.
      * @param allowableAccountTypes an optional string array of account types. These are used
      * both to filter the shown accounts and to filter the list of account types that are shown
-     * when adding an account.
+     * when adding an account. If not specified then this field will not limit the displayed
+     * account types when adding an account.
      * @param alwaysPromptForAccount if set the account chooser screen is always shown, otherwise
      * it is only shown when there is more than one account from which to choose
      * @param descriptionOverrideText if non-null this string is used as the description in the
diff --git a/core/java/android/accounts/ChooseTypeAndAccountActivity.java b/core/java/android/accounts/ChooseTypeAndAccountActivity.java
index 82c2159..c06b462 100644
--- a/core/java/android/accounts/ChooseTypeAndAccountActivity.java
+++ b/core/java/android/accounts/ChooseTypeAndAccountActivity.java
@@ -132,7 +132,6 @@
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
         if (Log.isLoggable(TAG, Log.VERBOSE)) {
             Log.v(TAG, "ChooseTypeAndAccountActivity.onCreate(savedInstanceState="
                     + savedInstanceState + ")");
@@ -192,7 +191,6 @@
         mAlwaysPromptForAccount = intent.getBooleanExtra(EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT, false);
         mDescriptionOverride = intent.getStringExtra(EXTRA_DESCRIPTION_TEXT_OVERRIDE);
 
-        // Need to do this once here to request the window feature. Can't do it in onResume
         mAccounts = getAcceptableAccountChoices(AccountManager.get(this));
         if (mAccounts.isEmpty()
                 && mDisallowAddAccounts) {
@@ -200,17 +198,11 @@
             setContentView(R.layout.app_not_authorized);
             mDontShowPicker = true;
         }
-    }
 
-    @Override
-    protected void onResume() {
-        super.onResume();
-
-        if (mDontShowPicker) return;
-
-        final AccountManager accountManager = AccountManager.get(this);
-
-        mAccounts = getAcceptableAccountChoices(accountManager);
+        if (mDontShowPicker) {
+            super.onCreate(savedInstanceState);
+            return;
+        }
 
         // In cases where the activity does not need to show an account picker, cut the chase
         // and return the result directly. Eg:
@@ -220,6 +212,7 @@
             // If there are no relevant accounts and only one relevant account type go directly to
             // add account. Otherwise let the user choose.
             if (mAccounts.isEmpty()) {
+                setNonLabelThemeAndCallSuperCreate(savedInstanceState);
                 if (mSetOfRelevantAccountTypes.size() == 1) {
                     runAddAccountForAuthenticator(mSetOfRelevantAccountTypes.iterator().next());
                 } else {
@@ -231,6 +224,7 @@
             // if there is only one allowable account return it
             if (!mAlwaysPromptForAccount && mAccounts.size() == 1) {
                 Account account = mAccounts.get(0);
+                super.onCreate(savedInstanceState);
                 setResultAndFinish(account.name, account.type);
                 return;
             }
@@ -240,8 +234,7 @@
         mSelectedItemIndex = getItemIndexToSelect(
             mAccounts, mSelectedAccountName, mSelectedAddNewAccount);
 
-        // Cannot set content view until we know that mPendingRequest is not null, otherwise
-        // would cause screen flicker.
+        super.onCreate(savedInstanceState);
         setContentView(R.layout.choose_type_and_account);
         overrideDescriptionIfSupplied(mDescriptionOverride);
         populateUIAccountList(listItems);
@@ -409,6 +402,17 @@
         finish();
     }
 
+    /**
+     * The default activity theme shows label at the top. Set a theme which does
+     * not show label, which effectively makes the activity invisible. Note that
+     * no content is being set. If something gets set, using this theme may be
+     * useless.
+     */
+    private void setNonLabelThemeAndCallSuperCreate(Bundle savedInstanceState) {
+        setTheme(R.style.Theme_Material_Light_Dialog_NoActionBar);
+        super.onCreate(savedInstanceState);
+    }
+
     private void onAccountSelected(Account account) {
       Log.d(TAG, "selected account " + account);
       setResultAndFinish(account.name, account.type);
@@ -489,8 +493,7 @@
               mCallingUid);
       ArrayList<Account> accountsToPopulate = new ArrayList<Account>(accounts.length);
       for (Account account : accounts) {
-          if (mSetOfAllowableAccounts != null
-                  && !mSetOfAllowableAccounts.contains(account)) {
+          if (mSetOfAllowableAccounts != null && !mSetOfAllowableAccounts.contains(account)) {
               continue;
           }
           if (mSetOfRelevantAccountTypes != null
@@ -503,7 +506,7 @@
     }
 
     /**
-     * Return a set of account types speficied by the intent as well as supported by the
+     * Return a set of account types specified by the intent as well as supported by the
      * AccountManager.
      */
     private Set<String> getReleventAccountTypes(final Intent intent) {
@@ -512,14 +515,16 @@
       Set<String> setOfRelevantAccountTypes = null;
       final String[] allowedAccountTypes =
               intent.getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY);
-      if (allowedAccountTypes != null) {
-          setOfRelevantAccountTypes = Sets.newHashSet(allowedAccountTypes);
-          AuthenticatorDescription[] descs = AccountManager.get(this).getAuthenticatorTypes();
-          Set<String> supportedAccountTypes = new HashSet<String>(descs.length);
-          for (AuthenticatorDescription desc : descs) {
-              supportedAccountTypes.add(desc.type);
-          }
-          setOfRelevantAccountTypes.retainAll(supportedAccountTypes);
+        AuthenticatorDescription[] descs = AccountManager.get(this).getAuthenticatorTypes();
+        Set<String> supportedAccountTypes = new HashSet<String>(descs.length);
+        for (AuthenticatorDescription desc : descs) {
+            supportedAccountTypes.add(desc.type);
+        }
+        if (allowedAccountTypes != null) {
+            setOfRelevantAccountTypes = Sets.newHashSet(allowedAccountTypes);
+            setOfRelevantAccountTypes.retainAll(supportedAccountTypes);
+        } else {
+            setOfRelevantAccountTypes = supportedAccountTypes;
       }
       return setOfRelevantAccountTypes;
     }