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 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
| 21 | import android.os.UserManager; |
| 22 | import android.text.Editable; |
| 23 | import android.text.TextWatcher; |
| 24 | import android.view.KeyEvent; |
| 25 | import android.view.LayoutInflater; |
| 26 | import android.view.View; |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 27 | import android.view.View.OnClickListener; |
| 28 | import android.widget.Button; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 29 | import android.widget.EditText; |
| 30 | import android.widget.TextView; |
| 31 | import android.widget.TextView.OnEditorActionListener; |
| 32 | |
| 33 | import com.android.internal.R; |
| 34 | |
| 35 | /** |
| 36 | * This activity is launched by Settings and other apps to either create a new PIN or |
| 37 | * challenge for an existing PIN. The PIN is maintained by UserManager. |
| 38 | */ |
| 39 | public class RestrictionsPinActivity extends AlertActivity |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 40 | implements OnClickListener, TextWatcher, OnEditorActionListener { |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 41 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 42 | protected UserManager mUserManager; |
| 43 | protected boolean mHasRestrictionsPin; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 44 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 45 | protected EditText mPinText; |
| 46 | protected TextView mPinErrorMessage; |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 47 | private Button mOkButton; |
| 48 | private Button mCancelButton; |
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); |
Amith Yamasani | d304af6 | 2013-09-05 09:30:23 -0700 | [diff] [blame] | 55 | mHasRestrictionsPin = mUserManager.hasRestrictionsChallenge(); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 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; |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 62 | ap.mTitle = getString(R.string.restr_pin_enter_admin_pin); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 63 | LayoutInflater inflater = |
| 64 | (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 65 | ap.mView = inflater.inflate(R.layout.restrictions_pin_challenge, null); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 66 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 67 | mPinErrorMessage = (TextView) ap.mView.findViewById(R.id.pin_error_message); |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 68 | mPinText = (EditText) ap.mView.findViewById(R.id.pin_text); |
| 69 | mOkButton = (Button) ap.mView.findViewById(R.id.pin_ok_button); |
| 70 | mCancelButton = (Button) ap.mView.findViewById(R.id.pin_cancel_button); |
| 71 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 72 | mPinText.addTextChangedListener(this); |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 73 | |
| 74 | mOkButton.setOnClickListener(this); |
| 75 | mCancelButton.setOnClickListener(this); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | protected boolean verifyingPin() { |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | public void onResume() { |
| 83 | super.onResume(); |
| 84 | |
| 85 | setPositiveButtonState(false); |
Amith Yamasani | d304af6 | 2013-09-05 09:30:23 -0700 | [diff] [blame] | 86 | boolean hasPin = mUserManager.hasRestrictionsChallenge(); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 87 | if (hasPin) { |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 88 | mPinErrorMessage.setVisibility(View.INVISIBLE); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 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 | |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 97 | protected void setPositiveButtonState(boolean enabled) { |
| 98 | mOkButton.setEnabled(enabled); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 101 | private boolean updatePinTimer(int pinTimerMs) { |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 102 | if (pinTimerMs < 0) { |
Amith Yamasani | d304af6 | 2013-09-05 09:30:23 -0700 | [diff] [blame] | 103 | pinTimerMs = mUserManager.checkRestrictionsChallenge(null); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 104 | } |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 105 | boolean enableInput; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 106 | if (pinTimerMs >= 200) { |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 107 | // Do the count down timer for less than a minute, otherwise just say try again later. |
| 108 | if (pinTimerMs <= 60000) { |
| 109 | final int seconds = (pinTimerMs + 200) / 1000; |
| 110 | final String formatString = getResources().getQuantityString( |
| 111 | R.plurals.restr_pin_countdown, |
| 112 | seconds); |
| 113 | mPinErrorMessage.setText(String.format(formatString, seconds)); |
| 114 | } else { |
| 115 | mPinErrorMessage.setText(R.string.restr_pin_try_later); |
| 116 | } |
| 117 | enableInput = false; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 118 | mPinErrorMessage.setVisibility(View.VISIBLE); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 119 | mPinText.setText(""); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 120 | mPinText.postDelayed(mCountdownRunnable, Math.min(1000, pinTimerMs)); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 121 | } else { |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 122 | enableInput = true; |
| 123 | mPinErrorMessage.setText(R.string.restr_pin_incorrect); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 124 | } |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 125 | mPinText.setEnabled(enableInput); |
| 126 | setPositiveButtonState(enableInput); |
| 127 | return enableInput; |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 130 | protected void performPositiveButtonAction() { |
Amith Yamasani | d304af6 | 2013-09-05 09:30:23 -0700 | [diff] [blame] | 131 | int result = mUserManager.checkRestrictionsChallenge(mPinText.getText().toString()); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 132 | if (result == UserManager.PIN_VERIFICATION_SUCCESS) { |
| 133 | setResult(RESULT_OK); |
| 134 | finish(); |
| 135 | } else if (result >= 0) { |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 136 | mPinErrorMessage.setText(R.string.restr_pin_incorrect); |
| 137 | mPinErrorMessage.setVisibility(View.VISIBLE); |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 138 | updatePinTimer(result); |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 139 | mPinText.setText(""); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 149 | CharSequence pin = mPinText.getText(); |
| 150 | setPositiveButtonState(pin != null && pin.length() >= 4); |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void afterTextChanged(Editable s) { |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 159 | performPositiveButtonAction(); |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | private Runnable mCountdownRunnable = new Runnable() { |
| 164 | public void run() { |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 165 | if (updatePinTimer(-1)) { |
| 166 | // If we are no longer counting down, clear the message. |
| 167 | mPinErrorMessage.setVisibility(View.INVISIBLE); |
| 168 | } |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 169 | } |
| 170 | }; |
Geoffrey Borggaard | 8135f3a | 2013-08-28 22:13:10 -0400 | [diff] [blame] | 171 | |
| 172 | @Override |
| 173 | public void onClick(View v) { |
| 174 | if (v == mOkButton) { |
| 175 | performPositiveButtonAction(); |
| 176 | } else if (v == mCancelButton) { |
| 177 | setResult(RESULT_CANCELED); |
| 178 | finish(); |
| 179 | } |
| 180 | } |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 181 | } |