2018-10-20 16:37:59 +02:00
|
|
|
package eu.siacs.conversations.ui;
|
|
|
|
|
2018-10-21 16:27:56 +02:00
|
|
|
import android.app.AlertDialog;
|
2018-10-20 23:38:03 +02:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipDescription;
|
|
|
|
import android.content.ClipboardManager;
|
|
|
|
import android.content.Context;
|
2018-10-20 16:37:59 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.databinding.DataBindingUtil;
|
|
|
|
import android.os.Bundle;
|
2018-10-22 20:08:05 +02:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.SystemClock;
|
2018-10-20 23:38:03 +02:00
|
|
|
import android.support.design.widget.Snackbar;
|
2018-10-20 16:37:59 +02:00
|
|
|
import android.support.v7.widget.Toolbar;
|
|
|
|
import android.text.Html;
|
|
|
|
import android.view.View;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.databinding.ActivityVerifyBinding;
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
2018-10-21 16:27:56 +02:00
|
|
|
import eu.siacs.conversations.services.QuickConversationsService;
|
2018-10-22 22:54:05 +02:00
|
|
|
import eu.siacs.conversations.ui.util.ApiErrorDialogHelper;
|
2018-10-20 16:37:59 +02:00
|
|
|
import eu.siacs.conversations.ui.util.PinEntryWrapper;
|
|
|
|
import eu.siacs.conversations.utils.AccountUtils;
|
|
|
|
import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
|
2018-10-22 20:08:05 +02:00
|
|
|
import eu.siacs.conversations.utils.TimeframeUtils;
|
|
|
|
import io.michaelrocks.libphonenumber.android.NumberParseException;
|
2018-10-20 16:37:59 +02:00
|
|
|
|
2018-10-20 23:38:03 +02:00
|
|
|
import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;
|
|
|
|
|
2018-10-22 20:08:05 +02:00
|
|
|
public class VerifyActivity extends XmppActivity implements ClipboardManager.OnPrimaryClipChangedListener, QuickConversationsService.OnVerification, QuickConversationsService.OnVerificationRequested {
|
|
|
|
|
|
|
|
public static final String EXTRA_RETRY_SMS_AFTER = "retry_sms_after";
|
2018-10-22 22:54:05 +02:00
|
|
|
private static final String EXTRA_RETRY_VERIFICATION_AFTER = "retry_verification_after";
|
|
|
|
private final Handler mHandler = new Handler();
|
2018-10-20 16:37:59 +02:00
|
|
|
private ActivityVerifyBinding binding;
|
|
|
|
private Account account;
|
|
|
|
private PinEntryWrapper pinEntryWrapper;
|
2018-10-20 23:38:03 +02:00
|
|
|
private ClipboardManager clipboardManager;
|
|
|
|
private String pasted = null;
|
2018-10-21 16:27:56 +02:00
|
|
|
private boolean verifying = false;
|
2018-10-22 20:08:05 +02:00
|
|
|
private boolean requestingVerification = false;
|
|
|
|
private long retrySmsAfter = 0;
|
|
|
|
private final Runnable SMS_TIMEOUT_UPDATER = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (setTimeoutLabelInResendButton()) {
|
2018-10-22 22:54:05 +02:00
|
|
|
mHandler.postDelayed(this, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private long retryVerificationAfter = 0;
|
|
|
|
private final Runnable VERIFICATION_TIMEOUT_UPDATER = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (setTimeoutLabelInNextButton()) {
|
|
|
|
mHandler.postDelayed(this, 300);
|
2018-10-22 20:08:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-10-20 23:38:03 +02:00
|
|
|
|
2018-10-22 20:08:05 +02:00
|
|
|
private boolean setTimeoutLabelInResendButton() {
|
|
|
|
if (retrySmsAfter != 0) {
|
|
|
|
long remaining = retrySmsAfter - SystemClock.elapsedRealtime();
|
|
|
|
if (remaining >= 0) {
|
|
|
|
binding.resendSms.setEnabled(false);
|
2018-10-22 22:54:05 +02:00
|
|
|
binding.resendSms.setText(getString(R.string.resend_sms_in, TimeframeUtils.resolve(VerifyActivity.this, remaining)));
|
2018-10-22 20:08:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
binding.resendSms.setEnabled(true);
|
|
|
|
binding.resendSms.setText(R.string.resend_sms);
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-20 16:37:59 +02:00
|
|
|
|
2018-10-22 22:54:05 +02:00
|
|
|
private boolean setTimeoutLabelInNextButton() {
|
|
|
|
if (retryVerificationAfter != 0) {
|
|
|
|
long remaining = retryVerificationAfter - SystemClock.elapsedRealtime();
|
|
|
|
if (remaining >= 0) {
|
|
|
|
binding.next.setEnabled(false);
|
|
|
|
binding.next.setText(getString(R.string.wait_x, TimeframeUtils.resolve(VerifyActivity.this, remaining)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.binding.next.setEnabled(!verifying);
|
|
|
|
this.binding.next.setText(verifying ? R.string.verifying : R.string.next);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-20 16:37:59 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(final Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2018-10-20 23:38:03 +02:00
|
|
|
String pin = savedInstanceState != null ? savedInstanceState.getString("pin") : null;
|
2018-10-21 16:27:56 +02:00
|
|
|
boolean verifying = savedInstanceState != null && savedInstanceState.getBoolean("verifying");
|
2018-10-22 20:08:05 +02:00
|
|
|
boolean requestingVerification = savedInstanceState != null && savedInstanceState.getBoolean("requesting_verification", false);
|
2018-10-20 23:38:03 +02:00
|
|
|
this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null;
|
2018-10-22 22:54:05 +02:00
|
|
|
this.retrySmsAfter = savedInstanceState != null ? savedInstanceState.getLong(EXTRA_RETRY_SMS_AFTER, 0L) : 0L;
|
|
|
|
this.retryVerificationAfter = savedInstanceState != null ? savedInstanceState.getLong(EXTRA_RETRY_VERIFICATION_AFTER, 0L) : 0L;
|
2018-10-20 16:37:59 +02:00
|
|
|
this.binding = DataBindingUtil.setContentView(this, R.layout.activity_verify);
|
|
|
|
setSupportActionBar((Toolbar) this.binding.toolbar);
|
|
|
|
this.pinEntryWrapper = new PinEntryWrapper(binding.pinBox);
|
2018-10-20 23:38:03 +02:00
|
|
|
if (pin != null) {
|
|
|
|
this.pinEntryWrapper.setPin(pin);
|
|
|
|
}
|
2018-10-20 16:37:59 +02:00
|
|
|
binding.back.setOnClickListener(this::onBackButton);
|
2018-10-21 16:27:56 +02:00
|
|
|
binding.next.setOnClickListener(this::onNextButton);
|
2018-10-22 20:08:05 +02:00
|
|
|
binding.resendSms.setOnClickListener(this::onResendSmsButton);
|
2018-10-20 23:38:03 +02:00
|
|
|
clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
2018-10-21 16:27:56 +02:00
|
|
|
setVerifyingState(verifying);
|
2018-10-22 20:08:05 +02:00
|
|
|
setRequestingVerificationState(requestingVerification);
|
2018-10-20 16:37:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void onBackButton(View view) {
|
2018-10-21 16:27:56 +02:00
|
|
|
if (this.verifying) {
|
|
|
|
setVerifyingState(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final Intent intent = new Intent(this, EnterPhoneNumberActivity.class);
|
2018-10-20 16:37:59 +02:00
|
|
|
if (this.account != null) {
|
2018-10-21 16:27:56 +02:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setMessage(R.string.abort_registration_procedure);
|
|
|
|
builder.setPositiveButton(R.string.yes, (dialog, which) -> {
|
|
|
|
xmppConnectionService.deleteAccount(account);
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
});
|
|
|
|
builder.setNegativeButton(R.string.no, null);
|
|
|
|
builder.create().show();
|
|
|
|
} else {
|
2018-10-20 16:37:59 +02:00
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 16:27:56 +02:00
|
|
|
private void onNextButton(View view) {
|
|
|
|
final String pin = pinEntryWrapper.getPin();
|
|
|
|
if (PinEntryWrapper.isValidPin(pin)) {
|
|
|
|
if (account != null && xmppConnectionService != null) {
|
|
|
|
setVerifyingState(true);
|
|
|
|
xmppConnectionService.getQuickConversationsService().verify(account, pin);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setMessage(R.string.please_enter_pin);
|
|
|
|
builder.setPositiveButton(R.string.ok, null);
|
|
|
|
builder.create().show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 20:08:05 +02:00
|
|
|
private void onResendSmsButton(View view) {
|
|
|
|
try {
|
|
|
|
xmppConnectionService.getQuickConversationsService().requestVerification(PhoneNumberUtilWrapper.toPhoneNumber(this, account.getJid()));
|
|
|
|
setRequestingVerificationState(true);
|
|
|
|
} catch (NumberParseException e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 16:27:56 +02:00
|
|
|
private void setVerifyingState(boolean verifying) {
|
|
|
|
this.verifying = verifying;
|
|
|
|
this.binding.back.setText(verifying ? R.string.cancel : R.string.back);
|
|
|
|
this.binding.next.setEnabled(!verifying);
|
|
|
|
this.binding.next.setText(verifying ? R.string.verifying : R.string.next);
|
|
|
|
this.binding.resendSms.setVisibility(verifying ? View.GONE : View.VISIBLE);
|
|
|
|
pinEntryWrapper.setEnabled(!verifying);
|
|
|
|
this.binding.progressBar.setVisibility(verifying ? View.VISIBLE : View.GONE);
|
|
|
|
this.binding.progressBar.setIndeterminate(verifying);
|
|
|
|
}
|
|
|
|
|
2018-10-22 20:08:05 +02:00
|
|
|
private void setRequestingVerificationState(boolean requesting) {
|
|
|
|
this.requestingVerification = requesting;
|
|
|
|
if (requesting) {
|
|
|
|
this.binding.resendSms.setEnabled(false);
|
|
|
|
this.binding.resendSms.setText(R.string.requesting_sms);
|
|
|
|
} else {
|
|
|
|
setTimeoutLabelInResendButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-20 16:37:59 +02:00
|
|
|
@Override
|
|
|
|
protected void refreshUiReal() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
void onBackendConnected() {
|
2018-10-21 16:27:56 +02:00
|
|
|
xmppConnectionService.getQuickConversationsService().addOnVerificationListener(this);
|
2018-10-22 20:08:05 +02:00
|
|
|
xmppConnectionService.getQuickConversationsService().addOnVerificationRequestedListener(this);
|
2018-10-20 16:37:59 +02:00
|
|
|
this.account = AccountUtils.getFirst(xmppConnectionService);
|
|
|
|
if (this.account == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-10-22 20:08:05 +02:00
|
|
|
this.binding.weHaveSent.setText(Html.fromHtml(getString(R.string.we_have_sent_you_an_sms_to_x, PhoneNumberUtilWrapper.toFormattedPhoneNumber(this, this.account.getJid()))));
|
2018-10-21 16:27:56 +02:00
|
|
|
setVerifyingState(xmppConnectionService.getQuickConversationsService().isVerifying());
|
2018-10-22 20:08:05 +02:00
|
|
|
setRequestingVerificationState(xmppConnectionService.getQuickConversationsService().isRequestingVerification());
|
2018-10-20 16:37:59 +02:00
|
|
|
}
|
2018-10-20 23:38:03 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle savedInstanceState) {
|
|
|
|
savedInstanceState.putString("pin", this.pinEntryWrapper.getPin());
|
2018-10-21 16:27:56 +02:00
|
|
|
savedInstanceState.putBoolean("verifying", this.verifying);
|
2018-10-22 20:08:05 +02:00
|
|
|
savedInstanceState.putBoolean("requesting_verification", this.requestingVerification);
|
|
|
|
savedInstanceState.putLong(EXTRA_RETRY_SMS_AFTER, this.retrySmsAfter);
|
2018-10-22 22:54:05 +02:00
|
|
|
savedInstanceState.putLong(EXTRA_RETRY_VERIFICATION_AFTER, this.retryVerificationAfter);
|
2018-10-20 23:38:03 +02:00
|
|
|
if (this.pasted != null) {
|
|
|
|
savedInstanceState.putString("pasted", this.pasted);
|
|
|
|
}
|
|
|
|
super.onSaveInstanceState(savedInstanceState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
clipboardManager.addPrimaryClipChangedListener(this);
|
2018-10-22 22:54:05 +02:00
|
|
|
final Intent intent = getIntent();
|
|
|
|
this.retrySmsAfter = intent != null ? intent.getLongExtra(EXTRA_RETRY_SMS_AFTER, this.retrySmsAfter) : this.retrySmsAfter;
|
2018-10-22 20:08:05 +02:00
|
|
|
if (this.retrySmsAfter > 0) {
|
|
|
|
mHandler.post(SMS_TIMEOUT_UPDATER);
|
|
|
|
}
|
2018-10-22 22:54:05 +02:00
|
|
|
if (this.retryVerificationAfter > 0) {
|
|
|
|
mHandler.post(VERIFICATION_TIMEOUT_UPDATER);
|
|
|
|
}
|
2018-10-20 23:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
super.onStop();
|
2018-10-22 20:08:05 +02:00
|
|
|
mHandler.removeCallbacks(SMS_TIMEOUT_UPDATER);
|
2018-10-22 22:54:05 +02:00
|
|
|
mHandler.removeCallbacks(VERIFICATION_TIMEOUT_UPDATER);
|
2018-10-20 23:38:03 +02:00
|
|
|
clipboardManager.removePrimaryClipChangedListener(this);
|
2018-10-21 16:27:56 +02:00
|
|
|
if (xmppConnectionService != null) {
|
|
|
|
xmppConnectionService.getQuickConversationsService().removeOnVerificationListener(this);
|
2018-10-22 20:08:05 +02:00
|
|
|
xmppConnectionService.getQuickConversationsService().removeOnVerificationRequestedListener(this);
|
2018-10-21 16:27:56 +02:00
|
|
|
}
|
2018-10-20 23:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (pinEntryWrapper.isEmpty()) {
|
|
|
|
pastePinFromClipboard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void pastePinFromClipboard() {
|
|
|
|
final ClipDescription description = clipboardManager != null ? clipboardManager.getPrimaryClipDescription() : null;
|
|
|
|
if (description != null && description.hasMimeType(MIMETYPE_TEXT_PLAIN)) {
|
|
|
|
final ClipData primaryClip = clipboardManager.getPrimaryClip();
|
|
|
|
if (primaryClip != null && primaryClip.getItemCount() > 0) {
|
|
|
|
final CharSequence clip = primaryClip.getItemAt(0).getText();
|
2018-10-21 16:27:56 +02:00
|
|
|
if (PinEntryWrapper.isValidPin(clip) && !clip.toString().equals(this.pasted)) {
|
2018-10-20 23:38:03 +02:00
|
|
|
this.pasted = clip.toString();
|
|
|
|
pinEntryWrapper.setPin(clip.toString());
|
|
|
|
final Snackbar snackbar = Snackbar.make(binding.coordinator, R.string.possible_pin, Snackbar.LENGTH_LONG);
|
|
|
|
snackbar.setAction(R.string.undo, v -> pinEntryWrapper.clear());
|
|
|
|
snackbar.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-22 22:54:05 +02:00
|
|
|
private void performPostVerificationRedirect() {
|
|
|
|
Intent intent = new Intent(this, PublishProfilePictureActivity.class);
|
|
|
|
intent.putExtra(PublishProfilePictureActivity.EXTRA_ACCOUNT, account.getJid().asBareJid().toEscapedString());
|
|
|
|
intent.putExtra("setup", true);
|
|
|
|
startActivity(intent);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
2018-10-20 23:38:03 +02:00
|
|
|
@Override
|
|
|
|
public void onPrimaryClipChanged() {
|
|
|
|
this.pasted = null;
|
|
|
|
if (pinEntryWrapper.isEmpty()) {
|
|
|
|
pastePinFromClipboard();
|
|
|
|
}
|
|
|
|
}
|
2018-10-21 16:27:56 +02:00
|
|
|
|
|
|
|
@Override
|
2018-10-22 22:54:05 +02:00
|
|
|
public void onVerificationFailed(final int code) {
|
2018-10-21 16:27:56 +02:00
|
|
|
runOnUiThread(() -> {
|
|
|
|
setVerifyingState(false);
|
2018-10-22 22:54:05 +02:00
|
|
|
if (code == 401) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
builder.setMessage(R.string.incorrect_pin);
|
|
|
|
builder.setPositiveButton(R.string.ok, null);
|
|
|
|
builder.create().show();
|
|
|
|
} else {
|
|
|
|
ApiErrorDialogHelper.create(this, code).show();
|
|
|
|
}
|
2018-10-21 16:27:56 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onVerificationSucceeded() {
|
2018-10-22 22:54:05 +02:00
|
|
|
runOnUiThread(this::performPostVerificationRedirect);
|
2018-10-21 16:27:56 +02:00
|
|
|
}
|
2018-10-22 20:08:05 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onVerificationRetryAt(long timestamp) {
|
2018-10-22 22:54:05 +02:00
|
|
|
this.retryVerificationAfter = timestamp;
|
|
|
|
runOnUiThread(() -> setVerifyingState(false));
|
|
|
|
mHandler.removeCallbacks(VERIFICATION_TIMEOUT_UPDATER);
|
|
|
|
runOnUiThread(VERIFICATION_TIMEOUT_UPDATER);
|
2018-10-22 20:08:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//send sms again button callback
|
|
|
|
@Override
|
|
|
|
public void onVerificationRequestFailed(int code) {
|
2018-10-22 22:54:05 +02:00
|
|
|
runOnUiThread(() -> {
|
2018-10-22 20:08:05 +02:00
|
|
|
setRequestingVerificationState(false);
|
2018-10-22 22:54:05 +02:00
|
|
|
ApiErrorDialogHelper.create(this, code).show();
|
2018-10-22 20:08:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//send sms again button callback
|
|
|
|
@Override
|
|
|
|
public void onVerificationRequested() {
|
2018-10-22 22:54:05 +02:00
|
|
|
runOnUiThread(() -> {
|
2018-10-22 20:08:05 +02:00
|
|
|
setRequestingVerificationState(false);
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
2018-10-22 22:54:05 +02:00
|
|
|
builder.setMessage(R.string.we_have_sent_you_another_sms);
|
2018-10-22 20:08:05 +02:00
|
|
|
builder.setPositiveButton(R.string.ok, null);
|
|
|
|
builder.create().show();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onVerificationRequestedRetryAt(long timestamp) {
|
|
|
|
this.retrySmsAfter = timestamp;
|
2018-10-22 22:54:05 +02:00
|
|
|
runOnUiThread(() -> setRequestingVerificationState(false));
|
2018-10-22 20:08:05 +02:00
|
|
|
mHandler.removeCallbacks(SMS_TIMEOUT_UPDATER);
|
|
|
|
runOnUiThread(SMS_TIMEOUT_UPDATER);
|
|
|
|
}
|
2018-10-20 16:37:59 +02:00
|
|
|
}
|