Settings: Add option to scramble pin layout when unlocking (1/2).

Change-Id: I3e2c200a0a31d3c765831bc30280029a50c88051
diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml
index ba485c9..d700ef6 100644
--- a/res/values/cm_strings.xml
+++ b/res/values/cm_strings.xml
@@ -46,4 +46,8 @@
     <string name="lockpattern_settings_enable_error_path_title">Show pattern error</string>
     <!-- Whether the dots will be drawn when using the lockscreen pattern -->
     <string name="lockpattern_settings_enable_dots_title">Show pattern dots</string>
+
+    <!-- PIN scramble -->
+    <string name="unlock_scramble_pin_layout_title">Scramble layout</string>
+    <string name="unlock_scramble_pin_layout_summary">Scramble PIN layout when unlocking device</string>
 </resources>
diff --git a/res/xml/screen_lock_settings.xml b/res/xml/screen_lock_settings.xml
index c0f9046..0bb06e7 100644
--- a/res/xml/screen_lock_settings.xml
+++ b/res/xml/screen_lock_settings.xml
@@ -59,4 +59,10 @@
         android:key="power_button_instantly_locks"
         android:title="@string/lockpattern_settings_enable_power_button_instantly_locks" />
 
+    <!-- Lineage additions, available in pin/pattern/password/slide -->
+    <SwitchPreferenceCompat
+        android:key="lockscreen_scramble_pin_layout"
+        android:title="@string/unlock_scramble_pin_layout_title"
+        android:summary="@string/unlock_scramble_pin_layout_summary" />
+
 </PreferenceScreen>
diff --git a/src/com/android/settings/security/screenlock/PinScramblePreferenceController.java b/src/com/android/settings/security/screenlock/PinScramblePreferenceController.java
new file mode 100644
index 0000000..d6c884f
--- /dev/null
+++ b/src/com/android/settings/security/screenlock/PinScramblePreferenceController.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2018 The LineageOS 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.security.screenlock;
+
+import android.app.admin.DevicePolicyManager;
+import android.content.Context;
+import android.provider.Settings;
+import androidx.preference.Preference;
+import androidx.preference.TwoStatePreference;
+
+import com.android.internal.widget.LockPatternUtils;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+public class PinScramblePreferenceController extends AbstractPreferenceController
+        implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
+
+    static final String KEY_LOCKSCREEN_SCRAMBLE_PIN_LAYOUT = "lockscreen_scramble_pin_layout";
+
+    private final int mUserId;
+    private final LockPatternUtils mLockPatternUtils;
+
+    public PinScramblePreferenceController(Context context, int userId,
+            LockPatternUtils lockPatternUtils) {
+        super(context);
+        mUserId = userId;
+        mLockPatternUtils = lockPatternUtils;
+    }
+
+    @Override
+    public boolean isAvailable() {
+        return isPinLock();
+    }
+
+    @Override
+    public String getPreferenceKey() {
+        return KEY_LOCKSCREEN_SCRAMBLE_PIN_LAYOUT;
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        ((TwoStatePreference) preference).setChecked(Settings.System.getInt(
+                mContext.getContentResolver(),
+                Settings.System.LOCKSCREEN_PIN_SCRAMBLE_LAYOUT,
+                0) == 1);
+    }
+
+    private boolean isPinLock() {
+        int quality = mLockPatternUtils.getKeyguardStoredPasswordQuality(mUserId);
+        boolean hasPin = quality == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC ||
+                quality == DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX;
+        return mLockPatternUtils.isSecure(mUserId) && hasPin;
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        Settings.System.putInt(
+                mContext.getContentResolver(),
+                Settings.System.LOCKSCREEN_PIN_SCRAMBLE_LAYOUT,
+                (Boolean) newValue ? 1 : 0);
+        return true;
+    }
+}
diff --git a/src/com/android/settings/security/screenlock/ScreenLockSettings.java b/src/com/android/settings/security/screenlock/ScreenLockSettings.java
index c49ec43..f811872 100644
--- a/src/com/android/settings/security/screenlock/ScreenLockSettings.java
+++ b/src/com/android/settings/security/screenlock/ScreenLockSettings.java
@@ -92,6 +92,8 @@
                 context, MY_USER_ID, lockPatternUtils));
         controllers.add(new AutoPinConfirmPreferenceController(
                 context, MY_USER_ID, lockPatternUtils, parent));
+        controllers.add(new PinScramblePreferenceController(
+                context, MY_USER_ID, lockPatternUtils));
         controllers.add(new OwnerInfoPreferenceController(context, parent));
         return controllers;
     }