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 {
disable.add("LongLogTag")

View File

@ -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")

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>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<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" />
</intent-filter>
</receiver>

View File

@ -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()

View File

@ -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)

View File

@ -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

View File

@ -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"
}