notecipher / app / src / info / guardianproject / notepadbot / LockScreenActivity.java @ 72d577fb
History | View | Annotate | Download (10.4 KB)
1 |
|
---|---|
2 |
package info.guardianproject.notepadbot; |
3 |
|
4 |
import android.content.Intent; |
5 |
import android.os.Bundle; |
6 |
import android.os.Handler; |
7 |
import android.os.ResultReceiver; |
8 |
import android.util.Log; |
9 |
import android.view.KeyEvent; |
10 |
import android.view.View; |
11 |
import android.view.View.OnClickListener; |
12 |
import android.view.animation.Animation; |
13 |
import android.view.animation.AnimationUtils; |
14 |
import android.view.inputmethod.EditorInfo; |
15 |
import android.view.inputmethod.InputMethodManager; |
16 |
import android.widget.Button; |
17 |
import android.widget.EditText; |
18 |
import android.widget.LinearLayout; |
19 |
import android.widget.TextView; |
20 |
import android.widget.TextView.OnEditorActionListener; |
21 |
import android.widget.Toast; |
22 |
import android.widget.ViewFlipper; |
23 |
|
24 |
import com.actionbarsherlock.app.SherlockActivity; |
25 |
|
26 |
import info.guardianproject.cacheword.CacheWordActivityHandler; |
27 |
import info.guardianproject.cacheword.ICacheWordSubscriber; |
28 |
|
29 |
import java.security.GeneralSecurityException; |
30 |
|
31 |
public class LockScreenActivity extends SherlockActivity implements ICacheWordSubscriber { |
32 |
private static final String TAG = "LockScreenActivity"; |
33 |
|
34 |
private EditText mEnterPassphrase;
|
35 |
private EditText mNewPassphrase;
|
36 |
private EditText mConfirmNewPassphrase;
|
37 |
private View mViewCreatePassphrase; |
38 |
private View mViewEnterPassphrase; |
39 |
private Button mBtnOpen; |
40 |
private CacheWordActivityHandler mCacheWord;
|
41 |
private String mPasswordError; |
42 |
private TwoViewSlider mSlider;
|
43 |
|
44 |
@Override
|
45 |
protected void onCreate(Bundle savedInstanceState) { |
46 |
super.onCreate(savedInstanceState);
|
47 |
setContentView(R.layout.activity_lock_screen); |
48 |
mCacheWord = new CacheWordActivityHandler(this, ((App)getApplication()).getCWSettings()); |
49 |
|
50 |
mViewCreatePassphrase = findViewById(R.id.llCreatePassphrase); |
51 |
mViewEnterPassphrase = findViewById(R.id.llEnterPassphrase); |
52 |
|
53 |
mEnterPassphrase = (EditText) findViewById(R.id.editEnterPassphrase); |
54 |
mNewPassphrase = (EditText) findViewById(R.id.editNewPassphrase); |
55 |
mConfirmNewPassphrase = (EditText) findViewById(R.id.editConfirmNewPassphrase); |
56 |
ViewFlipper vf = (ViewFlipper) findViewById(R.id.viewFlipper1); |
57 |
LinearLayout flipView1 = (LinearLayout) findViewById(R.id.flipView1); |
58 |
LinearLayout flipView2 = (LinearLayout) findViewById(R.id.flipView2); |
59 |
|
60 |
mSlider = new TwoViewSlider(vf, flipView1, flipView2, mNewPassphrase, mConfirmNewPassphrase);
|
61 |
} |
62 |
|
63 |
@Override
|
64 |
protected void onPause() { |
65 |
super.onPause();
|
66 |
mCacheWord.onPause(); |
67 |
} |
68 |
|
69 |
@Override
|
70 |
protected void onResume() { |
71 |
super.onResume();
|
72 |
mCacheWord.onResume(); |
73 |
} |
74 |
|
75 |
@Override
|
76 |
public void onCacheWordUninitialized() { |
77 |
initializePassphrase(); |
78 |
} |
79 |
|
80 |
@Override
|
81 |
public void onCacheWordLocked() { |
82 |
promptPassphrase(); |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public void onCacheWordOpened() { |
87 |
Intent intent = (Intent) getIntent().getParcelableExtra("originalIntent");
|
88 |
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
89 |
|
90 |
startActivity(intent); |
91 |
finish(); |
92 |
overridePendingTransition(0, 0); |
93 |
} |
94 |
|
95 |
private boolean newEqualsConfirmation() { |
96 |
return mNewPassphrase.getText().toString()
|
97 |
.equals(mConfirmNewPassphrase.getText().toString()); |
98 |
} |
99 |
|
100 |
private void showValidationError() { |
101 |
Toast.makeText(LockScreenActivity.this, mPasswordError, Toast.LENGTH_LONG).show(); |
102 |
mNewPassphrase.requestFocus(); |
103 |
} |
104 |
|
105 |
private void showInequalityError() { |
106 |
Toast.makeText(LockScreenActivity.this, |
107 |
R.string.lock_screen_passphrases_not_matching, |
108 |
Toast.LENGTH_SHORT).show(); |
109 |
clearNewFields(); |
110 |
} |
111 |
|
112 |
private void clearNewFields() { |
113 |
mNewPassphrase.getEditableText().clear(); |
114 |
mConfirmNewPassphrase.getEditableText().clear(); |
115 |
} |
116 |
|
117 |
private boolean isPasswordValid() { |
118 |
boolean valid = NConstants.validatePassword(mNewPassphrase.getText().toString().toCharArray());
|
119 |
if(!valid)
|
120 |
mPasswordError = getString(R.string.pass_err_length); |
121 |
return valid;
|
122 |
} |
123 |
|
124 |
private boolean isConfirmationFieldEmpty() { |
125 |
return mConfirmNewPassphrase.getText().toString().length() == 0; |
126 |
} |
127 |
|
128 |
private void initializePassphrase() { |
129 |
// Passphrase is not set, so allow the user to create one
|
130 |
|
131 |
mViewCreatePassphrase.setVisibility(View.VISIBLE);
|
132 |
mViewEnterPassphrase.setVisibility(View.GONE);
|
133 |
|
134 |
mNewPassphrase.setOnEditorActionListener(new OnEditorActionListener() {
|
135 |
|
136 |
@Override
|
137 |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
138 |
if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
|
139 |
if (!isPasswordValid())
|
140 |
showValidationError(); |
141 |
else
|
142 |
mSlider.showConfirmationField(); |
143 |
} |
144 |
return false; |
145 |
} |
146 |
}); |
147 |
|
148 |
mConfirmNewPassphrase.setOnEditorActionListener(new OnEditorActionListener() {
|
149 |
|
150 |
@Override
|
151 |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
152 |
if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_DONE) {
|
153 |
if (!newEqualsConfirmation()) {
|
154 |
showInequalityError(); |
155 |
mSlider.showNewPasswordField(); |
156 |
} |
157 |
} |
158 |
return false; |
159 |
} |
160 |
}); |
161 |
|
162 |
Button btnCreate = (Button) findViewById(R.id.btnCreate); |
163 |
btnCreate.setOnClickListener(new OnClickListener()
|
164 |
{ |
165 |
@Override
|
166 |
public void onClick(View v) { |
167 |
// validate
|
168 |
if (!isPasswordValid()) {
|
169 |
showValidationError(); |
170 |
mSlider.showNewPasswordField(); |
171 |
} else if (isConfirmationFieldEmpty()) { |
172 |
mSlider.showConfirmationField(); |
173 |
} else if (!newEqualsConfirmation()) { |
174 |
showInequalityError(); |
175 |
mSlider.showNewPasswordField(); |
176 |
} else {
|
177 |
try {
|
178 |
mCacheWord.setPassphrase(mNewPassphrase.getText().toString().toCharArray()); |
179 |
} catch (GeneralSecurityException e) { |
180 |
// TODO initialization failed
|
181 |
Log.e(TAG, "Cacheword pass initialization failed: " + e.getMessage());
|
182 |
} |
183 |
} |
184 |
} |
185 |
}); |
186 |
} |
187 |
|
188 |
private void promptPassphrase() { |
189 |
mViewCreatePassphrase.setVisibility(View.GONE);
|
190 |
mViewEnterPassphrase.setVisibility(View.VISIBLE);
|
191 |
|
192 |
mBtnOpen = (Button) findViewById(R.id.btnOpen);
|
193 |
mBtnOpen.setOnClickListener(new OnClickListener() {
|
194 |
@Override
|
195 |
public void onClick(View v) { |
196 |
if (mEnterPassphrase.getText().toString().length() == 0) |
197 |
return;
|
198 |
// Check passphrase
|
199 |
try {
|
200 |
mCacheWord.setPassphrase(mEnterPassphrase.getText().toString().toCharArray()); |
201 |
} catch (GeneralSecurityException e) { |
202 |
mEnterPassphrase.setText("");
|
203 |
// TODO implement try again and wipe if fail
|
204 |
Log.e(TAG, "Cacheword pass verification failed: " + e.getMessage());
|
205 |
return;
|
206 |
} |
207 |
} |
208 |
}); |
209 |
|
210 |
mEnterPassphrase.setOnEditorActionListener(new OnEditorActionListener()
|
211 |
{ |
212 |
@Override
|
213 |
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) |
214 |
{ |
215 |
if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_GO)
|
216 |
{ |
217 |
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); |
218 |
Handler threadHandler = new Handler(); |
219 |
imm.hideSoftInputFromWindow(v.getWindowToken(), 0, new ResultReceiver( |
220 |
threadHandler) |
221 |
{ |
222 |
@Override
|
223 |
protected void onReceiveResult(int resultCode, Bundle resultData) { |
224 |
super.onReceiveResult(resultCode, resultData);
|
225 |
mBtnOpen.performClick(); |
226 |
} |
227 |
}); |
228 |
return true; |
229 |
} |
230 |
return false; |
231 |
} |
232 |
}); |
233 |
} |
234 |
|
235 |
public class TwoViewSlider { |
236 |
|
237 |
private boolean firstIsShown = true; |
238 |
private ViewFlipper flipper;
|
239 |
private LinearLayout container1;
|
240 |
private LinearLayout container2;
|
241 |
private View firstView; |
242 |
private View secondView; |
243 |
private Animation pushRightIn;
|
244 |
private Animation pushRightOut;
|
245 |
private Animation pushLeftIn;
|
246 |
private Animation pushLeftOut;
|
247 |
|
248 |
public TwoViewSlider(ViewFlipper flipper, LinearLayout container1, LinearLayout container2,
|
249 |
View view1, View view2) { |
250 |
this.flipper = flipper;
|
251 |
this.container1 = container1;
|
252 |
this.container2 = container2;
|
253 |
this.firstView = view1;
|
254 |
this.secondView = view2;
|
255 |
|
256 |
pushRightIn = AnimationUtils.loadAnimation(LockScreenActivity.this, R.anim.push_right_in); |
257 |
pushRightOut = AnimationUtils.loadAnimation(LockScreenActivity.this, R.anim.push_right_out); |
258 |
pushLeftIn = AnimationUtils.loadAnimation(LockScreenActivity.this, R.anim.push_left_in); |
259 |
pushLeftOut = AnimationUtils.loadAnimation(LockScreenActivity.this, R.anim.push_left_out); |
260 |
|
261 |
} |
262 |
|
263 |
public void showNewPasswordField() { |
264 |
if (firstIsShown)
|
265 |
return;
|
266 |
|
267 |
flipper.setInAnimation(pushRightIn); |
268 |
flipper.setOutAnimation(pushRightOut); |
269 |
flip(); |
270 |
} |
271 |
|
272 |
public void showConfirmationField() { |
273 |
if (!firstIsShown)
|
274 |
return;
|
275 |
|
276 |
flipper.setInAnimation(pushLeftIn); |
277 |
flipper.setOutAnimation(pushLeftOut); |
278 |
flip(); |
279 |
} |
280 |
|
281 |
private void flip() { |
282 |
if (firstIsShown) {
|
283 |
firstIsShown = false;
|
284 |
container2.removeAllViews(); |
285 |
container2.addView(secondView); |
286 |
} else {
|
287 |
firstIsShown = true;
|
288 |
container1.removeAllViews(); |
289 |
container1.addView(firstView); |
290 |
} |
291 |
flipper.showNext(); |
292 |
} |
293 |
} |
294 |
} |