session-android/src/org/thoughtcrime/securesms/RegistrationActivity.java

1193 lines
51 KiB
Java
Raw Normal View History

2013-03-26 04:26:03 +00:00
package org.thoughtcrime.securesms;
2017-11-25 06:00:30 +00:00
import android.Manifest;
2017-11-08 20:20:11 +00:00
import android.animation.Animator;
import android.annotation.SuppressLint;
2018-05-31 11:55:11 +00:00
import android.content.ActivityNotFoundException;
2017-11-08 20:20:11 +00:00
import android.content.BroadcastReceiver;
import android.content.Context;
2013-03-26 04:26:03 +00:00
import android.content.Intent;
2017-11-08 20:20:11 +00:00
import android.content.IntentFilter;
import android.graphics.Color;
2018-05-31 11:55:11 +00:00
import android.net.Uri;
2017-11-08 20:20:11 +00:00
import android.os.AsyncTask;
2013-03-26 04:26:03 +00:00
import android.os.Bundle;
2017-11-08 20:20:11 +00:00
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
2013-03-26 04:26:03 +00:00
import android.text.Editable;
2017-11-08 20:20:11 +00:00
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
2014-11-12 19:15:05 +00:00
import android.text.TextUtils;
2013-03-26 04:26:03 +00:00
import android.text.TextWatcher;
2017-11-08 20:20:11 +00:00
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.LayoutInflater;
2013-03-26 04:26:03 +00:00
import android.view.MotionEvent;
import android.view.View;
2017-11-08 20:20:11 +00:00
import android.view.animation.OvershootInterpolator;
import android.view.inputmethod.InputMethodManager;
2013-03-26 04:26:03 +00:00
import android.widget.ArrayAdapter;
import android.widget.EditText;
2013-03-26 04:26:03 +00:00
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
2017-11-08 20:20:11 +00:00
import com.dd.CircularProgressButton;
import com.google.android.gms.auth.api.phone.SmsRetriever;
import com.google.android.gms.auth.api.phone.SmsRetrieverClient;
2014-07-23 22:40:45 +00:00
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.Status;
2017-11-08 20:20:11 +00:00
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.google.android.gms.tasks.Task;
2013-03-26 04:26:03 +00:00
import com.google.i18n.phonenumbers.AsYouTypeFormatter;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import net.sqlcipher.database.SQLiteDatabase;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
2017-11-08 20:20:11 +00:00
import org.thoughtcrime.securesms.animation.AnimationCompleteListener;
import org.thoughtcrime.securesms.backup.FullBackupBase;
import org.thoughtcrime.securesms.backup.FullBackupImporter;
2017-11-08 20:20:11 +00:00
import org.thoughtcrime.securesms.components.registration.CallMeCountDownView;
import org.thoughtcrime.securesms.components.registration.VerificationCodeView;
import org.thoughtcrime.securesms.components.registration.VerificationPinKeyboard;
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider;
2017-11-08 20:20:11 +00:00
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.crypto.PreKeyUtil;
import org.thoughtcrime.securesms.crypto.SessionUtil;
2018-05-22 09:13:10 +00:00
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
2017-11-08 20:20:11 +00:00
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.IdentityDatabase;
import org.thoughtcrime.securesms.database.NoExternalStorageException;
2017-11-08 20:20:11 +00:00
import org.thoughtcrime.securesms.jobs.DirectoryRefreshJob;
import org.thoughtcrime.securesms.jobs.GcmRefreshJob;
2018-05-22 09:13:10 +00:00
import org.thoughtcrime.securesms.jobs.RotateCertificateJob;
import org.thoughtcrime.securesms.lock.RegistrationLockReminders;
2018-05-22 09:13:10 +00:00
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
2017-11-25 06:00:30 +00:00
import org.thoughtcrime.securesms.permissions.Permissions;
2017-11-08 20:20:11 +00:00
import org.thoughtcrime.securesms.push.AccountManagerFactory;
import org.thoughtcrime.securesms.service.DirectoryRefreshListener;
import org.thoughtcrime.securesms.service.RotateSignedPreKeyListener;
import org.thoughtcrime.securesms.service.VerificationCodeParser;
import org.thoughtcrime.securesms.util.BackupUtil;
import org.thoughtcrime.securesms.util.DateUtils;
import org.thoughtcrime.securesms.util.Dialogs;
2017-11-08 20:20:11 +00:00
import org.thoughtcrime.securesms.util.PlayServicesUtil;
import org.thoughtcrime.securesms.util.PlayServicesUtil.PlayServicesStatus;
import org.thoughtcrime.securesms.util.ServiceUtil;
2014-07-23 22:40:45 +00:00
import org.thoughtcrime.securesms.util.TextSecurePreferences;
2014-11-12 19:15:05 +00:00
import org.thoughtcrime.securesms.util.Util;
2017-11-08 20:20:11 +00:00
import org.thoughtcrime.securesms.util.concurrent.AssertedSuccessListener;
import org.whispersystems.libsignal.IdentityKeyPair;
import org.whispersystems.libsignal.state.PreKeyRecord;
import org.whispersystems.libsignal.state.SignedPreKeyRecord;
import org.whispersystems.libsignal.util.KeyHelper;
import org.whispersystems.libsignal.util.guava.Optional;
2017-11-08 20:20:11 +00:00
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
2018-03-01 17:51:37 +00:00
import org.whispersystems.signalservice.api.push.exceptions.RateLimitException;
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
2018-03-01 17:51:37 +00:00
import org.whispersystems.signalservice.internal.push.LockedException;
2013-03-26 04:26:03 +00:00
2017-11-08 20:20:11 +00:00
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
2017-11-08 20:20:11 +00:00
import java.util.List;
import java.util.Locale;
2018-03-01 17:51:37 +00:00
import java.util.concurrent.TimeUnit;
2017-11-08 20:20:11 +00:00
2013-03-26 04:26:03 +00:00
/**
2013-07-11 00:54:38 +00:00
* The register account activity. Prompts ths user for their registration information
* and begins the account registration process.
2013-03-26 04:26:03 +00:00
*
* @author Moxie Marlinspike
*
*/
2017-11-08 20:20:11 +00:00
public class RegistrationActivity extends BaseActionBarActivity implements VerificationCodeView.OnCodeEnteredListener {
2013-03-26 04:26:03 +00:00
2017-11-08 20:20:11 +00:00
private static final int PICK_COUNTRY = 1;
private static final int SCENE_TRANSITION_DURATION = 250;
private static final int DEBUG_TAP_TARGET = 8;
private static final int DEBUG_TAP_ANNOUNCE = 4;
public static final String RE_REGISTRATION_EXTRA = "re_registration";
2013-03-26 04:26:03 +00:00
2017-11-08 20:20:11 +00:00
private static final String TAG = RegistrationActivity.class.getSimpleName();
2013-03-26 04:26:03 +00:00
2017-11-08 20:20:11 +00:00
private AsYouTypeFormatter countryFormatter;
private ArrayAdapter<String> countrySpinnerAdapter;
private Spinner countrySpinner;
private TextView countryCode;
private TextView number;
private CircularProgressButton createButton;
private TextView informationView;
private TextView informationToggleText;
private TextView title;
private TextView subtitle;
private View registrationContainer;
private View verificationContainer;
private FloatingActionButton fab;
private View restoreContainer;
private TextView restoreBackupTime;
private TextView restoreBackupSize;
private TextView restoreBackupProgress;
private CircularProgressButton restoreButton;
2018-03-01 17:51:37 +00:00
private View pinContainer;
private EditText pin;
private CircularProgressButton pinButton;
private TextView pinForgotButton;
private View pinClarificationContainer;
2017-11-08 20:20:11 +00:00
private CallMeCountDownView callMeCountDownView;
private VerificationPinKeyboard keyboard;
private VerificationCodeView verificationCodeView;
private RegistrationState registrationState;
private SmsRetrieverReceiver smsRetrieverReceiver;
2017-11-08 20:20:11 +00:00
private SignalServiceAccountManager accountManager;
private int debugTapCounter;
2013-03-26 04:26:03 +00:00
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
2013-03-26 04:26:03 +00:00
setContentView(R.layout.registration_activity);
initializeResources();
2013-07-11 00:54:38 +00:00
initializeSpinner();
2017-11-25 06:00:30 +00:00
initializePermissions();
2013-03-26 04:26:03 +00:00
initializeNumber();
2017-11-08 20:20:11 +00:00
initializeChallengeListener();
}
@Override
public void onDestroy() {
super.onDestroy();
shutdownChallengeListener();
markAsVerifying(false);
EventBus.getDefault().unregister(this);
2013-03-26 04:26:03 +00:00
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_COUNTRY && resultCode == RESULT_OK && data != null) {
2017-11-08 20:20:11 +00:00
this.countryCode.setText(String.valueOf(data.getIntExtra("country_code", 1)));
2013-03-26 04:26:03 +00:00
setCountryDisplay(data.getStringExtra("country_name"));
setCountryFormatter(data.getIntExtra("country_code", 1));
}
}
2017-11-25 06:00:30 +00:00
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
Permissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
}
2013-03-26 04:26:03 +00:00
private void initializeResources() {
TextView skipButton = findViewById(R.id.skip_button);
TextView restoreSkipButton = findViewById(R.id.skip_restore_button);
2018-05-31 11:55:11 +00:00
View termsLinkView = findViewById(R.id.terms_label);
2017-11-08 20:20:11 +00:00
this.countrySpinner = findViewById(R.id.country_spinner);
this.countryCode = findViewById(R.id.country_code);
this.number = findViewById(R.id.number);
this.createButton = findViewById(R.id.registerButton);
this.informationView = findViewById(R.id.registration_information);
this.informationToggleText = findViewById(R.id.information_label);
this.title = findViewById(R.id.verify_header);
this.subtitle = findViewById(R.id.verify_subheader);
this.registrationContainer = findViewById(R.id.registration_container);
this.verificationContainer = findViewById(R.id.verification_container);
this.fab = findViewById(R.id.fab);
this.verificationCodeView = findViewById(R.id.code);
this.keyboard = findViewById(R.id.keyboard);
this.callMeCountDownView = findViewById(R.id.call_me_count_down);
this.restoreContainer = findViewById(R.id.restore_container);
this.restoreBackupSize = findViewById(R.id.backup_size_text);
this.restoreBackupTime = findViewById(R.id.backup_created_text);
this.restoreBackupProgress = findViewById(R.id.backup_progress_text);
this.restoreButton = findViewById(R.id.restore_button);
2018-03-01 17:51:37 +00:00
this.pinContainer = findViewById(R.id.pin_container);
this.pin = findViewById(R.id.pin);
this.pinButton = findViewById(R.id.pinButton);
this.pinForgotButton = findViewById(R.id.forgot_button);
this.pinClarificationContainer = findViewById(R.id.pin_clarification_container);
2017-11-08 20:20:11 +00:00
this.registrationState = new RegistrationState(RegistrationState.State.INITIAL, null, null, null);
2013-03-26 04:26:03 +00:00
2013-07-11 00:54:38 +00:00
this.countryCode.addTextChangedListener(new CountryCodeChangedListener());
this.number.addTextChangedListener(new NumberChangedListener());
2017-11-08 20:20:11 +00:00
this.createButton.setOnClickListener(v -> handleRegister());
this.callMeCountDownView.setOnClickListener(v -> handlePhoneCallRequest());
2018-05-31 11:55:11 +00:00
skipButton.setOnClickListener(v -> handleCancel());
informationToggleText.setOnClickListener(new InformationToggleListener());
termsLinkView.setOnClickListener(this::onTermsLinkClicked);
restoreSkipButton.setOnClickListener(v -> displayInitialView(true));
if (getIntent().getBooleanExtra(RE_REGISTRATION_EXTRA, false)) {
2017-11-08 20:20:11 +00:00
skipButton.setVisibility(View.VISIBLE);
} else {
2017-11-08 20:20:11 +00:00
skipButton.setVisibility(View.INVISIBLE);
}
2017-11-08 20:20:11 +00:00
this.keyboard.setOnKeyPressListener(key -> {
if (key >= 0) verificationCodeView.append(key);
else verificationCodeView.delete();
});
fab.setOnClickListener(this::onDebugClick);
fab.setRippleColor(Color.TRANSPARENT);
2017-11-08 20:20:11 +00:00
this.verificationCodeView.setOnCompleteListener(this);
EventBus.getDefault().register(this);
2013-07-11 00:54:38 +00:00
}
private void onDebugClick(View view) {
debugTapCounter++;
if (debugTapCounter >= DEBUG_TAP_TARGET) {
startActivity(new Intent(this, LogSubmitActivity.class));
} else if (debugTapCounter >= DEBUG_TAP_ANNOUNCE) {
int remaining = DEBUG_TAP_TARGET - debugTapCounter;
Toast.makeText(this, getResources().getQuantityString(R.plurals.RegistrationActivity_debug_log_hint, remaining, remaining), Toast.LENGTH_SHORT).show();
}
}
2017-11-08 20:20:11 +00:00
@SuppressLint("ClickableViewAccessibility")
2013-07-11 00:54:38 +00:00
private void initializeSpinner() {
2014-11-12 19:15:05 +00:00
this.countrySpinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
2013-03-26 04:26:03 +00:00
this.countrySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
2013-07-11 00:54:38 +00:00
2013-07-09 23:36:15 +00:00
setCountryDisplay(getString(R.string.RegistrationActivity_select_your_country));
2013-03-26 04:26:03 +00:00
this.countrySpinner.setAdapter(this.countrySpinnerAdapter);
2017-11-08 20:20:11 +00:00
this.countrySpinner.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
Intent intent = new Intent(RegistrationActivity.this, CountrySelectionActivity.class);
startActivityForResult(intent, PICK_COUNTRY);
2013-03-26 04:26:03 +00:00
}
2017-11-08 20:20:11 +00:00
return true;
2013-03-26 04:26:03 +00:00
});
2017-11-08 20:20:11 +00:00
this.countrySpinner.setOnKeyListener((v, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER && event.getAction() == KeyEvent.ACTION_UP) {
Intent intent = new Intent(RegistrationActivity.this, CountrySelectionActivity.class);
startActivityForResult(intent, PICK_COUNTRY);
return true;
}
2017-11-08 20:20:11 +00:00
return false;
});
2013-03-26 04:26:03 +00:00
}
2017-11-25 06:00:30 +00:00
@SuppressLint("MissingPermission")
2013-03-26 04:26:03 +00:00
private void initializeNumber() {
2017-11-25 06:00:30 +00:00
Optional<Phonenumber.PhoneNumber> localNumber = Optional.absent();
if (Permissions.hasAll(this, Manifest.permission.READ_PHONE_STATE)) {
localNumber = Util.getDeviceNumber(this);
}
2013-03-26 04:26:03 +00:00
if (localNumber.isPresent()) {
this.countryCode.setText(String.valueOf(localNumber.get().getCountryCode()));
this.number.setText(String.valueOf(localNumber.get().getNationalNumber()));
} else {
Optional<String> simCountryIso = Util.getSimCountryIso(this);
if (simCountryIso.isPresent() && !TextUtils.isEmpty(simCountryIso.get())) {
2017-11-08 20:20:11 +00:00
this.countryCode.setText(String.valueOf(PhoneNumberUtil.getInstance().getCountryCodeForRegion(simCountryIso.get())));
2013-03-26 04:26:03 +00:00
}
}
}
2017-11-25 06:00:30 +00:00
@SuppressLint("InlinedApi")
private void initializePermissions() {
Permissions.with(RegistrationActivity.this)
.request(Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE)
2017-11-25 06:00:30 +00:00
.ifNecessary()
.withRationaleDialog(getString(R.string.RegistrationActivity_signal_needs_access_to_your_contacts_and_media_in_order_to_connect_with_friends),
R.drawable.ic_contacts_white_48dp, R.drawable.ic_folder_white_48dp)
.onSomeGranted(permissions -> {
if (permissions.contains(Manifest.permission.READ_PHONE_STATE)) {
initializeNumber();
}
if (permissions.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
initializeBackupDetection();
}
2017-11-25 06:00:30 +00:00
})
.execute();
}
@SuppressLint("StaticFieldLeak")
private void initializeBackupDetection() {
if (getIntent().getBooleanExtra(RE_REGISTRATION_EXTRA, false)) return;
new AsyncTask<Void, Void, BackupUtil.BackupInfo>() {
@Override
protected @Nullable BackupUtil.BackupInfo doInBackground(Void... voids) {
try {
return BackupUtil.getLatestBackup();
} catch (NoExternalStorageException e) {
Log.w(TAG, e);
return null;
}
}
@Override
protected void onPostExecute(@Nullable BackupUtil.BackupInfo backup) {
if (backup != null) displayRestoreView(backup);
}
}.execute();
}
2013-03-26 04:26:03 +00:00
private void setCountryDisplay(String value) {
this.countrySpinnerAdapter.clear();
this.countrySpinnerAdapter.add(value);
}
private void setCountryFormatter(int countryCode) {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
String regionCode = util.getRegionCodeForCountryCode(countryCode);
2013-07-09 23:36:15 +00:00
if (regionCode == null) this.countryFormatter = null;
else this.countryFormatter = util.getAsYouTypeFormatter(regionCode);
2013-03-26 04:26:03 +00:00
}
private String getConfiguredE164Number() {
return PhoneNumberFormatter.formatE164(countryCode.getText().toString(),
number.getText().toString());
}
@SuppressLint("StaticFieldLeak")
private void handleRestore(BackupUtil.BackupInfo backup) {
View view = LayoutInflater.from(this).inflate(R.layout.enter_backup_passphrase_dialog, null);
EditText prompt = view.findViewById(R.id.restore_passphrase_input);
new AlertDialog.Builder(this)
.setTitle(R.string.RegistrationActivity_enter_backup_passphrase)
.setView(view)
.setPositiveButton(getString(R.string.RegistrationActivity_restore), (dialog, which) -> {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(prompt.getWindowToken(), 0);
restoreButton.setIndeterminateProgressMode(true);
restoreButton.setProgress(50);
new AsyncTask<Void, Void, BackupImportResult>() {
@Override
protected BackupImportResult doInBackground(Void... voids) {
try {
Context context = RegistrationActivity.this;
String passphrase = prompt.getText().toString();
SQLiteDatabase database = DatabaseFactory.getBackupDatabase(context);
FullBackupImporter.importFile(context,
AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(),
database, backup.getFile(), passphrase);
DatabaseFactory.upgradeRestored(context, database);
NotificationChannels.restoreContactNotificationChannels(context);
TextSecurePreferences.setBackupEnabled(context, true);
TextSecurePreferences.setBackupPassphrase(context, passphrase);
return BackupImportResult.SUCCESS;
} catch (FullBackupImporter.DatabaseDowngradeException e) {
Log.w(TAG, "Failed due to the backup being from a newer version of Signal.", e);
return BackupImportResult.FAILURE_VERSION_DOWNGRADE;
} catch (IOException e) {
Log.w(TAG, e);
return BackupImportResult.FAILURE_UNKNOWN;
}
}
@Override
protected void onPostExecute(@NonNull BackupImportResult result) {
restoreButton.setIndeterminateProgressMode(false);
restoreButton.setProgress(0);
restoreBackupProgress.setText("");
switch (result) {
case SUCCESS:
displayInitialView(true);
break;
case FAILURE_VERSION_DOWNGRADE:
Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_backup_failure_downgrade, Toast.LENGTH_LONG).show();
break;
case FAILURE_UNKNOWN:
Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_incorrect_backup_passphrase, Toast.LENGTH_LONG).show();
break;
}
}
}.execute();
})
.setNegativeButton(android.R.string.cancel, null)
.show();
}
2017-11-08 20:20:11 +00:00
private void handleRegister() {
if (TextUtils.isEmpty(countryCode.getText())) {
Toast.makeText(this, getString(R.string.RegistrationActivity_you_must_specify_your_country_code), Toast.LENGTH_LONG).show();
return;
}
2013-03-26 04:26:03 +00:00
2017-11-08 20:20:11 +00:00
if (TextUtils.isEmpty(number.getText())) {
Toast.makeText(this, getString(R.string.RegistrationActivity_you_must_specify_your_phone_number), Toast.LENGTH_LONG).show();
return;
}
2013-03-26 04:26:03 +00:00
2017-11-08 20:20:11 +00:00
final String e164number = getConfiguredE164Number();
if (!PhoneNumberFormatter.isValidNumber(e164number)) {
Dialogs.showAlertDialog(this,
getString(R.string.RegistrationActivity_invalid_number),
String.format(getString(R.string.RegistrationActivity_the_number_you_specified_s_is_invalid), e164number));
2017-11-08 20:20:11 +00:00
return;
}
PlayServicesStatus gcmStatus = PlayServicesUtil.getPlayServicesStatus(this);
if (gcmStatus == PlayServicesStatus.SUCCESS) {
handleRequestVerification(e164number, true);
} else if (gcmStatus == PlayServicesStatus.MISSING) {
handlePromptForNoPlayServices(e164number);
} else if (gcmStatus == PlayServicesStatus.NEEDS_UPDATE) {
GoogleApiAvailability.getInstance().getErrorDialog(this, ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED, 0).show();
} else {
Dialogs.showAlertDialog(this, getString(R.string.RegistrationActivity_play_services_error),
getString(R.string.RegistrationActivity_google_play_services_is_updating_or_unavailable));
}
}
private void handleRequestVerification(@NonNull String e164number, boolean gcmSupported) {
createButton.setIndeterminateProgressMode(true);
createButton.setProgress(50);
if (gcmSupported) {
SmsRetrieverClient client = SmsRetriever.getClient(this);
Task<Void> task = client.startSmsRetriever();
task.addOnSuccessListener(none -> {
Log.i(TAG, "Successfully registered SMS listener.");
requestVerificationCode(e164number, true, true);
});
task.addOnFailureListener(e -> {
Log.w(TAG, "Failed to register SMS listener.", e);
requestVerificationCode(e164number, true, false);
});
} else {
requestVerificationCode(e164number, false, false);
}
}
@SuppressLint("StaticFieldLeak")
private void requestVerificationCode(@NonNull String e164number, boolean gcmSupported, boolean smsRetrieverSupported) {
2017-11-08 20:20:11 +00:00
new AsyncTask<Void, Void, Pair<String, Optional<String>>> () {
@Override
protected @Nullable Pair<String, Optional<String>> doInBackground(Void... voids) {
try {
markAsVerifying(true);
String password = Util.getSecret(18);
Optional<String> gcmToken;
if (gcmSupported) {
gcmToken = Optional.of(GoogleCloudMessaging.getInstance(RegistrationActivity.this).register(GcmRefreshJob.REGISTRATION_ID));
} else {
gcmToken = Optional.absent();
}
accountManager = AccountManagerFactory.createManager(RegistrationActivity.this, e164number, password);
accountManager.requestSmsVerificationCode(smsRetrieverSupported);
2017-11-08 20:20:11 +00:00
return new Pair<>(password, gcmToken);
} catch (IOException e) {
Log.w(TAG, "Error during account registration", e);
2017-11-08 20:20:11 +00:00
return null;
}
2013-03-26 04:26:03 +00:00
}
2017-11-08 20:20:11 +00:00
protected void onPostExecute(@Nullable Pair<String, Optional<String>> result) {
if (result == null) {
2017-11-25 06:00:30 +00:00
Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_unable_to_connect_to_service, Toast.LENGTH_LONG).show();
createButton.setIndeterminateProgressMode(false);
createButton.setProgress(0);
2017-11-08 20:20:11 +00:00
return;
}
2013-03-26 04:26:03 +00:00
2017-11-08 20:20:11 +00:00
registrationState = new RegistrationState(RegistrationState.State.VERIFYING, e164number, result.first, result.second);
displayVerificationView(e164number, 64);
2013-03-26 04:26:03 +00:00
}
2017-11-08 20:20:11 +00:00
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
2013-03-26 04:26:03 +00:00
private void handleVerificationCodeReceived(@Nullable String code) {
List<Integer> parsedCode = convertVerificationCodeToDigits(code);
2014-07-23 22:40:45 +00:00
for (int i = 0; i < parsedCode.size(); i++) {
int index = i;
verificationCodeView.postDelayed(() -> verificationCodeView.append(parsedCode.get(index)), i * 200);
}
}
private List<Integer> convertVerificationCodeToDigits(@Nullable String code) {
if (code == null || code.length() != 6 || registrationState.state != RegistrationState.State.VERIFYING) {
return Collections.emptyList();
}
List<Integer> result = new LinkedList<>();
try {
for (int i = 0; i < code.length(); i++) {
result.add(Integer.parseInt(Character.toString(code.charAt(i))));
2013-03-26 04:26:03 +00:00
}
} catch (NumberFormatException e) {
Log.w(TAG, "Failed to convert code into digits.",e );
return Collections.emptyList();
}
return result;
2017-11-08 20:20:11 +00:00
}
2013-03-26 04:26:03 +00:00
2017-11-08 20:20:11 +00:00
@SuppressLint("StaticFieldLeak")
@Override
public void onCodeComplete(@NonNull String code) {
this.registrationState = new RegistrationState(RegistrationState.State.CHECKING, this.registrationState);
callMeCountDownView.setVisibility(View.INVISIBLE);
keyboard.displayProgress();
2018-03-01 17:51:37 +00:00
new AsyncTask<Void, Void, Pair<Integer, Long>>() {
2017-11-08 20:20:11 +00:00
@Override
2018-03-01 17:51:37 +00:00
protected Pair<Integer, Long> doInBackground(Void... voids) {
2017-11-08 20:20:11 +00:00
try {
2018-03-01 17:51:37 +00:00
verifyAccount(code, null);
return new Pair<>(1, -1L);
} catch (LockedException e) {
Log.w(TAG, e);
return new Pair<>(2, e.getTimeRemaining());
2017-11-08 20:20:11 +00:00
} catch (IOException e) {
Log.w(TAG, e);
2018-03-01 17:51:37 +00:00
return new Pair<>(3, -1L);
2017-11-08 20:20:11 +00:00
}
}
@Override
2018-03-01 17:51:37 +00:00
protected void onPostExecute(Pair<Integer, Long> result) {
if (result.first == 1) {
2017-11-08 20:20:11 +00:00
keyboard.displaySuccess().addListener(new AssertedSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean result) {
2018-03-01 17:51:37 +00:00
handleSuccessfulRegistration();
}
});
} else if (result.first == 2) {
keyboard.displayLocked().addListener(new AssertedSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean r) {
registrationState = new RegistrationState(RegistrationState.State.PIN, registrationState);
displayPinView(code, result.second);
2017-11-08 20:20:11 +00:00
}
});
} else {
keyboard.displayFailure().addListener(new AssertedSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean result) {
registrationState = new RegistrationState(RegistrationState.State.VERIFYING, registrationState);
callMeCountDownView.setVisibility(View.VISIBLE);
verificationCodeView.clear();
keyboard.displayKeyboard();
}
});
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
2018-03-01 17:51:37 +00:00
@SuppressLint("StaticFieldLeak")
private void handleVerifyWithPinClicked(@NonNull String code, @Nullable String pin) {
if (TextUtils.isEmpty(pin) || TextUtils.isEmpty(pin.replace(" ", ""))) {
Toast.makeText(this, R.string.RegistrationActivity_you_must_enter_your_registration_lock_PIN, Toast.LENGTH_LONG).show();
return;
}
pinButton.setIndeterminateProgressMode(true);
pinButton.setProgress(50);
new AsyncTask<Void, Void, Integer>() {
@Override
protected Integer doInBackground(Void... voids) {
try {
verifyAccount(code, pin);
return 1;
} catch (LockedException e) {
Log.w(TAG, e);
return 2;
} catch (RateLimitException e) {
Log.w(TAG, e);
return 3;
} catch (IOException e) {
Log.w(TAG, e);
return 4;
}
}
@Override
protected void onPostExecute(Integer result) {
pinButton.setIndeterminateProgressMode(false);
pinButton.setProgress(0);
if (result == 1) {
TextSecurePreferences.setRegistrationLockPin(RegistrationActivity.this, pin);
TextSecurePreferences.setRegistrationtLockEnabled(RegistrationActivity.this, true);
TextSecurePreferences.setRegistrationLockLastReminderTime(RegistrationActivity.this, System.currentTimeMillis());
TextSecurePreferences.setRegistrationLockNextReminderInterval(RegistrationActivity.this, RegistrationLockReminders.INITIAL_INTERVAL);
2018-03-01 17:51:37 +00:00
handleSuccessfulRegistration();
} else if (result == 2) {
RegistrationActivity.this.pin.setText("");
Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_incorrect_registration_lock_pin, Toast.LENGTH_LONG).show();
} else if (result == 3) {
new AlertDialog.Builder(RegistrationActivity.this)
.setTitle(R.string.RegistrationActivity_too_many_attempts)
.setMessage(R.string.RegistrationActivity_you_have_made_too_many_incorrect_registration_lock_pin_attempts_please_try_again_in_a_day)
.setPositiveButton(android.R.string.ok, null)
.show();
} else if (result == 4) {
Toast.makeText(RegistrationActivity.this, R.string.RegistrationActivity_error_connecting_to_service, Toast.LENGTH_LONG).show();
}
}
}.execute();
}
private void handleForgottenPin(long timeRemaining) {
new AlertDialog.Builder(RegistrationActivity.this)
.setTitle(R.string.RegistrationActivity_oh_no)
.setMessage(getString(R.string.RegistrationActivity_registration_of_this_phone_number_will_be_possible_without_your_registration_lock_pin_after_seven_days_have_passed, (TimeUnit.MILLISECONDS.toDays(timeRemaining) + 1)))
.setPositiveButton(android.R.string.ok, null)
.show();
}
2017-11-08 20:20:11 +00:00
@SuppressLint("StaticFieldLeak")
private void handlePhoneCallRequest() {
if (registrationState.state == RegistrationState.State.VERIFYING) {
callMeCountDownView.startCountDown(300);
new AsyncTask<Void, Void, Void>() {
@Override
2017-11-08 20:20:11 +00:00
protected Void doInBackground(Void... voids) {
try {
2018-12-08 02:31:39 +00:00
accountManager.requestVoiceVerificationCode(Locale.getDefault());
2017-11-08 20:20:11 +00:00
} catch (IOException e) {
Log.w(TAG, e);
}
return null;
}
2017-11-08 20:20:11 +00:00
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
2017-11-08 20:20:11 +00:00
}
2018-03-01 17:51:37 +00:00
private void verifyAccount(@NonNull String code, @Nullable String pin) throws IOException {
2018-05-22 09:13:10 +00:00
int registrationId = KeyHelper.generateRegistrationId(false);
byte[] unidentifiedAccessKey = UnidentifiedAccessUtil.getSelfUnidentifiedAccessKey(RegistrationActivity.this);
boolean universalUnidentifiedAccess = TextSecurePreferences.isUniversalUnidentifiedAccess(RegistrationActivity.this);
2018-03-01 17:51:37 +00:00
TextSecurePreferences.setLocalRegistrationId(RegistrationActivity.this, registrationId);
SessionUtil.archiveAllSessions(RegistrationActivity.this);
String signalingKey = Util.getSecret(52);
2018-05-22 09:13:10 +00:00
accountManager.verifyAccountWithCode(code, signalingKey, registrationId, !registrationState.gcmToken.isPresent(), pin,
unidentifiedAccessKey, universalUnidentifiedAccess);
2018-03-01 17:51:37 +00:00
IdentityKeyPair identityKey = IdentityKeyUtil.getIdentityKeyPair(RegistrationActivity.this);
List<PreKeyRecord> records = PreKeyUtil.generatePreKeys(RegistrationActivity.this);
SignedPreKeyRecord signedPreKey = PreKeyUtil.generateSignedPreKey(RegistrationActivity.this, identityKey, true);
accountManager.setPreKeys(identityKey.getPublicKey(), signedPreKey, records);
if (registrationState.gcmToken.isPresent()) {
accountManager.setGcmId(registrationState.gcmToken);
}
TextSecurePreferences.setGcmRegistrationId(RegistrationActivity.this, registrationState.gcmToken.orNull());
TextSecurePreferences.setGcmDisabled(RegistrationActivity.this, !registrationState.gcmToken.isPresent());
TextSecurePreferences.setWebsocketRegistered(RegistrationActivity.this, true);
DatabaseFactory.getIdentityDatabase(RegistrationActivity.this)
.saveIdentity(Address.fromSerialized(registrationState.e164number),
identityKey.getPublicKey(), IdentityDatabase.VerifiedStatus.VERIFIED,
true, System.currentTimeMillis(), true);
TextSecurePreferences.setVerifying(RegistrationActivity.this, false);
TextSecurePreferences.setPushRegistered(RegistrationActivity.this, true);
TextSecurePreferences.setLocalNumber(RegistrationActivity.this, registrationState.e164number);
TextSecurePreferences.setPushServerPassword(RegistrationActivity.this, registrationState.password);
TextSecurePreferences.setSignalingKey(RegistrationActivity.this, signalingKey);
TextSecurePreferences.setSignedPreKeyRegistered(RegistrationActivity.this, true);
TextSecurePreferences.setPromptedPushRegistration(RegistrationActivity.this, true);
TextSecurePreferences.setUnauthorizedReceived(RegistrationActivity.this, false);
}
private void handleSuccessfulRegistration() {
ApplicationContext.getInstance(RegistrationActivity.this).getJobManager().add(new DirectoryRefreshJob(RegistrationActivity.this, false));
2018-05-22 09:13:10 +00:00
ApplicationContext.getInstance(RegistrationActivity.this).getJobManager().add(new RotateCertificateJob(RegistrationActivity.this));
2018-03-01 17:51:37 +00:00
DirectoryRefreshListener.schedule(RegistrationActivity.this);
RotateSignedPreKeyListener.schedule(RegistrationActivity.this);
Intent nextIntent = getIntent().getParcelableExtra("next_intent");
if (nextIntent == null) {
nextIntent = new Intent(RegistrationActivity.this, ConversationListActivity.class);
}
startActivity(nextIntent);
finish();
}
private void displayRestoreView(@NonNull BackupUtil.BackupInfo backup) {
2017-11-08 20:20:11 +00:00
title.animate().translationX(title.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
title.setText(R.string.RegistrationActivity_restore_from_backup);
2017-11-08 20:20:11 +00:00
title.clearAnimation();
title.setTranslationX(-1 * title.getWidth());
title.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
2017-11-08 20:20:11 +00:00
subtitle.animate().translationX(subtitle.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
subtitle.setText(R.string.RegistrationActivity_restore_your_messages_and_media_from_a_local_backup);
2017-11-08 20:20:11 +00:00
subtitle.clearAnimation();
subtitle.setTranslationX(-1 * subtitle.getWidth());
subtitle.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
2017-11-08 20:20:11 +00:00
}).start();
registrationContainer.animate().translationX(registrationContainer.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
registrationContainer.clearAnimation();
registrationContainer.setVisibility(View.INVISIBLE);
registrationContainer.setTranslationX(0);
restoreContainer.setTranslationX(-1 * registrationContainer.getWidth());
restoreContainer.setVisibility(View.VISIBLE);
restoreButton.setProgress(0);
restoreButton.setIndeterminateProgressMode(false);
restoreButton.setOnClickListener(v -> handleRestore(backup));
restoreBackupSize.setText(getString(R.string.RegistrationActivity_backup_size_s, Util.getPrettyFileSize(backup.getSize())));
restoreBackupTime.setText(getString(R.string.RegistrationActivity_backup_timestamp_s, DateUtils.getExtendedRelativeTimeSpanString(RegistrationActivity.this, Locale.US, backup.getTimestamp())));
restoreBackupProgress.setText("");
restoreContainer.animate().translationX(0).setDuration(SCENE_TRANSITION_DURATION).setListener(null).setInterpolator(new OvershootInterpolator()).start();
}
}).start();
fab.animate().rotationBy(375f).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
fab.clearAnimation();
fab.setImageResource(R.drawable.ic_restore_white_24dp);
fab.animate().rotationBy(360f).setDuration(SCENE_TRANSITION_DURATION).setListener(null).start();
}
}).start();
}
private void displayInitialView(boolean forwards) {
int startDirectionMultiplier = forwards ? -1 : 1;
int endDirectionMultiplier = forwards ? 1 : -1;
title.animate().translationX(startDirectionMultiplier * title.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
title.setText(R.string.registration_activity__verify_your_number);
title.clearAnimation();
title.setTranslationX(endDirectionMultiplier * title.getWidth());
title.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
subtitle.animate().translationX(startDirectionMultiplier * subtitle.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
subtitle.setText(R.string.registration_activity__please_enter_your_mobile_number_to_receive_a_verification_code_carrier_rates_may_apply);
subtitle.clearAnimation();
subtitle.setTranslationX(endDirectionMultiplier * subtitle.getWidth());
subtitle.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
View container;
if (verificationContainer.getVisibility() == View.VISIBLE) container = verificationContainer;
else container = restoreContainer;
container.animate().translationX(startDirectionMultiplier * container.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
2017-11-08 20:20:11 +00:00
@Override
public void onAnimationEnd(Animator animation) {
container.clearAnimation();
container.setVisibility(View.INVISIBLE);
container.setTranslationX(0);
2017-11-08 20:20:11 +00:00
registrationContainer.setTranslationX(endDirectionMultiplier * registrationContainer.getWidth());
2017-11-08 20:20:11 +00:00
registrationContainer.setVisibility(View.VISIBLE);
createButton.setProgress(0);
createButton.setIndeterminateProgressMode(false);
registrationContainer.animate().translationX(0).setDuration(SCENE_TRANSITION_DURATION).setListener(null).setInterpolator(new OvershootInterpolator()).start();
}
}).start();
fab.animate().rotationBy(startDirectionMultiplier * 360f).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
2017-11-08 20:20:11 +00:00
@Override
public void onAnimationEnd(Animator animation) {
fab.clearAnimation();
fab.setImageResource(R.drawable.ic_action_name);
fab.animate().rotationBy(startDirectionMultiplier * 375f).setDuration(SCENE_TRANSITION_DURATION).setListener(null).start();
2017-11-08 20:20:11 +00:00
}
}).start();
}
2017-11-08 20:20:11 +00:00
private void displayVerificationView(@NonNull String e164number, int callCountdown) {
ServiceUtil.getInputMethodManager(this)
.hideSoftInputFromWindow(countryCode.getWindowToken(), 0);
ServiceUtil.getInputMethodManager(this)
.hideSoftInputFromWindow(number.getWindowToken(), 0);
title.animate().translationX(-1 * title.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
title.setText(getString(R.string.RegistrationActivity_verify_s, e164number));
2017-11-08 20:20:11 +00:00
title.clearAnimation();
title.setTranslationX(title.getWidth());
title.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
subtitle.animate().translationX(-1 * subtitle.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
SpannableString subtitleDescription = new SpannableString(getString(R.string.RegistrationActivity_please_enter_the_verification_code_sent_to_s, e164number));
SpannableString wrongNumber = new SpannableString(getString(R.string.RegistrationActivity_wrong_number));
2017-11-08 20:20:11 +00:00
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
displayInitialView(false);
2017-11-08 20:20:11 +00:00
registrationState = new RegistrationState(RegistrationState.State.INITIAL, null, null, null);
}
2017-11-08 20:20:11 +00:00
@Override
public void updateDrawState(TextPaint paint) {
paint.setColor(Color.WHITE);
paint.setUnderlineText(true);
}
};
wrongNumber.setSpan(clickableSpan, 0, wrongNumber.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
subtitle.setText(new SpannableStringBuilder(subtitleDescription).append(" ").append(wrongNumber));
subtitle.setMovementMethod(LinkMovementMethod.getInstance());
subtitle.clearAnimation();
subtitle.setTranslationX(subtitle.getWidth());
subtitle.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
registrationContainer.animate().translationX(-1 * registrationContainer.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
registrationContainer.clearAnimation();
registrationContainer.setVisibility(View.INVISIBLE);
registrationContainer.setTranslationX(0);
verificationContainer.setTranslationX(verificationContainer.getWidth());
verificationContainer.setVisibility(View.VISIBLE);
verificationContainer.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
fab.animate().rotationBy(-360f).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
fab.clearAnimation();
fab.setImageResource(R.drawable.ic_textsms_24dp);
fab.animate().rotationBy(-375f).setDuration(SCENE_TRANSITION_DURATION).setListener(null).start();
}
2017-11-08 20:20:11 +00:00
}).start();
this.callMeCountDownView.startCountDown(callCountdown);
}
2018-03-01 17:51:37 +00:00
private void displayPinView(String code, long lockedUntil) {
title.animate().translationX(-1 * title.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
title.setText(R.string.RegistrationActivity_registration_lock_pin);
title.clearAnimation();
title.setTranslationX(title.getWidth());
title.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
subtitle.animate().translationX(-1 * subtitle.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
subtitle.setText(R.string.RegistrationActivity_this_phone_number_has_registration_lock_enabled_please_enter_the_registration_lock_pin);
subtitle.clearAnimation();
subtitle.setTranslationX(subtitle.getWidth());
subtitle.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
verificationContainer.animate().translationX(-1 * verificationContainer.getWidth()).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
verificationContainer.clearAnimation();
verificationContainer.setVisibility(View.INVISIBLE);
verificationContainer.setTranslationX(0);
pinContainer.setTranslationX(pinContainer.getWidth());
pinContainer.setVisibility(View.VISIBLE);
pinContainer.animate().translationX(0).setListener(null).setInterpolator(new OvershootInterpolator()).setDuration(SCENE_TRANSITION_DURATION).start();
}
}).start();
fab.animate().rotationBy(-360f).setDuration(SCENE_TRANSITION_DURATION).setListener(new AnimationCompleteListener() {
@Override
public void onAnimationEnd(Animator animation) {
fab.clearAnimation();
fab.setImageResource(R.drawable.ic_lock_white_24dp);
fab.animate().rotationBy(-360f).setDuration(SCENE_TRANSITION_DURATION).setListener(null).start();
}
}).start();
pinButton.setOnClickListener(v -> handleVerifyWithPinClicked(code, pin.getText().toString()));
pinForgotButton.setOnClickListener(v -> handleForgottenPin(lockedUntil));
pin.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (s != null && code.equals(s.toString())) pinClarificationContainer.setVisibility(View.VISIBLE);
else if (pinClarificationContainer.getVisibility() == View.VISIBLE) pinClarificationContainer.setVisibility(View.GONE);
}
});
}
2017-11-08 20:20:11 +00:00
private void handleCancel() {
TextSecurePreferences.setPromptedPushRegistration(RegistrationActivity.this, true);
Intent nextIntent = getIntent().getParcelableExtra("next_intent");
if (nextIntent == null) {
nextIntent = new Intent(RegistrationActivity.this, ConversationListActivity.class);
}
startActivity(nextIntent);
finish();
}
private void handlePromptForNoPlayServices(@NonNull String e164number) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(R.string.RegistrationActivity_missing_google_play_services);
dialog.setMessage(R.string.RegistrationActivity_this_device_is_missing_google_play_services);
dialog.setPositiveButton(R.string.RegistrationActivity_i_understand, (dialog1, which) -> handleRequestVerification(e164number, false));
dialog.setNegativeButton(android.R.string.cancel, null);
dialog.show();
}
private void initializeChallengeListener() {
smsRetrieverReceiver = new SmsRetrieverReceiver();
IntentFilter filter = new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION);
registerReceiver(smsRetrieverReceiver, filter);
2017-11-08 20:20:11 +00:00
}
private void shutdownChallengeListener() {
if (smsRetrieverReceiver != null) {
unregisterReceiver(smsRetrieverReceiver);
smsRetrieverReceiver = null;
2017-11-08 20:20:11 +00:00
}
}
private void markAsVerifying(boolean verifying) {
TextSecurePreferences.setVerifying(this, verifying);
if (verifying) {
TextSecurePreferences.setPushRegistered(this, false);
}
}
2018-05-31 11:55:11 +00:00
private void onTermsLinkClicked(View v) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://signal.org/legal"));
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, R.string.RegistrationActivity_no_browser, Toast.LENGTH_SHORT).show();
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(FullBackupBase.BackupEvent event) {
if (event.getCount() == 0) restoreBackupProgress.setText(R.string.RegistrationActivity_checking);
else restoreBackupProgress.setText(getString(R.string.RegistrationActivity_d_messages_so_far, event.getCount()));
}
private class SmsRetrieverReceiver extends BroadcastReceiver {
2017-11-08 20:20:11 +00:00
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "SmsRetrieverReceiver received a broadcast...");
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch (status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
Optional<String> code = VerificationCodeParser.parse(context, (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE));
if (code.isPresent()) {
Log.i(TAG, "Received verification code.");
handleVerificationCodeReceived(code.get());
} else {
Log.w(TAG, "Could not parse verification code.");
}
break;
case CommonStatusCodes.TIMEOUT:
Log.w(TAG, "Hit a timeout waiting for the SMS to arrive.");
break;
}
} else {
Log.w(TAG, "SmsRetrieverReceiver received the wrong action?");
}
}
2013-03-26 04:26:03 +00:00
}
private class CountryCodeChangedListener implements TextWatcher {
@Override
public void afterTextChanged(Editable s) {
if (TextUtils.isEmpty(s) || !TextUtils.isDigitsOnly(s)) {
2013-07-09 23:36:15 +00:00
setCountryDisplay(getString(R.string.RegistrationActivity_select_your_country));
2013-03-26 04:26:03 +00:00
countryFormatter = null;
return;
}
int countryCode = Integer.parseInt(s.toString());
String regionCode = PhoneNumberUtil.getInstance().getRegionCodeForCountryCode(countryCode);
2013-07-09 23:36:15 +00:00
2013-03-26 04:26:03 +00:00
setCountryFormatter(countryCode);
setCountryDisplay(PhoneNumberFormatter.getRegionDisplayName(regionCode));
2014-11-12 19:15:05 +00:00
if (!TextUtils.isEmpty(regionCode) && !regionCode.equals("ZZ")) {
2013-03-26 04:26:03 +00:00
number.requestFocus();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
private class NumberChangedListener implements TextWatcher {
@Override
public void afterTextChanged(Editable s) {
if (countryFormatter == null)
return;
2014-11-12 19:15:05 +00:00
if (TextUtils.isEmpty(s))
2013-03-26 04:26:03 +00:00
return;
countryFormatter.clear();
String number = s.toString().replaceAll("[^\\d.]", "");
String formattedNumber = null;
for (int i=0;i<number.length();i++) {
formattedNumber = countryFormatter.inputDigit(number.charAt(i));
}
if (formattedNumber != null && !s.toString().equals(formattedNumber)) {
2013-03-26 04:26:03 +00:00
s.replace(0, s.length(), formattedNumber);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
private class InformationToggleListener implements View.OnClickListener {
@Override
public void onClick(View v) {
if (informationView.getVisibility() == View.VISIBLE) {
informationView.setVisibility(View.GONE);
informationToggleText.setText(R.string.RegistrationActivity_more_information);
} else {
informationView.setVisibility(View.VISIBLE);
informationToggleText.setText(R.string.RegistrationActivity_less_information);
}
}
}
2017-11-08 20:20:11 +00:00
private static class RegistrationState {
private enum State {
2018-03-01 17:51:37 +00:00
INITIAL, VERIFYING, CHECKING, PIN
2017-11-08 20:20:11 +00:00
}
private final State state;
private final String e164number;
private final String password;
private final Optional<String> gcmToken;
RegistrationState(State state, String e164number, String password, Optional<String> gcmToken) {
this.state = state;
this.e164number = e164number;
this.password = password;
this.gcmToken = gcmToken;
}
RegistrationState(State state, RegistrationState previous) {
this.state = state;
this.e164number = previous.e164number;
this.password = previous.password;
this.gcmToken = previous.gcmToken;
}
}
private enum BackupImportResult {
SUCCESS, FAILURE_VERSION_DOWNGRADE, FAILURE_UNKNOWN
}
2013-03-26 04:26:03 +00:00
}