Moves the encryption interstitial to use the list layout

bug:27206957
Change-Id: Ib577440a2696539f44021bb4ba0100e9f1699c6c
diff --git a/res/layout/encryption_interstitial.xml b/res/layout/encryption_interstitial.xml
deleted file mode 100644
index 162ad0e..0000000
--- a/res/layout/encryption_interstitial.xml
+++ /dev/null
@@ -1,57 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-
-<!--
-  ~ Copyright (C) 2014 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:orientation="vertical"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:paddingStart="?attr/side_margin"
-        android:paddingEnd="?attr/side_margin">
-
-    <TextView
-            android:id="@+id/encryption_message"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_marginTop="16dp"
-            android:layout_marginStart="?android:attr/listPreferredItemPaddingStart"
-            android:layout_marginEnd="?android:attr/listPreferredItemPaddingEnd"
-            android:textAppearance="?android:attr/textAppearanceMedium" />
-
-    <RadioGroup
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_marginTop="16dp"
-            android:layout_marginStart="?android:attr/listPreferredItemPaddingStart"
-            android:layout_marginEnd="?android:attr/listPreferredItemPaddingEnd"
-            android:checkedButton="@+id/encrypt_require_password">
-
-        <RadioButton
-                android:id="@+id/encrypt_require_password"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_marginTop="16dp" />
-
-        <RadioButton
-                android:id="@+id/encrypt_dont_require_password"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_marginTop="16dp" />
-
-    </RadioGroup>
-
-</LinearLayout>
diff --git a/res/layout/encryption_interstitial_header.xml b/res/layout/encryption_interstitial_header.xml
new file mode 100644
index 0000000..8d36432
--- /dev/null
+++ b/res/layout/encryption_interstitial_header.xml
@@ -0,0 +1,27 @@
+<?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.
+-->
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:gravity="center_vertical"
+    android:minHeight="56dp"
+    android:paddingTop="16dp"
+    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
+    android:paddingBottom="16dp"
+    android:textAppearance="?android:attr/textAppearanceMedium" />
diff --git a/res/layout/setup_encryption_interstitial.xml b/res/layout/setup_encryption_interstitial.xml
deleted file mode 100644
index 8a36e30..0000000
--- a/res/layout/setup_encryption_interstitial.xml
+++ /dev/null
@@ -1,63 +0,0 @@
-<?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
--->
-
-<com.android.setupwizardlib.SetupWizardLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:settings="http://schemas.android.com/apk/res-auto"
-    android:id="@+id/setup_wizard_layout"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    settings:suwBackgroundTile="@drawable/setup_illustration_tile"
-    settings:suwHeaderText="@string/encryption_interstitial_header"
-    settings:suwIllustrationHorizontalTile="@drawable/setup_illustration_horizontal_tile"
-    settings:suwIllustrationImage="@drawable/setup_illustration_lock_screen">
-
-    <LinearLayout
-        style="@style/SuwContentFrame"
-        android:layout_width="match_parent"
-        android:layout_height="match_parent"
-        android:orientation="vertical">
-
-        <TextView
-            android:id="@+id/encryption_message"
-            style="@style/SuwDescription"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content" />
-
-        <RadioGroup
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:layout_marginTop="16dp"
-            android:checkedButton="@+id/encrypt_require_password">
-
-            <RadioButton
-                android:id="@+id/encrypt_require_password"
-                style="@style/SuwRadioButton"
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content" />
-
-            <RadioButton
-                android:id="@+id/encrypt_dont_require_password"
-                style="@style/SuwRadioButton"
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content" />
-
-        </RadioGroup>
-
-    </LinearLayout>
-
-</com.android.setupwizardlib.SetupWizardLayout>
diff --git a/res/layout/setup_encryption_interstitial_header.xml b/res/layout/setup_encryption_interstitial_header.xml
new file mode 100644
index 0000000..82344bb
--- /dev/null
+++ b/res/layout/setup_encryption_interstitial_header.xml
@@ -0,0 +1,28 @@
+<?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.
+-->
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:gravity="center_vertical"
+    android:minHeight="56dp"
+    android:paddingBottom="@dimen/suw_description_margin_bottom"
+    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
+    android:paddingTop="@dimen/suw_description_margin_top"
+    android:lineSpacingExtra="@dimen/suw_description_line_spacing_extra"
+    android:textAppearance="?android:attr/textAppearanceListItem" />
diff --git a/res/xml/security_settings_encryption_interstitial.xml b/res/xml/security_settings_encryption_interstitial.xml
new file mode 100644
index 0000000..6ee6bdd
--- /dev/null
+++ b/res/xml/security_settings_encryption_interstitial.xml
@@ -0,0 +1,30 @@
+<?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.
+-->
+
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+    android:title="@string/lock_settings_picker_title"
+    android:key="lock_settings_picker">
+
+    <Preference
+        android:key="encrypt_require_password"
+        android:persistent="false"/>
+
+    <Preference
+        android:key="encrypt_dont_require_password"
+        android:persistent="false"/>
+
+</PreferenceScreen>
diff --git a/src/com/android/settings/EncryptionInterstitial.java b/src/com/android/settings/EncryptionInterstitial.java
index b237813..16c5a55 100644
--- a/src/com/android/settings/EncryptionInterstitial.java
+++ b/src/com/android/settings/EncryptionInterstitial.java
@@ -22,17 +22,14 @@
 import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 import android.content.DialogInterface;
-import android.content.DialogInterface.OnClickListener;
 import android.content.Intent;
 import android.os.Bundle;
 import android.os.UserHandle;
+import android.support.v7.preference.Preference;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
-import android.view.ViewGroup;
 import android.view.accessibility.AccessibilityManager;
-import android.widget.Button;
-import android.widget.RadioButton;
 import android.widget.TextView;
 
 import com.android.internal.logging.MetricsProto.MetricsEvent;
@@ -63,10 +60,6 @@
     public static Intent createStartIntent(Context ctx, int quality,
             boolean requirePasswordDefault, Intent unlockMethodIntent) {
         return new Intent(ctx, EncryptionInterstitial.class)
-                .putExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, true)
-                .putExtra(EXTRA_PREFS_SET_BACK_TEXT, (String) null)
-                .putExtra(EXTRA_PREFS_SET_NEXT_TEXT, ctx.getString(
-                        R.string.encryption_continue_button))
                 .putExtra(EXTRA_PASSWORD_QUALITY, quality)
                 .putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID, R.string.encryption_interstitial_header)
                 .putExtra(EXTRA_REQUIRE_PASSWORD, requirePasswordDefault)
@@ -74,12 +67,15 @@
     }
 
     public static class EncryptionInterstitialFragment extends SettingsPreferenceFragment
-            implements View.OnClickListener, OnClickListener {
+            implements DialogInterface.OnClickListener {
 
         private static final int ACCESSIBILITY_WARNING_DIALOG = 1;
-        private RadioButton mRequirePasswordToDecryptButton;
-        private RadioButton mDontRequirePasswordToDecryptButton;
-        private TextView mEncryptionMessage;
+        private static final String KEY_ENCRYPT_REQUIRE_PASSWORD = "encrypt_require_password";
+        private static final String KEY_ENCRYPT_DONT_REQUIRE_PASSWORD =
+                "encrypt_dont_require_password";
+
+        private Preference mRequirePasswordToDecrypt;
+        private Preference mDontRequirePasswordToDecrypt;
         private boolean mPasswordRequired;
         private Intent mUnlockMethodIntent;
 
@@ -89,20 +85,13 @@
         }
 
         @Override
-        public View onCreateView(LayoutInflater inflater, ViewGroup container,
-                Bundle savedInstanceState) {
-            return inflater.inflate(R.layout.encryption_interstitial, container, false);
-        }
+        public void onCreate(Bundle savedInstanceState) {
+            super.onCreate(savedInstanceState);
 
-        @Override
-        public void onViewCreated(View view, Bundle savedInstanceState) {
-            super.onViewCreated(view, savedInstanceState);
-            mRequirePasswordToDecryptButton =
-                    (RadioButton) view.findViewById(R.id.encrypt_require_password);
-            mDontRequirePasswordToDecryptButton =
-                    (RadioButton) view.findViewById(R.id.encrypt_dont_require_password);
-            mEncryptionMessage =
-                    (TextView) view.findViewById(R.id.encryption_message);
+            addPreferencesFromResource(R.xml.security_settings_encryption_interstitial);
+
+            mRequirePasswordToDecrypt = findPreference(KEY_ENCRYPT_REQUIRE_PASSWORD);
+            mDontRequirePasswordToDecrypt = findPreference(KEY_ENCRYPT_DONT_REQUIRE_PASSWORD);
             boolean forFingerprint = getActivity().getIntent().getBooleanExtra(
                     ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, false);
             Intent intent = getActivity().getIntent();
@@ -135,26 +124,22 @@
                     disableId = R.string.encrypt_dont_require_password;
                     break;
             }
-            mEncryptionMessage.setText(msgId);
+            TextView message = createHeaderView();
+            message.setText(msgId);
+            setHeaderView(message);
 
-            mRequirePasswordToDecryptButton.setOnClickListener(this);
-            mRequirePasswordToDecryptButton.setText(enableId);
+            mRequirePasswordToDecrypt.setTitle(enableId);
 
-            mDontRequirePasswordToDecryptButton.setOnClickListener(this);
-            mDontRequirePasswordToDecryptButton.setText(disableId);
+            mDontRequirePasswordToDecrypt.setTitle(disableId);
 
             setRequirePasswordState(getActivity().getIntent().getBooleanExtra(
                     EXTRA_REQUIRE_PASSWORD, true));
+        }
 
-            Button nextButton = getNextButton();
-            if (nextButton != null) {
-                nextButton.setOnClickListener(new View.OnClickListener() {
-                    @Override
-                    public void onClick(View v) {
-                        startLockIntent();
-                    }
-                });
-            }
+        protected TextView createHeaderView() {
+            TextView message = (TextView) LayoutInflater.from(getActivity()).inflate(
+                    R.layout.encryption_interstitial_header, null, false);
+            return message;
         }
 
         protected void startLockIntent() {
@@ -177,18 +162,22 @@
         }
 
         @Override
-        public void onClick(View v) {
-            if (v == mRequirePasswordToDecryptButton) {
+        public boolean onPreferenceTreeClick(Preference preference) {
+            final String key = preference.getKey();
+            if (key.equals(KEY_ENCRYPT_REQUIRE_PASSWORD)) {
                 final boolean accEn = AccessibilityManager.getInstance(getActivity()).isEnabled();
                 if (accEn && !mPasswordRequired) {
                     setRequirePasswordState(false); // clear the UI state
                     showDialog(ACCESSIBILITY_WARNING_DIALOG);
                 } else {
                     setRequirePasswordState(true);
+                    startLockIntent();
                 }
             } else {
                 setRequirePasswordState(false);
+                startLockIntent();
             }
+            return true;
         }
 
         @Override
@@ -242,14 +231,13 @@
 
         private void setRequirePasswordState(boolean required) {
             mPasswordRequired = required;
-            mRequirePasswordToDecryptButton.setChecked(required);
-            mDontRequirePasswordToDecryptButton.setChecked(!required);
         }
 
         @Override
         public void onClick(DialogInterface dialog, int which) {
             if (which == DialogInterface.BUTTON_POSITIVE) {
                 setRequirePasswordState(true);
+                startLockIntent();
             } else if (which == DialogInterface.BUTTON_NEGATIVE) {
                 setRequirePasswordState(false);
             }
diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java
index 49d72b4..e871c82 100644
--- a/src/com/android/settings/SettingsPreferenceFragment.java
+++ b/src/com/android/settings/SettingsPreferenceFragment.java
@@ -242,9 +242,18 @@
 
     protected void setHeaderView(int resource) {
         mHeader = new LayoutPreference(getPrefContext(), resource);
-        mHeader.setOrder(ORDER_FIRST);
+        addPreferenceToTop(mHeader);
+    }
+
+    protected void setHeaderView(View view) {
+        mHeader = new LayoutPreference(getPrefContext(), view);
+        addPreferenceToTop(mHeader);
+    }
+
+    private void addPreferenceToTop(LayoutPreference preference) {
+        preference.setOrder(ORDER_FIRST);
         if (getPreferenceScreen() != null) {
-            getPreferenceScreen().addPreference(mHeader);
+            getPreferenceScreen().addPreference(preference);
         }
     }
 
diff --git a/src/com/android/settings/SetupEncryptionInterstitial.java b/src/com/android/settings/SetupEncryptionInterstitial.java
index 8d061ec..dfb32a6 100644
--- a/src/com/android/settings/SetupEncryptionInterstitial.java
+++ b/src/com/android/settings/SetupEncryptionInterstitial.java
@@ -21,11 +21,15 @@
 import android.content.Intent;
 import android.content.res.Resources;
 import android.os.Bundle;
+import android.support.v7.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Button;
+import android.widget.TextView;
 
 import com.android.setupwizardlib.SetupWizardLayout;
+import com.android.setupwizardlib.SetupWizardPreferenceLayout;
 import com.android.setupwizardlib.view.NavigationBar;
 
 /**
@@ -70,26 +74,44 @@
             implements NavigationBar.NavigationBarListener {
 
         @Override
-        public View onCreateView(LayoutInflater inflater, ViewGroup container,
-                Bundle savedInstanceState) {
-            return inflater.inflate(R.layout.setup_encryption_interstitial, container, false);
-        }
-
-        @Override
         public void onViewCreated(View view, Bundle savedInstanceState) {
             super.onViewCreated(view, savedInstanceState);
 
-            final SetupWizardLayout layout =
-                    (SetupWizardLayout) view.findViewById(R.id.setup_wizard_layout);
+            final SetupWizardPreferenceLayout layout = (SetupWizardPreferenceLayout) view;
+            layout.setDividerInset(getContext().getResources().getDimensionPixelSize(
+                    R.dimen.suw_items_text_divider_inset));
+            layout.setIllustration(R.drawable.setup_illustration_lock_screen,
+                    R.drawable.setup_illustration_horizontal_tile);
 
             final NavigationBar navigationBar = layout.getNavigationBar();
             navigationBar.setNavigationBarListener(this);
+            Button nextButton = navigationBar.getNextButton();
+            nextButton.setText(null);
+            nextButton.setEnabled(false);
 
+            layout.setHeaderText(R.string.encryption_interstitial_header);
             Activity activity = getActivity();
             if (activity != null) {
-                activity.setTitle(R.string.encryption_interstitial_header);
                 SetupWizardUtils.setImmersiveMode(activity);
             }
+
+            // Use the dividers in SetupWizardRecyclerLayout. Suppress the dividers in
+            // PreferenceFragment.
+            setDivider(null);
+        }
+
+        @Override
+        protected TextView createHeaderView() {
+            TextView message = (TextView) LayoutInflater.from(getActivity()).inflate(
+                    R.layout.setup_encryption_interstitial_header, null, false);
+            return message;
+        }
+
+        @Override
+        public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent,
+                                                 Bundle savedInstanceState) {
+            SetupWizardPreferenceLayout layout = (SetupWizardPreferenceLayout) parent;
+            return layout.onCreateRecyclerView(inflater, parent, savedInstanceState);
         }
 
         @Override
@@ -102,7 +124,7 @@
 
         @Override
         public void onNavigateNext() {
-            startLockIntent();
+            // next is handled via the onPreferenceTreeClick method in EncryptionInterstitial
         }
     }
 }