From 8e0c02f3cc231384a87a6d76dfe9a5823fda3b90 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 11 Jan 2020 11:08:09 +0100 Subject: [PATCH] use installreferrer libray instead of broadcast listener --- build.gradle | 26 ++++++-- src/conversations/AndroidManifest.xml | 9 --- .../services/InstallReferrerService.java | 39 ------------ .../conversations/ui/MagicCreateActivity.java | 9 +-- .../conversations/ui/WelcomeActivity.java | 35 +++-------- .../conversations/utils/SignupUtils.java | 2 - .../utils/InstallReferrerUtils.java | 15 +++++ .../utils/InstallReferrerUtils.java | 63 +++++++++++++++++++ 8 files changed, 108 insertions(+), 90 deletions(-) delete mode 100644 src/conversations/java/eu/siacs/conversations/services/InstallReferrerService.java create mode 100644 src/conversationsFree/java/eu/siacs/conversations/utils/InstallReferrerUtils.java create mode 100644 src/conversationsPlaystore/java/eu/siacs/conversations/utils/InstallReferrerUtils.java diff --git a/build.gradle b/build.gradle index f68542d54..ee50e5c98 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,8 @@ configurations { playstoreImplementation compatImplementation conversationsFreeCompatImplementation + conversationsPlaystoreCompatImplementation + conversationsPlaystoreSystemImplementation quicksyFreeCompatImplementation quicksyImplementation } @@ -36,6 +38,8 @@ dependencies { exclude group: 'com.google.firebase', module: 'firebase-analytics' exclude group: 'com.google.firebase', module: 'firebase-measurement-connector' } + conversationsPlaystoreCompatImplementation("com.android.installreferrer:installreferrer:1.1") + conversationsPlaystoreSystemImplementation("com.android.installreferrer:installreferrer:1.1") implementation 'org.sufficientlysecure:openpgp-api:10.0' implementation('com.theartofdev.edmodo:android-image-cropper:2.7.+') { exclude group: 'com.android.support', module: 'appcompat-v7' @@ -86,8 +90,8 @@ android { defaultConfig { minSdkVersion 16 targetSdkVersion 28 - versionCode 353 - versionName "2.6.2" + versionCode 359 + versionName "2.6.3-alpha.6" archivesBaseName += "-$versionName" applicationId "eu.siacs.conversations" resValue "string", "applicationId", applicationId @@ -147,12 +151,12 @@ android { sourceSets { quicksyFreeCompat { java { - srcDirs 'src/freeCompat/java' + srcDir 'src/freeCompat/java' } } quicksyPlaystoreCompat { java { - srcDirs 'src/playstoreCompat/java' + srcDir 'src/playstoreCompat/java' } res { srcDir 'src/playstoreCompat/res' @@ -166,12 +170,19 @@ android { } conversationsFreeCompat { java { - srcDirs 'src/freeCompat/java' + srcDir 'src/freeCompat/java' + srcDir 'src/conversationsFree/java' + } + } + conversationsFreeSystem { + java { + srcDir 'src/conversationsFree/java' } } conversationsPlaystoreCompat { java { - srcDirs 'src/playstoreCompat/java' + srcDir 'src/playstoreCompat/java' + srcDir 'src/conversationsPlaystore/java' } res { srcDir 'src/playstoreCompat/res' @@ -179,6 +190,9 @@ android { } } conversationsPlaystoreSystem { + java { + srcDir 'src/conversationsPlaystore/java' + } res { srcDir 'src/conversationsPlaystore/res' } diff --git a/src/conversations/AndroidManifest.xml b/src/conversations/AndroidManifest.xml index 0491acfbf..2100d9719 100644 --- a/src/conversations/AndroidManifest.xml +++ b/src/conversations/AndroidManifest.xml @@ -39,14 +39,5 @@ - - - - - - diff --git a/src/conversations/java/eu/siacs/conversations/services/InstallReferrerService.java b/src/conversations/java/eu/siacs/conversations/services/InstallReferrerService.java deleted file mode 100644 index 7fdaf4c48..000000000 --- a/src/conversations/java/eu/siacs/conversations/services/InstallReferrerService.java +++ /dev/null @@ -1,39 +0,0 @@ -package eu.siacs.conversations.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 eu.siacs.conversations.Config; -import eu.siacs.conversations.ui.StartConversationActivity; -import eu.siacs.conversations.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); - } - } -} diff --git a/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java b/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java index b76e2c3bb..405bb53b6 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java +++ b/src/conversations/java/eu/siacs/conversations/ui/MagicCreateActivity.java @@ -1,15 +1,12 @@ package eu.siacs.conversations.ui; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.databinding.DataBindingUtil; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; -import android.util.Log; import android.view.View; import android.widget.Toast; @@ -20,7 +17,7 @@ import eu.siacs.conversations.R; import eu.siacs.conversations.databinding.MagicCreateBinding; import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.utils.CryptoHelper; -import eu.siacs.conversations.utils.SignupUtils; +import eu.siacs.conversations.utils.InstallReferrerUtils; import rocks.xmpp.addr.Jid; public class MagicCreateActivity extends XmppActivity implements TextWatcher { @@ -127,9 +124,7 @@ public class MagicCreateActivity extends XmppActivity implements TextWatcher { @Override public void onDestroy() { - Log.d(Config.LOGTAG,"purge install referrer"); - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); - preferences.edit().remove(SignupUtils.INSTALL_REFERRER).apply(); + InstallReferrerUtils.markInstallReferrerExecuted(this); super.onDestroy(); } diff --git a/src/conversations/java/eu/siacs/conversations/ui/WelcomeActivity.java b/src/conversations/java/eu/siacs/conversations/ui/WelcomeActivity.java index a63846fc8..adb57e4ae 100644 --- a/src/conversations/java/eu/siacs/conversations/ui/WelcomeActivity.java +++ b/src/conversations/java/eu/siacs/conversations/ui/WelcomeActivity.java @@ -1,14 +1,9 @@ package eu.siacs.conversations.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.databinding.DataBindingUtil; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; @@ -23,7 +18,7 @@ import eu.siacs.conversations.Config; import eu.siacs.conversations.R; import eu.siacs.conversations.databinding.ActivityWelcomeBinding; import eu.siacs.conversations.entities.Account; -import eu.siacs.conversations.services.InstallReferrerService; +import eu.siacs.conversations.utils.InstallReferrerUtils; import eu.siacs.conversations.utils.SignupUtils; import eu.siacs.conversations.utils.XmppUri; import rocks.xmpp.addr.Jid; @@ -37,18 +32,13 @@ public class WelcomeActivity extends XmppActivity { 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); + public void onInstallReferrerDiscovered(final String referrer) { + Log.d(Config.LOGTAG,"welcome activity: on install referrer discovered "+referrer); + if (referrer != null) { + final XmppUri xmppUri = new XmppUri(referrer); + runOnUiThread(() -> processXmppUri(xmppUri)); } - }; + } private boolean processXmppUri(final XmppUri xmppUri) { if (xmppUri.isValidJid()) { @@ -90,14 +80,11 @@ 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); + new InstallReferrerUtils(this); } @Override public void onStop() { - unregisterReceiver(installReferrerBroadcastReceiver); super.onStop(); } @@ -114,12 +101,6 @@ public class WelcomeActivity extends XmppActivity { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } super.onCreate(savedInstanceState); - 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; - } ActivityWelcomeBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_welcome); setSupportActionBar((Toolbar) binding.toolbar); configureActionBar(getSupportActionBar(), false); diff --git a/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java b/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java index bcf601a8f..a47dfbca8 100644 --- a/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java +++ b/src/conversations/java/eu/siacs/conversations/utils/SignupUtils.java @@ -17,8 +17,6 @@ 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/conversationsFree/java/eu/siacs/conversations/utils/InstallReferrerUtils.java b/src/conversationsFree/java/eu/siacs/conversations/utils/InstallReferrerUtils.java new file mode 100644 index 000000000..3aa49798d --- /dev/null +++ b/src/conversationsFree/java/eu/siacs/conversations/utils/InstallReferrerUtils.java @@ -0,0 +1,15 @@ +package eu.siacs.conversations.utils; + +import eu.siacs.conversations.ui.MagicCreateActivity; +import eu.siacs.conversations.ui.WelcomeActivity; + +public class InstallReferrerUtils { + + public InstallReferrerUtils(WelcomeActivity welcomeActivity) { + + } + + public static void markInstallReferrerExecuted(MagicCreateActivity magicCreateActivity) { + //stub + } +} \ No newline at end of file diff --git a/src/conversationsPlaystore/java/eu/siacs/conversations/utils/InstallReferrerUtils.java b/src/conversationsPlaystore/java/eu/siacs/conversations/utils/InstallReferrerUtils.java new file mode 100644 index 000000000..977ee2a17 --- /dev/null +++ b/src/conversationsPlaystore/java/eu/siacs/conversations/utils/InstallReferrerUtils.java @@ -0,0 +1,63 @@ +package eu.siacs.conversations.utils; + +import android.app.Activity; +import android.content.SharedPreferences; +import android.os.RemoteException; +import android.preference.PreferenceManager; +import android.util.Log; + +import com.android.installreferrer.api.InstallReferrerClient; +import com.android.installreferrer.api.InstallReferrerStateListener; +import com.android.installreferrer.api.ReferrerDetails; + +import eu.siacs.conversations.Config; +import eu.siacs.conversations.ui.WelcomeActivity; + +public class InstallReferrerUtils implements InstallReferrerStateListener { + + private static final String PROCESSED_INSTALL_REFERRER = "processed_install_referrer"; + + + private final WelcomeActivity welcomeActivity; + private final InstallReferrerClient installReferrerClient; + + + public InstallReferrerUtils(WelcomeActivity welcomeActivity) { + this.welcomeActivity = welcomeActivity; + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(welcomeActivity); + if (preferences.getBoolean(PROCESSED_INSTALL_REFERRER, false)) { + Log.d(Config.LOGTAG, "install referrer already processed"); + this.installReferrerClient = null; + return; + } + this.installReferrerClient = InstallReferrerClient.newBuilder(welcomeActivity).build(); + this.installReferrerClient.startConnection(this); + } + + public static void markInstallReferrerExecuted(final Activity context) { + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + preferences.edit().putBoolean(PROCESSED_INSTALL_REFERRER, true).apply(); + } + + @Override + public void onInstallReferrerSetupFinished(int responseCode) { + switch (responseCode) { + case InstallReferrerClient.InstallReferrerResponse.OK: + try { + final ReferrerDetails referrerDetails = installReferrerClient.getInstallReferrer(); + final String referrer = referrerDetails.getInstallReferrer(); + welcomeActivity.onInstallReferrerDiscovered(referrer); + } catch (RemoteException e) { + Log.d(Config.LOGTAG, "unable to get install referrer", e); + } + break; + default: + Log.d(Config.LOGTAG, "unable to setup install referrer client. code=" + responseCode); + } + } + + @Override + public void onInstallReferrerServiceDisconnected() { + + } +} \ No newline at end of file