VerifyActivity.java

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