ChooseLockPassword.java revision 8df7173043d2c9dd5213f1701fd6f68c9e2383db
100d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller/*
200d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * Copyright (C) 2010 The Android Open Source Project
300d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller *
400d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * Licensed under the Apache License, Version 2.0 (the "License");
500d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * you may not use this file except in compliance with the License.
600d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * You may obtain a copy of the License at
700d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller *
800d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller *      http://www.apache.org/licenses/LICENSE-2.0
900d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller *
1000d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * Unless required by applicable law or agreed to in writing, software
1100d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * distributed under the License is distributed on an "AS IS" BASIS,
1200d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1300d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * See the License for the specific language governing permissions and
1400d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller * limitations under the License.
1500d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller */
1600d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
172eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lampackage com.android.settings.password;
1800d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
19c41e408aea1bf2f28160265cdadd7662e626d228Maurice Lamimport static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
20c41e408aea1bf2f28160265cdadd7662e626d228Maurice Lamimport static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC;
21c41e408aea1bf2f28160265cdadd7662e626d228Maurice Lamimport static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_COMPLEX;
22c41e408aea1bf2f28160265cdadd7662e626d228Maurice Lamimport static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
23c41e408aea1bf2f28160265cdadd7662e626d228Maurice Lamimport static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX;
24c41e408aea1bf2f28160265cdadd7662e626d228Maurice Lam
2500d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.app.Activity;
2617e9e1933055b207473b488595b2887bfcaea32eJim Millerimport android.app.Fragment;
27af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Millerimport android.app.admin.DevicePolicyManager;
2827ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scullimport android.app.admin.PasswordMetrics;
290698a216311acd9ab52224602d1a75c08bf89f04Jim Millerimport android.content.Context;
3000d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.content.Intent;
31edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lamimport android.graphics.Insets;
3200d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.os.Bundle;
339ba765b875ebeb66d8a870a29a00fe53e9da306dTony Makimport android.os.Handler;
349ba765b875ebeb66d8a870a29a00fe53e9da306dTony Makimport android.os.Message;
350bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Makimport android.support.v7.widget.LinearLayoutManager;
360bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Makimport android.support.v7.widget.RecyclerView;
3700d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.text.Editable;
382e28acedc623527aa5e84ba66635700d6547569aJim Millerimport android.text.InputType;
39122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Millerimport android.text.Selection;
40122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Millerimport android.text.Spannable;
4100d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.text.TextUtils;
42122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Millerimport android.text.TextWatcher;
4391e6c499ca8f33cc093fed4277d2b48ab780b309Andres Moralesimport android.util.Log;
44122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Millerimport android.view.KeyEvent;
4517e9e1933055b207473b488595b2887bfcaea32eJim Millerimport android.view.LayoutInflater;
4600d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.view.View;
4700d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.view.View.OnClickListener;
4839b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport android.view.ViewGroup;
49c41e408aea1bf2f28160265cdadd7662e626d228Maurice Lamimport android.view.inputmethod.EditorInfo;
5000d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.widget.Button;
510bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Makimport android.widget.EditText;
52d189ac52a4e49291b729d817fc903116abc086d0Maurice Lamimport android.widget.LinearLayout;
5300d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Millerimport android.widget.TextView;
54122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Millerimport android.widget.TextView.OnEditorActionListener;
5500d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
56265d3c2a0c36251bf8a9f571d7239b6dd404d942Tamas Berghammerimport com.android.internal.logging.nano.MetricsProto.MetricsEvent;
5739b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport com.android.internal.widget.LockPatternUtils;
5839b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport com.android.internal.widget.LockPatternUtils.RequestThrottledException;
5939b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport com.android.internal.widget.TextViewInputDisabler;
602eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lamimport com.android.settings.EncryptionInterstitial;
612eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lamimport com.android.settings.R;
622eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lamimport com.android.settings.SettingsActivity;
632eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lamimport com.android.settings.Utils;
642d0b3447360666847c483e6ce2521846c66c25c1Fan Zhangimport com.android.settings.core.InstrumentedPreferenceFragment;
6539b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport com.android.settings.notification.RedactionInterstitial;
66d189ac52a4e49291b729d817fc903116abc086d0Maurice Lamimport com.android.setupwizardlib.GlifLayout;
6739b467482d1bf256a111c757e9b7621c6f523271Jason Monk
680bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Makimport java.util.ArrayList;
690bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Makimport java.util.List;
700bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
71263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Megliopublic class ChooseLockPassword extends SettingsActivity {
7200d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller    public static final String PASSWORD_MIN_KEY = "lockscreen.password_min";
7300d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller    public static final String PASSWORD_MAX_KEY = "lockscreen.password_max";
7457fbf694a53afb9c1924d81272179dceb3acfa4dKonstantin Lopyrev    public static final String PASSWORD_MIN_LETTERS_KEY = "lockscreen.password_min_letters";
7557fbf694a53afb9c1924d81272179dceb3acfa4dKonstantin Lopyrev    public static final String PASSWORD_MIN_LOWERCASE_KEY = "lockscreen.password_min_lowercase";
7657fbf694a53afb9c1924d81272179dceb3acfa4dKonstantin Lopyrev    public static final String PASSWORD_MIN_UPPERCASE_KEY = "lockscreen.password_min_uppercase";
7757fbf694a53afb9c1924d81272179dceb3acfa4dKonstantin Lopyrev    public static final String PASSWORD_MIN_NUMERIC_KEY = "lockscreen.password_min_numeric";
7857fbf694a53afb9c1924d81272179dceb3acfa4dKonstantin Lopyrev    public static final String PASSWORD_MIN_SYMBOLS_KEY = "lockscreen.password_min_symbols";
792df65e4facac659314c2440d0af6316924166e2bKonstantin Lopyrev    public static final String PASSWORD_MIN_NONLETTER_KEY = "lockscreen.password_min_nonletter";
8000d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
8191e6c499ca8f33cc093fed4277d2b48ab780b309Andres Morales    private static final String TAG = "ChooseLockPassword";
8291e6c499ca8f33cc093fed4277d2b48ab780b309Andres Morales
8317e9e1933055b207473b488595b2887bfcaea32eJim Miller    @Override
8417e9e1933055b207473b488595b2887bfcaea32eJim Miller    public Intent getIntent() {
8517e9e1933055b207473b488595b2887bfcaea32eJim Miller        Intent modIntent = new Intent(super.getIntent());
866b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam        modIntent.putExtra(EXTRA_SHOW_FRAGMENT, getFragmentClass().getName());
8717e9e1933055b207473b488595b2887bfcaea32eJim Miller        return modIntent;
8817e9e1933055b207473b488595b2887bfcaea32eJim Miller    }
89122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
902eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam    public static class IntentBuilder {
912eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam
922eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        private final Intent mIntent;
932eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam
942eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        public IntentBuilder(Context context) {
952eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent = new Intent(context, ChooseLockPassword.class);
962eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(ChooseLockGeneric.CONFIRM_CREDENTIALS, false);
972eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(EncryptionInterstitial.EXTRA_REQUIRE_PASSWORD, false);
982eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        }
992eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam
1002eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        public IntentBuilder setPasswordQuality(int quality) {
1012eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(LockPatternUtils.PASSWORD_TYPE_KEY, quality);
1022eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            return this;
1032eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        }
1042eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam
1052eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        public IntentBuilder setPasswordLengthRange(int min, int max) {
1062eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(PASSWORD_MIN_KEY, min);
1072eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(PASSWORD_MAX_KEY, max);
1082eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            return this;
1092eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        }
1102eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam
1112eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        public IntentBuilder setUserId(int userId) {
1122eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(Intent.EXTRA_USER_ID, userId);
1132eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            return this;
1142eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        }
1152eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam
1162eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        public IntentBuilder setChallenge(long challenge) {
1172eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, true);
1182eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, challenge);
1192eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            return this;
1202eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        }
1212eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam
1222eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        public IntentBuilder setPassword(String password) {
1232eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false);
1242eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            mIntent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD, password);
1252eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            return this;
1262eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        }
1272eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam
1282eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        public Intent build() {
1292eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam            return mIntent;
1302eb170cd6ff43db01dc0ff3c1fcac5ebba4489deMaurice Lam        }
131fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri    }
132fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri
13317e9e1933055b207473b488595b2887bfcaea32eJim Miller    @Override
134a677ee210c67d13d15ba0663887675bda16354d8Amith Yamasani    protected boolean isValidFragment(String fragmentName) {
135a677ee210c67d13d15ba0663887675bda16354d8Amith Yamasani        if (ChooseLockPasswordFragment.class.getName().equals(fragmentName)) return true;
136a677ee210c67d13d15ba0663887675bda16354d8Amith Yamasani        return false;
137a677ee210c67d13d15ba0663887675bda16354d8Amith Yamasani    }
138a677ee210c67d13d15ba0663887675bda16354d8Amith Yamasani
1396b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam    /* package */ Class<? extends Fragment> getFragmentClass() {
1406b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam        return ChooseLockPasswordFragment.class;
1416b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam    }
1426b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam
143a677ee210c67d13d15ba0663887675bda16354d8Amith Yamasani    @Override
14471fde52ae36f134e93835bee53bc4bfe5481bba0Udam Saini    protected void onCreate(Bundle savedInstanceState) {
14517e9e1933055b207473b488595b2887bfcaea32eJim Miller        super.onCreate(savedInstanceState);
1467da8cae44b622518fbffd6d265f291c39e9fc722Jim Miller        CharSequence msg = getText(R.string.lockpassword_choose_your_password_header);
147263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio        setTitle(msg);
148d189ac52a4e49291b729d817fc903116abc086d0Maurice Lam        LinearLayout layout = (LinearLayout) findViewById(R.id.content_parent);
149d189ac52a4e49291b729d817fc903116abc086d0Maurice Lam        layout.setFitsSystemWindows(false);
15017e9e1933055b207473b488595b2887bfcaea32eJim Miller    }
151122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
1522d0b3447360666847c483e6ce2521846c66c25c1Fan Zhang    public static class ChooseLockPasswordFragment extends InstrumentedPreferenceFragment
1530bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            implements OnClickListener, OnEditorActionListener, TextWatcher,
1543e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            SaveAndFinishWorker.Listener {
15517e9e1933055b207473b488595b2887bfcaea32eJim Miller        private static final String KEY_FIRST_PIN = "first_pin";
15617e9e1933055b207473b488595b2887bfcaea32eJim Miller        private static final String KEY_UI_STAGE = "ui_stage";
157a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales        private static final String KEY_CURRENT_PASSWORD = "current_password";
1583e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        private static final String FRAGMENT_TAG_SAVE_AND_FINISH = "save_and_finish_worker";
159a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales
160a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales        private String mCurrentPassword;
161cff37ecbdd4a3d8f9b81fc295155bde5fcf8ea0cPaul Lawrence        private String mChosenPassword;
1626609b0c22ae89a24d1b07dc3c4143452616d4450Andres Morales        private boolean mHasChallenge;
1636609b0c22ae89a24d1b07dc3c4143452616d4450Andres Morales        private long mChallenge;
1640bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private EditText mPasswordEntry;
165f2c2c19339b5d821cb6196911cee04dc9ac647adXiyuan Xia        private TextViewInputDisabler mPasswordEntryInputDisabler;
1664c00a611835e2257502ddcdd26b4e827c5b64f80Adrian Roos        private int mPasswordMinLength = LockPatternUtils.MIN_LOCK_PASSWORD_SIZE;
16717e9e1933055b207473b488595b2887bfcaea32eJim Miller        private int mPasswordMaxLength = 16;
16817e9e1933055b207473b488595b2887bfcaea32eJim Miller        private int mPasswordMinLetters = 0;
16917e9e1933055b207473b488595b2887bfcaea32eJim Miller        private int mPasswordMinUpperCase = 0;
17017e9e1933055b207473b488595b2887bfcaea32eJim Miller        private int mPasswordMinLowerCase = 0;
17117e9e1933055b207473b488595b2887bfcaea32eJim Miller        private int mPasswordMinSymbols = 0;
17217e9e1933055b207473b488595b2887bfcaea32eJim Miller        private int mPasswordMinNumeric = 0;
17317e9e1933055b207473b488595b2887bfcaea32eJim Miller        private int mPasswordMinNonLetter = 0;
1749ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak        private int mPasswordMinLengthToFulfillAllPolicies = 0;
175edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        protected int mUserId;
1760bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private boolean mHideDrawer = false;
1770bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        /**
1780bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         * Password requirements that we need to verify.
1790bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         */
1800bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private int[] mPasswordRequirements;
1810bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
18217e9e1933055b207473b488595b2887bfcaea32eJim Miller        private LockPatternUtils mLockPatternUtils;
1833e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        private SaveAndFinishWorker mSaveAndFinishWorker;
18417e9e1933055b207473b488595b2887bfcaea32eJim Miller        private int mRequestedQuality = DevicePolicyManager.PASSWORD_QUALITY_NUMERIC;
18517e9e1933055b207473b488595b2887bfcaea32eJim Miller        private ChooseLockSettingsHelper mChooseLockSettingsHelper;
18617e9e1933055b207473b488595b2887bfcaea32eJim Miller        private Stage mUiStage = Stage.Introduction;
1870bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private PasswordRequirementAdapter mPasswordRequirementAdapter;
1888df7173043d2c9dd5213f1701fd6f68c9e2383dbMaurice Lam        private GlifLayout mLayout;
1891f8fb0f8ce1e82266a3a12b3d31b9062cff3fc44Paul Lawrence
19017e9e1933055b207473b488595b2887bfcaea32eJim Miller        private String mFirstPin;
1910bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private RecyclerView mPasswordRestrictionView;
192edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam        protected boolean mIsAlphaMode;
19317e9e1933055b207473b488595b2887bfcaea32eJim Miller        private Button mCancelButton;
19417e9e1933055b207473b488595b2887bfcaea32eJim Miller        private Button mNextButton;
1950bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
1969ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak        private TextChangedHandler mTextChangedHandler;
1979ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak
19817e9e1933055b207473b488595b2887bfcaea32eJim Miller        private static final int CONFIRM_EXISTING_REQUEST = 58;
19917e9e1933055b207473b488595b2887bfcaea32eJim Miller        static final int RESULT_FINISHED = RESULT_FIRST_USER;
200552bc61ae2aaf67fd2f19564ca2ced002e8675e6Amith Yamasani
2010bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int MIN_LETTER_IN_PASSWORD = 0;
2020bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int MIN_UPPER_LETTERS_IN_PASSWORD = 1;
2030bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int MIN_LOWER_LETTERS_IN_PASSWORD = 2;
2040bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int MIN_SYMBOLS_IN_PASSWORD = 3;
2050bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int MIN_NUMBER_IN_PASSWORD = 4;
2060bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int MIN_NON_LETTER_IN_PASSWORD = 5;
2070bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
2080bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        // Error code returned from {@link #validatePassword(String)}.
2090bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int NO_ERROR = 0;
2100bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int CONTAIN_INVALID_CHARACTERS = 1 << 0;
2110bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int TOO_SHORT = 1 << 1;
2120bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int TOO_LONG = 1 << 2;
2130bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int CONTAIN_NON_DIGITS = 1 << 3;
2140bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int CONTAIN_SEQUENTIAL_DIGITS = 1 << 4;
2150bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int RECENTLY_USED = 1 << 5;
2160bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int NOT_ENOUGH_LETTER = 1 << 6;
2170bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int NOT_ENOUGH_UPPER_CASE = 1 << 7;
2180bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int NOT_ENOUGH_LOWER_CASE = 1 << 8;
2190bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int NOT_ENOUGH_DIGITS = 1 << 9;
2200bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int NOT_ENOUGH_SYMBOLS = 1 << 10;
2210bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private static final int NOT_ENOUGH_NON_LETTER = 1 << 11;
22200d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
22300d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller        /**
22417e9e1933055b207473b488595b2887bfcaea32eJim Miller         * Keep track internally of where the user is in choosing a pattern.
22500d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller         */
22617e9e1933055b207473b488595b2887bfcaea32eJim Miller        protected enum Stage {
22717e9e1933055b207473b488595b2887bfcaea32eJim Miller
22817e9e1933055b207473b488595b2887bfcaea32eJim Miller            Introduction(R.string.lockpassword_choose_your_password_header,
22917e9e1933055b207473b488595b2887bfcaea32eJim Miller                    R.string.lockpassword_choose_your_pin_header,
23017e9e1933055b207473b488595b2887bfcaea32eJim Miller                    R.string.lockpassword_continue_label),
23117e9e1933055b207473b488595b2887bfcaea32eJim Miller
23217e9e1933055b207473b488595b2887bfcaea32eJim Miller            NeedToConfirm(R.string.lockpassword_confirm_your_password_header,
23317e9e1933055b207473b488595b2887bfcaea32eJim Miller                    R.string.lockpassword_confirm_your_pin_header,
23417e9e1933055b207473b488595b2887bfcaea32eJim Miller                    R.string.lockpassword_ok_label),
23517e9e1933055b207473b488595b2887bfcaea32eJim Miller
23617e9e1933055b207473b488595b2887bfcaea32eJim Miller            ConfirmWrong(R.string.lockpassword_confirm_passwords_dont_match,
23717e9e1933055b207473b488595b2887bfcaea32eJim Miller                    R.string.lockpassword_confirm_pins_dont_match,
23817e9e1933055b207473b488595b2887bfcaea32eJim Miller                    R.string.lockpassword_continue_label);
23917e9e1933055b207473b488595b2887bfcaea32eJim Miller
24017e9e1933055b207473b488595b2887bfcaea32eJim Miller            Stage(int hintInAlpha, int hintInNumeric, int nextButtonText) {
24117e9e1933055b207473b488595b2887bfcaea32eJim Miller                this.alphaHint = hintInAlpha;
24217e9e1933055b207473b488595b2887bfcaea32eJim Miller                this.numericHint = hintInNumeric;
24317e9e1933055b207473b488595b2887bfcaea32eJim Miller                this.buttonText = nextButtonText;
24417e9e1933055b207473b488595b2887bfcaea32eJim Miller            }
24517e9e1933055b207473b488595b2887bfcaea32eJim Miller
24617e9e1933055b207473b488595b2887bfcaea32eJim Miller            public final int alphaHint;
24717e9e1933055b207473b488595b2887bfcaea32eJim Miller            public final int numericHint;
24817e9e1933055b207473b488595b2887bfcaea32eJim Miller            public final int buttonText;
24900d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller        }
25000d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
25117e9e1933055b207473b488595b2887bfcaea32eJim Miller        // required constructor for fragments
25217e9e1933055b207473b488595b2887bfcaea32eJim Miller        public ChooseLockPasswordFragment() {
25300d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
25400d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller        }
25500d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
25617e9e1933055b207473b488595b2887bfcaea32eJim Miller        @Override
25717e9e1933055b207473b488595b2887bfcaea32eJim Miller        public void onCreate(Bundle savedInstanceState) {
25817e9e1933055b207473b488595b2887bfcaea32eJim Miller            super.onCreate(savedInstanceState);
25917e9e1933055b207473b488595b2887bfcaea32eJim Miller            mLockPatternUtils = new LockPatternUtils(getActivity());
26017e9e1933055b207473b488595b2887bfcaea32eJim Miller            Intent intent = getActivity().getIntent();
26166026773bbf1d7631743a5b892a4f768c694f868Amith Yamasani            if (!(getActivity() instanceof ChooseLockPassword)) {
26266026773bbf1d7631743a5b892a4f768c694f868Amith Yamasani                throw new SecurityException("Fragment contained in wrong activity");
26366026773bbf1d7631743a5b892a4f768c694f868Amith Yamasani            }
264fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri            // Only take this argument into account if it belongs to the current profile.
265194300dff340d67255d12762ffb376b7ba201c87Benjamin Franz            mUserId = Utils.getUserIdFromBundle(getActivity(), intent.getExtras());
2660bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            processPasswordRequirements(intent);
26717e9e1933055b207473b488595b2887bfcaea32eJim Miller            mChooseLockSettingsHelper = new ChooseLockSettingsHelper(getActivity());
26892779ce70156031f4fa3b8c2d84305f5b3b44105Udam Saini            mHideDrawer = getActivity().getIntent().getBooleanExtra(EXTRA_HIDE_DRAWER, false);
26962775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos
27062775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos            if (intent.getBooleanExtra(
27162775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                    ChooseLockSettingsHelper.EXTRA_KEY_FOR_CHANGE_CRED_REQUIRED_FOR_BOOT, false)) {
27262775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                SaveAndFinishWorker w = new SaveAndFinishWorker();
27362775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                final boolean required = getActivity().getIntent().getBooleanExtra(
27462775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                        EncryptionInterstitial.EXTRA_REQUIRE_PASSWORD, true);
27562775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                String current = intent.getStringExtra(
27662775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                        ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
27762775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                w.setBlocking(true);
27862775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                w.setListener(this);
27962775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                w.start(mChooseLockSettingsHelper.utils(), required,
28062775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos                        false, 0, current, current, mRequestedQuality, mUserId);
28162775bf756a8eca9423bb4fab3fc7b71ab22d1a7Adrian Roos            }
2829ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            mTextChangedHandler = new TextChangedHandler();
28317e9e1933055b207473b488595b2887bfcaea32eJim Miller        }
2842f9dbcb49174d85218dd18e3b2097d3b9fc789c7Jim Miller
28517e9e1933055b207473b488595b2887bfcaea32eJim Miller        @Override
28617e9e1933055b207473b488595b2887bfcaea32eJim Miller        public View onCreateView(LayoutInflater inflater, ViewGroup container,
28717e9e1933055b207473b488595b2887bfcaea32eJim Miller                Bundle savedInstanceState) {
28844f2736f5453ec3bf1fffd3471ecda24da57a094Maurice Lam            return inflater.inflate(R.layout.choose_lock_password, container, false);
2896b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam        }
29017e9e1933055b207473b488595b2887bfcaea32eJim Miller
2916b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam        @Override
2926b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam        public void onViewCreated(View view, Bundle savedInstanceState) {
2936b19fa9017800b94c75238339a337fd8f6c3e808Maurice Lam            super.onViewCreated(view, savedInstanceState);
29417e9e1933055b207473b488595b2887bfcaea32eJim Miller
2958df7173043d2c9dd5213f1701fd6f68c9e2383dbMaurice Lam            mLayout = (GlifLayout) view;
2968df7173043d2c9dd5213f1701fd6f68c9e2383dbMaurice Lam
297edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            // Make the password container consume the optical insets so the edit text is aligned
298edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            // with the sides of the parent visually.
299edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            ViewGroup container = view.findViewById(R.id.password_container);
300edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam            container.setOpticalInsets(Insets.NONE);
301edb39449842dd1360fa5f92c990785bf7c8dcdcdMaurice Lam
30217e9e1933055b207473b488595b2887bfcaea32eJim Miller            mCancelButton = (Button) view.findViewById(R.id.cancel_button);
30317e9e1933055b207473b488595b2887bfcaea32eJim Miller            mCancelButton.setOnClickListener(this);
30417e9e1933055b207473b488595b2887bfcaea32eJim Miller            mNextButton = (Button) view.findViewById(R.id.next_button);
30517e9e1933055b207473b488595b2887bfcaea32eJim Miller            mNextButton.setOnClickListener(this);
30617e9e1933055b207473b488595b2887bfcaea32eJim Miller
30717e9e1933055b207473b488595b2887bfcaea32eJim Miller            mIsAlphaMode = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC == mRequestedQuality
30817e9e1933055b207473b488595b2887bfcaea32eJim Miller                    || DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC == mRequestedQuality
30917e9e1933055b207473b488595b2887bfcaea32eJim Miller                    || DevicePolicyManager.PASSWORD_QUALITY_COMPLEX == mRequestedQuality;
3100bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
3110bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            setupPasswordRequirementsView(view);
3120bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
3130bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordRestrictionView.setLayoutManager(new LinearLayoutManager(getActivity()));
3140bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordEntry = (EditText) view.findViewById(R.id.password_entry);
31517e9e1933055b207473b488595b2887bfcaea32eJim Miller            mPasswordEntry.setOnEditorActionListener(this);
31617e9e1933055b207473b488595b2887bfcaea32eJim Miller            mPasswordEntry.addTextChangedListener(this);
3179c26c9dba6998fda4b3af27e315220b8bec6e0c5Tony Mak            mPasswordEntry.requestFocus();
318f2c2c19339b5d821cb6196911cee04dc9ac647adXiyuan Xia            mPasswordEntryInputDisabler = new TextViewInputDisabler(mPasswordEntry);
31917e9e1933055b207473b488595b2887bfcaea32eJim Miller
3209757e30dfa0ab691c4c15366568367e166071a7dJim Miller            final Activity activity = getActivity();
32117e9e1933055b207473b488595b2887bfcaea32eJim Miller
3222e28acedc623527aa5e84ba66635700d6547569aJim Miller            int currentType = mPasswordEntry.getInputType();
3232e28acedc623527aa5e84ba66635700d6547569aJim Miller            mPasswordEntry.setInputType(mIsAlphaMode ? currentType
3243b46f49d2a5abb47c1bdf239f855d2063b60595eJim Miller                    : (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD));
3252e28acedc623527aa5e84ba66635700d6547569aJim Miller
32617e9e1933055b207473b488595b2887bfcaea32eJim Miller            Intent intent = getActivity().getIntent();
3276609b0c22ae89a24d1b07dc3c4143452616d4450Andres Morales            final boolean confirmCredentials = intent.getBooleanExtra(
3286609b0c22ae89a24d1b07dc3c4143452616d4450Andres Morales                    ChooseLockGeneric.CONFIRM_CREDENTIALS, true);
329a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales            mCurrentPassword = intent.getStringExtra(ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
3306609b0c22ae89a24d1b07dc3c4143452616d4450Andres Morales            mHasChallenge = intent.getBooleanExtra(
3316609b0c22ae89a24d1b07dc3c4143452616d4450Andres Morales                    ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false);
3326609b0c22ae89a24d1b07dc3c4143452616d4450Andres Morales            mChallenge = intent.getLongExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0);
33317e9e1933055b207473b488595b2887bfcaea32eJim Miller            if (savedInstanceState == null) {
33417e9e1933055b207473b488595b2887bfcaea32eJim Miller                updateStage(Stage.Introduction);
33517e9e1933055b207473b488595b2887bfcaea32eJim Miller                if (confirmCredentials) {
33617e9e1933055b207473b488595b2887bfcaea32eJim Miller                    mChooseLockSettingsHelper.launchConfirmationActivity(CONFIRM_EXISTING_REQUEST,
337fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri                            getString(R.string.unlock_set_unlock_launch_picker_title), true,
338fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri                            mUserId);
33917e9e1933055b207473b488595b2887bfcaea32eJim Miller                }
34017e9e1933055b207473b488595b2887bfcaea32eJim Miller            } else {
341a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales                // restore from previous state
34217e9e1933055b207473b488595b2887bfcaea32eJim Miller                mFirstPin = savedInstanceState.getString(KEY_FIRST_PIN);
34317e9e1933055b207473b488595b2887bfcaea32eJim Miller                final String state = savedInstanceState.getString(KEY_UI_STAGE);
34417e9e1933055b207473b488595b2887bfcaea32eJim Miller                if (state != null) {
34517e9e1933055b207473b488595b2887bfcaea32eJim Miller                    mUiStage = Stage.valueOf(state);
34617e9e1933055b207473b488595b2887bfcaea32eJim Miller                    updateStage(mUiStage);
34717e9e1933055b207473b488595b2887bfcaea32eJim Miller                }
348a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales
349a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales                if (mCurrentPassword == null) {
350a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales                    mCurrentPassword = savedInstanceState.getString(KEY_CURRENT_PASSWORD);
351a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales                }
3523e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
3533e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                // Re-attach to the exiting worker if there is one.
3543e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                mSaveAndFinishWorker = (SaveAndFinishWorker) getFragmentManager().findFragmentByTag(
3553e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                        FRAGMENT_TAG_SAVE_AND_FINISH);
35617e9e1933055b207473b488595b2887bfcaea32eJim Miller            }
3570bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
358263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio            if (activity instanceof SettingsActivity) {
359263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                final SettingsActivity sa = (SettingsActivity) activity;
360d75905991c5a1783ca27dc188c2d086f8f13ff94Jim Miller                int id = mIsAlphaMode ? R.string.lockpassword_choose_your_password_header
361d75905991c5a1783ca27dc188c2d086f8f13ff94Jim Miller                        : R.string.lockpassword_choose_your_pin_header;
3629757e30dfa0ab691c4c15366568367e166071a7dJim Miller                CharSequence title = getText(id);
363263bcc8b732dbb47d3ce63904e0e05191fabbad6Fabrice Di Meglio                sa.setTitle(title);
3648df7173043d2c9dd5213f1701fd6f68c9e2383dbMaurice Lam                mLayout.setHeaderText(title);
3659757e30dfa0ab691c4c15366568367e166071a7dJim Miller            }
36617e9e1933055b207473b488595b2887bfcaea32eJim Miller        }
367122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
3680bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private void setupPasswordRequirementsView(View view) {
3690bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            // Construct passwordRequirements and requirementDescriptions.
3700bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            List<Integer> passwordRequirements = new ArrayList<>();
3710bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            List<String> requirementDescriptions = new ArrayList<>();
3720bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if (mPasswordMinUpperCase > 0) {
3730bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                passwordRequirements.add(MIN_UPPER_LETTERS_IN_PASSWORD);
3740bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                requirementDescriptions.add(getResources().getQuantityString(
3750bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_uppercase, mPasswordMinUpperCase,
3760bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinUpperCase));
3770bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
3780bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if (mPasswordMinLowerCase > 0) {
3790bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                passwordRequirements.add(MIN_LOWER_LETTERS_IN_PASSWORD);
3800bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                requirementDescriptions.add(getResources().getQuantityString(
3810bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_lowercase, mPasswordMinLowerCase,
3820bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinLowerCase));
3830bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
3840bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if (mPasswordMinLetters > 0) {
3859ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                if (mPasswordMinLetters > mPasswordMinUpperCase + mPasswordMinLowerCase) {
3869ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                    passwordRequirements.add(MIN_LETTER_IN_PASSWORD);
3879ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                    requirementDescriptions.add(getResources().getQuantityString(
3889ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                            R.plurals.lockpassword_password_requires_letters, mPasswordMinLetters,
3899ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                            mPasswordMinLetters));
3909ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                }
3910bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
3920bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if (mPasswordMinNumeric > 0) {
3930bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                passwordRequirements.add(MIN_NUMBER_IN_PASSWORD);
3940bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                requirementDescriptions.add(getResources().getQuantityString(
3950bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_numeric, mPasswordMinNumeric,
3960bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinNumeric));
3970bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
3980bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if (mPasswordMinSymbols > 0) {
3990bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                passwordRequirements.add(MIN_SYMBOLS_IN_PASSWORD);
4000bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                requirementDescriptions.add(getResources().getQuantityString(
4010bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_symbols, mPasswordMinSymbols,
4020bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinSymbols));
4030bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
4040bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if (mPasswordMinNonLetter > 0) {
4059ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                if (mPasswordMinNonLetter > mPasswordMinNumeric + mPasswordMinSymbols) {
4069ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                    passwordRequirements.add(MIN_NON_LETTER_IN_PASSWORD);
4079ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                    requirementDescriptions.add(getResources().getQuantityString(
4089ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                            R.plurals.lockpassword_password_requires_nonletter, mPasswordMinNonLetter,
4099ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak
4109ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                            mPasswordMinNonLetter));
4119ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                }
4120bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
4130bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            // Convert list to array.
4140bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordRequirements = passwordRequirements.stream().mapToInt(i -> i).toArray();
4150bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordRestrictionView =
4160bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    (RecyclerView) view.findViewById(R.id.password_requirements_view);
4170bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordRestrictionView.setLayoutManager(new LinearLayoutManager(getActivity()));
4180bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordRequirementAdapter = new PasswordRequirementAdapter();
4190bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordRestrictionView.setAdapter(mPasswordRequirementAdapter);
4200bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        }
4210bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
42217e9e1933055b207473b488595b2887bfcaea32eJim Miller        @Override
4236507613ebcd22e4691c2af92a5c161bd327db336Fan Zhang        public int getMetricsCategory() {
4249d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wren            return MetricsEvent.CHOOSE_LOCK_PASSWORD;
4258a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren        }
4268a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
4278a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren        @Override
42817e9e1933055b207473b488595b2887bfcaea32eJim Miller        public void onResume() {
42917e9e1933055b207473b488595b2887bfcaea32eJim Miller            super.onResume();
430122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller            updateStage(mUiStage);
4313e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            if (mSaveAndFinishWorker != null) {
4323e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                mSaveAndFinishWorker.setListener(this);
4333e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            } else {
4349c26c9dba6998fda4b3af27e315220b8bec6e0c5Tony Mak                mPasswordEntry.requestFocus();
4353e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            }
436122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        }
437122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
43817e9e1933055b207473b488595b2887bfcaea32eJim Miller        @Override
439552bc61ae2aaf67fd2f19564ca2ced002e8675e6Amith Yamasani        public void onPause() {
4403e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            if (mSaveAndFinishWorker != null) {
4413e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                mSaveAndFinishWorker.setListener(null);
442bf9ac6b4fb6428d0194a5163181b30cccaedb8ebXiyuan Xia            }
443552bc61ae2aaf67fd2f19564ca2ced002e8675e6Amith Yamasani            super.onPause();
444552bc61ae2aaf67fd2f19564ca2ced002e8675e6Amith Yamasani        }
445552bc61ae2aaf67fd2f19564ca2ced002e8675e6Amith Yamasani
446552bc61ae2aaf67fd2f19564ca2ced002e8675e6Amith Yamasani        @Override
44717e9e1933055b207473b488595b2887bfcaea32eJim Miller        public void onSaveInstanceState(Bundle outState) {
44817e9e1933055b207473b488595b2887bfcaea32eJim Miller            super.onSaveInstanceState(outState);
44917e9e1933055b207473b488595b2887bfcaea32eJim Miller            outState.putString(KEY_UI_STAGE, mUiStage.name());
45017e9e1933055b207473b488595b2887bfcaea32eJim Miller            outState.putString(KEY_FIRST_PIN, mFirstPin);
451a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales            outState.putString(KEY_CURRENT_PASSWORD, mCurrentPassword);
45200d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller        }
45300d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
45417e9e1933055b207473b488595b2887bfcaea32eJim Miller        @Override
45517e9e1933055b207473b488595b2887bfcaea32eJim Miller        public void onActivityResult(int requestCode, int resultCode,
45617e9e1933055b207473b488595b2887bfcaea32eJim Miller                Intent data) {
45717e9e1933055b207473b488595b2887bfcaea32eJim Miller            super.onActivityResult(requestCode, resultCode, data);
45817e9e1933055b207473b488595b2887bfcaea32eJim Miller            switch (requestCode) {
45917e9e1933055b207473b488595b2887bfcaea32eJim Miller                case CONFIRM_EXISTING_REQUEST:
46017e9e1933055b207473b488595b2887bfcaea32eJim Miller                    if (resultCode != Activity.RESULT_OK) {
46117e9e1933055b207473b488595b2887bfcaea32eJim Miller                        getActivity().setResult(RESULT_FINISHED);
46217e9e1933055b207473b488595b2887bfcaea32eJim Miller                        getActivity().finish();
463a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales                    } else {
464a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales                        mCurrentPassword = data.getStringExtra(
465a0e1236fa970ea953bfbcc84fcae1f3fb7f189d1Andres Morales                                ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD);
46617e9e1933055b207473b488595b2887bfcaea32eJim Miller                    }
46717e9e1933055b207473b488595b2887bfcaea32eJim Miller                    break;
46817e9e1933055b207473b488595b2887bfcaea32eJim Miller            }
46900d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller        }
47017e9e1933055b207473b488595b2887bfcaea32eJim Miller
471ecd2b7b81fd2faa2f2f3dbe5a169c749321f3d89Maurice Lam        protected Intent getRedactionInterstitialIntent(Context context) {
472f6077f4757183ae8b31af0928c895c806241b593Clara Bayarri            return RedactionInterstitial.createStartIntent(context, mUserId);
473ecd2b7b81fd2faa2f2f3dbe5a169c749321f3d89Maurice Lam        }
474ecd2b7b81fd2faa2f2f3dbe5a169c749321f3d89Maurice Lam
47517e9e1933055b207473b488595b2887bfcaea32eJim Miller        protected void updateStage(Stage stage) {
476eef72c39ae83697b1beaa148ec56aced67382fecalanv            final Stage previousStage = mUiStage;
47717e9e1933055b207473b488595b2887bfcaea32eJim Miller            mUiStage = stage;
47817e9e1933055b207473b488595b2887bfcaea32eJim Miller            updateUi();
479eef72c39ae83697b1beaa148ec56aced67382fecalanv
480eef72c39ae83697b1beaa148ec56aced67382fecalanv            // If the stage changed, announce the header for accessibility. This
481eef72c39ae83697b1beaa148ec56aced67382fecalanv            // is a no-op when accessibility is disabled.
482eef72c39ae83697b1beaa148ec56aced67382fecalanv            if (previousStage != stage) {
4838df7173043d2c9dd5213f1701fd6f68c9e2383dbMaurice Lam                mLayout.announceForAccessibility(mLayout.getHeaderText());
484eef72c39ae83697b1beaa148ec56aced67382fecalanv            }
48500d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller        }
48617e9e1933055b207473b488595b2887bfcaea32eJim Miller
48717e9e1933055b207473b488595b2887bfcaea32eJim Miller        /**
4880bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         * Read the requirements from {@link DevicePolicyManager} and intent and aggregate them.
4890bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         *
4900bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         * @param intent the incoming intent
4910bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         */
4920bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private void processPasswordRequirements(Intent intent) {
4930bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            final int dpmPasswordQuality = mLockPatternUtils.getRequestedPasswordQuality(mUserId);
4940bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mRequestedQuality = Math.max(intent.getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY,
4950bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mRequestedQuality), dpmPasswordQuality);
4960bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordMinLength = Math.max(Math.max(
4970bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    LockPatternUtils.MIN_LOCK_PASSWORD_SIZE,
4980bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    intent.getIntExtra(PASSWORD_MIN_KEY, mPasswordMinLength)),
4990bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mLockPatternUtils.getRequestedMinimumPasswordLength(mUserId));
5000bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordMaxLength = intent.getIntExtra(PASSWORD_MAX_KEY, mPasswordMaxLength);
5010bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordMinLetters = Math.max(intent.getIntExtra(PASSWORD_MIN_LETTERS_KEY,
5020bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinLetters), mLockPatternUtils.getRequestedPasswordMinimumLetters(
5030bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mUserId));
5040bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordMinUpperCase = Math.max(intent.getIntExtra(PASSWORD_MIN_UPPERCASE_KEY,
5050bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinUpperCase), mLockPatternUtils.getRequestedPasswordMinimumUpperCase(
5060bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mUserId));
5070bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordMinLowerCase = Math.max(intent.getIntExtra(PASSWORD_MIN_LOWERCASE_KEY,
5080bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinLowerCase), mLockPatternUtils.getRequestedPasswordMinimumLowerCase(
5090bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mUserId));
5100bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordMinNumeric = Math.max(intent.getIntExtra(PASSWORD_MIN_NUMERIC_KEY,
5110bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinNumeric), mLockPatternUtils.getRequestedPasswordMinimumNumeric(
5120bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mUserId));
5130bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordMinSymbols = Math.max(intent.getIntExtra(PASSWORD_MIN_SYMBOLS_KEY,
5140bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinSymbols), mLockPatternUtils.getRequestedPasswordMinimumSymbols(
5150bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mUserId));
5160bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            mPasswordMinNonLetter = Math.max(intent.getIntExtra(PASSWORD_MIN_NONLETTER_KEY,
5170bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinNonLetter), mLockPatternUtils.getRequestedPasswordMinimumNonLetter(
5180bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mUserId));
5190bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
5200bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            // Modify the value based on dpm policy.
5210bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            switch (dpmPasswordQuality) {
5220bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                case PASSWORD_QUALITY_ALPHABETIC:
5230bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    if (mPasswordMinLetters == 0) {
5240bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinLetters = 1;
5250bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    }
5260bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    break;
5270bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                case PASSWORD_QUALITY_ALPHANUMERIC:
5280bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    if (mPasswordMinLetters == 0) {
5290bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinLetters = 1;
5300bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    }
5310bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    if (mPasswordMinNumeric == 0) {
5320bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinNumeric = 1;
5330bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    }
5340bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    break;
5350bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                case PASSWORD_QUALITY_COMPLEX:
5360bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    // Reserve all the requirements.
5370bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    break;
5380bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                default:
5390bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinNumeric = 0;
5400bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinLetters = 0;
5410bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinUpperCase = 0;
5420bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinLowerCase = 0;
5430bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinSymbols = 0;
5440bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    mPasswordMinNonLetter = 0;
5450bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
5469ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            mPasswordMinLengthToFulfillAllPolicies = getMinLengthToFulfillAllPolicies();
5470bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        }
5480bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
5490bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        /**
5500bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         * Validates PIN and returns the validation result.
5510bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         *
55217e9e1933055b207473b488595b2887bfcaea32eJim Miller         * @param password the raw password the user typed in
5530bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         * @return the validation result.
55417e9e1933055b207473b488595b2887bfcaea32eJim Miller         */
5550bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private int validatePassword(String password) {
5560bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            int errorCode = NO_ERROR;
5570bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
55817e9e1933055b207473b488595b2887bfcaea32eJim Miller            if (password.length() < mPasswordMinLength) {
5599ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                if (mPasswordMinLength > mPasswordMinLengthToFulfillAllPolicies) {
5609ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                    errorCode |= TOO_SHORT;
5619ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                }
5620bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            } else if (password.length() > mPasswordMaxLength) {
5630bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                errorCode |= TOO_LONG;
5640bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            } else {
5650bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                // The length requirements are fulfilled.
5660bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                if (mRequestedQuality == PASSWORD_QUALITY_NUMERIC_COMPLEX) {
5670bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    // Check for repeated characters or sequences (e.g. '1234', '0000', '2468')
56827ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                    final int sequence = PasswordMetrics.maxLengthSequence(password);
56927ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                    if (sequence > PasswordMetrics.MAX_ALLOWED_SEQUENCE) {
5700bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        errorCode |= CONTAIN_SEQUENTIAL_DIGITS;
5710bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    }
5720bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                }
5730bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                // Is the password recently used?
5740bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                if (mLockPatternUtils.checkPasswordHistory(password, mUserId)) {
5750bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    errorCode |= RECENTLY_USED;
5760bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                }
57700d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller            }
5780bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
57927ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull            // Allow non-control Latin-1 characters only.
58017e9e1933055b207473b488595b2887bfcaea32eJim Miller            for (int i = 0; i < password.length(); i++) {
58117e9e1933055b207473b488595b2887bfcaea32eJim Miller                char c = password.charAt(i);
5827e7912de4fb44481ded8e17d5ac1d7a1c9a0d184Nick Kralevich                if (c < 32 || c > 127) {
5830bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    errorCode |= CONTAIN_INVALID_CHARACTERS;
58427ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                    break;
58517e9e1933055b207473b488595b2887bfcaea32eJim Miller                }
58657fbf694a53afb9c1924d81272179dceb3acfa4dKonstantin Lopyrev            }
5870bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
58827ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull            final PasswordMetrics metrics = PasswordMetrics.computeForPassword(password);
58927ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull
5900bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            // Ensure no non-digits if we are requesting numbers. This shouldn't be possible unless
5910bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            // user finds some way to bring up soft keyboard.
5920bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if (mRequestedQuality == PASSWORD_QUALITY_NUMERIC
5930bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    || mRequestedQuality == PASSWORD_QUALITY_NUMERIC_COMPLEX) {
59427ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                if (metrics.letters > 0 || metrics.symbols > 0) {
5950bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    errorCode |= CONTAIN_NON_DIGITS;
59617e9e1933055b207473b488595b2887bfcaea32eJim Miller                }
597af366a3ed66dcb3c3ecb1dd101e5d8869b518598Jim Miller            }
5988fd852e7c2f488650095024aca9c51f8c7ddc3b3Nicolas Prevot
5990bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            // Check the requirements one by one.
6000bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            for (int i = 0; i < mPasswordRequirements.length; i++) {
6010bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                int passwordRestriction = mPasswordRequirements[i];
6020bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                switch (passwordRestriction) {
6030bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    case MIN_LETTER_IN_PASSWORD:
60427ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                        if (metrics.letters < mPasswordMinLetters) {
6050bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                            errorCode |= NOT_ENOUGH_LETTER;
6060bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        }
6070bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        break;
6080bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    case MIN_UPPER_LETTERS_IN_PASSWORD:
60927ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                        if (metrics.upperCase < mPasswordMinUpperCase) {
6100bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                            errorCode |= NOT_ENOUGH_UPPER_CASE;
6110bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        }
6120bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        break;
6130bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    case MIN_LOWER_LETTERS_IN_PASSWORD:
61427ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                        if (metrics.lowerCase < mPasswordMinLowerCase) {
6150bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                            errorCode |= NOT_ENOUGH_LOWER_CASE;
6160bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        }
6170bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        break;
6180bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    case MIN_SYMBOLS_IN_PASSWORD:
61927ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                        if (metrics.symbols < mPasswordMinSymbols) {
6200bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                            errorCode |= NOT_ENOUGH_SYMBOLS;
6210bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        }
6220bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        break;
6230bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    case MIN_NUMBER_IN_PASSWORD:
62427ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                        if (metrics.numeric < mPasswordMinNumeric) {
6250bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                            errorCode |= NOT_ENOUGH_DIGITS;
6260bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        }
6270bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        break;
6280bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                    case MIN_NON_LETTER_IN_PASSWORD:
62927ca263b7f6cf64f6a789a052a0f737b18a9aef3Andrew Scull                        if (metrics.nonLetter < mPasswordMinNonLetter) {
6300bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                            errorCode |= NOT_ENOUGH_NON_LETTER;
6310bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        }
6320bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        break;
6330bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                }
6340bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
6350bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            return errorCode;
636122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        }
63700d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
638cff37ecbdd4a3d8f9b81fc295155bde5fcf8ea0cPaul Lawrence        public void handleNext() {
6393e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            if (mSaveAndFinishWorker != null) return;
640cff37ecbdd4a3d8f9b81fc295155bde5fcf8ea0cPaul Lawrence            mChosenPassword = mPasswordEntry.getText().toString();
641cff37ecbdd4a3d8f9b81fc295155bde5fcf8ea0cPaul Lawrence            if (TextUtils.isEmpty(mChosenPassword)) {
64217e9e1933055b207473b488595b2887bfcaea32eJim Miller                return;
643122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller            }
64417e9e1933055b207473b488595b2887bfcaea32eJim Miller            if (mUiStage == Stage.Introduction) {
6450bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                if (validatePassword(mChosenPassword) == NO_ERROR) {
646cff37ecbdd4a3d8f9b81fc295155bde5fcf8ea0cPaul Lawrence                    mFirstPin = mChosenPassword;
64717e9e1933055b207473b488595b2887bfcaea32eJim Miller                    mPasswordEntry.setText("");
648eef72c39ae83697b1beaa148ec56aced67382fecalanv                    updateStage(Stage.NeedToConfirm);
64917e9e1933055b207473b488595b2887bfcaea32eJim Miller                }
65017e9e1933055b207473b488595b2887bfcaea32eJim Miller            } else if (mUiStage == Stage.NeedToConfirm) {
651cff37ecbdd4a3d8f9b81fc295155bde5fcf8ea0cPaul Lawrence                if (mFirstPin.equals(mChosenPassword)) {
6523e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                    startSaveAndFinish();
65317e9e1933055b207473b488595b2887bfcaea32eJim Miller                } else {
65417e9e1933055b207473b488595b2887bfcaea32eJim Miller                    CharSequence tmp = mPasswordEntry.getText();
65517e9e1933055b207473b488595b2887bfcaea32eJim Miller                    if (tmp != null) {
65617e9e1933055b207473b488595b2887bfcaea32eJim Miller                        Selection.setSelection((Spannable) tmp, 0, tmp.length());
65717e9e1933055b207473b488595b2887bfcaea32eJim Miller                    }
658eef72c39ae83697b1beaa148ec56aced67382fecalanv                    updateStage(Stage.ConfirmWrong);
659122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller                }
660122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller            }
661122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        }
662122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
663af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam        protected void setNextEnabled(boolean enabled) {
664af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam            mNextButton.setEnabled(enabled);
665af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam        }
666af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam
667af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam        protected void setNextText(int text) {
668af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam            mNextButton.setText(text);
669af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam        }
670af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam
67117e9e1933055b207473b488595b2887bfcaea32eJim Miller        public void onClick(View v) {
67217e9e1933055b207473b488595b2887bfcaea32eJim Miller            switch (v.getId()) {
67317e9e1933055b207473b488595b2887bfcaea32eJim Miller                case R.id.next_button:
67417e9e1933055b207473b488595b2887bfcaea32eJim Miller                    handleNext();
67517e9e1933055b207473b488595b2887bfcaea32eJim Miller                    break;
67600d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
67717e9e1933055b207473b488595b2887bfcaea32eJim Miller                case R.id.cancel_button:
67817e9e1933055b207473b488595b2887bfcaea32eJim Miller                    getActivity().finish();
67917e9e1933055b207473b488595b2887bfcaea32eJim Miller                    break;
68017e9e1933055b207473b488595b2887bfcaea32eJim Miller            }
68100d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller        }
68200d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller
68317e9e1933055b207473b488595b2887bfcaea32eJim Miller        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
68474b1e0a7330af195621ad0965748bee66c46f4b6Jim Miller            // Check if this was the result of hitting the enter or "done" key
68574b1e0a7330af195621ad0965748bee66c46f4b6Jim Miller            if (actionId == EditorInfo.IME_NULL
68674b1e0a7330af195621ad0965748bee66c46f4b6Jim Miller                    || actionId == EditorInfo.IME_ACTION_DONE
68774b1e0a7330af195621ad0965748bee66c46f4b6Jim Miller                    || actionId == EditorInfo.IME_ACTION_NEXT) {
68817e9e1933055b207473b488595b2887bfcaea32eJim Miller                handleNext();
68917e9e1933055b207473b488595b2887bfcaea32eJim Miller                return true;
69017e9e1933055b207473b488595b2887bfcaea32eJim Miller            }
69117e9e1933055b207473b488595b2887bfcaea32eJim Miller            return false;
692122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        }
693122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
69417e9e1933055b207473b488595b2887bfcaea32eJim Miller        /**
6950bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         * @param errorCode error code returned from {@link #validatePassword(String)}.
6960bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         * @return an array of messages describing the error, important messages come first.
6970bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak         */
6980bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private String[] convertErrorCodeToMessages(int errorCode) {
6990bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            List<String> messages = new ArrayList<>();
7000bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & CONTAIN_INVALID_CHARACTERS) > 0) {
7010bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getString(R.string.lockpassword_illegal_character));
7020bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7030bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & CONTAIN_NON_DIGITS) > 0) {
7040bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getString(R.string.lockpassword_pin_contains_non_digits));
7050bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7060bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & NOT_ENOUGH_UPPER_CASE) > 0) {
7070bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getResources().getQuantityString(
7080bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_uppercase, mPasswordMinUpperCase,
7090bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinUpperCase));
7100bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7110bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & NOT_ENOUGH_LOWER_CASE) > 0) {
7120bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getResources().getQuantityString(
7130bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_lowercase, mPasswordMinLowerCase,
7140bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinLowerCase));
7150bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7169ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            if ((errorCode & NOT_ENOUGH_LETTER) > 0) {
7179ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                messages.add(getResources().getQuantityString(
7189ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                        R.plurals.lockpassword_password_requires_letters, mPasswordMinLetters,
7199ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                        mPasswordMinLetters));
7209ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            }
7210bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & NOT_ENOUGH_DIGITS) > 0) {
7220bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getResources().getQuantityString(
7230bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_numeric, mPasswordMinNumeric,
7240bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinNumeric));
7250bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7260bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & NOT_ENOUGH_SYMBOLS) > 0) {
7270bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getResources().getQuantityString(
7280bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_symbols, mPasswordMinSymbols,
7290bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinSymbols));
7300bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7310bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & NOT_ENOUGH_NON_LETTER) > 0) {
7320bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getResources().getQuantityString(
7330bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.plurals.lockpassword_password_requires_nonletter, mPasswordMinNonLetter,
7340bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mPasswordMinNonLetter));
7350bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7360bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & TOO_SHORT) > 0) {
7370bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getString(mIsAlphaMode ?
7380bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.string.lockpassword_password_too_short
7390bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        : R.string.lockpassword_pin_too_short, mPasswordMinLength));
7400bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7410bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & TOO_LONG) > 0) {
7420bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getString(mIsAlphaMode ?
7430bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        R.string.lockpassword_password_too_long
7440bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        : R.string.lockpassword_pin_too_long, mPasswordMaxLength + 1));
7450bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7460bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & CONTAIN_SEQUENTIAL_DIGITS) > 0) {
7470bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getString(R.string.lockpassword_pin_no_sequential_digits));
7480bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7490bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            if ((errorCode & RECENTLY_USED) > 0) {
7500bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                messages.add(getString((mIsAlphaMode) ? R.string.lockpassword_password_recently_used
7510bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        : R.string.lockpassword_pin_recently_used));
7520bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7530bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            return messages.toArray(new String[0]);
7540bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        }
7550bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
7569ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak        private int getMinLengthToFulfillAllPolicies() {
7579ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            final int minLengthForLetters = Math.max(mPasswordMinLetters,
7589ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                    mPasswordMinUpperCase + mPasswordMinLowerCase);
7599ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            final int minLengthForNonLetters = Math.max(mPasswordMinNonLetter,
7609ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                    mPasswordMinSymbols + mPasswordMinNumeric);
7619ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            return minLengthForLetters + minLengthForNonLetters;
7629ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak        }
7639ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak
7640bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        /**
76517e9e1933055b207473b488595b2887bfcaea32eJim Miller         * Update the hint based on current Stage and length of password entry
76617e9e1933055b207473b488595b2887bfcaea32eJim Miller         */
76717e9e1933055b207473b488595b2887bfcaea32eJim Miller        private void updateUi() {
7683e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            final boolean canInput = mSaveAndFinishWorker == null;
76917e9e1933055b207473b488595b2887bfcaea32eJim Miller            String password = mPasswordEntry.getText().toString();
77017e9e1933055b207473b488595b2887bfcaea32eJim Miller            final int length = password.length();
771163dee9fb78ef7c86929b250d150fa989484c2c7Maurice Lam            if (mUiStage == Stage.Introduction) {
7720bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                mPasswordRestrictionView.setVisibility(View.VISIBLE);
7730bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                final int errorCode = validatePassword(password);
7740bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                String[] messages = convertErrorCodeToMessages(errorCode);
7750bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                // Update the fulfillment of requirements.
7760bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                mPasswordRequirementAdapter.setRequirements(messages);
7770bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                // Enable/Disable the next button accordingly.
7780bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                setNextEnabled(errorCode == NO_ERROR);
77917e9e1933055b207473b488595b2887bfcaea32eJim Miller            } else {
7800bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                // Hide password requirement view when we are just asking user to confirm the pw.
7810bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                mPasswordRestrictionView.setVisibility(View.GONE);
7820bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                setHeaderText(getString(
7830bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                        mIsAlphaMode ? mUiStage.alphaHint : mUiStage.numericHint));
7843e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                setNextEnabled(canInput && length > 0);
785122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller            }
786af103b4eb476881fb27cfb9b6dc94b3fa5e74f28Maurice Lam            setNextText(mUiStage.buttonText);
7873e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            mPasswordEntryInputDisabler.setInputEnabled(canInput);
788122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        }
789122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
7900bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        private void setHeaderText(String text) {
7910bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            // Only set the text if it is different than the existing one to avoid announcing again.
7928df7173043d2c9dd5213f1701fd6f68c9e2383dbMaurice Lam            if (!TextUtils.isEmpty(mLayout.getHeaderText())
7938df7173043d2c9dd5213f1701fd6f68c9e2383dbMaurice Lam                    && mLayout.getHeaderText().toString().equals(text)) {
7940bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak                return;
7950bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak            }
7968df7173043d2c9dd5213f1701fd6f68c9e2383dbMaurice Lam            mLayout.setHeaderText(text);
7970bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak        }
7980bbcdccdbfa1f8473eb1c14e275c7de6dd43b4d4Tony Mak
79917e9e1933055b207473b488595b2887bfcaea32eJim Miller        public void afterTextChanged(Editable s) {
80017e9e1933055b207473b488595b2887bfcaea32eJim Miller            // Changing the text while error displayed resets to NeedToConfirm state
80117e9e1933055b207473b488595b2887bfcaea32eJim Miller            if (mUiStage == Stage.ConfirmWrong) {
80217e9e1933055b207473b488595b2887bfcaea32eJim Miller                mUiStage = Stage.NeedToConfirm;
80317e9e1933055b207473b488595b2887bfcaea32eJim Miller            }
8049ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            // Schedule the UI update.
8059ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            mTextChangedHandler.notifyAfterTextChanged();
806122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller        }
807122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
80817e9e1933055b207473b488595b2887bfcaea32eJim Miller        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
809122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
81017e9e1933055b207473b488595b2887bfcaea32eJim Miller        }
811122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
81217e9e1933055b207473b488595b2887bfcaea32eJim Miller        public void onTextChanged(CharSequence s, int start, int before, int count) {
813122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller
81417e9e1933055b207473b488595b2887bfcaea32eJim Miller        }
8153e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8163e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        private void startSaveAndFinish() {
8173e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            if (mSaveAndFinishWorker != null) {
8183e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                Log.w(TAG, "startSaveAndFinish with an existing SaveAndFinishWorker.");
8193e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                return;
8203e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            }
8213e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8223e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            mPasswordEntryInputDisabler.setInputEnabled(false);
8233e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            setNextEnabled(false);
8243e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8253e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            mSaveAndFinishWorker = new SaveAndFinishWorker();
8265bb1e4fb909a5f0e0574d3572ea1edcf32a2190fRobin Lee            mSaveAndFinishWorker.setListener(this);
8275bb1e4fb909a5f0e0574d3572ea1edcf32a2190fRobin Lee
8283e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            getFragmentManager().beginTransaction().add(mSaveAndFinishWorker,
8293e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                    FRAGMENT_TAG_SAVE_AND_FINISH).commit();
8305bb1e4fb909a5f0e0574d3572ea1edcf32a2190fRobin Lee            getFragmentManager().executePendingTransactions();
8313e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8323e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            final boolean required = getActivity().getIntent().getBooleanExtra(
8333e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                    EncryptionInterstitial.EXTRA_REQUIRE_PASSWORD, true);
8343e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            mSaveAndFinishWorker.start(mLockPatternUtils, required, mHasChallenge, mChallenge,
835fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri                    mChosenPassword, mCurrentPassword, mRequestedQuality, mUserId);
8363e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        }
8373e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8383e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        @Override
8393e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        public void onChosenLockSaveFinished(boolean wasSecureBefore, Intent resultData) {
8403e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            getActivity().setResult(RESULT_FINISHED, resultData);
8413e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8423e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            if (!wasSecureBefore) {
8433e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                Intent intent = getRedactionInterstitialIntent(getActivity());
8443e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                if (intent != null) {
84592779ce70156031f4fa3b8c2d84305f5b3b44105Udam Saini                    intent.putExtra(EXTRA_HIDE_DRAWER, mHideDrawer);
8463e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                    startActivity(intent);
8473e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                }
8483e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            }
849686ae494f485a5025dfa456b4f32427b3715c337Udam Saini            getActivity().finish();
8503e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        }
8519ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak
8529ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak        class TextChangedHandler extends Handler {
8539ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            private static final int ON_TEXT_CHANGED = 1;
8549ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            private static final int DELAY_IN_MILLISECOND = 100;
8559ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak
8569ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            /**
8579ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak             * With the introduction of delay, we batch processing the text changed event to reduce
8589ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak             * unnecessary UI updates.
8599ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak             */
8609ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            private void notifyAfterTextChanged() {
8619ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                removeMessages(ON_TEXT_CHANGED);
8629ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                sendEmptyMessageDelayed(ON_TEXT_CHANGED, DELAY_IN_MILLISECOND);
8639ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            }
8649ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak
8659ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            @Override
8669ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            public void handleMessage(Message msg) {
867f958f2e14ee07098543291181113c0afdb3b193fDoris Ling                if (getActivity() == null) {
868f958f2e14ee07098543291181113c0afdb3b193fDoris Ling                    return;
869f958f2e14ee07098543291181113c0afdb3b193fDoris Ling                }
8709ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                if (msg.what == ON_TEXT_CHANGED) {
8719ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                    updateUi();
8729ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak                }
8739ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak            }
8749ba765b875ebeb66d8a870a29a00fe53e9da306dTony Mak        }
8753e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia    }
8763e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
87777c4345373bdf6d6543bc06a04abc026ce762dbeClara Bayarri    public static class SaveAndFinishWorker extends SaveChosenLockWorkerBase {
8783e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8793e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        private String mChosenPassword;
8803e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        private String mCurrentPassword;
8813e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        private int mRequestedQuality;
8823e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8833e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        public void start(LockPatternUtils utils, boolean required,
8843e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                boolean hasChallenge, long challenge,
885fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri                String chosenPassword, String currentPassword, int requestedQuality, int userId) {
886b8a22e42d42e6de8fce1a2026c112c12fd0f0f7aClara Bayarri            prepare(utils, required, hasChallenge, challenge, userId);
8873e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8883e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            mChosenPassword = chosenPassword;
8893e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            mCurrentPassword = currentPassword;
8903e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            mRequestedQuality = requestedQuality;
891fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri            mUserId = userId;
8923e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8933e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            start();
8943e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        }
8953e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
8963e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        @Override
8973e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        protected Intent saveAndVerifyInBackground() {
8983e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            Intent result = null;
8993e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            mUtils.saveLockPassword(mChosenPassword, mCurrentPassword, mRequestedQuality,
900fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri                    mUserId);
9013e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
9023e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            if (mHasChallenge) {
9033e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                byte[] token;
9043e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                try {
905fe432e838e5588cd4ac664d7e74f3d70a99d7df1Clara Bayarri                    token = mUtils.verifyPassword(mChosenPassword, mChallenge, mUserId);
9063e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                } catch (RequestThrottledException e) {
9073e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                    token = null;
9083e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                }
9093e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
9103e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                if (token == null) {
9113e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                    Log.e(TAG, "critical: no token returned for known good password.");
9123e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                }
9133e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
9143e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                result = new Intent();
9153e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia                result.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, token);
9163e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            }
9173e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia
9183e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia            return result;
9193e7e3d6c594018e4bc7fc77acc19e148d8bf1bddXiyuan Xia        }
920122b6c85a9bc7076eacc388142f8c4fccc5462c9Jim Miller    }
92100d2476d8fc122b4eef59d6be6f7aa41e9c56fdeJim Miller}
922