From 584cf43b3d6753b4c80c5d72c70b659370e55954 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 22 Oct 2018 22:54:05 +0200 Subject: [PATCH] added all callbacks to sign up activities --- src/main/res/values/strings.xml | 12 ++- .../services/QuickConversationsService.java | 19 ++++- .../ui/EnterPhoneNumberActivity.java | 9 +- .../conversations/ui/VerifyActivity.java | 83 ++++++++++++++----- .../ui/util/ApiErrorDialogHelper.java | 47 +++++++++++ .../res/layout/activity_enter_number.xml | 9 +- 6 files changed, 151 insertions(+), 28 deletions(-) create mode 100644 src/quick/java/eu/siacs/conversations/ui/util/ApiErrorDialogHelper.java diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index b006e3478..44bbb6a09 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -761,10 +761,11 @@ Search countries Verify %s %s.]]> - We have sent you the SMS again. + We have sent you another SMS with a 6 digit code. Please enter the 6 digit pin below. Resend SMS Resend SMS (%s) + Please wait (%s) back Automatically pasted possible pin from clipboard. Please enter your 6 digit pin. @@ -773,4 +774,13 @@ No Verifying… Requesting SMS… + The pin you have entered is incorrect. + Unknown network error. + Unknown response from server. + Unable to connect to server. + Unable to establish secure connection. + Unable to find server. + Invalid user input + Temporarily unavailable. Try again later. + No network connection. diff --git a/src/quick/java/eu/siacs/conversations/services/QuickConversationsService.java b/src/quick/java/eu/siacs/conversations/services/QuickConversationsService.java index 1ce9a78fb..e625a8e60 100644 --- a/src/quick/java/eu/siacs/conversations/services/QuickConversationsService.java +++ b/src/quick/java/eu/siacs/conversations/services/QuickConversationsService.java @@ -18,6 +18,8 @@ import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.atomic.AtomicBoolean; +import javax.net.ssl.SSLHandshakeException; + import eu.siacs.conversations.Config; import eu.siacs.conversations.crypto.sasl.Plain; import eu.siacs.conversations.entities.Account; @@ -33,8 +35,10 @@ public class QuickConversationsService { public static final int API_ERROR_OTHER = -1; public static final int API_ERROR_UNKNOWN_HOST = -2; public static final int API_ERROR_CONNECT = -3; + public static final int API_ERROR_SSL_HANDSHAKE = -4; + public static final int API_ERROR_AIRPLANE_MODE = -5; - private static final String BASE_URL = "https://venus.fritz.box:4567"; + private static final String BASE_URL = "http://venus.fritz.box:4567"; private final XmppConnectionService service; @@ -145,7 +149,7 @@ public class QuickConversationsService { } } - public void verify(Account account, String pin) { + public void verify(final Account account, String pin) { /** * POST /password * authentication gesetzt mit telephone nummber und verification code @@ -177,6 +181,9 @@ public class QuickConversationsService { connection.connect(); final int code = connection.getResponseCode(); if (code == 200) { + account.setOption(Account.OPTION_UNVERIFIED, false); + account.setOption(Account.OPTION_DISABLED, false); + service.updateAccount(account); synchronized (mOnVerification) { for (OnVerification onVerification : mOnVerification) { onVerification.onVerificationSucceeded(); @@ -210,11 +217,15 @@ public class QuickConversationsService { } } - private static int getApiErrorCode(Exception e) { - if (e instanceof UnknownHostException) { + private int getApiErrorCode(Exception e) { + if (!service.hasInternetConnection()) { + return API_ERROR_AIRPLANE_MODE; + } else if (e instanceof UnknownHostException) { return API_ERROR_UNKNOWN_HOST; } else if (e instanceof ConnectException) { return API_ERROR_CONNECT; + } else if (e instanceof SSLHandshakeException) { + return API_ERROR_SSL_HANDSHAKE; } else { Log.d(Config.LOGTAG,e.getClass().getName()); return API_ERROR_OTHER; diff --git a/src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java b/src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java index 24670a31b..a98059372 100644 --- a/src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java +++ b/src/quick/java/eu/siacs/conversations/ui/EnterPhoneNumberActivity.java @@ -20,6 +20,7 @@ import eu.siacs.conversations.R; import eu.siacs.conversations.databinding.ActivityEnterNumberBinding; import eu.siacs.conversations.services.QuickConversationsService; import eu.siacs.conversations.ui.drawable.TextDrawable; +import eu.siacs.conversations.ui.util.ApiErrorDialogHelper; import eu.siacs.conversations.utils.PhoneNumberUtilWrapper; import io.michaelrocks.libphonenumber.android.NumberParseException; import io.michaelrocks.libphonenumber.android.PhoneNumberUtil; @@ -32,8 +33,6 @@ public class EnterPhoneNumberActivity extends XmppActivity implements QuickConve private ActivityEnterNumberBinding binding; private String region = null; - private boolean requestingVerification = false; - private final TextWatcher countryCodeTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { @@ -65,6 +64,7 @@ public class EnterPhoneNumberActivity extends XmppActivity implements QuickConve } } }; + private boolean requestingVerification = false; @Override protected void refreshUiReal() { @@ -159,6 +159,8 @@ public class EnterPhoneNumberActivity extends XmppActivity implements QuickConve this.binding.number.setEnabled(!requesting); this.binding.next.setEnabled(!requesting); this.binding.next.setText(requesting ? R.string.requesting_sms : R.string.next); + this.binding.progressBar.setVisibility(requesting ? View.VISIBLE : View.GONE); + this.binding.progressBar.setIndeterminate(requesting); } @Override @@ -176,8 +178,9 @@ public class EnterPhoneNumberActivity extends XmppActivity implements QuickConve @Override public void onVerificationRequestFailed(int code) { - runOnUiThread(()->{ + runOnUiThread(() -> { setRequestingVerificationState(false); + ApiErrorDialogHelper.create(this, code).show(); }); } diff --git a/src/quick/java/eu/siacs/conversations/ui/VerifyActivity.java b/src/quick/java/eu/siacs/conversations/ui/VerifyActivity.java index edecb52ae..e6718d809 100644 --- a/src/quick/java/eu/siacs/conversations/ui/VerifyActivity.java +++ b/src/quick/java/eu/siacs/conversations/ui/VerifyActivity.java @@ -13,14 +13,13 @@ import android.os.SystemClock; import android.support.design.widget.Snackbar; import android.support.v7.widget.Toolbar; import android.text.Html; -import android.util.Log; import android.view.View; -import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.databinding.ActivityVerifyBinding; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.services.QuickConversationsService; +import eu.siacs.conversations.ui.util.ApiErrorDialogHelper; import eu.siacs.conversations.ui.util.PinEntryWrapper; import eu.siacs.conversations.utils.AccountUtils; import eu.siacs.conversations.utils.PhoneNumberUtilWrapper; @@ -32,7 +31,8 @@ import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN; public class VerifyActivity extends XmppActivity implements ClipboardManager.OnPrimaryClipChangedListener, QuickConversationsService.OnVerification, QuickConversationsService.OnVerificationRequested { public static final String EXTRA_RETRY_SMS_AFTER = "retry_sms_after"; - + private static final String EXTRA_RETRY_VERIFICATION_AFTER = "retry_verification_after"; + private final Handler mHandler = new Handler(); private ActivityVerifyBinding binding; private Account account; private PinEntryWrapper pinEntryWrapper; @@ -41,15 +41,20 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP private boolean verifying = false; private boolean requestingVerification = false; private long retrySmsAfter = 0; - - private final Handler mHandler = new Handler(); - - private final Runnable SMS_TIMEOUT_UPDATER = new Runnable() { @Override public void run() { if (setTimeoutLabelInResendButton()) { - mHandler.postDelayed(this,300); + 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); } } }; @@ -59,7 +64,7 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP long remaining = retrySmsAfter - SystemClock.elapsedRealtime(); if (remaining >= 0) { binding.resendSms.setEnabled(false); - binding.resendSms.setText(getString(R.string.resend_sms_in, TimeframeUtils.resolve(VerifyActivity.this,remaining))); + binding.resendSms.setText(getString(R.string.resend_sms_in, TimeframeUtils.resolve(VerifyActivity.this, remaining))); return true; } } @@ -68,6 +73,20 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP return false; } + 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; + } + @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -75,7 +94,8 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP boolean verifying = savedInstanceState != null && savedInstanceState.getBoolean("verifying"); boolean requestingVerification = savedInstanceState != null && savedInstanceState.getBoolean("requesting_verification", false); this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null; - this.retrySmsAfter = savedInstanceState != null ? savedInstanceState.getLong(EXTRA_RETRY_SMS_AFTER,0L) : 0L; + this.retrySmsAfter = savedInstanceState != null ? savedInstanceState.getLong(EXTRA_RETRY_SMS_AFTER, 0L) : 0L; + this.retryVerificationAfter = savedInstanceState != null ? savedInstanceState.getLong(EXTRA_RETRY_VERIFICATION_AFTER, 0L) : 0L; this.binding = DataBindingUtil.setContentView(this, R.layout.activity_verify); setSupportActionBar((Toolbar) this.binding.toolbar); this.pinEntryWrapper = new PinEntryWrapper(binding.pinBox); @@ -182,6 +202,7 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP savedInstanceState.putBoolean("verifying", this.verifying); savedInstanceState.putBoolean("requesting_verification", this.requestingVerification); savedInstanceState.putLong(EXTRA_RETRY_SMS_AFTER, this.retrySmsAfter); + savedInstanceState.putLong(EXTRA_RETRY_VERIFICATION_AFTER, this.retryVerificationAfter); if (this.pasted != null) { savedInstanceState.putString("pasted", this.pasted); } @@ -192,15 +213,21 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP public void onStart() { super.onStart(); clipboardManager.addPrimaryClipChangedListener(this); + final Intent intent = getIntent(); + this.retrySmsAfter = intent != null ? intent.getLongExtra(EXTRA_RETRY_SMS_AFTER, this.retrySmsAfter) : this.retrySmsAfter; if (this.retrySmsAfter > 0) { mHandler.post(SMS_TIMEOUT_UPDATER); } + if (this.retryVerificationAfter > 0) { + mHandler.post(VERIFICATION_TIMEOUT_UPDATER); + } } @Override public void onStop() { super.onStop(); mHandler.removeCallbacks(SMS_TIMEOUT_UPDATER); + mHandler.removeCallbacks(VERIFICATION_TIMEOUT_UPDATER); clipboardManager.removePrimaryClipChangedListener(this); if (xmppConnectionService != null) { xmppConnectionService.getQuickConversationsService().removeOnVerificationListener(this); @@ -233,6 +260,14 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP } } + 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(); + } + @Override public void onPrimaryClipChanged() { this.pasted = null; @@ -242,39 +277,49 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP } @Override - public void onVerificationFailed(int code) { + public void onVerificationFailed(final int code) { runOnUiThread(() -> { setVerifyingState(false); + 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(); + } }); - Log.d(Config.LOGTAG,"code="+code); } @Override public void onVerificationSucceeded() { - + runOnUiThread(this::performPostVerificationRedirect); } @Override public void onVerificationRetryAt(long timestamp) { - + this.retryVerificationAfter = timestamp; + runOnUiThread(() -> setVerifyingState(false)); + mHandler.removeCallbacks(VERIFICATION_TIMEOUT_UPDATER); + runOnUiThread(VERIFICATION_TIMEOUT_UPDATER); } //send sms again button callback @Override public void onVerificationRequestFailed(int code) { - runOnUiThread(()->{ + runOnUiThread(() -> { setRequestingVerificationState(false); + ApiErrorDialogHelper.create(this, code).show(); }); - Log.d(Config.LOGTAG,"code="+code); } //send sms again button callback @Override public void onVerificationRequested() { - runOnUiThread(()-> { + runOnUiThread(() -> { setRequestingVerificationState(false); AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage(R.string.we_have_sent_you_the_sms_again); + builder.setMessage(R.string.we_have_sent_you_another_sms); builder.setPositiveButton(R.string.ok, null); builder.create().show(); }); @@ -283,7 +328,7 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP @Override public void onVerificationRequestedRetryAt(long timestamp) { this.retrySmsAfter = timestamp; - runOnUiThread(()-> setRequestingVerificationState(false)); + runOnUiThread(() -> setRequestingVerificationState(false)); mHandler.removeCallbacks(SMS_TIMEOUT_UPDATER); runOnUiThread(SMS_TIMEOUT_UPDATER); } diff --git a/src/quick/java/eu/siacs/conversations/ui/util/ApiErrorDialogHelper.java b/src/quick/java/eu/siacs/conversations/ui/util/ApiErrorDialogHelper.java new file mode 100644 index 000000000..e22551570 --- /dev/null +++ b/src/quick/java/eu/siacs/conversations/ui/util/ApiErrorDialogHelper.java @@ -0,0 +1,47 @@ +package eu.siacs.conversations.ui.util; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.Context; +import android.support.annotation.StringRes; + +import eu.siacs.conversations.R; +import eu.siacs.conversations.services.QuickConversationsService; + +public class ApiErrorDialogHelper { + + public static Dialog create(Context context, int code) { + @StringRes final int res; + switch (code) { + case QuickConversationsService.API_ERROR_AIRPLANE_MODE: + res = R.string.no_network_connection; + break; + case QuickConversationsService.API_ERROR_OTHER: + res = R.string.unknown_api_error_network; + break; + case QuickConversationsService.API_ERROR_CONNECT: + res = R.string.unable_to_connect_to_server; + break; + case QuickConversationsService.API_ERROR_SSL_HANDSHAKE: + res = R.string.unable_to_establish_secure_connection; + break; + case QuickConversationsService.API_ERROR_UNKNOWN_HOST: + res = R.string.unable_to_find_server; + break; + case 400: + res = R.string.invalid_user_input; + break; + case 502: + case 503: + case 504: + res = R.string.temporarily_unavailable; + break; + default: + res = R.string.unknown_api_error_response; + } + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setMessage(res); + builder.setPositiveButton(R.string.ok, null); + return builder.create(); + } +} diff --git a/src/quick/res/layout/activity_enter_number.xml b/src/quick/res/layout/activity_enter_number.xml index aba47feb0..78d183dd4 100644 --- a/src/quick/res/layout/activity_enter_number.xml +++ b/src/quick/res/layout/activity_enter_number.xml @@ -33,7 +33,7 @@ + +