Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 68430df6 authored by Adnan's avatar Adnan Committed by Abhisek Devkota
Browse files

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

Change-Id: Ibe8611eb0b8a5bd9275bc96682d2cbafaa37d8f5
parent 020d844a
Loading
Loading
Loading
Loading
+37 −2
Original line number Diff line number Diff line
@@ -17,20 +17,29 @@
package com.android.keyguard;

import android.content.Context;
import android.provider.Settings;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView.OnEditorActionListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Displays a PIN pad for unlocking.
 */
public class KeyguardPINView extends KeyguardAbsKeyInputView
        implements KeyguardSecurityView, OnEditorActionListener, TextWatcher {

    private static List<Integer> sNumbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);

    public KeyguardPINView(Context context) {
        this(context, null);
    }
@@ -40,7 +49,7 @@ public class KeyguardPINView extends KeyguardAbsKeyInputView
    }

    protected void resetState() {
        if (KeyguardUpdateMonitor.getInstance(mContext).getMaxBiometricUnlockAttemptsReached()) {
        if (KeyguardUpdateMonitor.getInstance(getContext()).getMaxBiometricUnlockAttemptsReached()) {
            mSecurityMessageDisplay.setMessage(R.string.faceunlock_multiple_failures, true);
        } else {
            mSecurityMessageDisplay.setMessage(R.string.kg_pin_instructions, false);
@@ -56,7 +65,6 @@ public class KeyguardPINView extends KeyguardAbsKeyInputView
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        final View ok = findViewById(R.id.key_enter);
        if (ok != null) {
            ok.setOnClickListener(new View.OnClickListener() {
@@ -71,6 +79,33 @@ public class KeyguardPINView extends KeyguardAbsKeyInputView
            ok.setOnHoverListener(new LiftToActivateListener(getContext()));
        }

        boolean scramblePin = (Settings.System.getInt(getContext().getContentResolver(),
                Settings.System.LOCKSCREEN_PIN_SCRAMBLE_LAYOUT, 0) == 1);

        if (scramblePin) {
            Collections.shuffle(sNumbers);
            // get all children who are NumPadKey's
            LinearLayout bouncer = (LinearLayout) findViewById(R.id.keyguard_bouncer_frame);
            List<NumPadKey> views = new ArrayList<NumPadKey>();
            for (int i = 0; i < bouncer.getChildCount(); i++) {
                if (bouncer.getChildAt(i) instanceof LinearLayout) {
                    LinearLayout nestedLayout = ((LinearLayout) bouncer.getChildAt(i));
                    for (int j = 0; j < nestedLayout.getChildCount(); j++){
                        View view = nestedLayout.getChildAt(j);
                        if (view.getClass() == NumPadKey.class) {
                            views.add((NumPadKey) view);
                        }
                    }
                }
            }

            // reset the digits in the views
            for (int i = 0; i < sNumbers.size(); i++) {
                NumPadKey view = views.get(i);
                view.setDigit(sNumbers.get(i));
            }
        }

        // The delete button is of the PIN keyboard itself in some (e.g. tablet) layouts,
        // not a separate view
        View pinDelete = findViewById(R.id.delete_button);
+21 −12
Original line number Diff line number Diff line
@@ -66,7 +66,6 @@ public class NumPadKey extends Button {

    public NumPadKey(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NumPadKey);
        mDigit = a.getInt(R.styleable.NumPadKey_digit, mDigit);
        setTextViewResId(a.getResourceId(R.styleable.NumPadKey_textView, 0));
@@ -76,12 +75,28 @@ public class NumPadKey extends Button {
        setAccessibilityDelegate(new ObscureSpeechDelegate(context));

        mEnableHaptics = new LockPatternUtils(context).isTactileFeedbackEnabled();
        updateText();
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        // Reset the "announced headset" flag when detached.
        ObscureSpeechDelegate.sAnnouncedHeadset = false;
    }

    public void setDigit(int digit) {
        mDigit = digit;
        updateText();
    }

    private void updateText() {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        builder.append(String.valueOf(mDigit));
        if (mDigit >= 0) {
            if (sKlondike == null) {
                sKlondike = context.getResources().getStringArray(
                sKlondike = getContext().getResources().getStringArray(
                        R.array.lockscreen_num_pad_klondike);
            }
            if (sKlondike != null && sKlondike.length > mDigit) {
@@ -91,22 +106,16 @@ public class NumPadKey extends Button {
                    builder.append(" ");
                    builder.append(extra);
                    builder.setSpan(
                        new TextAppearanceSpan(context, R.style.TextAppearance_NumPadKey_Klondike),
                        new TextAppearanceSpan(getContext(),
                                R.style.TextAppearance_NumPadKey_Klondike),
                            builder.length()-extraLen, builder.length(), 0);
                }
            }
        }
            setText(builder);
        }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();

        // Reset the "announced headset" flag when detached.
        ObscureSpeechDelegate.sAnnouncedHeadset = false;
    }


    public void setTextView(TextView tv) {
        mTextView = tv;
    }