-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPaperKeyActivity.java
More file actions
243 lines (208 loc) · 9.08 KB
/
PaperKeyActivity.java
File metadata and controls
243 lines (208 loc) · 9.08 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
package com.ravenwallet.presenter.activities;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.legacy.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.UnderlineSpan;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import com.google.android.gms.common.util.AndroidUtilsLight;
import com.ravenwallet.R;
import com.ravenwallet.presenter.activities.util.BRActivity;
import com.ravenwallet.presenter.customviews.BRDialogView;
import com.ravenwallet.presenter.fragments.FragmentPhraseWord;
import com.ravenwallet.tools.animation.BRAnimator;
import com.ravenwallet.tools.animation.BRDialog;
import com.ravenwallet.tools.manager.BRReportsManager;
import com.ravenwallet.tools.security.PostAuth;
import com.ravenwallet.tools.util.Utils;
import java.util.Locale;
public class PaperKeyActivity extends BRActivity {
private static final String TAG = PaperKeyActivity.class.getName();
private ViewPager wordViewPager;
private Button nextButton;
private Button previousButton;
private TextView itemIndexText;
private SparseArray<String> wordMap;
public static boolean appVisible = false;
private static PaperKeyActivity app;
private ImageButton close;
public static PaperKeyActivity getApp() {
return app;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paper_key);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
wordViewPager = (ViewPager) findViewById(R.id.phrase_words_pager);
wordViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {
}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
public void onPageSelected(int position) {
if (position == 0)
setButtonEnabled(false);
else
setButtonEnabled(true);
updateItemIndexText();
}
});
nextButton = findViewById(R.id.send_button);
previousButton = findViewById(R.id.button_previous);
close = findViewById(R.id.close_button);
itemIndexText = findViewById(R.id.item_index_text);
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateWordView(true);
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!BRAnimator.isClickAllowed()) return;
// Intent intent = new Intent(PaperKeyActivity.this, TermsAndConditionsActivity.class);
// startActivity(intent);
// overridePendingTransition(R.anim.enter_from_bottom, R.anim.empty_300);
if (!isDestroyed()) finish();
}
});
previousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateWordView(false);
}
});
itemIndexText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!BRAnimator.isClickAllowed()) return;
//Only allow clicking on the final page which has the fast-restore key
if(wordViewPager.getCurrentItem() != 12) return;
BRDialog.showCustomDialog(app, app.getString(R.string.RecoverWallet_fastrestore_title), app.getString(R.string.SecurityCenter_fastrestoreExplanation),
app.getString(R.string.AccessibilityLabels_close), null, new BRDialogView.BROnClickListener() {
@Override
public void onClick(BRDialogView brDialogView) {
brDialogView.dismissWithAnimation();
}
}, null, null, 0);
}
});
String cleanPhrase = getIntent().getExtras() == null ? null : getIntent().getStringExtra("phrase");
wordMap = new SparseArray<>();
if (Utils.isNullOrEmpty(cleanPhrase)) {
throw new RuntimeException(TAG + ": cleanPhrase is null");
}
String wordArray[] = cleanPhrase.split(" ");
if (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 ravenwallet.com: " + wordArray.length), true);
} else {
if (wordArray.length != 12) {
BRReportsManager.reportBug(new IllegalArgumentException("Wrong number of paper keys: " + wordArray.length + ", lang: " + Locale.getDefault().getLanguage()), true);
}
WordPagerAdapter adapter = new WordPagerAdapter(getFragmentManager());
adapter.setWords(wordArray);
adapter.setFastRestoreKey(Utils.getCurrentFastRestoreKey());
wordViewPager.setAdapter(adapter);
for (int i = 0; i < wordArray.length; i++) {
wordMap.append(i, wordArray[i]);
}
updateItemIndexText();
}
}
private void updateWordView(boolean isNext) {
int currentIndex = wordViewPager.getCurrentItem();
if (isNext) {
setButtonEnabled(true);
if (currentIndex >= wordMap.size()) {
PostAuth.getInstance().onPhraseProveAuth(this, false);
} else {
wordViewPager.setCurrentItem(currentIndex + 1);
}
} else {
if (currentIndex <= 1) {
wordViewPager.setCurrentItem(currentIndex - 1);
setButtonEnabled(false);
} else {
wordViewPager.setCurrentItem(currentIndex - 1);
}
}
}
private void setButtonEnabled(boolean b) {
// previousButton.setTextColor(getColor(b ? R.color.white : R.color.extra_light_gray));
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, b ? 8 : 0, r.getDisplayMetrics());
previousButton.setElevation(px);
previousButton.setEnabled(b);
}
@Override
protected void onResume() {
super.onResume();
appVisible = true;
app = this;
}
@Override
protected void onPause() {
super.onPause();
appVisible = false;
}
private void updateItemIndexText() {
SpannableString text;
if(wordViewPager.getCurrentItem() == wordMap.size()){
//Last item: AKA the fast restore key, Underline it to show it is clickable
text = new SpannableString(String.format(Locale.getDefault(), getString(R.string.SecurityCenter_fastrestoreDetails)));
text.setSpan(new UnderlineSpan(), 0, text.length(), 0);
} else {
text = new SpannableString(String.format(Locale.getDefault(), getString(R.string.WritePaperPhrase_step), wordViewPager.getCurrentItem() + 1, wordMap.size()));
}
itemIndexText.setText(text);
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.enter_from_right, R.anim.exit_to_left);
}
private class WordPagerAdapter extends FragmentPagerAdapter {
private String[] words;
private int fastRestoreKey = -1;
public WordPagerAdapter(FragmentManager fm) {
super(fm);
}
public void setWords(String[] words) {
this.words = words;
}
public void setFastRestoreKey(int fastRestoreKey) { this.fastRestoreKey = fastRestoreKey; }
@Override
public Fragment getItem(int pos) {
if(pos < words.length)
return FragmentPhraseWord.newInstance(words[pos]);
else
return FragmentPhraseWord.newInstance(Integer.toString(fastRestoreKey));
}
@Override
public int getCount() {
if(words == null)
return 0;
else
return words.length + (fastRestoreKey == -1 ? 0 : 1);
}
}
}