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 | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 19 | import android.app.AlertDialog; |
| 20 | import android.content.Context; |
| 21 | import android.content.DialogInterface; |
| 22 | import android.os.UserManager; |
| 23 | import android.text.Editable; |
| 24 | import android.text.TextUtils; |
| 25 | import android.view.KeyEvent; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.widget.EditText; |
| 29 | import android.widget.TextView; |
| 30 | |
| 31 | import com.android.internal.R; |
| 32 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 33 | /** |
| 34 | * This activity is launched by Settings and other apps to either create a new PIN or |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 35 | * change an existing PIN. The PIN is maintained by UserManager. |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 36 | */ |
| 37 | public class RestrictionsPinSetupActivity extends RestrictionsPinActivity { |
| 38 | |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 39 | private EditText mNewPinText; |
| 40 | private EditText mConfirmPinText; |
| 41 | |
| 42 | protected void initUi() { |
| 43 | AlertController.AlertParams ap = mAlertParams; |
| 44 | ap.mTitle = getString(R.string.restr_pin_enter_pin); |
| 45 | ap.mPositiveButtonText = getString(R.string.ok); |
| 46 | ap.mNegativeButtonText = getString(R.string.cancel); |
| 47 | ap.mPositiveButtonListener = this; |
| 48 | ap.mNegativeButtonListener = this; |
| 49 | LayoutInflater inflater = |
| 50 | (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 51 | ap.mView = inflater.inflate(R.layout.restrictions_pin_setup, null); |
| 52 | |
| 53 | mPinText = (EditText) ap.mView.findViewById(R.id.pin_text); |
| 54 | mPinMessage = (TextView) ap.mView.findViewById(R.id.pin_message); |
| 55 | mNewPinText = (EditText) ap.mView.findViewById(R.id.pin_new_text); |
| 56 | mConfirmPinText = (EditText) ap.mView.findViewById(R.id.pin_confirm_text); |
| 57 | mPinErrorMessage = (TextView) ap.mView.findViewById(R.id.pin_error_message); |
| 58 | mNewPinText.addTextChangedListener(this); |
| 59 | mConfirmPinText.addTextChangedListener(this); |
| 60 | |
| 61 | if (!mHasRestrictionsPin) { |
| 62 | mPinText.setVisibility(View.GONE); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public void onResume() { |
| 67 | super.onResume(); |
| 68 | setPositiveButtonState(false); |
| 69 | } |
| 70 | |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 71 | protected boolean verifyingPin() { |
| 72 | return false; |
| 73 | } |
Amith Yamasani | 1a7472e | 2013-07-02 11:17:30 -0700 | [diff] [blame] | 74 | |
| 75 | private void setPositiveButtonState(boolean enabled) { |
| 76 | mAlert.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(enabled); |
| 77 | } |
| 78 | |
| 79 | public void onClick(DialogInterface dialog, int which) { |
| 80 | setResult(RESULT_CANCELED); |
| 81 | if (which == AlertDialog.BUTTON_POSITIVE) { |
| 82 | performPositiveButtonAction(); |
| 83 | } else if (which == AlertDialog.BUTTON_NEGATIVE) { |
| 84 | finish(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | protected void performPositiveButtonAction() { |
| 89 | if (mHasRestrictionsPin) { |
| 90 | int result = mUserManager.checkRestrictionsPin(mPinText.getText().toString()); |
| 91 | if (result != UserManager.PIN_VERIFICATION_SUCCESS) { |
| 92 | // TODO: Set message that existing pin doesn't match |
| 93 | return; |
| 94 | } |
| 95 | } |
| 96 | if (mUserManager.changeRestrictionsPin(mNewPinText.getText().toString())) { |
| 97 | // TODO: Send message to PIN recovery agent about the recovery email address |
| 98 | setResult(RESULT_OK); |
| 99 | finish(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 109 | CharSequence pin = mPinText.getText(); |
| 110 | CharSequence pin1 = mNewPinText.getText(); |
| 111 | CharSequence pin2 = mConfirmPinText.getText(); |
| 112 | boolean match = pin1 != null && pin2 != null && pin1.length() >= 4 |
| 113 | && pin1.toString().equals(pin2.toString()) |
| 114 | && (!mHasRestrictionsPin || (pin != null && pin.length() >= 4)); |
| 115 | boolean showError = !TextUtils.isEmpty(pin1) && !TextUtils.isEmpty(pin2); |
| 116 | // TODO: Check recovery email address as well |
| 117 | setPositiveButtonState(match); |
| 118 | mPinErrorMessage.setVisibility((match || !showError) ? View.INVISIBLE : View.VISIBLE); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void afterTextChanged(Editable s) { |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 127 | performPositiveButtonAction(); |
| 128 | return true; |
| 129 | } |
Amith Yamasani | 655d0e2 | 2013-06-12 14:19:10 -0700 | [diff] [blame] | 130 | } |