Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.internal.app; |
| 18 | |
| 19 | import android.app.AlertDialog; |
| 20 | import android.content.Context; |
| 21 | import android.content.DialogInterface; |
| 22 | import android.os.Bundle; |
| 23 | import android.os.UserManager; |
| 24 | import android.text.Editable; |
| 25 | import android.text.TextWatcher; |
| 26 | import android.view.KeyEvent; |
| 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 29 | import android.view.WindowManager; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 30 | import android.widget.EditText; |
| 31 | import android.widget.TextView; |
| 32 | import android.widget.TextView.OnEditorActionListener; |
| 33 | |
| 34 | import com.android.internal.R; |
| 35 | |
| 36 | /** |
| 37 | * This activity is launched by Settings and other apps to either create a new PIN or |
| 38 | * challenge for an existing PIN. The PIN is maintained by UserManager. |
| 39 | */ |
| 40 | public class RestrictionsPinActivity extends AlertActivity |
| 41 | implements DialogInterface.OnClickListener, TextWatcher, OnEditorActionListener { |
| 42 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 43 | protected UserManager mUserManager; |
| 44 | protected boolean mHasRestrictionsPin; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 45 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 46 | protected EditText mPinText; |
| 47 | protected TextView mPinErrorMessage; |
| 48 | protected TextView mPinMessage; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 49 | |
| 50 | @Override |
| 51 | public void onCreate(Bundle icicle) { |
| 52 | super.onCreate(icicle); |
| 53 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 54 | mUserManager = (UserManager) getSystemService(Context.USER_SERVICE); |
| 55 | mHasRestrictionsPin = mUserManager.hasRestrictionsPin(); |
| 56 | initUi(); |
| 57 | setupAlert(); |
| 58 | } |
| 59 | |
| 60 | protected void initUi() { |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 61 | AlertController.AlertParams ap = mAlertParams; |
| 62 | ap.mTitle = getString(R.string.restr_pin_enter_pin); |
| 63 | ap.mPositiveButtonText = getString(R.string.ok); |
| 64 | ap.mNegativeButtonText = getString(R.string.cancel); |
| 65 | ap.mPositiveButtonListener = this; |
| 66 | ap.mNegativeButtonListener = this; |
| 67 | LayoutInflater inflater = |
| 68 | (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 69 | ap.mView = inflater.inflate(R.layout.restrictions_pin_challenge, null); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 70 | |
| 71 | mPinMessage = (TextView) ap.mView.findViewById(R.id.pin_message); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 72 | mPinText = (EditText) ap.mView.findViewById(R.id.pin_text); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 73 | mPinErrorMessage = (TextView) ap.mView.findViewById(R.id.pin_error_message); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 74 | mPinText.addTextChangedListener(this); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | protected boolean verifyingPin() { |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | public void onResume() { |
| 82 | super.onResume(); |
| 83 | |
| 84 | setPositiveButtonState(false); |
| 85 | boolean hasPin = mUserManager.hasRestrictionsPin(); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 86 | if (hasPin) { |
| 87 | mPinMessage.setVisibility(View.GONE); |
| 88 | mPinErrorMessage.setVisibility(View.GONE); |
| 89 | mPinText.setOnEditorActionListener(this); |
| 90 | updatePinTimer(-1); |
| 91 | } else if (verifyingPin()) { |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 92 | setResult(RESULT_OK); |
| 93 | finish(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private void setPositiveButtonState(boolean enabled) { |
| 98 | mAlert.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(enabled); |
| 99 | } |
| 100 | |
| 101 | private void updatePinTimer(int pinTimerMs) { |
| 102 | if (pinTimerMs < 0) { |
| 103 | pinTimerMs = mUserManager.checkRestrictionsPin(null); |
| 104 | } |
| 105 | if (pinTimerMs >= 200) { |
| 106 | final int seconds = (pinTimerMs + 200) / 1000; |
| 107 | final String formatString = getResources().getQuantityString( |
| 108 | R.plurals.restr_pin_countdown, |
| 109 | seconds); |
| 110 | mPinErrorMessage.setText(String.format(formatString, seconds)); |
| 111 | mPinErrorMessage.setVisibility(View.VISIBLE); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 112 | mPinText.setEnabled(false); |
| 113 | mPinText.setText(""); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 114 | setPositiveButtonState(false); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 115 | mPinText.postDelayed(mCountdownRunnable, Math.min(1000, pinTimerMs)); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 116 | } else { |
| 117 | mPinErrorMessage.setVisibility(View.INVISIBLE); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 118 | mPinText.setEnabled(true); |
| 119 | mPinText.setText(""); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | public void onClick(DialogInterface dialog, int which) { |
| 124 | setResult(RESULT_CANCELED); |
| 125 | if (which == AlertDialog.BUTTON_POSITIVE) { |
| 126 | performPositiveButtonAction(); |
| 127 | } else if (which == AlertDialog.BUTTON_NEGATIVE) { |
| 128 | finish(); |
| 129 | } |
| 130 | } |
| 131 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 132 | protected void performPositiveButtonAction() { |
| 133 | int result = mUserManager.checkRestrictionsPin(mPinText.getText().toString()); |
| 134 | if (result == UserManager.PIN_VERIFICATION_SUCCESS) { |
| 135 | setResult(RESULT_OK); |
| 136 | finish(); |
| 137 | } else if (result >= 0) { |
| 138 | updatePinTimer(result); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 148 | CharSequence pin = mPinText.getText(); |
| 149 | setPositiveButtonState(pin != null && pin.length() >= 4); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void afterTextChanged(Editable s) { |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 158 | performPositiveButtonAction(); |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | private Runnable mCountdownRunnable = new Runnable() { |
| 163 | public void run() { |
| 164 | updatePinTimer(-1); |
| 165 | } |
| 166 | }; |
| 167 | } |