1package eu.siacs.conversations.ui;
2
3import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;
4
5import android.app.AlertDialog;
6import android.content.ClipData;
7import android.content.ClipDescription;
8import android.content.ClipboardManager;
9import android.content.Context;
10import android.content.Intent;
11import android.os.Bundle;
12import android.os.Handler;
13import android.os.SystemClock;
14import android.text.Html;
15import android.view.View;
16
17import androidx.databinding.DataBindingUtil;
18
19import com.google.android.material.dialog.MaterialAlertDialogBuilder;
20import com.google.android.material.snackbar.Snackbar;
21
22import eu.siacs.conversations.R;
23import eu.siacs.conversations.databinding.ActivityVerifyBinding;
24import eu.siacs.conversations.entities.Account;
25import eu.siacs.conversations.services.QuickConversationsService;
26import eu.siacs.conversations.ui.util.ApiDialogHelper;
27import eu.siacs.conversations.ui.util.PinEntryWrapper;
28import eu.siacs.conversations.utils.AccountUtils;
29import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
30import eu.siacs.conversations.utils.TimeFrameUtils;
31
32import io.michaelrocks.libphonenumber.android.NumberParseException;
33
34import java.util.concurrent.atomic.AtomicBoolean;
35
36public class VerifyActivity extends XmppActivity implements ClipboardManager.OnPrimaryClipChangedListener, QuickConversationsService.OnVerification, QuickConversationsService.OnVerificationRequested {
37
38 public static final String EXTRA_RETRY_SMS_AFTER = "retry_sms_after";
39 private static final String EXTRA_RETRY_VERIFICATION_AFTER = "retry_verification_after";
40 private final Handler mHandler = new Handler();
41 private ActivityVerifyBinding binding;
42 private Account account;
43 private PinEntryWrapper pinEntryWrapper;
44 private ClipboardManager clipboardManager;
45 private String pasted = null;
46 private boolean verifying = false;
47 private boolean requestingVerification = false;
48 private long retrySmsAfter = 0;
49 private final Runnable SMS_TIMEOUT_UPDATER = new Runnable() {
50 @Override
51 public void run() {
52 if (setTimeoutLabelInResendButton()) {
53 mHandler.postDelayed(this, 300);
54 }
55 }
56 };
57 private long retryVerificationAfter = 0;
58 private final Runnable VERIFICATION_TIMEOUT_UPDATER = new Runnable() {
59 @Override
60 public void run() {
61 if (setTimeoutLabelInNextButton()) {
62 mHandler.postDelayed(this, 300);
63 }
64 }
65 };
66 private final AtomicBoolean redirectInProgress = new AtomicBoolean(false);
67
68 private boolean setTimeoutLabelInResendButton() {
69 if (retrySmsAfter != 0) {
70 long remaining = retrySmsAfter - SystemClock.elapsedRealtime();
71 if (remaining >= 0) {
72 binding.resendSms.setEnabled(false);
73 binding.resendSms.setText(getString(R.string.resend_sms_in, TimeFrameUtils.resolve(VerifyActivity.this, remaining)));
74 return true;
75 }
76 }
77 binding.resendSms.setEnabled(true);
78 binding.resendSms.setText(R.string.resend_sms);
79 return false;
80 }
81
82 private boolean setTimeoutLabelInNextButton() {
83 if (retryVerificationAfter != 0) {
84 long remaining = retryVerificationAfter - SystemClock.elapsedRealtime();
85 if (remaining >= 0) {
86 binding.next.setEnabled(false);
87 binding.next.setText(getString(R.string.wait_x, TimeFrameUtils.resolve(VerifyActivity.this, remaining)));
88 return true;
89 }
90 }
91 this.binding.next.setEnabled(!verifying);
92 this.binding.next.setText(verifying ? R.string.verifying : R.string.next);
93 return false;
94 }
95
96 @Override
97 protected void onCreate(final Bundle savedInstanceState) {
98 super.onCreate(savedInstanceState);
99 String pin = savedInstanceState != null ? savedInstanceState.getString("pin") : null;
100 boolean verifying = savedInstanceState != null && savedInstanceState.getBoolean("verifying");
101 boolean requestingVerification = savedInstanceState != null && savedInstanceState.getBoolean("requesting_verification", false);
102 this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null;
103 this.retrySmsAfter = savedInstanceState != null ? savedInstanceState.getLong(EXTRA_RETRY_SMS_AFTER, 0L) : 0L;
104 this.retryVerificationAfter = savedInstanceState != null ? savedInstanceState.getLong(EXTRA_RETRY_VERIFICATION_AFTER, 0L) : 0L;
105 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_verify);
106 Activities.setStatusAndNavigationBarColors(this, binding.getRoot());
107 setSupportActionBar(this.binding.toolbar);
108 this.pinEntryWrapper = new PinEntryWrapper(binding.pinBox);
109 if (pin != null) {
110 this.pinEntryWrapper.setPin(pin);
111 }
112 binding.back.setOnClickListener(this::onBackButton);
113 binding.next.setOnClickListener(this::onNextButton);
114 binding.resendSms.setOnClickListener(this::onResendSmsButton);
115 clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
116 setVerifyingState(verifying);
117 setRequestingVerificationState(requestingVerification);
118 }
119
120 private void onBackButton(View view) {
121 if (this.verifying) {
122 setVerifyingState(false);
123 return;
124 }
125 final Intent intent = new Intent(this, EnterPhoneNumberActivity.class);
126 if (this.account != null) {
127 final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
128 builder.setMessage(R.string.abort_registration_procedure);
129 builder.setPositiveButton(R.string.yes, (dialog, which) -> {
130 xmppConnectionService.deleteAccount(account);
131 startActivity(intent);
132 finish();
133 });
134 builder.setNegativeButton(R.string.no, null);
135 builder.create().show();
136 } else {
137 startActivity(intent);
138 finish();
139 }
140 }
141
142 private void onNextButton(View view) {
143 final String pin = pinEntryWrapper.getPin();
144 if (PinEntryWrapper.isValidPin(pin)) {
145 if (account != null && xmppConnectionService != null) {
146 setVerifyingState(true);
147 xmppConnectionService.getQuickConversationsService().verify(account, pin);
148 }
149 } else {
150 AlertDialog.Builder builder = new AlertDialog.Builder(this);
151 builder.setMessage(R.string.please_enter_pin);
152 builder.setPositiveButton(R.string.ok, null);
153 builder.create().show();
154 }
155 }
156
157 private void onResendSmsButton(View view) {
158 try {
159 xmppConnectionService.getQuickConversationsService().requestVerification(PhoneNumberUtilWrapper.toPhoneNumber(this, account.getJid()));
160 setRequestingVerificationState(true);
161 } catch (NumberParseException e) {
162
163 }
164 }
165
166 private void setVerifyingState(boolean verifying) {
167 this.verifying = verifying;
168 this.binding.back.setText(verifying ? R.string.cancel : R.string.back);
169 this.binding.next.setEnabled(!verifying);
170 this.binding.next.setText(verifying ? R.string.verifying : R.string.next);
171 this.binding.resendSms.setVisibility(verifying ? View.GONE : View.VISIBLE);
172 pinEntryWrapper.setEnabled(!verifying);
173 this.binding.progressBar.setVisibility(verifying ? View.VISIBLE : View.GONE);
174 this.binding.progressBar.setIndeterminate(verifying);
175 }
176
177 private void setRequestingVerificationState(boolean requesting) {
178 this.requestingVerification = requesting;
179 if (requesting) {
180 this.binding.resendSms.setEnabled(false);
181 this.binding.resendSms.setText(R.string.requesting_sms);
182 } else {
183 setTimeoutLabelInResendButton();
184 }
185
186 }
187
188 @Override
189 protected void refreshUiReal() {
190
191 }
192
193 @Override
194 public void onBackendConnected() {
195 xmppConnectionService.getQuickConversationsService().addOnVerificationListener(this);
196 xmppConnectionService.getQuickConversationsService().addOnVerificationRequestedListener(this);
197 this.account = AccountUtils.getFirst(xmppConnectionService);
198 if (this.account == null) {
199 return;
200 }
201 if (!account.isOptionSet(Account.OPTION_UNVERIFIED) && !account.isOptionSet(Account.OPTION_DISABLED)) {
202 runOnUiThread(this::performPostVerificationRedirect);
203 return;
204 }
205 this.binding.weHaveSent.setText(Html.fromHtml(getString(R.string.we_have_sent_you_an_sms_to_x, PhoneNumberUtilWrapper.toFormattedPhoneNumber(this, this.account.getJid()))));
206 setVerifyingState(xmppConnectionService.getQuickConversationsService().isVerifying());
207 setRequestingVerificationState(xmppConnectionService.getQuickConversationsService().isRequestingVerification());
208 }
209
210 @Override
211 public void onSaveInstanceState(Bundle savedInstanceState) {
212 savedInstanceState.putString("pin", this.pinEntryWrapper.getPin());
213 savedInstanceState.putBoolean("verifying", this.verifying);
214 savedInstanceState.putBoolean("requesting_verification", this.requestingVerification);
215 savedInstanceState.putLong(EXTRA_RETRY_SMS_AFTER, this.retrySmsAfter);
216 savedInstanceState.putLong(EXTRA_RETRY_VERIFICATION_AFTER, this.retryVerificationAfter);
217 if (this.pasted != null) {
218 savedInstanceState.putString("pasted", this.pasted);
219 }
220 super.onSaveInstanceState(savedInstanceState);
221 }
222
223 @Override
224 public void onStart() {
225 super.onStart();
226 clipboardManager.addPrimaryClipChangedListener(this);
227 final Intent intent = getIntent();
228 this.retrySmsAfter = intent != null ? intent.getLongExtra(EXTRA_RETRY_SMS_AFTER, this.retrySmsAfter) : this.retrySmsAfter;
229 if (this.retrySmsAfter > 0) {
230 mHandler.post(SMS_TIMEOUT_UPDATER);
231 }
232 if (this.retryVerificationAfter > 0) {
233 mHandler.post(VERIFICATION_TIMEOUT_UPDATER);
234 }
235 }
236
237 @Override
238 public void onStop() {
239 super.onStop();
240 mHandler.removeCallbacks(SMS_TIMEOUT_UPDATER);
241 mHandler.removeCallbacks(VERIFICATION_TIMEOUT_UPDATER);
242 clipboardManager.removePrimaryClipChangedListener(this);
243 if (xmppConnectionService != null) {
244 xmppConnectionService.getQuickConversationsService().removeOnVerificationListener(this);
245 xmppConnectionService.getQuickConversationsService().removeOnVerificationRequestedListener(this);
246 }
247 }
248
249 @Override
250 public void onResume() {
251 super.onResume();
252 if (pinEntryWrapper.isEmpty()) {
253 //starting with Android P we need input focus
254 pinEntryWrapper.requestFocus();
255 pastePinFromClipboard();
256 }
257 }
258
259 private void pastePinFromClipboard() {
260 final ClipDescription description = clipboardManager != null ? clipboardManager.getPrimaryClipDescription() : null;
261 if (description != null && description.hasMimeType(MIMETYPE_TEXT_PLAIN)) {
262 final ClipData primaryClip = clipboardManager.getPrimaryClip();
263 if (primaryClip != null && primaryClip.getItemCount() > 0) {
264 final CharSequence clip = primaryClip.getItemAt(0).getText();
265 if (PinEntryWrapper.isValidPin(clip) && !clip.toString().equals(this.pasted)) {
266 this.pasted = clip.toString();
267 pinEntryWrapper.setPin(clip.toString());
268 final Snackbar snackbar = Snackbar.make(binding.coordinator, R.string.possible_pin, Snackbar.LENGTH_LONG);
269 snackbar.setAction(R.string.undo, v -> pinEntryWrapper.clear());
270 snackbar.show();
271 }
272 }
273 }
274 }
275
276 private void performPostVerificationRedirect() {
277 if (redirectInProgress.compareAndSet(false, true)) {
278 Intent intent = new Intent(this, EnterNameActivity.class);
279 startActivity(intent);
280 finish();
281 }
282 }
283
284 @Override
285 public void onPrimaryClipChanged() {
286 this.pasted = null;
287 if (pinEntryWrapper.isEmpty()) {
288 pastePinFromClipboard();
289 }
290 }
291
292 @Override
293 public void onVerificationFailed(final int code) {
294 runOnUiThread(() -> {
295 setVerifyingState(false);
296 if (code == 401 || code == 404) {
297 AlertDialog.Builder builder = new AlertDialog.Builder(this);
298 builder.setMessage(code == 404 ? R.string.pin_expired : R.string.incorrect_pin);
299 builder.setPositiveButton(R.string.ok, null);
300 builder.create().show();
301 } else {
302 ApiDialogHelper.createError(this, code).show();
303 }
304 });
305 }
306
307 @Override
308 public void onVerificationSucceeded() {
309 runOnUiThread(this::performPostVerificationRedirect);
310 }
311
312 @Override
313 public void onVerificationRetryAt(long timestamp) {
314 this.retryVerificationAfter = timestamp;
315 runOnUiThread(() -> {
316 ApiDialogHelper.createTooManyAttempts(this).show();
317 setVerifyingState(false);
318 });
319 mHandler.removeCallbacks(VERIFICATION_TIMEOUT_UPDATER);
320 runOnUiThread(VERIFICATION_TIMEOUT_UPDATER);
321 }
322
323 @Override
324 public void startBackgroundVerification(String pin) {
325 pinEntryWrapper.setPin(pin);
326 setVerifyingState(true);
327 }
328
329 //send sms again button callback
330 @Override
331 public void onVerificationRequestFailed(int code) {
332 runOnUiThread(() -> {
333 setRequestingVerificationState(false);
334 ApiDialogHelper.createError(this, code).show();
335 });
336 }
337
338 //send sms again button callback
339 @Override
340 public void onVerificationRequested() {
341 runOnUiThread(() -> {
342 pinEntryWrapper.clear();
343 setRequestingVerificationState(false);
344 AlertDialog.Builder builder = new AlertDialog.Builder(this);
345 builder.setMessage(R.string.we_have_sent_you_another_sms);
346 builder.setPositiveButton(R.string.ok, null);
347 builder.create().show();
348 });
349 }
350
351 @Override
352 public void onVerificationRequestedRetryAt(long timestamp) {
353 this.retrySmsAfter = timestamp;
354 runOnUiThread(() -> {
355 ApiDialogHelper.createRateLimited(this, timestamp).show();
356 setRequestingVerificationState(false);
357 });
358 mHandler.removeCallbacks(SMS_TIMEOUT_UPDATER);
359 runOnUiThread(SMS_TIMEOUT_UPDATER);
360 }
361}