-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPaperKeyProveActivity.java
More file actions
291 lines (237 loc) · 11.2 KB
/
PaperKeyProveActivity.java
File metadata and controls
291 lines (237 loc) · 11.2 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.ravenwallet.presenter.activities;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.transition.TransitionManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.ravenwallet.R;
import com.ravenwallet.presenter.activities.util.BRActivity;
import com.ravenwallet.presenter.customviews.BRDialogView;
import com.ravenwallet.presenter.interfaces.BROnSignalCompletion;
import com.ravenwallet.tools.animation.BRAnimator;
import com.ravenwallet.tools.animation.BRDialog;
import com.ravenwallet.tools.animation.SpringAnimator;
import com.ravenwallet.tools.manager.BRReportsManager;
import com.ravenwallet.tools.manager.BRSharedPrefs;
import com.ravenwallet.tools.security.SmartValidator;
import com.ravenwallet.tools.util.Bip39Wordlist;
import com.ravenwallet.tools.util.Utils;
import java.util.Locale;
import java.util.Random;
public class PaperKeyProveActivity extends BRActivity {
private static final String TAG = PaperKeyProveActivity.class.getName();
private Button submit;
private EditText wordEditFirst;
private EditText wordEditSecond;
private TextView wordTextFirst;
private TextView wordTextSecond;
private ImageView checkMark1;
private ImageView checkMark2;
private SparseArray<String> sparseArrayWords = new SparseArray<>();
public static boolean appVisible = false;
private static PaperKeyProveActivity app;
private ConstraintLayout constraintLayout;
private ConstraintSet applyConstraintSet = new ConstraintSet();
private ConstraintSet resetConstraintSet = new ConstraintSet();
public static PaperKeyProveActivity getApp() {
return app;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paper_key_prove);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
submit = findViewById(R.id.button_submit);
wordEditFirst = findViewById(R.id.word_edittext_first);
wordEditSecond = findViewById(R.id.word_edittext_second);
wordTextFirst = findViewById(R.id.word_number_first);
wordTextSecond = findViewById(R.id.word_number_second);
checkMark1 = findViewById(R.id.check_mark_1);
checkMark2 = findViewById(R.id.check_mark_2);
// wordEditFirst.setOnFocusChangeListener(new FocusListener());
// wordEditSecond.setOnFocusChangeListener(new FocusListener());
wordEditFirst.addTextChangedListener(new BRTextWatcher());
wordEditSecond.addTextChangedListener(new BRTextWatcher());
constraintLayout = findViewById(R.id.constraintLayout);
resetConstraintSet.clone(constraintLayout);
applyConstraintSet.clone(constraintLayout);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
TransitionManager.beginDelayedTransition(constraintLayout);
applyConstraintSet.setMargin(R.id.word_number_first, ConstraintSet.TOP, 8);
applyConstraintSet.setMargin(R.id.line1, ConstraintSet.TOP, 16);
applyConstraintSet.setMargin(R.id.line2, ConstraintSet.TOP, 16);
applyConstraintSet.setMargin(R.id.word_number_second, ConstraintSet.TOP, 8);
applyConstraintSet.applyTo(constraintLayout);
}
}, 500);
wordEditSecond.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
if (id == R.id.submit || id == EditorInfo.IME_NULL) {
submit.performClick();
return true;
}
return false;
}
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!BRAnimator.isClickAllowed()) return;
if (isWordCorrect(true) && isWordCorrect(false)) {
Utils.hideKeyboard(PaperKeyProveActivity.this);
BRSharedPrefs.putPhraseWroteDown(PaperKeyProveActivity.this, true);
BRAnimator.showRvnSignal(PaperKeyProveActivity.this, getString(R.string.Alerts_paperKeySet), getString(R.string.Alerts_paperKeySetSubheader), R.drawable.ic_check_mark_white, new BROnSignalCompletion() {
@Override
public void onComplete() {
Intent intent = new Intent(PaperKeyProveActivity.this, HomeActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.enter_from_bottom, R.anim.empty_300);
finishAffinity();
}
});
} else {
if (!isWordCorrect(true)) {
wordEditFirst.setTextColor(getColor(R.color.red_text));
SpringAnimator.failShakeAnimation(PaperKeyProveActivity.this, wordEditFirst);
}
if (!isWordCorrect(false)) {
wordEditSecond.setTextColor(getColor(R.color.red_text));
SpringAnimator.failShakeAnimation(PaperKeyProveActivity.this, wordEditSecond);
}
}
}
});
String cleanPhrase = null;
cleanPhrase = getIntent().getExtras() == null ? null : getIntent().getStringExtra("phrase");
if (Utils.isNullOrEmpty(cleanPhrase)) {
throw new RuntimeException(TAG + ": cleanPhrase is null");
}
String wordArray[] = cleanPhrase.split(" ");
if (wordArray.length == 12 && cleanPhrase.charAt(cleanPhrase.length() - 1) == '\0') {
BRDialog.showCustomDialog(this, getString(R.string.JailbreakWarnings_title),
getString(R.string.Alert_keystore_generic_android), getString(R.string.Button_ok), null, new BRDialogView.BROnClickListener() {
@Override
public void onClick(BRDialogView brDialogView) {
brDialogView.dismissWithAnimation();
}
}, null, null, 0);
BRReportsManager.reportBug(new IllegalArgumentException("Paper Key error, please contact support at breadwallet.com"), false);
} else {
randomWordsSetUp(wordArray);
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setStatusBarGradiant(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
Drawable background = activity.getResources().getDrawable(R.drawable.regular_blue);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
window.setNavigationBarColor(activity.getResources().getColor(android.R.color.transparent));
final int lFlags = window.getDecorView().getSystemUiVisibility();
// update the SystemUiVisibility depending on whether we want a Light or Dark theme.
window.getDecorView().setSystemUiVisibility((lFlags & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR));
window.setBackgroundDrawable(background);
}
}
@Override
protected void onResume() {
super.onResume();
appVisible = true;
app = this;
setStatusBarGradiant(app);
}
@Override
protected void onPause() {
super.onPause();
appVisible = false;
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.enter_from_left, R.anim.exit_to_right);
}
private void randomWordsSetUp(String[] words) {
final Random random = new Random();
int n = random.nextInt(10) + 1;
sparseArrayWords.append(n, words[n]);
while (sparseArrayWords.get(n) != null) {
n = random.nextInt(10) + 1;
}
sparseArrayWords.append(n, words[n]);
wordTextFirst.setText(String.format(Locale.getDefault(), getString(R.string.ConfirmPaperPhrase_word), (sparseArrayWords.keyAt(0) + 1)));
wordTextSecond.setText(String.format(Locale.getDefault(), getString(R.string.ConfirmPaperPhrase_word), (sparseArrayWords.keyAt(1) + 1)));
}
private boolean isWordCorrect(boolean first) {
if (first) {
String firstWordClean = Bip39Wordlist.cleanWord(wordEditFirst.getText().toString());
return SmartValidator.isPhraseWordValid(PaperKeyProveActivity.this, firstWordClean) &&
firstWordClean.equalsIgnoreCase(sparseArrayWords.get(sparseArrayWords.keyAt(0)));
} else {
String secondWordClean = Bip39Wordlist.cleanWord(wordEditSecond.getText().toString());
return SmartValidator.isPhraseWordValid(PaperKeyProveActivity.this, secondWordClean) &&
secondWordClean.equalsIgnoreCase(sparseArrayWords.get(sparseArrayWords.keyAt(1)));
}
}
// private class FocusListener implements View.OnFocusChangeListener {
//
// @Override
// public void onFocusChange(View v, boolean hasFocus) {
// if (!hasFocus) {
// validateWord((EditText) v);
// } else {
// ((EditText) v).setTextColor(getColor(R.color.light_gray));
// }
// }
// }
private void validateWord(EditText view) {
String word = view.getText().toString();
boolean valid = SmartValidator.isPhraseWordValid(this, word);
view.setTextColor(getColor(valid ? R.color.light_gray : R.color.red_text));
// if (!valid)
// SpringAnimator.failShakeAnimation(this, view);
if (isWordCorrect(true)) {
checkMark1.setVisibility(View.VISIBLE);
} else {
checkMark1.setVisibility(View.INVISIBLE);
}
if (isWordCorrect(false)) {
checkMark2.setVisibility(View.VISIBLE);
} else {
checkMark2.setVisibility(View.INVISIBLE);
}
}
private class BRTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
validateWord(wordEditFirst);
validateWord(wordEditSecond);
}
@Override
public void afterTextChanged(Editable s) {
}
}
}