-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathAbstractPasscodeKeyboardActivity.java
More file actions
182 lines (154 loc) · 6.21 KB
/
AbstractPasscodeKeyboardActivity.java
File metadata and controls
182 lines (154 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package org.wordpress.passcodelock;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v4.os.CancellationSignal;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public abstract class AbstractPasscodeKeyboardActivity extends Activity {
public static final String KEY_MESSAGE = "message";
protected EditText mPinCodeField;
protected InputFilter[] filters = null;
protected TextView topMessage = null;
protected FingerprintManagerCompat mFingerprintManager;
protected CancellationSignal mCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!getResources().getBoolean(R.bool.allow_rotation)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
setContentView(R.layout.app_passcode_keyboard);
topMessage = (TextView) findViewById(R.id.passcodelock_prompt);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String message = extras.getString(KEY_MESSAGE);
if (message != null) {
topMessage.setText(message);
}
}
filters = new InputFilter[2];
filters[0]= new InputFilter.LengthFilter(1);
filters[1] = onlyNumber;
mPinCodeField = (EditText)findViewById(R.id.pin_field);
//setup the keyboard
findViewById(R.id.button0).setOnClickListener(defaultButtonListener);
findViewById(R.id.button1).setOnClickListener(defaultButtonListener);
findViewById(R.id.button2).setOnClickListener(defaultButtonListener);
findViewById(R.id.button3).setOnClickListener(defaultButtonListener);
findViewById(R.id.button4).setOnClickListener(defaultButtonListener);
findViewById(R.id.button5).setOnClickListener(defaultButtonListener);
findViewById(R.id.button6).setOnClickListener(defaultButtonListener);
findViewById(R.id.button7).setOnClickListener(defaultButtonListener);
findViewById(R.id.button8).setOnClickListener(defaultButtonListener);
findViewById(R.id.button9).setOnClickListener(defaultButtonListener);
findViewById(R.id.button_erase).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View arg0) {
String curText = mPinCodeField.getText().toString();
if (curText.length() > 0) {
mPinCodeField.setText(curText.substring(0, curText.length() - 1));
mPinCodeField.setSelection(mPinCodeField.length());
}
}
});
mFingerprintManager = FingerprintManagerCompat.from(this);
}
@Override
public void onPause() {
super.onPause();
if (mCancel != null) {
mCancel.cancel();
}
}
protected AbstractAppLock getAppLock() {
return AppLockManager.getInstance().getAppLock();
}
private OnClickListener defaultButtonListener = new OnClickListener() {
@Override
public void onClick(View arg0) {
int currentValue = -1;
int id = arg0.getId();
if (id == R.id.button0) {
currentValue = 0;
} else if (id == R.id.button1) {
currentValue = 1;
} else if (id == R.id.button2) {
currentValue = 2;
} else if (id == R.id.button3) {
currentValue = 3;
} else if (id == R.id.button4) {
currentValue = 4;
} else if (id == R.id.button5) {
currentValue = 5;
} else if (id == R.id.button6) {
currentValue = 6;
} else if (id == R.id.button7) {
currentValue = 7;
} else if (id == R.id.button8) {
currentValue = 8;
} else if (id == R.id.button9) {
currentValue = 9;
}
//set the value and move the focus
String currentValueString = mPinCodeField.getText() + String.valueOf(currentValue);
mPinCodeField.setText(currentValueString);
mPinCodeField.setSelection(mPinCodeField.length());
if (mPinCodeField.length() >= 4) {
onPinLockInserted();
}
}
};
protected void authenticationSucceeded() {
setResult(RESULT_OK);
finish();
}
protected void authenticationFailed() {
Thread shake = new Thread() {
public void run() {
Animation shake = AnimationUtils.loadAnimation(AbstractPasscodeKeyboardActivity.this, R.anim.shake);
findViewById(R.id.AppUnlockLinearLayout1).startAnimation(shake);
showPasswordError();
mPinCodeField.setText("");
}
};
runOnUiThread(shake);
}
protected void showPasswordError(){
Toast toast = Toast.makeText(AbstractPasscodeKeyboardActivity.this, getString(R.string.passcode_wrong_passcode), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 30);
toast.show();
}
protected abstract void onPinLockInserted();
protected abstract FingerprintManagerCompat.AuthenticationCallback getFingerprintCallback();
private InputFilter onlyNumber = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source.length() > 1) {
return "";
}
if (source.length() == 0) {
return null;
}
try {
int number = Integer.parseInt(source.toString());
if (number >= 0 && number <= 9) {
return String.valueOf(number);
}
return "";
} catch (NumberFormatException e) {
return "";
}
}
};
}