From 6025e5de288afe4a2f8767b252cbbd345c50da26 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 9 Jan 2020 20:10:19 +0100 Subject: [PATCH] make registration uris work with fixed usernames --- .../conversations/ui/MagicCreateActivity.java | 47 +++++++++++++------ .../conversations/utils/SignupUtils.java | 10 +++- src/conversations/res/layout/magic_create.xml | 2 + src/conversations/res/values/strings.xml | 2 + .../siacs/conversations/entities/Account.java | 1 + .../conversations/ui/EditAccountActivity.java | 12 ++++- .../conversations/ui/UriHandlerActivity.java | 13 +++-- 7 files changed, 61 insertions(+), 26 deletions(-) diff --git a/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java b/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java index bc9b06d5f..7c8bbeac4 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java +++ b/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java @@ -8,9 +8,6 @@ import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; import android.view.View; -import android.widget.Button; -import android.widget.EditText; -import android.widget.TextView; import android.widget.Toast; import java.security.SecureRandom; @@ -26,9 +23,11 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { public static final String EXTRA_DOMAIN = "domain"; public static final String EXTRA_PRE_AUTH = "pre_auth"; + public static final String EXTRA_USERNAME = "username"; private MagicCreateBinding binding; private String domain; + private String username; private String preAuth; @Override @@ -55,6 +54,7 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { final Intent data = getIntent(); this.domain = data == null ? null : data.getStringExtra(EXTRA_DOMAIN); this.preAuth = data == null ? null : data.getStringExtra(EXTRA_PRE_AUTH); + this.username = data == null ? null : data.getStringExtra(EXTRA_USERNAME); if (getResources().getBoolean(R.bool.portrait_only)) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } @@ -62,7 +62,14 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { this.binding = DataBindingUtil.setContentView(this, R.layout.magic_create); setSupportActionBar((Toolbar) this.binding.toolbar); configureActionBar(getSupportActionBar(), this.domain == null); - if (domain != null) { + if (username != null && domain != null) { + binding.title.setText(R.string.your_server_invitation); + binding.instructions.setText(getString(R.string.magic_create_text_fixed, domain)); + binding.finePrint.setVisibility(View.INVISIBLE); + binding.username.setEnabled(false); + binding.username.setText(this.username); + updateFullJidInformation(this.username); + } else if (domain != null) { binding.instructions.setText(getString(R.string.magic_create_text_on_x, domain)); binding.finePrint.setVisibility(View.INVISIBLE); } @@ -70,12 +77,18 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { try { final String username = binding.username.getText().toString(); final Jid jid; - if (this.domain == null) { - jid = Jid.ofLocalAndDomain(username, Config.MAGIC_CREATE_DOMAIN); - } else { + final boolean fixedUsername; + if (this.domain != null && this.username != null) { + fixedUsername = true; + jid = Jid.ofLocalAndDomain(this.username, this.domain); + } else if (this.domain != null) { + fixedUsername = false; jid = Jid.ofLocalAndDomain(username, this.domain); + } else { + fixedUsername = false; + jid = Jid.ofLocalAndDomain(username, Config.MAGIC_CREATE_DOMAIN); } - if (!jid.getEscapedLocal().equals(jid.getLocal()) || username.length() < 3) { + if (!jid.getEscapedLocal().equals(jid.getLocal()) || (this.username == null && username.length() < 3)) { binding.username.setError(getString(R.string.invalid_username)); binding.username.requestFocus(); } else { @@ -86,6 +99,7 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { account.setOption(Account.OPTION_REGISTER, true); account.setOption(Account.OPTION_DISABLED, true); account.setOption(Account.OPTION_MAGIC_CREATE, true); + account.setOption(Account.OPTION_FIXED_USERNAME, fixedUsername); if (this.preAuth != null) { account.setKey(Account.PRE_AUTH_REGISTRATION_TOKEN, this.preAuth); } @@ -118,23 +132,26 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { } @Override - public void afterTextChanged(Editable s) { - if (s.toString().trim().length() > 0) { + public void afterTextChanged(final Editable s) { + updateFullJidInformation(s.toString()); + } + + private void updateFullJidInformation(final String username) { + if (username.trim().isEmpty()) { + binding.fullJid.setVisibility(View.INVISIBLE); + } else { try { binding.fullJid.setVisibility(View.VISIBLE); final Jid jid; if (this.domain == null) { - jid = Jid.ofLocalAndDomain(s.toString(), Config.MAGIC_CREATE_DOMAIN); + jid = Jid.ofLocalAndDomain(username, Config.MAGIC_CREATE_DOMAIN); } else { - jid = Jid.ofLocalAndDomain(s.toString(), this.domain); + jid = Jid.ofLocalAndDomain(username, this.domain); } binding.fullJid.setText(getString(R.string.your_full_jid_will_be, jid.toEscapedString())); } catch (IllegalArgumentException e) { binding.fullJid.setVisibility(View.INVISIBLE); } - - } else { - binding.fullJid.setVisibility(View.INVISIBLE); } } } diff --git a/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java b/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java index e4fc28393..a47dfbca8 100644 --- a/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java +++ b/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java @@ -13,6 +13,7 @@ import eu.siacs.conversations.ui.ManageAccountActivity; import eu.siacs.conversations.ui.PickServerActivity; import eu.siacs.conversations.ui.StartConversationActivity; import eu.siacs.conversations.ui.WelcomeActivity; +import rocks.xmpp.addr.Jid; public class SignupUtils { @@ -20,9 +21,14 @@ public class SignupUtils { return true; } - public static Intent getTokenRegistrationIntent(final Activity activity, String domain, String preAuth) { + public static Intent getTokenRegistrationIntent(final Activity activity, Jid jid, String preAuth) { final Intent intent = new Intent(activity, MagicCreateActivity.class); - intent.putExtra(MagicCreateActivity.EXTRA_DOMAIN, domain); + if (jid.isDomainJid()) { + intent.putExtra(MagicCreateActivity.EXTRA_DOMAIN, jid.getDomain()); + } else { + intent.putExtra(MagicCreateActivity.EXTRA_DOMAIN, jid.getDomain()); + intent.putExtra(MagicCreateActivity.EXTRA_USERNAME, jid.getEscapedLocal()); + } intent.putExtra(MagicCreateActivity.EXTRA_PRE_AUTH, preAuth); return intent; } diff --git a/src/conversations/res/layout/magic_create.xml b/src/conversations/res/layout/magic_create.xml index 51ef104ea..cc0337062 100644 --- a/src/conversations/res/layout/magic_create.xml +++ b/src/conversations/res/layout/magic_create.xml @@ -38,6 +38,7 @@ android:layout_weight="1" /> Do you already have an XMPP account? This might be the case if you are already using a different XMPP client or have used Conversations before. If not you can create a new XMPP account right now.\nHint: Some email providers also provide XMPP accounts. XMPP is a provider independent instant messaging network. You can use this client with what ever XMPP server you choose.\nHowever for your convenience we made it easy to create an account on conversations.imĀ¹; a provider specially suited for the use with Conversations. You have been invited to %1$s. We will guide you through the process of creating an account.\nWhen picking %1$s as a provider you will be able to communicate with users of other providers by giving them your full XMPP address. + You have been invited to %1$s. A username has already been picked for you. We will guide you through the process of creating an account.\nYou will be able to communicate with users of other providers by giving them your full XMPP address. + Your server invitation \ No newline at end of file diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 82a8126a7..fdc5634da 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -61,6 +61,7 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable public static final int OPTION_LOGGED_IN_SUCCESSFULLY = 6; public static final int OPTION_HTTP_UPLOAD_AVAILABLE = 7; public static final int OPTION_UNVERIFIED = 8; + public static final int OPTION_FIXED_USERNAME = 9; private static final String KEY_PGP_SIGNATURE = "pgp_signature"; private static final String KEY_PGP_ID = "pgp_id"; public final HashSet> inProgressDiscoFetches = new HashSet<>(); diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index 9edd0293d..c2bdd05de 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -407,7 +407,14 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat final Jid jid = mAccount == null ? null : mAccount.getJid(); if (SignupUtils.isSupportTokenRegistry() && jid != null && magicCreate && !jid.getDomain().equals(Config.MAGIC_CREATE_DOMAIN)) { - final Intent intent = SignupUtils.getTokenRegistrationIntent(this, jid.getDomain(), mAccount.getKey(Account.PRE_AUTH_REGISTRATION_TOKEN)); + final Jid preset; + if (mAccount.isOptionSet(Account.OPTION_FIXED_USERNAME)) { + preset = jid.asBareJid(); + } else { + preset = Jid.ofDomain(jid.getDomain()); + } + final Intent intent = SignupUtils.getTokenRegistrationIntent(this, preset, mAccount.getKey(Account.PRE_AUTH_REGISTRATION_TOKEN)); + StartConversationActivity.addInviteUri(intent, getIntent()); startActivity(intent); return; } @@ -415,6 +422,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat if (xmppConnectionService.getAccounts().size() == 0 && Config.MAGIC_CREATE_DOMAIN != null) { Intent intent = SignupUtils.getSignUpIntent(this, mForceRegister != null && mForceRegister); + StartConversationActivity.addInviteUri(intent, getIntent()); startActivity(intent); } } @@ -972,7 +980,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat } - final boolean editable = !mAccount.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY) && QuickConversationsService.isConversations(); + final boolean editable = !mAccount.isOptionSet(Account.OPTION_LOGGED_IN_SUCCESSFULLY) && !mAccount.isOptionSet(Account.OPTION_FIXED_USERNAME) && QuickConversationsService.isConversations(); this.binding.accountJid.setEnabled(editable); this.binding.accountJid.setFocusable(editable); this.binding.accountJid.setFocusableInTouchMode(editable); diff --git a/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java b/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java index 67736c90c..621ca629b 100644 --- a/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java @@ -28,7 +28,7 @@ public class UriHandlerActivity extends AppCompatActivity { public static final String ACTION_SCAN_QR_CODE = "scan_qr_code"; private static final int REQUEST_SCAN_QR_CODE = 0x1234; private static final int REQUEST_CAMERA_PERMISSIONS_TO_SCAN = 0x6789; - + private static final Pattern VCARD_XMPP_PATTERN = Pattern.compile("\nIMPP([^:]*):(xmpp:.+)\n"); private boolean handled = false; public static void scan(Activity activity) { @@ -93,15 +93,16 @@ public class UriHandlerActivity extends AppCompatActivity { final String preauth = xmppUri.getParamater("preauth"); final Jid jid = xmppUri.getJid(); if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) { - if (jid.isDomainJid()) { - intent = SignupUtils.getTokenRegistrationIntent(this, jid.getDomain(), preauth); - startActivity(intent); + if (jid.getEscapedLocal() != null && accounts.contains(jid.asBareJid())) { + Toast.makeText(this, R.string.account_already_exists, Toast.LENGTH_LONG).show(); return; } + intent = SignupUtils.getTokenRegistrationIntent(this, jid, preauth); + startActivity(intent); return; } if (xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParamater("ibr"))) { - intent = SignupUtils.getTokenRegistrationIntent(this, jid.getDomain(), preauth); + intent = SignupUtils.getTokenRegistrationIntent(this, Jid.ofDomain(jid.getDomain()), preauth); intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString()); startActivity(intent); return; @@ -195,8 +196,6 @@ public class UriHandlerActivity extends AppCompatActivity { finish(); } - private static final Pattern VCARD_XMPP_PATTERN = Pattern.compile("\nIMPP([^:]*):(xmpp:.+)\n"); - @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, requestCode, intent);