Move radio buttons to left, install button to list

Change-Id: I499b1052ff352f163d3166f1ebb03a21b8baf622
Fix: 28320152
diff --git a/res/drawable/ic_menu_add.xml b/res/drawable/ic_menu_add.xml
new file mode 100644
index 0000000..3387f29
--- /dev/null
+++ b/res/drawable/ic_menu_add.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2015 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.
+-->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24.0"
+    android:viewportHeight="24.0"
+    android:tint="?android:attr/colorAccent">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
+</vector>
diff --git a/res/layout/cert_chooser_footer.xml b/res/layout/cert_chooser_footer.xml
deleted file mode 100644
index b253820..0000000
--- a/res/layout/cert_chooser_footer.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.
--->
-<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"
-    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 0550153..d004ef6 100644
--- a/res/layout/cert_chooser_header.xml
+++ b/res/layout/cert_chooser_header.xml
@@ -18,4 +18,5 @@
     android:layout_height="wrap_content"
     android:paddingStart="?android:attr/dialogPreferredPadding"
     android:paddingEnd="?android:attr/dialogPreferredPadding"
+    android:paddingBottom="20dp"
 />
diff --git a/res/layout/cert_install.xml b/res/layout/cert_install.xml
new file mode 100644
index 0000000..c10b32a
--- /dev/null
+++ b/res/layout/cert_install.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 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.
+-->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    android:gravity="center_vertical"
+    android:background="?android:attr/selectableItemBackground"
+    android:minHeight="?android:attr/listPreferredItemHeight"
+    android:paddingStart="?android:attr/dialogPreferredPadding"
+    android:paddingEnd="?android:attr/dialogPreferredPadding"
+    >
+    <ImageView
+        android:id="@+id/add_cert_icon"
+        android:layout_width="42dp"
+        android:layout_height="wrap_content"
+        android:gravity="center_vertical"
+        android:paddingEnd="@dimen/radio_label_padding"
+        android:clickable="false"
+        android:focusable="false"
+        android:layout_weight="0"
+        android:src="@drawable/ic_menu_add"
+        android:tint="?android:attr/textColorSecondary"
+        android:tintMode="src_in"
+    />
+    <TextView
+        android:id="@+id/install_certificate"
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:text="@string/install_new_cert_button_label"
+        android:singleLine="true"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:ellipsize="marquee"
+    />
+</LinearLayout>
diff --git a/res/layout/cert_item.xml b/res/layout/cert_item.xml
index 9708a41..0f07cd5 100644
--- a/res/layout/cert_item.xml
+++ b/res/layout/cert_item.xml
@@ -23,6 +23,16 @@
     android:paddingStart="?android:attr/dialogPreferredPadding"
     android:paddingEnd="?android:attr/dialogPreferredPadding"
     >
+    <RadioButton
+        android:id="@+id/cert_item_selected"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:gravity="center_vertical"
+        android:clickable="false"
+        android:focusable="false"
+        android:layout_weight="0"
+        android:paddingEnd="@dimen/radio_label_padding"
+    />
     <RelativeLayout
         android:layout_width="0px"
         android:layout_height="wrap_content"
@@ -47,13 +57,4 @@
             android:textColor="?android:attr/textColorSecondary"
         />
     </RelativeLayout>
-    <RadioButton
-        android:id="@+id/cert_item_selected"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:gravity="center_vertical"
-        android:clickable="false"
-        android:focusable="false"
-        android:layout_weight="0"
-    />
 </LinearLayout>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
new file mode 100644
index 0000000..6a11f35
--- /dev/null
+++ b/res/values/dimens.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+* Copyright (C) 2016 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.
+*/
+-->
+
+<resources xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- Distance between a radio button and the label referring to it. -->
+    <dimen name="radio_label_padding">10dp</dimen>
+</resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 166555c..b1a8df6 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -32,10 +32,10 @@
     <string name="install_new_cert_message">You can install certificates from a PKCS#12 file with a %1$s or a %2$s extension located in external storage.</string>
 
     <!-- Label of button to send the user to the CertInstaller to install a certificate -->
-    <string name="install_new_cert_button_label">Install</string>
+    <string name="install_new_cert_button_label">Install certificate</string>
 
-    <!-- Label of button to send the allow the application certificate request -->
-    <string name="allow_button">Allow</string>
+    <!-- Label of button to select the certificate and allow the application certificate request -->
+    <string name="allow_button">Select</string>
 
     <!-- Label of button to send the deny the application certificate request -->
     <string name="deny_button">Deny</string>
diff --git a/src/com/android/keychain/KeyChainActivity.java b/src/com/android/keychain/KeyChainActivity.java
index 9d03f77..ffbdd4b 100644
--- a/src/com/android/keychain/KeyChainActivity.java
+++ b/src/com/android/keychain/KeyChainActivity.java
@@ -209,7 +209,7 @@
 
         boolean empty = adapter.mAliases.isEmpty();
         int negativeLabel = empty ? android.R.string.cancel : R.string.deny_button;
-        builder.setNeutralButton(negativeLabel, new DialogInterface.OnClickListener() {
+        builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener() {
             @Override public void onClick(DialogInterface dialog, int id) {
                 dialog.cancel(); // will cause OnDismissListener to be called
             }
@@ -255,29 +255,30 @@
         }
         builder.setTitle(title);
         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.
+        // 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.addFooterView(View.inflate(this, R.layout.cert_install, null));
         lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-                lv.setItemChecked(position, true);
-                adapter.notifyDataSetChanged();
+                if (position == 0) {
+                    // Header. Just text; ignore clicks.
+                    return;
+                } else if (position == adapter.getCount() + 1) {
+                    // Footer. Remove dialog so that we will recreate with possibly new content
+                    // after install returns.
+                    dialog.dismiss();
+                    Credentials.getInstance().install(KeyChainActivity.this);
+                } else {
+                    dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
+                    lv.setItemChecked(position, true);
+                    adapter.notifyDataSetChanged();
+                }
             }
         });
 
@@ -307,10 +308,14 @@
         }
         contextView.setText(contextMessage);
 
-        String installMessage = String.format(res.getString(R.string.install_new_cert_message),
-                                              Credentials.EXTENSION_PFX, Credentials.EXTENSION_P12);
-        installText.setText(installMessage);
-
+        if (selectedItem == -1) {
+            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
+                @Override
+                public void onShow(DialogInterface dialogInterface) {
+                     dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
+                }
+            });
+        }
         dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
             @Override public void onCancel(DialogInterface dialog) {
                 finish(null);