gradle: restore play runtime detection

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2023-05-08 13:08:09 +02:00
parent 40eaa54cf0
commit 33a8a51962
5 changed files with 19 additions and 41 deletions

View File

@ -26,7 +26,6 @@ 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
@ -49,11 +48,6 @@ android {
applicationIdSuffix = ".debug" applicationIdSuffix = ".debug"
versionNameSuffix = "-debug" versionNameSuffix = "-debug"
} }
create("googleplay") {
initWith(getByName("release"))
matchingFallbacks += "release"
buildConfigField("boolean", "IS_GOOGLE_PLAY", true.toString())
}
} }
lint { lint {
disable += "LongLogTag" disable += "LongLogTag"

View File

@ -1,13 +0,0 @@
<?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

@ -12,8 +12,8 @@ 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) {
@ -23,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 (BuildConfig.IS_GOOGLE_PLAY) { if (Updater.installerIsGooglePlay(context)) {
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

@ -88,9 +88,6 @@ 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

@ -53,6 +53,21 @@ object Updater {
private val updaterScope = CoroutineScope(Job() + Dispatchers.IO) private val updaterScope = CoroutineScope(Job() + Dispatchers.IO)
private fun installer(context: Context): String = 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) {
""
}
fun installerIsGooglePlay(context: Context): Boolean = installer(context) == "com.android.vending"
sealed class Progress { sealed class Progress {
object Complete : Progress() object Complete : Progress()
class Available(val version: String) : Progress() { class Available(val version: String) : Progress() {
@ -340,7 +355,7 @@ object Updater {
} }
fun monitorForUpdates() { fun monitorForUpdates() {
if (BuildConfig.IS_GOOGLE_PLAY) if (installerIsGooglePlay(Application.get()))
return return
updaterScope.launch { updaterScope.launch {
@ -382,25 +397,10 @@ object Updater {
class AppUpdatedReceiver : BroadcastReceiver() { class AppUpdatedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
if (BuildConfig.IS_GOOGLE_PLAY)
return
if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED) if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED)
return return
val installer = try { if (installer(context) != context.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) {
""
}
if (installer != context.packageName)
return return
/* TODO: does not work because of restrictions placed on broadcast receivers. */ /* TODO: does not work because of restrictions placed on broadcast receivers. */