Support rotation in new dialog based KeyChainActivity

Using showDialog, the framework manged the dialog for me. So on
rotation, it skiped onResume and right to onCreateDialog but then the
mCertificateAdapter was null. I considered showing the list first, and
filling in the content later, more like I do
TrustedCredentialsSettings. However, here the dialog content is
different if there are no entries.

To fix, I just don't use showDialog. Then onResume is called and I
recreate the dialog the same way in both cases.

Bug: 4967378
Change-Id: I306e3a32069dce4e00864be79b50e081869851d5
diff --git a/src/com/android/keychain/KeyChainActivity.java b/src/com/android/keychain/KeyChainActivity.java
index 0702c3f..80b537a 100644
--- a/src/com/android/keychain/KeyChainActivity.java
+++ b/src/com/android/keychain/KeyChainActivity.java
@@ -59,8 +59,6 @@
 
     private static final int REQUEST_UNLOCK = 1;
 
-    private static final int DIALOG_CERT_CHOOSER = 0;
-
     private static enum State { INITIAL, UNLOCK_REQUESTED };
 
     private State mState;
@@ -71,8 +69,6 @@
     // be done on the UI thread.
     private KeyStore mKeyStore = KeyStore.getInstance();
 
-    private CertificateAdapter mCertificateAdapter;
-
     // the KeyStore.state operation is safe to do on the UI thread, it
     // does not do a file operation.
     private boolean isKeyStoreUnlocked() {
@@ -107,7 +103,7 @@
                     // onActivityResult is called with REQUEST_UNLOCK
                     return;
                 }
-                new AliasLoader().execute();
+                showCertChooserDialog();
                 return;
             case UNLOCK_REQUESTED:
                 // we've already asked, but have not heard back, probably just rotated.
@@ -118,6 +114,10 @@
         }
     }
 
+    private void showCertChooserDialog() {
+        new AliasLoader().execute();
+    }
+
     private class AliasLoader extends AsyncTask<Void, Void, CertificateAdapter> {
         @Override protected CertificateAdapter doInBackground(Void... params) {
             String[] aliasArray = mKeyStore.saw(Credentials.USER_PRIVATE_KEY);
@@ -127,26 +127,18 @@
             Collections.sort(aliasList);
             return new CertificateAdapter(aliasList);
         }
-        @Override protected void onPostExecute(CertificateAdapter result) {
-            mCertificateAdapter = result;
-            showDialog(DIALOG_CERT_CHOOSER);
+        @Override protected void onPostExecute(CertificateAdapter adapter) {
+            displayCertChooserDialog(adapter);
         }
     }
 
-    @Override protected Dialog onCreateDialog(int id, Bundle args) {
-        if (id == DIALOG_CERT_CHOOSER) {
-            return createCertChooserDialog();
-        }
-        throw new AssertionError();
-    }
-
-    private Dialog createCertChooserDialog() {
+    private void displayCertChooserDialog(final CertificateAdapter adapter) {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 
         View view = View.inflate(this, R.layout.cert_chooser, null);
         builder.setView(view);
 
-        boolean empty = mCertificateAdapter.mAliases.isEmpty();
+        boolean empty = adapter.mAliases.isEmpty();
         int negativeLabel = empty ? android.R.string.cancel : R.string.deny_button;
         builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener() {
             @Override public void onClick(DialogInterface dialog, int id) {
@@ -161,10 +153,10 @@
         } else {
             title = res.getString(R.string.title_select_cert);
             final ListView lv = (ListView) view.findViewById(R.id.cert_chooser_cert_list);
-            lv.setAdapter(mCertificateAdapter);
+            lv.setAdapter(adapter);
             String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS);
             if (alias != null) {
-                int position = mCertificateAdapter.mAliases.indexOf(alias);
+                int position = adapter.mAliases.indexOf(alias);
                 if (position != -1) {
                     lv.setItemChecked(position, true);
                 }
@@ -174,7 +166,7 @@
                 @Override public void onClick(DialogInterface dialog, int id) {
                     int pos = lv.getCheckedItemPosition();
                     String alias = ((pos != ListView.INVALID_POSITION)
-                                    ? mCertificateAdapter.getItem(pos)
+                                    ? adapter.getItem(pos)
                                     : null);
                     finish(alias);
                 }
@@ -183,6 +175,7 @@
             lv.setVisibility(View.VISIBLE);
         }
         builder.setTitle(title);
+        final Dialog dialog = builder.create();
 
         PendingIntent sender = getIntent().getParcelableExtra(KeyChain.EXTRA_SENDER);
         if (sender == null) {
@@ -234,18 +227,17 @@
             @Override public void onClick(View v) {
                 // remove dialog so that we will recreate with
                 // possibly new content after install returns
-                removeDialog(DIALOG_CERT_CHOOSER);
+                dialog.dismiss();
                 Credentials.getInstance().install(KeyChainActivity.this);
             }
         });
 
-        Dialog dialog = builder.create();
         dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
             @Override public void onCancel(DialogInterface dialog) {
                 finish(null);
             }
         });
-        return dialog;
+        dialog.show();
     }
 
     private class CertificateAdapter extends BaseAdapter {
@@ -338,7 +330,7 @@
         switch (requestCode) {
             case REQUEST_UNLOCK:
                 if (isKeyStoreUnlocked()) {
-                    showDialog(DIALOG_CERT_CHOOSER);
+                    showCertChooserDialog();
                 } else {
                     // user must have canceled unlock, give up
                     finish(null);