gradle: remove runtime detection of play store

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-05-04 16:45:01 +02:00
parent 318f36a205
commit cf4fca8086
8 changed files with 56 additions and 30 deletions

View File

@ -48,6 +48,9 @@ android {
} }
} }
} }
create("googleplay") {
initWith(getByName("release"))
}
} }
lint { lint {
disable.add("LongLogTag") disable.add("LongLogTag")

View File

@ -23,6 +23,7 @@ android {
versionCode = providers.gradleProperty("wireguardVersionCode").get().toInt() versionCode = providers.gradleProperty("wireguardVersionCode").get().toInt()
versionName = providers.gradleProperty("wireguardVersionName").get() versionName = providers.gradleProperty("wireguardVersionName").get()
buildConfigField("int", "MIN_SDK_VERSION", minSdk.toString()) buildConfigField("int", "MIN_SDK_VERSION", minSdk.toString())
buildConfigField("boolean", "IS_GOOGLE_PLAY", false.toString())
} }
compileOptions { compileOptions {
sourceCompatibility = JavaVersion.VERSION_17 sourceCompatibility = JavaVersion.VERSION_17
@ -45,6 +46,10 @@ android {
applicationIdSuffix = ".debug" applicationIdSuffix = ".debug"
versionNameSuffix = "-debug" versionNameSuffix = "-debug"
} }
create("googleplay") {
initWith(getByName("release"))
buildConfigField("boolean", "IS_GOOGLE_PLAY", true.toString())
}
} }
lint { lint {
disable.add("LongLogTag") disable.add("LongLogTag")

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" tools:node="remove" />
<application>
<receiver android:name=".updater.Updater$AppUpdatedReceiver" tools:node="remove" />
</application>
</manifest>

View File

@ -104,6 +104,11 @@
<intent-filter> <intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" /> <action android:name="android.intent.action.ACTION_SHUTDOWN" />
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".updater.Updater$AppUpdatedReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" /> <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter> </intent-filter>
</receiver> </receiver>

View File

@ -8,25 +8,13 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.Log import android.util.Log
import com.wireguard.android.activity.MainActivity
import com.wireguard.android.backend.WgQuickBackend import com.wireguard.android.backend.WgQuickBackend
import com.wireguard.android.updater.Updater
import com.wireguard.android.util.applicationScope import com.wireguard.android.util.applicationScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
class BootShutdownReceiver : BroadcastReceiver() { class BootShutdownReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
val action = intent.action ?: return 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 { applicationScope.launch {
if (Application.getBackend() !is WgQuickBackend) return@launch if (Application.getBackend() !is WgQuickBackend) return@launch
val tunnelManager = Application.getTunnelManager() val tunnelManager = Application.getTunnelManager()

View File

@ -8,13 +8,12 @@ package com.wireguard.android.preference
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.Toast import android.widget.Toast
import androidx.preference.Preference import androidx.preference.Preference
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.wireguard.android.BuildConfig
import com.wireguard.android.R import com.wireguard.android.R
import com.wireguard.android.updater.Updater
import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.ErrorMessages
class DonatePreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) { class DonatePreference(context: Context, attrs: AttributeSet?) : Preference(context, attrs) {
@ -24,7 +23,7 @@ class DonatePreference(context: Context, attrs: AttributeSet?) : Preference(cont
override fun onClick() { override fun onClick() {
/* Google Play Store forbids links to our donation page. */ /* Google Play Store forbids links to our donation page. */
if (Updater.installerIsGooglePlay()) { if (BuildConfig.IS_GOOGLE_PLAY) {
MaterialAlertDialogBuilder(context) MaterialAlertDialogBuilder(context)
.setTitle(R.string.donate_title) .setTitle(R.string.donate_title)
.setMessage(R.string.donate_google_play_disappointment) .setMessage(R.string.donate_google_play_disappointment)

View File

@ -11,6 +11,7 @@ import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.google.android.material.snackbar.BaseTransientBottomBar import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.wireguard.android.BuildConfig
import com.wireguard.android.R import com.wireguard.android.R
import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.ErrorMessages
import com.wireguard.android.util.QuantityFormatter import com.wireguard.android.util.QuantityFormatter
@ -88,6 +89,9 @@ object SnackbarUpdateShower {
} }
fun attachToActivity(activity: FragmentActivity, view: View, anchor: View?) { fun attachToActivity(activity: FragmentActivity, view: View, anchor: View?) {
if (BuildConfig.IS_GOOGLE_PLAY)
return
val snackbar = SwapableSnackbar(activity, view, anchor) val snackbar = SwapableSnackbar(activity, view, anchor)
val context = activity.applicationContext val context = activity.applicationContext

View File

@ -17,6 +17,7 @@ import androidx.core.content.ContextCompat
import androidx.core.content.IntentCompat import androidx.core.content.IntentCompat
import com.wireguard.android.Application import com.wireguard.android.Application
import com.wireguard.android.BuildConfig import com.wireguard.android.BuildConfig
import com.wireguard.android.activity.MainActivity
import com.wireguard.android.util.UserKnobs import com.wireguard.android.util.UserKnobs
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -360,7 +361,7 @@ object Updater {
} }
fun monitorForUpdates() { fun monitorForUpdates() {
if (installerIsGooglePlay()) if (BuildConfig.IS_GOOGLE_PLAY)
return return
updaterScope.launch { updaterScope.launch {
@ -400,21 +401,34 @@ object Updater {
}.launchIn(Application.getCoroutineScope()) }.launchIn(Application.getCoroutineScope())
} }
fun installer(): String { class AppUpdatedReceiver : BroadcastReceiver() {
val context = Application.get().applicationContext override fun onReceive(context: Context, intent: Intent) {
return try { if (BuildConfig.IS_GOOGLE_PLAY)
val packageName = context.packageName return
val pm = context.packageManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED)
pm.getInstallSourceInfo(packageName).installingPackageName ?: "" return
} else {
@Suppress("DEPRECATION") val installer = try {
pm.getInstallerPackageName(packageName) ?: "" 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"
} }