diff options
author | Christian Schneppe <christian.schneppe@pix-art.de> | 2020-01-24 19:43:49 +0100 |
---|---|---|
committer | Christian Schneppe <christian.schneppe@pix-art.de> | 2020-01-24 19:43:49 +0100 |
commit | 04595db099f2c1107fc58420a1e8fa5ef207195d (patch) | |
tree | 287d64ddf16c7dc2b9a61acaafbda66d56226b19 /src/main/java/de/pixart/messenger | |
parent | 504e4f21a270e21fe3ae4e479998c24bf182626a (diff) |
parse install referrer from gplay
Diffstat (limited to '')
12 files changed, 148 insertions, 27 deletions
diff --git a/src/main/java/de/pixart/messenger/parser/PresenceParser.java b/src/main/java/de/pixart/messenger/parser/PresenceParser.java index a2f97149b..0006b66a3 100644 --- a/src/main/java/de/pixart/messenger/parser/PresenceParser.java +++ b/src/main/java/de/pixart/messenger/parser/PresenceParser.java @@ -191,7 +191,7 @@ public class PresenceParser extends AbstractParser implements final Jid alternate; if (gone != null) { final XmppUri xmppUri = new XmppUri(gone); - if (xmppUri.isJidValid()) { + if (xmppUri.isValidJid()) { alternate = xmppUri.getJid(); } else { alternate = null; diff --git a/src/main/java/de/pixart/messenger/services/InstallReferrerService.java b/src/main/java/de/pixart/messenger/services/InstallReferrerService.java new file mode 100644 index 000000000..d6e267820 --- /dev/null +++ b/src/main/java/de/pixart/messenger/services/InstallReferrerService.java @@ -0,0 +1,39 @@ +package de.pixart.messenger.services; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.util.Log; + +import java.net.URLDecoder; + +import de.pixart.messenger.Config; +import de.pixart.messenger.ui.StartConversationActivity; +import de.pixart.messenger.utils.SignupUtils; + +public class InstallReferrerService extends BroadcastReceiver { + + public static final String INSTALL_REFERRER_BROADCAST_ACTION = "eu.siacs.conversations.install_referrer"; + + @Override + public void onReceive(final Context context, final Intent intent) { + final String referrer = intent == null ? null : intent.getStringExtra("referrer"); + if (referrer == null) { + Log.d(Config.LOGTAG, "received empty referrer"); + return; + } + try { + final String decoded = URLDecoder.decode(referrer, "UTF-8"); + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + preferences.edit().putString(SignupUtils.INSTALL_REFERRER, decoded).apply(); + Log.d(Config.LOGTAG, "stored referrer: " + decoded); + final Intent broadcastIntent = new Intent(INSTALL_REFERRER_BROADCAST_ACTION); + broadcastIntent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, decoded); + context.sendBroadcast(broadcastIntent); + } catch (final Exception e) { + Log.d(Config.LOGTAG, "unable to process referrer", e); + } + } +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java b/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java index 249b5e1a8..2e8c828c4 100644 --- a/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ChooseContactActivity.java @@ -347,7 +347,7 @@ public class ChooseContactActivity extends AbstractSearchableListItemActivity im if (activityResult.resultCode == RESULT_OK && activityResult.requestCode == ScanActivity.REQUEST_SCAN_QR_CODE) { String result = activityResult.data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT); XmppUri uri = new XmppUri(result == null ? "" : result); - if (uri.isJidValid()) { + if (uri.isValidJid()) { showEnterJidDialog(uri); } } diff --git a/src/main/java/de/pixart/messenger/ui/ConversationsActivity.java b/src/main/java/de/pixart/messenger/ui/ConversationsActivity.java index 26e44a0f2..43ed14a13 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationsActivity.java @@ -539,7 +539,7 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio public boolean onXmppUriClicked(Uri uri) { XmppUri xmppUri = new XmppUri(uri); - if (xmppUri.isJidValid() && !xmppUri.hasFingerprints()) { + if (xmppUri.isValidJid() && !xmppUri.hasFingerprints()) { final Conversation conversation = xmppConnectionService.findUniqueConversationByJid(xmppUri); if (conversation != null) { openConversation(conversation, null); diff --git a/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java b/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java index f3f294828..096b7527e 100644 --- a/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java +++ b/src/main/java/de/pixart/messenger/ui/MagicCreateActivity.java @@ -1,10 +1,13 @@ package de.pixart.messenger.ui; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.os.Bundle; +import android.preference.PreferenceManager; import android.text.Editable; import android.text.TextWatcher; +import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; @@ -24,6 +27,7 @@ import de.pixart.messenger.R; import de.pixart.messenger.databinding.ActivityMagicCreateBinding; import de.pixart.messenger.entities.Account; import de.pixart.messenger.utils.CryptoHelper; +import de.pixart.messenger.utils.SignupUtils; import rocks.xmpp.addr.Jid; public class MagicCreateActivity extends XmppActivity implements TextWatcher, AdapterView.OnItemSelectedListener, CompoundButton.OnCheckedChangeListener { @@ -211,6 +215,14 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher, Ad } @Override + public void onDestroy() { + Log.d(Config.LOGTAG, "purge install referrer"); + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + preferences.edit().remove(SignupUtils.INSTALL_REFERRER).apply(); + super.onDestroy(); + } + + @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (binding.useOwn.isChecked()) { binding.server.setEnabled(false); diff --git a/src/main/java/de/pixart/messenger/ui/StartConversationActivity.java b/src/main/java/de/pixart/messenger/ui/StartConversationActivity.java index a83c7ea11..c85a7e8bb 100644 --- a/src/main/java/de/pixart/messenger/ui/StartConversationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/StartConversationActivity.java @@ -796,7 +796,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne if (inviteUri != null) { final Invite invite = new Invite(inviteUri); invite.account = intent.getStringExtra(EXTRA_ACCOUNT); - if (invite.isJidValid()) { + if (invite.isValidJid()) { return invite.invite(); } } @@ -991,7 +991,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne conferenceJid = Jid.of(input); } catch (final IllegalArgumentException e) { final XmppUri xmppUri = new XmppUri(input); - if (xmppUri.isJidValid() && xmppUri.isAction(XmppUri.ACTION_JOIN)) { + if (xmppUri.isValidJid() && xmppUri.isAction(XmppUri.ACTION_JOIN)) { final Editable editable = jid.getEditableText(); editable.clear(); editable.append(xmppUri.getJid().toEscapedString()); @@ -1253,7 +1253,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne } boolean invite() { - if (!isJidValid()) { + if (!isValidJid()) { Toast.makeText(StartConversationActivity.this, R.string.invalid_jid, Toast.LENGTH_SHORT).show(); return false; } diff --git a/src/main/java/de/pixart/messenger/ui/UriHandlerActivity.java b/src/main/java/de/pixart/messenger/ui/UriHandlerActivity.java index 59a5665f1..0ab443a81 100644 --- a/src/main/java/de/pixart/messenger/ui/UriHandlerActivity.java +++ b/src/main/java/de/pixart/messenger/ui/UriHandlerActivity.java @@ -87,7 +87,7 @@ public class UriHandlerActivity extends AppCompatActivity { final Intent intent; final XmppUri xmppUri = new XmppUri(uri); final List<Jid> accounts = DatabaseBackend.getInstance(this).getAccountJids(true); - if (SignupUtils.isSupportTokenRegistry() && xmppUri.isJidValid()) { + if (SignupUtils.isSupportTokenRegistry() && xmppUri.isValidJid()) { final String preauth = xmppUri.getParamater("preauth"); final Jid jid = xmppUri.getJid(); if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) { @@ -107,7 +107,7 @@ public class UriHandlerActivity extends AppCompatActivity { } } if (accounts.size() == 0) { - if (xmppUri.isJidValid()) { + if (xmppUri.isValidJid()) { intent = SignupUtils.getSignUpIntent(this); intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString()); startActivity(intent); @@ -148,7 +148,7 @@ public class UriHandlerActivity extends AppCompatActivity { intent.putExtra("jid", xmppUri.getJid().asBareJid().toString()); intent.setData(uri); intent.putExtra("scanned", scanned); - } else if (xmppUri.isJidValid()) { + } else if (xmppUri.isValidJid()) { intent = new Intent(getApplicationContext(), StartConversationActivity.class); intent.setAction(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); diff --git a/src/main/java/de/pixart/messenger/ui/WelcomeActivity.java b/src/main/java/de/pixart/messenger/ui/WelcomeActivity.java index c75b1bc4e..95b269cf5 100644 --- a/src/main/java/de/pixart/messenger/ui/WelcomeActivity.java +++ b/src/main/java/de/pixart/messenger/ui/WelcomeActivity.java @@ -1,8 +1,14 @@ package de.pixart.messenger.ui; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.os.Bundle; +import android.preference.PreferenceManager; +import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; @@ -11,9 +17,16 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.databinding.DataBindingUtil; +import de.pixart.messenger.Config; import de.pixart.messenger.R; +import de.pixart.messenger.databinding.WelcomeBinding; +import de.pixart.messenger.services.InstallReferrerService; import de.pixart.messenger.ui.util.IntroHelper; +import de.pixart.messenger.utils.SignupUtils; +import de.pixart.messenger.utils.XmppUri; +import rocks.xmpp.addr.Jid; import static de.pixart.messenger.Config.DISALLOW_REGISTRATION_IN_UI; import static de.pixart.messenger.utils.PermissionUtils.allGranted; @@ -22,6 +35,45 @@ import static de.pixart.messenger.utils.PermissionUtils.readGranted; public class WelcomeActivity extends XmppActivity { private static final int REQUEST_IMPORT_BACKUP = 0x63fb; + private static final int REQUEST_READ_EXTERNAL_STORAGE = 0XD737; + + private XmppUri inviteUri; + + private BroadcastReceiver installReferrerBroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent data) { + final String invite = data.getStringExtra(StartConversationActivity.EXTRA_INVITE_URI); + if (invite == null) { + return; + } + Log.d(Config.LOGTAG, "welcome activity received install referrer uri: " + invite); + final XmppUri xmppUri = new XmppUri(invite); + processXmppUri(xmppUri); + } + }; + + private boolean processXmppUri(final XmppUri xmppUri) { + if (xmppUri.isValidJid()) { + final String preauth = xmppUri.getParamater("preauth"); + final Jid jid = xmppUri.getJid(); + final Intent intent; + if (xmppUri.isAction(XmppUri.ACTION_REGISTER)) { + intent = SignupUtils.getTokenRegistrationIntent(this, jid, preauth); + } else if (xmppUri.isAction(XmppUri.ACTION_ROSTER) && "y".equals(xmppUri.getParamater("ibr"))) { + intent = SignupUtils.getTokenRegistrationIntent(this, Jid.ofDomain(jid.getDomain()), preauth); + intent.putExtra(StartConversationActivity.EXTRA_INVITE_URI, xmppUri.toString()); + } else { + intent = null; + } + if (intent != null) { + startActivity(intent); + finish(); + return true; + } + this.inviteUri = xmppUri; + } + return false; + } @Override protected void refreshUiReal() { @@ -31,8 +83,6 @@ public class WelcomeActivity extends XmppActivity { void onBackendConnected() { } - private static final int REQUEST_READ_EXTERNAL_STORAGE = 0XD737; - @Override public void onStart() { super.onStart(); @@ -40,6 +90,15 @@ public class WelcomeActivity extends XmppActivity { if (this.mTheme != theme) { recreate(); } + final IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(InstallReferrerService.INSTALL_REFERRER_BROADCAST_ACTION); + registerReceiver(installReferrerBroadcastReceiver, intentFilter); + } + + @Override + public void onStop() { + unregisterReceiver(installReferrerBroadcastReceiver); + super.onStop(); } @Override @@ -55,7 +114,13 @@ public class WelcomeActivity extends XmppActivity { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } super.onCreate(savedInstanceState); - setContentView(R.layout.welcome); + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + final String referrer = preferences.getString(SignupUtils.INSTALL_REFERRER, null); + final XmppUri referrerUri = referrer == null ? null : new XmppUri(referrer); + if (referrerUri != null && processXmppUri(referrerUri)) { + return; + } + WelcomeBinding binding = DataBindingUtil.setContentView(this, R.layout.welcome); setSupportActionBar(findViewById(R.id.toolbar)); final ActionBar ab = getSupportActionBar(); if (ab != null) { @@ -63,27 +128,23 @@ public class WelcomeActivity extends XmppActivity { ab.setDisplayHomeAsUpEnabled(false); } IntroHelper.showIntro(this, false); - final Button ImportDatabase = findViewById(R.id.import_database); - final TextView ImportText = findViewById(R.id.import_text); if (hasStoragePermission(REQUEST_IMPORT_BACKUP)) { - ImportDatabase.setVisibility(View.VISIBLE); - ImportText.setVisibility(View.VISIBLE); + binding.importDatabase.setVisibility(View.VISIBLE); + binding.importText.setVisibility(View.VISIBLE); } - ImportDatabase.setOnClickListener(v -> startActivity(new Intent(this, ImportBackupActivity.class))); + binding.importDatabase.setOnClickListener(v -> startActivity(new Intent(this, ImportBackupActivity.class))); - final Button createAccount = findViewById(R.id.create_account); - createAccount.setOnClickListener(v -> { + binding.createAccount.setOnClickListener(v -> { final Intent intent = new Intent(WelcomeActivity.this, MagicCreateActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); addInviteUri(intent); startActivity(intent); }); if (DISALLOW_REGISTRATION_IN_UI) { - createAccount.setVisibility(View.GONE); + binding.createAccount.setVisibility(View.GONE); } - final Button useExistingAccount = findViewById(R.id.use_existing_account); - useExistingAccount.setOnClickListener(v -> { + binding.useExistingAccount.setOnClickListener(v -> { Intent intent = new Intent(WelcomeActivity.this, EditAccountActivity.class); intent.putExtra("init", true); intent.putExtra("existing", true); @@ -96,8 +157,15 @@ public class WelcomeActivity extends XmppActivity { } - public void addInviteUri(Intent intent) { - StartConversationActivity.addInviteUri(intent, getIntent()); + public void addInviteUri(Intent to) { + final Intent from = getIntent(); + if (from != null && from.hasExtra(StartConversationActivity.EXTRA_INVITE_URI)) { + final String invite = from.getStringExtra(StartConversationActivity.EXTRA_INVITE_URI); + to.putExtra(StartConversationActivity.EXTRA_INVITE_URI, invite); + } else if (this.inviteUri != null) { + Log.d(Config.LOGTAG, "injecting referrer uri into on-boarding flow"); + to.putExtra(StartConversationActivity.EXTRA_INVITE_URI, this.inviteUri.toString()); + } } public static void launch(AppCompatActivity activity) { diff --git a/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java b/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java index 401c2cd40..3c3c12da3 100644 --- a/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java +++ b/src/main/java/de/pixart/messenger/ui/util/MyLinkify.java @@ -121,7 +121,7 @@ public class MyLinkify { private static final Linkify.MatchFilter XMPPURI_MATCH_FILTER = (s, start, end) -> { XmppUri uri = new XmppUri(s.subSequence(start, end).toString()); - return uri.isJidValid(); + return uri.isValidJid(); }; private static boolean isAlphabetic(final int code) { diff --git a/src/main/java/de/pixart/messenger/ui/util/ShareUtil.java b/src/main/java/de/pixart/messenger/ui/util/ShareUtil.java index 6d76cf6eb..ffa352ede 100644 --- a/src/main/java/de/pixart/messenger/ui/util/ShareUtil.java +++ b/src/main/java/de/pixart/messenger/ui/util/ShareUtil.java @@ -135,7 +135,7 @@ public class ShareUtil { Matcher xmppPatternMatcher = Patterns.XMPP_PATTERN.matcher(body); if (xmppPatternMatcher.find()) { try { - return new XmppUri(body.substring(xmppPatternMatcher.start(), xmppPatternMatcher.end())).isJidValid(); + return new XmppUri(body.substring(xmppPatternMatcher.start(), xmppPatternMatcher.end())).isValidJid(); } catch (Exception e) { return false; } diff --git a/src/main/java/de/pixart/messenger/utils/SignupUtils.java b/src/main/java/de/pixart/messenger/utils/SignupUtils.java index 52534b89d..a97c1b85b 100644 --- a/src/main/java/de/pixart/messenger/utils/SignupUtils.java +++ b/src/main/java/de/pixart/messenger/utils/SignupUtils.java @@ -16,6 +16,8 @@ import rocks.xmpp.addr.Jid; public class SignupUtils { + public static final String INSTALL_REFERRER = "install_referrer"; + public static boolean isSupportTokenRegistry() { return true; } diff --git a/src/main/java/de/pixart/messenger/utils/XmppUri.java b/src/main/java/de/pixart/messenger/utils/XmppUri.java index 9cb9c3a46..0e9fe7087 100644 --- a/src/main/java/de/pixart/messenger/utils/XmppUri.java +++ b/src/main/java/de/pixart/messenger/utils/XmppUri.java @@ -182,7 +182,7 @@ public class XmppUri { } } - public boolean isJidValid() { + public boolean isValidJid() { if (jid == null) { return false; } |