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