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 android.databinding.DataBindingUtil;
 10import android.os.Bundle;
 11import android.os.Handler;
 12import android.os.SystemClock;
 13import android.support.design.widget.Snackbar;
 14import android.support.v7.widget.Toolbar;
 15import android.text.Html;
 16import android.util.Log;
 17import android.view.View;
 18
 19import eu.siacs.conversations.Config;
 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.PinEntryWrapper;
 25import eu.siacs.conversations.utils.AccountUtils;
 26import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
 27import eu.siacs.conversations.utils.TimeframeUtils;
 28import io.michaelrocks.libphonenumber.android.NumberParseException;
 29
 30import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;
 31
 32public class VerifyActivity extends XmppActivity implements ClipboardManager.OnPrimaryClipChangedListener, QuickConversationsService.OnVerification, QuickConversationsService.OnVerificationRequested {
 33
 34    public static final String EXTRA_RETRY_SMS_AFTER = "retry_sms_after";
 35
 36    private ActivityVerifyBinding binding;
 37    private Account account;
 38    private PinEntryWrapper pinEntryWrapper;
 39    private ClipboardManager clipboardManager;
 40    private String pasted = null;
 41    private boolean verifying = false;
 42    private boolean requestingVerification = false;
 43    private long retrySmsAfter = 0;
 44
 45    private final Handler mHandler = new Handler();
 46
 47
 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
 57    private boolean setTimeoutLabelInResendButton() {
 58        if (retrySmsAfter != 0) {
 59            long remaining = retrySmsAfter - SystemClock.elapsedRealtime();
 60            if (remaining >= 0) {
 61                binding.resendSms.setEnabled(false);
 62                binding.resendSms.setText(getString(R.string.resend_sms_in, TimeframeUtils.resolve(VerifyActivity.this,remaining)));
 63                return true;
 64            }
 65        }
 66        binding.resendSms.setEnabled(true);
 67        binding.resendSms.setText(R.string.resend_sms);
 68        return false;
 69    }
 70
 71    @Override
 72    protected void onCreate(final Bundle savedInstanceState) {
 73        super.onCreate(savedInstanceState);
 74        String pin = savedInstanceState != null ? savedInstanceState.getString("pin") : null;
 75        boolean verifying = savedInstanceState != null && savedInstanceState.getBoolean("verifying");
 76        boolean requestingVerification = savedInstanceState != null && savedInstanceState.getBoolean("requesting_verification", false);
 77        this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null;
 78        this.retrySmsAfter = savedInstanceState != null ? savedInstanceState.getLong(EXTRA_RETRY_SMS_AFTER,0L) : 0L;
 79        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_verify);
 80        setSupportActionBar((Toolbar) this.binding.toolbar);
 81        this.pinEntryWrapper = new PinEntryWrapper(binding.pinBox);
 82        if (pin != null) {
 83            this.pinEntryWrapper.setPin(pin);
 84        }
 85        binding.back.setOnClickListener(this::onBackButton);
 86        binding.next.setOnClickListener(this::onNextButton);
 87        binding.resendSms.setOnClickListener(this::onResendSmsButton);
 88        clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
 89        setVerifyingState(verifying);
 90        setRequestingVerificationState(requestingVerification);
 91    }
 92
 93    private void onBackButton(View view) {
 94        if (this.verifying) {
 95            setVerifyingState(false);
 96            return;
 97        }
 98        final Intent intent = new Intent(this, EnterPhoneNumberActivity.class);
 99        if (this.account != null) {
100            AlertDialog.Builder builder = new AlertDialog.Builder(this);
101            builder.setMessage(R.string.abort_registration_procedure);
102            builder.setPositiveButton(R.string.yes, (dialog, which) -> {
103                xmppConnectionService.deleteAccount(account);
104                startActivity(intent);
105                finish();
106            });
107            builder.setNegativeButton(R.string.no, null);
108            builder.create().show();
109        } else {
110            startActivity(intent);
111            finish();
112        }
113    }
114
115    private void onNextButton(View view) {
116        final String pin = pinEntryWrapper.getPin();
117        if (PinEntryWrapper.isValidPin(pin)) {
118            if (account != null && xmppConnectionService != null) {
119                setVerifyingState(true);
120                xmppConnectionService.getQuickConversationsService().verify(account, pin);
121            }
122        } else {
123            AlertDialog.Builder builder = new AlertDialog.Builder(this);
124            builder.setMessage(R.string.please_enter_pin);
125            builder.setPositiveButton(R.string.ok, null);
126            builder.create().show();
127        }
128    }
129
130    private void onResendSmsButton(View view) {
131        try {
132            xmppConnectionService.getQuickConversationsService().requestVerification(PhoneNumberUtilWrapper.toPhoneNumber(this, account.getJid()));
133            setRequestingVerificationState(true);
134        } catch (NumberParseException e) {
135
136        }
137    }
138
139    private void setVerifyingState(boolean verifying) {
140        this.verifying = verifying;
141        this.binding.back.setText(verifying ? R.string.cancel : R.string.back);
142        this.binding.next.setEnabled(!verifying);
143        this.binding.next.setText(verifying ? R.string.verifying : R.string.next);
144        this.binding.resendSms.setVisibility(verifying ? View.GONE : View.VISIBLE);
145        pinEntryWrapper.setEnabled(!verifying);
146        this.binding.progressBar.setVisibility(verifying ? View.VISIBLE : View.GONE);
147        this.binding.progressBar.setIndeterminate(verifying);
148    }
149
150    private void setRequestingVerificationState(boolean requesting) {
151        this.requestingVerification = requesting;
152        if (requesting) {
153            this.binding.resendSms.setEnabled(false);
154            this.binding.resendSms.setText(R.string.requesting_sms);
155        } else {
156            setTimeoutLabelInResendButton();
157        }
158
159    }
160
161    @Override
162    protected void refreshUiReal() {
163
164    }
165
166    @Override
167    void onBackendConnected() {
168        xmppConnectionService.getQuickConversationsService().addOnVerificationListener(this);
169        xmppConnectionService.getQuickConversationsService().addOnVerificationRequestedListener(this);
170        this.account = AccountUtils.getFirst(xmppConnectionService);
171        if (this.account == null) {
172            return;
173        }
174        this.binding.weHaveSent.setText(Html.fromHtml(getString(R.string.we_have_sent_you_an_sms_to_x, PhoneNumberUtilWrapper.toFormattedPhoneNumber(this, this.account.getJid()))));
175        setVerifyingState(xmppConnectionService.getQuickConversationsService().isVerifying());
176        setRequestingVerificationState(xmppConnectionService.getQuickConversationsService().isRequestingVerification());
177    }
178
179    @Override
180    public void onSaveInstanceState(Bundle savedInstanceState) {
181        savedInstanceState.putString("pin", this.pinEntryWrapper.getPin());
182        savedInstanceState.putBoolean("verifying", this.verifying);
183        savedInstanceState.putBoolean("requesting_verification", this.requestingVerification);
184        savedInstanceState.putLong(EXTRA_RETRY_SMS_AFTER, this.retrySmsAfter);
185        if (this.pasted != null) {
186            savedInstanceState.putString("pasted", this.pasted);
187        }
188        super.onSaveInstanceState(savedInstanceState);
189    }
190
191    @Override
192    public void onStart() {
193        super.onStart();
194        clipboardManager.addPrimaryClipChangedListener(this);
195        if (this.retrySmsAfter > 0) {
196            mHandler.post(SMS_TIMEOUT_UPDATER);
197        }
198    }
199
200    @Override
201    public void onStop() {
202        super.onStop();
203        mHandler.removeCallbacks(SMS_TIMEOUT_UPDATER);
204        clipboardManager.removePrimaryClipChangedListener(this);
205        if (xmppConnectionService != null) {
206            xmppConnectionService.getQuickConversationsService().removeOnVerificationListener(this);
207            xmppConnectionService.getQuickConversationsService().removeOnVerificationRequestedListener(this);
208        }
209    }
210
211    @Override
212    public void onResume() {
213        super.onResume();
214        if (pinEntryWrapper.isEmpty()) {
215            pastePinFromClipboard();
216        }
217    }
218
219    private void pastePinFromClipboard() {
220        final ClipDescription description = clipboardManager != null ? clipboardManager.getPrimaryClipDescription() : null;
221        if (description != null && description.hasMimeType(MIMETYPE_TEXT_PLAIN)) {
222            final ClipData primaryClip = clipboardManager.getPrimaryClip();
223            if (primaryClip != null && primaryClip.getItemCount() > 0) {
224                final CharSequence clip = primaryClip.getItemAt(0).getText();
225                if (PinEntryWrapper.isValidPin(clip) && !clip.toString().equals(this.pasted)) {
226                    this.pasted = clip.toString();
227                    pinEntryWrapper.setPin(clip.toString());
228                    final Snackbar snackbar = Snackbar.make(binding.coordinator, R.string.possible_pin, Snackbar.LENGTH_LONG);
229                    snackbar.setAction(R.string.undo, v -> pinEntryWrapper.clear());
230                    snackbar.show();
231                }
232            }
233        }
234    }
235
236    @Override
237    public void onPrimaryClipChanged() {
238        this.pasted = null;
239        if (pinEntryWrapper.isEmpty()) {
240            pastePinFromClipboard();
241        }
242    }
243
244    @Override
245    public void onVerificationFailed(int code) {
246        runOnUiThread(() -> {
247            setVerifyingState(false);
248        });
249        Log.d(Config.LOGTAG,"code="+code);
250    }
251
252    @Override
253    public void onVerificationSucceeded() {
254
255    }
256
257    @Override
258    public void onVerificationRetryAt(long timestamp) {
259
260    }
261
262    //send sms again button callback
263    @Override
264    public void onVerificationRequestFailed(int code) {
265        runOnUiThread(()->{
266            setRequestingVerificationState(false);
267        });
268        Log.d(Config.LOGTAG,"code="+code);
269    }
270
271    //send sms again button callback
272    @Override
273    public void onVerificationRequested() {
274        runOnUiThread(()-> {
275            setRequestingVerificationState(false);
276            AlertDialog.Builder builder = new AlertDialog.Builder(this);
277            builder.setMessage(R.string.we_have_sent_you_the_sms_again);
278            builder.setPositiveButton(R.string.ok, null);
279            builder.create().show();
280        });
281    }
282
283    @Override
284    public void onVerificationRequestedRetryAt(long timestamp) {
285        this.retrySmsAfter = timestamp;
286        runOnUiThread(()-> setRequestingVerificationState(false));
287        mHandler.removeCallbacks(SMS_TIMEOUT_UPDATER);
288        runOnUiThread(SMS_TIMEOUT_UPDATER);
289    }
290}