351 lines
14 KiB
Java
351 lines
14 KiB
Java
/*
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.android.settings;
|
|
|
|
import android.annotation.Nullable;
|
|
import android.os.UserHandle;
|
|
import android.text.TextUtils;
|
|
import com.android.internal.logging.MetricsLogger;
|
|
import com.android.internal.widget.LockPatternChecker;
|
|
import com.android.internal.widget.LockPatternUtils;
|
|
|
|
import android.app.admin.DevicePolicyManager;
|
|
import android.content.Intent;
|
|
import android.os.AsyncTask;
|
|
import android.os.Bundle;
|
|
import android.os.CountDownTimer;
|
|
import android.os.Handler;
|
|
import android.os.SystemClock;
|
|
import android.os.storage.StorageManager;
|
|
import android.text.InputType;
|
|
import android.view.KeyEvent;
|
|
import android.view.LayoutInflater;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.View.OnClickListener;
|
|
import android.view.ViewGroup;
|
|
import android.view.inputmethod.EditorInfo;
|
|
import android.widget.TextView;
|
|
import android.widget.TextView.OnEditorActionListener;
|
|
|
|
public class ConfirmLockPassword extends ConfirmDeviceCredentialBaseActivity {
|
|
|
|
public static class InternalActivity extends ConfirmLockPassword {
|
|
}
|
|
|
|
@Override
|
|
public Intent getIntent() {
|
|
Intent modIntent = new Intent(super.getIntent());
|
|
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, ConfirmLockPasswordFragment.class.getName());
|
|
return modIntent;
|
|
}
|
|
|
|
@Override
|
|
protected boolean isValidFragment(String fragmentName) {
|
|
if (ConfirmLockPasswordFragment.class.getName().equals(fragmentName)) return true;
|
|
return false;
|
|
}
|
|
|
|
public static class ConfirmLockPasswordFragment extends ConfirmDeviceCredentialBaseFragment
|
|
implements OnClickListener, OnEditorActionListener {
|
|
private static final String KEY_NUM_WRONG_CONFIRM_ATTEMPTS
|
|
= "confirm_lock_password_fragment.key_num_wrong_confirm_attempts";
|
|
private static final long ERROR_MESSAGE_TIMEOUT = 3000;
|
|
private TextView mPasswordEntry;
|
|
private LockPatternUtils mLockPatternUtils;
|
|
private AsyncTask<?, ?, ?> mPendingLockCheck;
|
|
private TextView mHeaderTextView;
|
|
private TextView mDetailsTextView;
|
|
private TextView mErrorTextView;
|
|
private Handler mHandler = new Handler();
|
|
private int mNumWrongConfirmAttempts;
|
|
private CountDownTimer mCountdownTimer;
|
|
private boolean mIsAlpha;
|
|
|
|
// required constructor for fragments
|
|
public ConfirmLockPasswordFragment() {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
mLockPatternUtils = new LockPatternUtils(getActivity());
|
|
if (savedInstanceState != null) {
|
|
mNumWrongConfirmAttempts = savedInstanceState.getInt(
|
|
KEY_NUM_WRONG_CONFIRM_ATTEMPTS, 0);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
Bundle savedInstanceState) {
|
|
final int storedQuality = mLockPatternUtils.getKeyguardStoredPasswordQuality(
|
|
UserHandle.myUserId());
|
|
View view = inflater.inflate(R.layout.confirm_lock_password, null);
|
|
|
|
mPasswordEntry = (TextView) view.findViewById(R.id.password_entry);
|
|
mPasswordEntry.setOnEditorActionListener(this);
|
|
|
|
mHeaderTextView = (TextView) view.findViewById(R.id.headerText);
|
|
mDetailsTextView = (TextView) view.findViewById(R.id.detailsText);
|
|
mErrorTextView = (TextView) view.findViewById(R.id.errorText);
|
|
mIsAlpha = DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC == storedQuality
|
|
|| DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC == storedQuality
|
|
|| DevicePolicyManager.PASSWORD_QUALITY_COMPLEX == storedQuality;
|
|
|
|
Intent intent = getActivity().getIntent();
|
|
if (intent != null) {
|
|
CharSequence headerMessage = intent.getCharSequenceExtra(
|
|
ConfirmDeviceCredentialBaseFragment.HEADER_TEXT);
|
|
CharSequence detailsMessage = intent.getCharSequenceExtra(
|
|
ConfirmDeviceCredentialBaseFragment.DETAILS_TEXT);
|
|
if (TextUtils.isEmpty(headerMessage)) {
|
|
headerMessage = getString(getDefaultHeader());
|
|
}
|
|
if (TextUtils.isEmpty(detailsMessage)) {
|
|
detailsMessage = getString(getDefaultDetails());
|
|
}
|
|
mHeaderTextView.setText(headerMessage);
|
|
mDetailsTextView.setText(detailsMessage);
|
|
}
|
|
int currentType = mPasswordEntry.getInputType();
|
|
mPasswordEntry.setInputType(mIsAlpha ? currentType
|
|
: (InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD));
|
|
return view;
|
|
}
|
|
|
|
private int getDefaultHeader() {
|
|
return mIsAlpha ? R.string.lockpassword_confirm_your_password_header
|
|
: R.string.lockpassword_confirm_your_pin_header;
|
|
}
|
|
|
|
private int getDefaultDetails() {
|
|
return mIsAlpha ? R.string.lockpassword_confirm_your_password_generic
|
|
: R.string.lockpassword_confirm_your_pin_generic;
|
|
}
|
|
|
|
private int getErrorMessage() {
|
|
return mIsAlpha ? R.string.lockpassword_invalid_password
|
|
: R.string.lockpassword_invalid_pin;
|
|
}
|
|
|
|
@Override
|
|
public void onPause() {
|
|
super.onPause();
|
|
if (mCountdownTimer != null) {
|
|
mCountdownTimer.cancel();
|
|
mCountdownTimer = null;
|
|
}
|
|
if (mPendingLockCheck != null) {
|
|
mPendingLockCheck.cancel(false);
|
|
mPendingLockCheck = null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
protected int getMetricsCategory() {
|
|
return MetricsLogger.CONFIRM_LOCK_PASSWORD;
|
|
}
|
|
|
|
@Override
|
|
public void onResume() {
|
|
super.onResume();
|
|
long deadline = mLockPatternUtils.getLockoutAttemptDeadline(UserHandle.myUserId());
|
|
if (deadline != 0) {
|
|
handleAttemptLockout(deadline);
|
|
} else {
|
|
mPasswordEntry.setEnabled(true);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
super.onSaveInstanceState(outState);
|
|
outState.putInt(KEY_NUM_WRONG_CONFIRM_ATTEMPTS, mNumWrongConfirmAttempts);
|
|
}
|
|
|
|
@Override
|
|
protected void authenticationSucceeded() {
|
|
Intent intent = new Intent();
|
|
getActivity().setResult(RESULT_OK, intent);
|
|
getActivity().finish();
|
|
}
|
|
|
|
private void handleNext() {
|
|
mPasswordEntry.setEnabled(false);
|
|
if (mPendingLockCheck != null) {
|
|
mPendingLockCheck.cancel(false);
|
|
}
|
|
|
|
final String pin = mPasswordEntry.getText().toString();
|
|
final boolean verifyChallenge = getActivity().getIntent().getBooleanExtra(
|
|
ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, false);
|
|
Intent intent = new Intent();
|
|
if (verifyChallenge) {
|
|
if (isInternalActivity()) {
|
|
startVerifyPassword(pin, intent);
|
|
return;
|
|
}
|
|
} else {
|
|
startCheckPassword(pin, intent);
|
|
return;
|
|
}
|
|
|
|
onPasswordChecked(false, intent);
|
|
}
|
|
|
|
private boolean isInternalActivity() {
|
|
return getActivity() instanceof ConfirmLockPassword.InternalActivity;
|
|
}
|
|
|
|
private void startVerifyPassword(final String pin, final Intent intent) {
|
|
long challenge = getActivity().getIntent().getLongExtra(
|
|
ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, 0);
|
|
mPendingLockCheck = LockPatternChecker.verifyPassword(
|
|
mLockPatternUtils,
|
|
pin,
|
|
challenge,
|
|
UserHandle.myUserId(),
|
|
new LockPatternChecker.OnVerifyCallback() {
|
|
@Override
|
|
public void onVerified(byte[] token) {
|
|
mPendingLockCheck = null;
|
|
boolean matched = false;
|
|
if (token != null) {
|
|
matched = true;
|
|
intent.putExtra(
|
|
ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN,
|
|
token);
|
|
}
|
|
onPasswordChecked(matched, intent);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void startCheckPassword(final String pin, final Intent intent) {
|
|
mPendingLockCheck = LockPatternChecker.checkPassword(
|
|
mLockPatternUtils,
|
|
pin,
|
|
UserHandle.myUserId(),
|
|
new LockPatternChecker.OnCheckCallback() {
|
|
@Override
|
|
public void onChecked(boolean matched) {
|
|
mPendingLockCheck = null;
|
|
if (matched && isInternalActivity()) {
|
|
intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_TYPE,
|
|
mIsAlpha ? StorageManager.CRYPT_TYPE_PASSWORD
|
|
: StorageManager.CRYPT_TYPE_PIN);
|
|
intent.putExtra(
|
|
ChooseLockSettingsHelper.EXTRA_KEY_PASSWORD, pin);
|
|
}
|
|
onPasswordChecked(matched, intent);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void onPasswordChecked(boolean matched, Intent intent) {
|
|
mPasswordEntry.setEnabled(true);
|
|
if (matched) {
|
|
getActivity().setResult(RESULT_OK, intent);
|
|
getActivity().finish();
|
|
} else {
|
|
if (++mNumWrongConfirmAttempts >= LockPatternUtils.FAILED_ATTEMPTS_BEFORE_TIMEOUT) {
|
|
long deadline = mLockPatternUtils.setLockoutAttemptDeadline(
|
|
UserHandle.myUserId());
|
|
handleAttemptLockout(deadline);
|
|
} else {
|
|
showError(getErrorMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
private void handleAttemptLockout(long elapsedRealtimeDeadline) {
|
|
long elapsedRealtime = SystemClock.elapsedRealtime();
|
|
mPasswordEntry.setEnabled(false);
|
|
mCountdownTimer = new CountDownTimer(
|
|
elapsedRealtimeDeadline - elapsedRealtime,
|
|
LockPatternUtils.FAILED_ATTEMPT_COUNTDOWN_INTERVAL_MS) {
|
|
|
|
@Override
|
|
public void onTick(long millisUntilFinished) {
|
|
final int secondsCountdown = (int) (millisUntilFinished / 1000);
|
|
showError(getString(
|
|
R.string.lockpattern_too_many_failed_confirmation_attempts,
|
|
secondsCountdown), 0);
|
|
}
|
|
|
|
@Override
|
|
public void onFinish() {
|
|
mPasswordEntry.setEnabled(true);
|
|
mErrorTextView.setText("");
|
|
mNumWrongConfirmAttempts = 0;
|
|
}
|
|
}.start();
|
|
}
|
|
|
|
public void onClick(View v) {
|
|
switch (v.getId()) {
|
|
case R.id.next_button:
|
|
handleNext();
|
|
break;
|
|
|
|
case R.id.cancel_button:
|
|
getActivity().setResult(RESULT_CANCELED);
|
|
getActivity().finish();
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void showError(int msg) {
|
|
showError(msg, ERROR_MESSAGE_TIMEOUT);
|
|
}
|
|
|
|
private final Runnable mResetErrorRunnable = new Runnable() {
|
|
public void run() {
|
|
mErrorTextView.setText("");
|
|
}
|
|
};
|
|
|
|
private void showError(CharSequence msg, long timeout) {
|
|
mErrorTextView.setText(msg);
|
|
mErrorTextView.announceForAccessibility(mErrorTextView.getText());
|
|
mPasswordEntry.setText(null);
|
|
mHandler.removeCallbacks(mResetErrorRunnable);
|
|
if (timeout != 0) {
|
|
mHandler.postDelayed(mResetErrorRunnable, timeout);
|
|
}
|
|
}
|
|
|
|
private void showError(int msg, long timeout) {
|
|
showError(getText(msg), timeout);
|
|
}
|
|
|
|
// {@link OnEditorActionListener} methods.
|
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
|
// Check if this was the result of hitting the enter or "done" key
|
|
if (actionId == EditorInfo.IME_NULL
|
|
|| actionId == EditorInfo.IME_ACTION_DONE
|
|
|| actionId == EditorInfo.IME_ACTION_NEXT) {
|
|
handleNext();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|