You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
288 lines
10 KiB
288 lines
10 KiB
/*
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
*
|
|
* 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 net.micode.notes.ui;
|
|
|
|
import android.app.Activity;
|
|
import android.content.Intent;
|
|
import android.os.Bundle;
|
|
import android.os.Vibrator;
|
|
import android.text.TextUtils;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import net.micode.notes.R;
|
|
import net.micode.notes.model.WorkingNote;
|
|
import net.micode.notes.tool.LockPasswordUtils;
|
|
|
|
/**
|
|
* 数字密码输入界面
|
|
* 用于设置、修改或删除6位数字密码
|
|
*/
|
|
public class NumericPasswordActivity extends Activity {
|
|
private static final String TAG = "NumericPasswordActivity";
|
|
|
|
public static final String EXTRA_NOTE_ID = "note_id";
|
|
public static final String EXTRA_MODE = "mode";
|
|
public static final String MODE_SET = "set";
|
|
public static final String MODE_CHANGE = "change";
|
|
public static final String MODE_REMOVE = "remove";
|
|
public static final String MODE_VERIFY = "verify";
|
|
|
|
private TextView mPasswordDisplay;
|
|
private TextView mHintText;
|
|
private Button[] mNumberButtons = new Button[10];
|
|
private Button mDeleteButton;
|
|
private Button mCancelButton;
|
|
|
|
private long mNoteId;
|
|
private String mMode;
|
|
private WorkingNote mWorkingNote;
|
|
|
|
private String mFirstPassword;
|
|
private String mSecondPassword;
|
|
private String mOldPassword;
|
|
private StringBuilder mCurrentPassword;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_numeric_password);
|
|
|
|
mNoteId = getIntent().getLongExtra(EXTRA_NOTE_ID, 0);
|
|
mMode = getIntent().getStringExtra(EXTRA_MODE);
|
|
|
|
if (mNoteId <= 0) {
|
|
finish();
|
|
return;
|
|
}
|
|
|
|
mWorkingNote = WorkingNote.load(this, mNoteId);
|
|
mCurrentPassword = new StringBuilder();
|
|
|
|
initViews();
|
|
setupMode();
|
|
}
|
|
|
|
private void initViews() {
|
|
mPasswordDisplay = (TextView) findViewById(R.id.tv_password_display);
|
|
mHintText = (TextView) findViewById(R.id.tv_password_hint);
|
|
mDeleteButton = (Button) findViewById(R.id.btn_delete);
|
|
mCancelButton = (Button) findViewById(R.id.btn_cancel);
|
|
|
|
mNumberButtons[0] = (Button) findViewById(R.id.btn_0);
|
|
mNumberButtons[1] = (Button) findViewById(R.id.btn_1);
|
|
mNumberButtons[2] = (Button) findViewById(R.id.btn_2);
|
|
mNumberButtons[3] = (Button) findViewById(R.id.btn_3);
|
|
mNumberButtons[4] = (Button) findViewById(R.id.btn_4);
|
|
mNumberButtons[5] = (Button) findViewById(R.id.btn_5);
|
|
mNumberButtons[6] = (Button) findViewById(R.id.btn_6);
|
|
mNumberButtons[7] = (Button) findViewById(R.id.btn_7);
|
|
mNumberButtons[8] = (Button) findViewById(R.id.btn_8);
|
|
mNumberButtons[9] = (Button) findViewById(R.id.btn_9);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
final int number = i;
|
|
mNumberButtons[i].setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
onNumberPressed(number);
|
|
}
|
|
});
|
|
}
|
|
|
|
mDeleteButton.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
onDeletePressed();
|
|
}
|
|
});
|
|
|
|
mCancelButton.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
finish();
|
|
}
|
|
});
|
|
}
|
|
|
|
private void setupMode() {
|
|
if (MODE_SET.equals(mMode)) {
|
|
mHintText.setText(R.string.numeric_password_hint_set);
|
|
} else if (MODE_CHANGE.equals(mMode)) {
|
|
mHintText.setText(R.string.numeric_password_hint_verify);
|
|
} else if (MODE_REMOVE.equals(mMode)) {
|
|
mHintText.setText(R.string.numeric_password_hint_remove);
|
|
} else if (MODE_VERIFY.equals(mMode)) {
|
|
mHintText.setText(R.string.numeric_password_hint_verify);
|
|
}
|
|
}
|
|
|
|
private void onNumberPressed(int number) {
|
|
if (mCurrentPassword.length() < 6) {
|
|
mCurrentPassword.append(number);
|
|
updatePasswordDisplay();
|
|
|
|
if (mCurrentPassword.length() == 6) {
|
|
handlePasswordComplete();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void onDeletePressed() {
|
|
if (mCurrentPassword.length() > 0) {
|
|
mCurrentPassword.deleteCharAt(mCurrentPassword.length() - 1);
|
|
updatePasswordDisplay();
|
|
}
|
|
}
|
|
|
|
private void updatePasswordDisplay() {
|
|
StringBuilder display = new StringBuilder();
|
|
for (int i = 0; i < mCurrentPassword.length(); i++) {
|
|
display.append("●");
|
|
}
|
|
mPasswordDisplay.setText(display.toString());
|
|
}
|
|
|
|
private void handlePasswordComplete() {
|
|
String password = mCurrentPassword.toString();
|
|
|
|
if (MODE_SET.equals(mMode)) {
|
|
handleSetMode(password);
|
|
} else if (MODE_CHANGE.equals(mMode)) {
|
|
handleChangeMode(password);
|
|
} else if (MODE_REMOVE.equals(mMode)) {
|
|
handleRemoveMode(password);
|
|
} else if (MODE_VERIFY.equals(mMode)) {
|
|
handleVerifyMode(password);
|
|
}
|
|
}
|
|
|
|
private void handleSetMode(String password) {
|
|
if (mFirstPassword == null) {
|
|
mFirstPassword = password;
|
|
mHintText.setText(R.string.numeric_password_confirm);
|
|
mCurrentPassword.setLength(0);
|
|
updatePasswordDisplay();
|
|
} else {
|
|
mSecondPassword = password;
|
|
if (mFirstPassword.equals(mSecondPassword)) {
|
|
String encryptedPassword = LockPasswordUtils.encryptPassword(mFirstPassword);
|
|
mWorkingNote.setLocked(true);
|
|
mWorkingNote.setPasswordType(LockPasswordUtils.TYPE_NUMERIC);
|
|
mWorkingNote.setNumericPassword(encryptedPassword);
|
|
mWorkingNote.setLockPassword("");
|
|
mWorkingNote.saveNote();
|
|
Toast.makeText(this, R.string.numeric_password_success, Toast.LENGTH_SHORT).show();
|
|
finish();
|
|
} else {
|
|
mHintText.setText(R.string.numeric_password_confirm_error);
|
|
mFirstPassword = null;
|
|
mSecondPassword = null;
|
|
mCurrentPassword.setLength(0);
|
|
updatePasswordDisplay();
|
|
vibrate();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void handleChangeMode(String password) {
|
|
if (mOldPassword == null) {
|
|
String encryptedInput = LockPasswordUtils.encryptPassword(password);
|
|
String storedPassword = mWorkingNote.getNumericPassword();
|
|
|
|
if (encryptedInput.equals(storedPassword)) {
|
|
mOldPassword = storedPassword;
|
|
mHintText.setText(R.string.numeric_password_hint_new);
|
|
mCurrentPassword.setLength(0);
|
|
updatePasswordDisplay();
|
|
} else {
|
|
mHintText.setText(R.string.numeric_password_error);
|
|
mCurrentPassword.setLength(0);
|
|
updatePasswordDisplay();
|
|
vibrate();
|
|
}
|
|
} else if (mFirstPassword == null) {
|
|
mFirstPassword = password;
|
|
mHintText.setText(R.string.numeric_password_confirm);
|
|
mCurrentPassword.setLength(0);
|
|
updatePasswordDisplay();
|
|
} else {
|
|
mSecondPassword = password;
|
|
if (mFirstPassword.equals(mSecondPassword)) {
|
|
String encryptedPassword = LockPasswordUtils.encryptPassword(mFirstPassword);
|
|
mWorkingNote.setNumericPassword(encryptedPassword);
|
|
mWorkingNote.saveNote();
|
|
Toast.makeText(this, R.string.numeric_password_success, Toast.LENGTH_SHORT).show();
|
|
finish();
|
|
} else {
|
|
mHintText.setText(R.string.numeric_password_confirm_error);
|
|
mFirstPassword = null;
|
|
mSecondPassword = null;
|
|
mCurrentPassword.setLength(0);
|
|
updatePasswordDisplay();
|
|
vibrate();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void handleRemoveMode(String password) {
|
|
String encryptedInput = LockPasswordUtils.encryptPassword(password);
|
|
String storedPassword = mWorkingNote.getNumericPassword();
|
|
|
|
if (encryptedInput.equals(storedPassword)) {
|
|
mWorkingNote.setLocked(false);
|
|
mWorkingNote.setNumericPassword("");
|
|
mWorkingNote.setPasswordType("");
|
|
mWorkingNote.saveNote();
|
|
Toast.makeText(this, R.string.numeric_password_removed, Toast.LENGTH_SHORT).show();
|
|
finish();
|
|
} else {
|
|
mHintText.setText(R.string.numeric_password_error);
|
|
mCurrentPassword.setLength(0);
|
|
updatePasswordDisplay();
|
|
vibrate();
|
|
}
|
|
}
|
|
|
|
private void handleVerifyMode(String password) {
|
|
String encryptedInput = LockPasswordUtils.encryptPassword(password);
|
|
String storedPassword = mWorkingNote.getNumericPassword();
|
|
|
|
if (encryptedInput.equals(storedPassword)) {
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
intent.putExtra(Intent.EXTRA_UID, mNoteId);
|
|
startActivity(intent);
|
|
finish();
|
|
} else {
|
|
mHintText.setText(R.string.numeric_password_error);
|
|
mCurrentPassword.setLength(0);
|
|
updatePasswordDisplay();
|
|
vibrate();
|
|
}
|
|
}
|
|
|
|
private void vibrate() {
|
|
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
|
|
if (vibrator != null && vibrator.hasVibrator()) {
|
|
vibrator.vibrate(200);
|
|
}
|
|
}
|
|
}
|