Materialize KeyChainActivity

- Set 24dip padding for the "Select a certificate" dialog.
- Put install button as Neutral button - previously it had been part of the
  content View.
- Use the material AlertController listview instead of the default ListView
  which doesn't show upper/lower borders during scroll.

Bug: 28320152
Change-Id: Ie3f3a8ad0f54e54c039b00f1419cd072aa81e89b
diff --git a/res/layout/cert_chooser.xml b/res/layout/cert_chooser.xml
deleted file mode 100644
index f6b73dc..0000000
--- a/res/layout/cert_chooser.xml
+++ /dev/null
@@ -1,22 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2011 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-<ListView xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="fill_parent"
-    android:layout_height="fill_parent"
-    android:drawSelectorOnTop="false"
-    android:textFilterEnabled="true"
-    android:choiceMode="singleChoice"
-/>
diff --git a/res/layout/cert_chooser_footer.xml b/res/layout/cert_chooser_footer.xml
index a3521e3..b253820 100644
--- a/res/layout/cert_chooser_footer.xml
+++ b/res/layout/cert_chooser_footer.xml
@@ -13,32 +13,10 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical"
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/cert_chooser_install_message"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
-    >
-    <View
-        android:layout_width="fill_parent"
-        android:layout_height="1dip"
-        android:background="@android:drawable/divider_horizontal_dark"
-    />
-    <TextView
-        android:id="@+id/cert_chooser_install_message"
-        android:layout_width="fill_parent"
-        android:layout_height="wrap_content"
-        android:layout_margin="6dip"
-    />
-    <LinearLayout
-        android:gravity="right"
-        android:layout_width="fill_parent"
-        android:layout_height="wrap_content">
-        <Button
-            android:id="@+id/cert_chooser_install_button"
-            android:layout_height="wrap_content"
-            android:layout_width="wrap_content"
-            android:text="@string/install_new_cert_button_label"
-            android:layout_margin="6dip"
-        />
-    </LinearLayout>
-</LinearLayout>
+    android:paddingStart="?android:attr/dialogPreferredPadding"
+    android:paddingEnd="?android:attr/dialogPreferredPadding"
+/>
diff --git a/res/layout/cert_chooser_header.xml b/res/layout/cert_chooser_header.xml
index 63d2c7a..0550153 100644
--- a/res/layout/cert_chooser_header.xml
+++ b/res/layout/cert_chooser_header.xml
@@ -16,5 +16,6 @@
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
-    android:padding="6dip"
+    android:paddingStart="?android:attr/dialogPreferredPadding"
+    android:paddingEnd="?android:attr/dialogPreferredPadding"
 />
diff --git a/res/layout/cert_item.xml b/res/layout/cert_item.xml
index b77e9fc..9708a41 100644
--- a/res/layout/cert_item.xml
+++ b/res/layout/cert_item.xml
@@ -18,9 +18,10 @@
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:gravity="center_vertical"
-    android:paddingRight="?android:attr/scrollbarSize"
     android:background="?android:attr/selectableItemBackground"
-    android:padding="15dip"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:paddingStart="?android:attr/dialogPreferredPadding"
+    android:paddingEnd="?android:attr/dialogPreferredPadding"
     >
     <RelativeLayout
         android:layout_width="0px"
diff --git a/src/com/android/keychain/KeyChainActivity.java b/src/com/android/keychain/KeyChainActivity.java
index b5058b2..9d03f77 100644
--- a/src/com/android/keychain/KeyChainActivity.java
+++ b/src/com/android/keychain/KeyChainActivity.java
@@ -207,67 +207,79 @@
     private void displayCertChooserDialog(final CertificateAdapter adapter) {
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 
-        TextView contextView = (TextView) View.inflate(this, R.layout.cert_chooser_header, null);
-        View footer = View.inflate(this, R.layout.cert_chooser_footer, null);
-
-        final ListView lv = (ListView) View.inflate(this, R.layout.cert_chooser, null);
-        lv.addHeaderView(contextView, null, false);
-        lv.addFooterView(footer, null, false);
-        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
-        lv.setAdapter(adapter);
-        builder.setView(lv);
-
-        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
-
-                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                    lv.setItemChecked(position, true);
-                    adapter.notifyDataSetChanged();
-                }
-        });
-
         boolean empty = adapter.mAliases.isEmpty();
         int negativeLabel = empty ? android.R.string.cancel : R.string.deny_button;
-        builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener() {
+        builder.setNeutralButton(negativeLabel, new DialogInterface.OnClickListener() {
             @Override public void onClick(DialogInterface dialog, int id) {
                 dialog.cancel(); // will cause OnDismissListener to be called
             }
         });
 
         String title;
+        int selectedItem = -1;
         Resources res = getResources();
         if (empty) {
             title = res.getString(R.string.title_no_certs);
         } else {
             title = res.getString(R.string.title_select_cert);
             String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS);
+
             if (alias != null) {
                 // if alias was requested, set it if found
                 int adapterPosition = adapter.mAliases.indexOf(alias);
                 if (adapterPosition != -1) {
-                    int listViewPosition = adapterPosition+1;
-                    lv.setItemChecked(listViewPosition, true);
+                    // increase by 1 to account for item 0 being the header.
+                    selectedItem = adapterPosition + 1;
                 }
             } else if (adapter.mAliases.size() == 1) {
                 // if only one choice, preselect it
-                int adapterPosition = 0;
-                int listViewPosition = adapterPosition+1;
-                lv.setItemChecked(listViewPosition, true);
+                selectedItem = 1;
             }
 
             builder.setPositiveButton(R.string.allow_button, new DialogInterface.OnClickListener() {
                 @Override public void onClick(DialogInterface dialog, int id) {
-                    int listViewPosition = lv.getCheckedItemPosition();
-                    int adapterPosition = listViewPosition-1;
-                    String alias = ((adapterPosition >= 0)
-                                    ? adapter.getItem(adapterPosition)
-                                    : null);
-                    finish(alias);
+                    if (dialog instanceof AlertDialog) {
+                        ListView lv = ((AlertDialog) dialog).getListView();
+                        int listViewPosition = lv.getCheckedItemPosition();
+                        int adapterPosition = listViewPosition-1;
+                        String alias = ((adapterPosition >= 0)
+                                        ? adapter.getItem(adapterPosition)
+                                        : null);
+                        finish(alias);
+                    } else {
+                        Log.wtf(TAG, "Expected AlertDialog, got " + dialog, new Exception());
+                        finish(null);
+                    }
                 }
             });
         }
         builder.setTitle(title);
-        final Dialog dialog = builder.create();
+        builder.setSingleChoiceItems(adapter, selectedItem, null);
+        builder.setNegativeButton(R.string.install_new_cert_button_label,
+                new DialogInterface.OnClickListener() {
+                    @Override public void onClick(DialogInterface dialog, int id) {
+                        // remove dialog so that we will recreate with
+                        // possibly new content after install returns
+                        dialog.dismiss();
+                        Credentials.getInstance().install(KeyChainActivity.this);
+                    }
+                });
+        final AlertDialog dialog = builder.create();
 
+        // Show text above and below the list, to explain what the certificate will be used for
+        // and how to install another one respectively.
+        TextView contextView = (TextView) View.inflate(this, R.layout.cert_chooser_header, null);
+        TextView installText = (TextView) View.inflate(this, R.layout.cert_chooser_footer, null);
+
+        final ListView lv = dialog.getListView();
+        lv.addHeaderView(contextView, null, false);
+        lv.addFooterView(installText, null, false);
+        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+                lv.setItemChecked(position, true);
+                adapter.notifyDataSetChanged();
+            }
+        });
 
         // getTargetPackage guarantees that the returned string is
         // supplied by the system, so that an application can not
@@ -297,19 +309,8 @@
 
         String installMessage = String.format(res.getString(R.string.install_new_cert_message),
                                               Credentials.EXTENSION_PFX, Credentials.EXTENSION_P12);
-        TextView installText = (TextView) footer.findViewById(R.id.cert_chooser_install_message);
         installText.setText(installMessage);
 
-        Button installButton = (Button) footer.findViewById(R.id.cert_chooser_install_button);
-        installButton.setOnClickListener(new View.OnClickListener() {
-            @Override public void onClick(View v) {
-                // remove dialog so that we will recreate with
-                // possibly new content after install returns
-                dialog.dismiss();
-                Credentials.getInstance().install(KeyChainActivity.this);
-            }
-        });
-
         dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
             @Override public void onCancel(DialogInterface dialog) {
                 finish(null);