From cf4fca8086858319f1b395c9059c67b436222cbb Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Thu, 4 May 2023 16:45:01 +0200 Subject: gradle: remove runtime detection of play store Signed-off-by: Jason A. Donenfeld --- tunnel/build.gradle.kts | 3 ++ ui/build.gradle.kts | 5 +++ ui/src/googleplay/AndroidManifest.xml | 8 ++++ ui/src/main/AndroidManifest.xml | 5 +++ .../com/wireguard/android/BootShutdownReceiver.kt | 12 ------ .../android/preference/DonatePreference.kt | 5 +-- .../android/updater/SnackbarUpdateShower.kt | 4 ++ .../java/com/wireguard/android/updater/Updater.kt | 44 ++++++++++++++-------- 8 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 ui/src/googleplay/AndroidManifest.xml diff --git a/tunnel/build.gradle.kts b/tunnel/build.gradle.kts index f71d57c2..63535199 100644 --- a/tunnel/build.gradle.kts +++ b/tunnel/build.gradle.kts @@ -48,6 +48,9 @@ android { } } } + create("googleplay") { + initWith(getByName("release")) + } } lint { disable.add("LongLogTag") diff --git a/ui/build.gradle.kts b/ui/build.gradle.kts index 67e7a12d..8832a358 100644 --- a/ui/build.gradle.kts +++ b/ui/build.gradle.kts @@ -23,6 +23,7 @@ android { versionCode = providers.gradleProperty("wireguardVersionCode").get().toInt() versionName = providers.gradleProperty("wireguardVersionName").get() buildConfigField("int", "MIN_SDK_VERSION", minSdk.toString()) + buildConfigField("boolean", "IS_GOOGLE_PLAY", false.toString()) } compileOptions { sourceCompatibility = JavaVersion.VERSION_17 @@ -45,6 +46,10 @@ android { applicationIdSuffix = ".debug" versionNameSuffix = "-debug" } + create("googleplay") { + initWith(getByName("release")) + buildConfigField("boolean", "IS_GOOGLE_PLAY", true.toString()) + } } lint { disable.add("LongLogTag") diff --git a/ui/src/googleplay/AndroidManifest.xml b/ui/src/googleplay/AndroidManifest.xml new file mode 100644 index 00000000..1343edbb --- /dev/null +++ b/ui/src/googleplay/AndroidManifest.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/ui/src/main/AndroidManifest.xml b/ui/src/main/AndroidManifest.xml index e4103b10..42341226 100644 --- a/ui/src/main/AndroidManifest.xml +++ b/ui/src/main/AndroidManifest.xml @@ -104,6 +104,11 @@ + + + + + diff --git a/ui/src/main/java/com/wireguard/android/BootShutdownReceiver.kt b/ui/src/main/java/com/wireguard/android/BootShutdownReceiver.kt index 2b9312a0..a859bf71 100644 --- a/ui/src/main/java/com/wireguard/android/BootShutdownReceiver.kt +++ b/ui/src/main/java/com/wireguard/android/BootShutdownReceiver.kt @@ -8,25 +8,13 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.util.Log -import com.wireguard.android.activity.MainActivity import com.wireguard.android.backend.WgQuickBackend -import com.wireguard.android.updater.Updater import com.wireguard.android.util.applicationScope import kotlinx.coroutines.launch class BootShutdownReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { val action = intent.action ?: return - - if (Intent.ACTION_MY_PACKAGE_REPLACED == action && Updater.installer() == context.packageName) { - /* TODO: does not work because of restrictions placed on broadcast receivers. */ - val start = Intent(context, MainActivity::class.java) - start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - context.startActivity(start) - return - } - applicationScope.launch { if (Application.getBackend() !is WgQuickBackend) return@launch val tunnelManager = Application.getTunnelManager() diff --git a/ui/src/main/java/com/wireguard/android/preference/DonatePreference.kt b/ui/src/main/java/com/wireguard/android/preference/DonatePreference.kt index bf0624bc..59980dcb 100644 --- a/ui/src/main/java/com/wireguard/android/preference/DonatePreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/DonatePreference.kt @@ -8,13 +8,12 @@ package com.wireguard.android.preference import android.content.Context import android.content.Intent import android.net.Uri -import android.os.Build import android.util.AttributeSet import android.widget.Toast import androidx.preference.Preference import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.wireguard.android.BuildConfig import com.wireguard.android.R -import com.wireguard.android.updater.Updater import com.wireguard.android.util.ErrorMessages class DonatePreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) { @@ -24,7 +23,7 @@ class DonatePreference(context: Context, attrs: AttributeSet?) : Preference(cont override fun onClick() { /* Google Play Store forbids links to our donation page. */ - if (Updater.installerIsGooglePlay()) { + if (BuildConfig.IS_GOOGLE_PLAY) { MaterialAlertDialogBuilder(context) .setTitle(R.string.donate_title) .setMessage(R.string.donate_google_play_disappointment) diff --git a/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt b/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt index f7d32abb..d3c1b4f8 100644 --- a/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt +++ b/ui/src/main/java/com/wireguard/android/updater/SnackbarUpdateShower.kt @@ -11,6 +11,7 @@ import androidx.fragment.app.FragmentActivity import androidx.lifecycle.lifecycleScope import com.google.android.material.snackbar.BaseTransientBottomBar import com.google.android.material.snackbar.Snackbar +import com.wireguard.android.BuildConfig import com.wireguard.android.R import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.QuantityFormatter @@ -88,6 +89,9 @@ object SnackbarUpdateShower { } fun attachToActivity(activity: FragmentActivity, view: View, anchor: View?) { + if (BuildConfig.IS_GOOGLE_PLAY) + return + val snackbar = SwapableSnackbar(activity, view, anchor) val context = activity.applicationContext diff --git a/ui/src/main/java/com/wireguard/android/updater/Updater.kt b/ui/src/main/java/com/wireguard/android/updater/Updater.kt index d0bd18a1..aa3256d4 100644 --- a/ui/src/main/java/com/wireguard/android/updater/Updater.kt +++ b/ui/src/main/java/com/wireguard/android/updater/Updater.kt @@ -17,6 +17,7 @@ import androidx.core.content.ContextCompat import androidx.core.content.IntentCompat import com.wireguard.android.Application import com.wireguard.android.BuildConfig +import com.wireguard.android.activity.MainActivity import com.wireguard.android.util.UserKnobs import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -360,7 +361,7 @@ object Updater { } fun monitorForUpdates() { - if (installerIsGooglePlay()) + if (BuildConfig.IS_GOOGLE_PLAY) return updaterScope.launch { @@ -400,21 +401,34 @@ object Updater { }.launchIn(Application.getCoroutineScope()) } - fun installer(): String { - val context = Application.get().applicationContext - return try { - val packageName = context.packageName - val pm = context.packageManager - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - pm.getInstallSourceInfo(packageName).installingPackageName ?: "" - } else { - @Suppress("DEPRECATION") - pm.getInstallerPackageName(packageName) ?: "" + class AppUpdatedReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + if (BuildConfig.IS_GOOGLE_PLAY) + return + + if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) + return + + val installer = try { + val packageName = context.packageName + val pm = context.packageManager + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + pm.getInstallSourceInfo(packageName).installingPackageName ?: "" + } else { + @Suppress("DEPRECATION") + pm.getInstallerPackageName(packageName) ?: "" + } + } catch (_: Throwable) { + "" } - } catch (_: Throwable) { - "" + if (installer != context.packageName) + return + + /* TODO: does not work because of restrictions placed on broadcast receivers. */ + val start = Intent(context, MainActivity::class.java) + start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + context.startActivity(start) } } - - fun installerIsGooglePlay(): Boolean = installer() == "com.android.vending" } \ No newline at end of file -- cgit v1.2.3