Fix a bug in the account chooser where relaunching an in-progress flow
results in a blank screen.

The fix involves making the first activity just call the account
type chooser to get the account type to add, rather than having
the account type chooser also kick off the AccountManager.addAccount()
request itself. Instead the first activity will get the type
back and then call AccountManager.addAccount()

Bug: 5346810
Change-Id: I4a0cf2370971b98f8ee0910f5401d97e999e546b
diff --git a/core/java/android/accounts/ChooseAccountTypeActivity.java b/core/java/android/accounts/ChooseAccountTypeActivity.java
index 5239e8c..448b2c0 100644
--- a/core/java/android/accounts/ChooseAccountTypeActivity.java
+++ b/core/java/android/accounts/ChooseAccountTypeActivity.java
@@ -33,7 +33,6 @@
 import android.widget.TextView;
 import com.android.internal.R;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -43,7 +42,7 @@
 /**
  * @hide
  */
-public class ChooseAccountTypeActivity extends Activity implements AccountManagerCallback<Bundle> {
+public class ChooseAccountTypeActivity extends Activity {
     private static final String TAG = "AccountManager";
 
     private HashMap<String, AuthInfo> mTypeToAuthenticatorInfo = new HashMap<String, AuthInfo>();
@@ -52,7 +51,6 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        setContentView(R.layout.choose_account_type);
 
         // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes
         Set<String> setOfAllowableAccountTypes = null;
@@ -90,10 +88,11 @@
         }
 
         if (mAuthenticatorInfosToDisplay.size() == 1) {
-            runAddAccountForAuthenticator(mAuthenticatorInfosToDisplay.get(0));
+            setResultAndFinish(mAuthenticatorInfosToDisplay.get(0).desc.type);
             return;
         }
 
+        setContentView(R.layout.choose_account_type);
         // Setup the list
         ListView list = (ListView) findViewById(android.R.id.list);
         // Use an existing ListAdapter that will map an array of strings to TextViews
@@ -103,11 +102,20 @@
         list.setTextFilterEnabled(false);
         list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
-                runAddAccountForAuthenticator(mAuthenticatorInfosToDisplay.get(position));
+                setResultAndFinish(mAuthenticatorInfosToDisplay.get(position).desc.type);
             }
         });
     }
 
+    private void setResultAndFinish(final String type) {
+        Bundle bundle = new Bundle();
+        bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, type);
+        setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
+        Log.d(TAG, "ChooseAccountTypeActivity.setResultAndFinish: "
+                + "selected account type " + type);
+        finish();
+    }
+
     private void buildTypeToAuthDescriptionMap() {
         for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) {
             String name = null;
@@ -136,42 +144,6 @@
         }
     }
 
-    protected void runAddAccountForAuthenticator(AuthInfo authInfo) {
-        Log.d(TAG, "selected account type " + authInfo.name);
-        final Bundle options = getIntent().getBundleExtra(
-                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE);
-        final String[] requiredFeatures = getIntent().getStringArrayExtra(
-                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY);
-        final String authTokenType = getIntent().getStringExtra(
-                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING);
-        AccountManager.get(this).addAccount(authInfo.desc.type, authTokenType, requiredFeatures,
-                options, this, this, null /* Handler */);
-    }
-
-    public void run(final AccountManagerFuture<Bundle> accountManagerFuture) {
-        try {
-            Bundle accountManagerResult = accountManagerFuture.getResult();
-            Bundle bundle = new Bundle();
-            bundle.putString(AccountManager.KEY_ACCOUNT_NAME,
-                    accountManagerResult.getString(AccountManager.KEY_ACCOUNT_NAME));
-            bundle.putString(AccountManager.KEY_ACCOUNT_TYPE,
-                    accountManagerResult.getString(AccountManager.KEY_ACCOUNT_TYPE));
-            setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
-            finish();
-            return;
-        } catch (OperationCanceledException e) {
-            setResult(Activity.RESULT_CANCELED);
-            finish();
-            return;
-        } catch (IOException e) {
-        } catch (AuthenticatorException e) {
-        }
-        Bundle bundle = new Bundle();
-        bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "error communicating with server");
-        setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
-        finish();
-    }
-
     private static class AuthInfo {
         final AuthenticatorDescription desc;
         final String name;
diff --git a/core/java/android/accounts/ChooseTypeAndAccountActivity.java b/core/java/android/accounts/ChooseTypeAndAccountActivity.java
index 852c4dd..8cc2002 100644
--- a/core/java/android/accounts/ChooseTypeAndAccountActivity.java
+++ b/core/java/android/accounts/ChooseTypeAndAccountActivity.java
@@ -36,6 +36,7 @@
 import android.widget.TextView;
 import com.android.internal.R;
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -44,7 +45,8 @@
 /**
  * @hide
  */
-public class ChooseTypeAndAccountActivity extends Activity {
+public class ChooseTypeAndAccountActivity extends Activity
+        implements AccountManagerCallback<Bundle> {
     private static final String TAG = "AccountManager";
 
     /**
@@ -211,10 +213,9 @@
     protected void onActivityResult(final int requestCode, final int resultCode,
             final Intent data) {
         if (resultCode == RESULT_OK && data != null) {
-            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
             String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
-            if (accountName != null && accountType != null) {
-                setResultAndFinish(accountName, accountType);
+            if (accountType != null) {
+                runAddAccountForAuthenticator(accountType);
                 return;
             }
         }
@@ -223,6 +224,43 @@
         finish();
     }
 
+    protected void runAddAccountForAuthenticator(String type) {
+        Log.d(TAG, "selected account type " + type);
+        final Bundle options = getIntent().getBundleExtra(
+                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE);
+        final String[] requiredFeatures = getIntent().getStringArrayExtra(
+                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY);
+        final String authTokenType = getIntent().getStringExtra(
+                ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING);
+        AccountManager.get(this).addAccount(type, authTokenType, requiredFeatures,
+                options, this, this, null /* Handler */);
+    }
+
+    public void run(final AccountManagerFuture<Bundle> accountManagerFuture) {
+        try {
+            final Bundle accountManagerResult = accountManagerFuture.getResult();
+            final String name = accountManagerResult.getString(AccountManager.KEY_ACCOUNT_NAME);
+            final String type = accountManagerResult.getString(AccountManager.KEY_ACCOUNT_TYPE);
+            if (name != null && type != null) {
+                final Bundle bundle = new Bundle();
+                bundle.putString(AccountManager.KEY_ACCOUNT_NAME, name);
+                bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, type);
+                setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
+                finish();
+                return;
+            }
+        } catch (OperationCanceledException e) {
+            setResult(Activity.RESULT_CANCELED);
+            finish();
+            return;
+        } catch (IOException e) {
+        } catch (AuthenticatorException e) {
+        }
+        Bundle bundle = new Bundle();
+        bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "error communicating with server");
+        setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
+        finish();
+    }
 
     private Drawable getDrawableForType(
             final HashMap<String, AuthenticatorDescription> typeToAuthDescription,
@@ -266,6 +304,7 @@
 
     private void startChooseAccountTypeActivity() {
         final Intent intent = new Intent(this, ChooseAccountTypeActivity.class);
+        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
         intent.putExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
                 getIntent().getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY));
         intent.putExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,