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-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-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-20 23:38:03 +02:00
|
|
|
import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;
|
|
|
|
|
2018-10-21 16:27:56 +02:00
|
|
|
public class VerifyActivity extends XmppActivity implements ClipboardManager.OnPrimaryClipChangedListener, QuickConversationsService.OnVerification {
|
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-20 23:38:03 +02:00
|
|
|
|
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-20 23:38:03 +02:00
|
|
|
this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null;
|
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-20 23:38:03 +02:00
|
|
|
clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
2018-10-21 16:27:56 +02:00
|
|
|
setVerifyingState(verifying);
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-20 16:37:59 +02:00
|
|
|
this.account = AccountUtils.getFirst(xmppConnectionService);
|
|
|
|
if (this.account == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.binding.weHaveSent.setText(Html.fromHtml(getString(R.string.we_have_sent_you_an_sms, PhoneNumberUtilWrapper.prettyPhoneNumber(this, this.account.getJid()))));
|
2018-10-21 16:27:56 +02:00
|
|
|
setVerifyingState(xmppConnectionService.getQuickConversationsService().isVerifying());
|
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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
clipboardManager.removePrimaryClipChangedListener(this);
|
2018-10-21 16:27:56 +02:00
|
|
|
if (xmppConnectionService != null) {
|
|
|
|
xmppConnectionService.getQuickConversationsService().removeOnVerificationListener(this);
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPrimaryClipChanged() {
|
|
|
|
this.pasted = null;
|
|
|
|
if (pinEntryWrapper.isEmpty()) {
|
|
|
|
pastePinFromClipboard();
|
|
|
|
}
|
|
|
|
}
|
2018-10-21 16:27:56 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onVerificationFailed() {
|
|
|
|
runOnUiThread(() -> {
|
|
|
|
setVerifyingState(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onVerificationSucceeded() {
|
|
|
|
|
|
|
|
}
|
2018-10-20 16:37:59 +02:00
|
|
|
}
|