Add setting to allow RemoteInput on keyguard

Bug: 26440855
Change-Id: I57e02876c615f558eee279f2dc7edca8b28fc495
diff --git a/res/layout/lockscreen_remote_input.xml b/res/layout/lockscreen_remote_input.xml
new file mode 100644
index 0000000..af4bee0
--- /dev/null
+++ b/res/layout/lockscreen_remote_input.xml
@@ -0,0 +1,35 @@
+<?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
+  -->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+
+    <CheckBox
+            android:id="@+id/lockscreen_remote_input"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginStart="20dp"
+            android:paddingStart="20dp"
+            android:paddingEnd="?android:attr/dialogPreferredPadding"
+            android:minHeight="?android:attr/listPreferredItemHeightSmall"
+            android:textAppearance="?android:attr/textAppearanceMedium"
+            android:textColor="?android:attr/textColorAlertDialogListItem"
+            android:gravity="center_vertical"
+            android:text="@string/lockscreen_remote_input"
+        />
+</FrameLayout>
\ No newline at end of file
diff --git a/res/layout/restricted_dialog_singlechoice.xml b/res/layout/restricted_dialog_singlechoice.xml
index e5e449b..0e0fe4c 100644
--- a/res/layout/restricted_dialog_singlechoice.xml
+++ b/res/layout/restricted_dialog_singlechoice.xml
@@ -15,7 +15,8 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<com.android.settings.CheckableLinearLayout
+        xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
@@ -41,4 +42,4 @@
             android:baselineAlignBottom="true"
             android:scaleType="centerInside"
             android:visibility="gone" />
-</LinearLayout>
\ No newline at end of file
+</com.android.settings.CheckableLinearLayout>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 5f6327a..f1f43c0 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5912,7 +5912,7 @@
     <string name="notification_pulse_title">Pulse notification light</string>
 
     <!-- Configure Notifications: Title for the option controlling notifications on the lockscreen. [CHAR LIMIT=30] -->
-    <string name="lock_screen_notifications_title">When device is locked</string>
+    <string name="lock_screen_notifications_title">On the lock screen</string>
 
     <!-- Configure Notifications: Value for lockscreen notifications:  all information will be
          shown in notifications shown on a secure lock screen
@@ -7221,6 +7221,9 @@
     <!-- [CHAR_LIMIT=NONE] Label for when app is ignoring battery optimizations -->
     <string name="not_battery_optimizing">Not using battery optimization</string>
 
+    <!-- Text for the setting on whether you can type text into notifications without unlocking the device. -->
+    <string name="lockscreen_remote_input">If device is locked, prevent typing replies or other text in notifications</string>
+
     <string-array name="bytes_picker_sizes" translatable="false">
         <item>@*android:string/megabyteShort</item>
         <item>@*android:string/gigabyteShort</item>
diff --git a/res/xml/configure_notification_settings.xml b/res/xml/configure_notification_settings.xml
index 88d42df..cb93792 100644
--- a/res/xml/configure_notification_settings.xml
+++ b/res/xml/configure_notification_settings.xml
@@ -25,7 +25,7 @@
             android:title="@string/notification_pulse_title" />
 
     <!-- When device is locked -->
-    <com.android.settingslib.RestrictedDropDownPreference
+    <com.android.settings.notification.NotificationLockscreenPreference
             android:key="lock_screen_notifications"
             android:title="@string/lock_screen_notifications_title"
             android:summary="%s" />
diff --git a/src/com/android/settings/CustomListPreference.java b/src/com/android/settings/CustomListPreference.java
index 1603d2d..143c909 100644
--- a/src/com/android/settings/CustomListPreference.java
+++ b/src/com/android/settings/CustomListPreference.java
@@ -17,12 +17,14 @@
 package com.android.settings;
 
 import android.app.AlertDialog;
+import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.os.Bundle;
 import android.support.v14.preference.ListPreferenceDialogFragment;
 import android.support.v7.preference.ListPreference;
 import android.util.AttributeSet;
+import android.view.View;
 
 public class CustomListPreference extends ListPreference {
 
@@ -42,6 +44,13 @@
     protected void onDialogClosed(boolean positiveResult) {
     }
 
+    protected void onDialogCreated(Dialog dialog) {
+    }
+
+    protected boolean isAutoClosePreference() {
+        return true;
+    }
+
     public static class CustomListPreferenceDialogFragment extends ListPreferenceDialogFragment {
 
         private int mClickedDialogEntryIndex;
@@ -64,6 +73,23 @@
             mClickedDialogEntryIndex = getCustomizablePreference()
                     .findIndexOfValue(getCustomizablePreference().getValue());
             getCustomizablePreference().onPrepareDialogBuilder(builder, getOnItemClickListener());
+            if (!getCustomizablePreference().isAutoClosePreference()) {
+                builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int which) {
+                        CustomListPreferenceDialogFragment.this.onClick(dialog,
+                                DialogInterface.BUTTON_POSITIVE);
+                        dialog.dismiss();
+                    }
+                });
+            }
+        }
+
+        @Override
+        public Dialog onCreateDialog(Bundle savedInstanceState) {
+            Dialog dialog = super.onCreateDialog(savedInstanceState);
+            getCustomizablePreference().onDialogCreated(dialog);
+            return dialog;
         }
 
         protected DialogInterface.OnClickListener getOnItemClickListener() {
@@ -71,13 +97,16 @@
                 public void onClick(DialogInterface dialog, int which) {
                     setClickedDialogEntryIndex(which);
 
-                    /*
-                     * Clicking on an item simulates the positive button
-                     * click, and dismisses the dialog.
-                     */
-                    CustomListPreferenceDialogFragment.this.onClick(dialog,
-                            DialogInterface.BUTTON_POSITIVE);
-                    dialog.dismiss();
+
+                    if (getCustomizablePreference().isAutoClosePreference()) {
+                        /*
+                         * Clicking on an item simulates the positive button
+                         * click, and dismisses the dialog.
+                         */
+                        CustomListPreferenceDialogFragment.this.onClick(dialog,
+                                DialogInterface.BUTTON_POSITIVE);
+                        dialog.dismiss();
+                    }
                 }
             };
         }
diff --git a/src/com/android/settings/RestrictedListPreference.java b/src/com/android/settings/RestrictedListPreference.java
index 77af19e..fca0071 100644
--- a/src/com/android/settings/RestrictedListPreference.java
+++ b/src/com/android/settings/RestrictedListPreference.java
@@ -149,8 +149,12 @@
                 text.setChecked(false);
                 padlock.setVisibility(View.VISIBLE);
             } else {
-                text.setChecked(position == mSelectedIndex);
-                text.setEnabled(true);
+                if (mSelectedIndex != -1) {
+                    text.setChecked(position == mSelectedIndex);
+                }
+                if (!text.isEnabled()) {
+                    text.setEnabled(true);
+                }
                 padlock.setVisibility(View.GONE);
             }
             return root;
@@ -199,13 +203,15 @@
                         setClickedDialogEntryIndex(which);
                     }
 
-                    /*
-                     * Clicking on an item simulates the positive button
-                     * click, and dismisses the dialog.
-                     */
-                    RestrictedListPreferenceDialogFragment.this.onClick(dialog,
-                            DialogInterface.BUTTON_POSITIVE);
-                    dialog.dismiss();
+                    if (getCustomizablePreference().isAutoClosePreference()) {
+                        /*
+                         * Clicking on an item simulates the positive button
+                         * click, and dismisses the dialog.
+                         */
+                        RestrictedListPreferenceDialogFragment.this.onClick(dialog,
+                                DialogInterface.BUTTON_POSITIVE);
+                        dialog.dismiss();
+                    }
                 }
             };
         }
diff --git a/src/com/android/settings/notification/ConfigureNotificationSettings.java b/src/com/android/settings/notification/ConfigureNotificationSettings.java
index 17571c9..842b06f 100644
--- a/src/com/android/settings/notification/ConfigureNotificationSettings.java
+++ b/src/com/android/settings/notification/ConfigureNotificationSettings.java
@@ -32,13 +32,11 @@
 import com.android.internal.widget.LockPatternUtils;
 import com.android.settings.InstrumentedFragment;
 import com.android.settings.R;
+import com.android.settings.RestrictedListPreference.RestrictedItem;
 import com.android.settings.SettingsPreferenceFragment;
-import com.android.settingslib.RestrictedDropDownPreference;
-import com.android.settingslib.RestrictedDropDownPreference.RestrictedItem;
 import com.android.settingslib.RestrictedLockUtils;
 
 import java.util.ArrayList;
-import java.util.List;
 
 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
@@ -56,7 +54,7 @@
     private Context mContext;
 
     private TwoStatePreference mNotificationPulse;
-    private RestrictedDropDownPreference mLockscreen;
+    private NotificationLockscreenPreference mLockscreen;
     private boolean mSecure;
     private int mLockscreenSelectedValue;
 
@@ -131,7 +129,7 @@
     // === Lockscreen (public / private) notifications ===
 
     private void initLockscreenNotifications() {
-        mLockscreen = (RestrictedDropDownPreference) getPreferenceScreen().findPreference(
+        mLockscreen = (NotificationLockscreenPreference) getPreferenceScreen().findPreference(
                 KEY_LOCK_SCREEN_NOTIFICATIONS);
         if (mLockscreen == null) {
             Log.i(TAG, "Preference not found: " + KEY_LOCK_SCREEN_NOTIFICATIONS);
diff --git a/src/com/android/settings/notification/NotificationLockscreenPreference.java b/src/com/android/settings/notification/NotificationLockscreenPreference.java
new file mode 100644
index 0000000..60f451f
--- /dev/null
+++ b/src/com/android/settings/notification/NotificationLockscreenPreference.java
@@ -0,0 +1,118 @@
+/*
+ * 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
+ */
+
+package com.android.settings.notification;
+
+import com.android.settings.R;
+import com.android.settings.RestrictedListPreference;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.provider.Settings;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.CheckBox;
+import android.widget.CompoundButton;
+import android.widget.ListAdapter;
+
+public class NotificationLockscreenPreference extends RestrictedListPreference {
+
+    private boolean mAllowRemoteInput;
+    private int mInitialIndex;
+    private Listener mListener;
+    private boolean mShowRemoteInput;
+
+    public NotificationLockscreenPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
+            DialogInterface.OnClickListener innerListener) {
+
+        final String selectedValue = getValue();
+        mInitialIndex = (selectedValue == null) ? -1 : findIndexOfValue(selectedValue);
+        mListener = new Listener(innerListener);
+        builder.setSingleChoiceItems(createListAdapter(), mInitialIndex, mListener);
+        mShowRemoteInput = getEntryValues().length == 3;
+        mAllowRemoteInput = Settings.Secure.getInt(getContext().getContentResolver(),
+                Settings.Secure.LOCK_SCREEN_ALLOW_REMOTE_INPUT, 0) != 0;
+        builder.setView(R.layout.lockscreen_remote_input);
+    }
+
+    @Override
+    protected void onDialogCreated(Dialog dialog) {
+        super.onDialogCreated(dialog);
+        dialog.create();
+        CheckBox view = (CheckBox) dialog.findViewById(R.id.lockscreen_remote_input);
+        view.setChecked(mAllowRemoteInput);
+        view.setOnCheckedChangeListener(mListener);
+        View panel = dialog.findViewById(com.android.internal.R.id.customPanel);
+        panel.setVisibility(checkboxVisibilityForSelectedIndex(mInitialIndex, mShowRemoteInput));
+        mListener.setView(panel);
+    }
+
+    @Override
+    protected ListAdapter createListAdapter() {
+        return new RestrictedArrayAdapter(getContext(), getEntries(), -1);
+    }
+
+    @Override
+    protected void onDialogClosed(boolean positiveResult) {
+        super.onDialogClosed(positiveResult);
+        Settings.Secure.putInt(getContext().getContentResolver(),
+                Settings.Secure.LOCK_SCREEN_ALLOW_REMOTE_INPUT, mAllowRemoteInput ? 1 : 0);
+    }
+
+    @Override
+    protected boolean isAutoClosePreference() {
+        return false;
+    }
+
+    private static int checkboxVisibilityForSelectedIndex(int selected, boolean showRemoteAtAll) {
+        return selected == 0 && showRemoteAtAll ? View.VISIBLE : View.GONE;
+    }
+
+    private class Listener implements DialogInterface.OnClickListener,
+            CompoundButton.OnCheckedChangeListener {
+
+        private final DialogInterface.OnClickListener mInner;
+        private View mView;
+
+        public Listener(DialogInterface.OnClickListener inner) {
+            mInner = inner;
+        }
+
+        @Override
+        public void onClick(DialogInterface dialog, int which) {
+            mInner.onClick(dialog, which);
+            if (mView != null) {
+                mView.setVisibility(checkboxVisibilityForSelectedIndex(which, mShowRemoteInput));
+            }
+        }
+
+        @Override
+        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+            mAllowRemoteInput = isChecked;
+        }
+
+        public void setView(View view) {
+            mView = view;
+        }
+    }
+}