android: use PackageManager to determine install AppSourceChecker

We were using MaybeGoogle to determine whether the app was installed from the Play Store, but this has not worked since the refactor.
Fixes tailscale/tailscale#13442
Updates tailscale/corp#23283

Signed-off-by: kari-ts <kari@tailscale.com>
pull/517/head
kari-ts 1 year ago
parent 22de0cdb7e
commit 3666147231

@ -77,7 +77,7 @@ class App : UninitializedApp(), libtailscale.AppContext, ViewModelStoreOwner {
override fun getPlatformDNSConfig(): String = dns.dnsConfigAsString override fun getPlatformDNSConfig(): String = dns.dnsConfigAsString
override fun isPlayVersion(): Boolean = MaybeGoogle.isGoogle() override fun getInstallSource(): String = AppSourceChecker.getInstallSource(this)
override fun shouldUseGoogleDNSFallback() : Boolean = BuildConfig.USE_GOOGLE_DNS_FALLBACK override fun shouldUseGoogleDNSFallback() : Boolean = BuildConfig.USE_GOOGLE_DNS_FALLBACK

@ -0,0 +1,34 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package com.tailscale.ipn
import android.content.Context
import android.os.Build
import android.util.Log
object AppSourceChecker {
const val TAG = "AppSourceChecker"
fun getInstallSource(context: Context): String {
val packageManager = context.packageManager
val packageName = context.packageName
Log.d(TAG, "Package name: $packageName")
val installerPackageName = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
packageManager.getInstallSourceInfo(packageName).installingPackageName
} else {
packageManager.getInstallerPackageName(packageName)
}
Log.d(TAG, "Installer package name: $installerPackageName")
return when (installerPackageName) {
"com.android.vending" -> "googleplay"
"org.fdroid.fdroid" -> "fdroid"
"com.amazon.venezia" -> "amazon"
null -> "unknown"
else -> "unknown($installerPackageName)"
}
}
}

@ -1,35 +0,0 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package com.tailscale.ipn;
import android.app.Activity;
import java.lang.reflect.Method;
public class MaybeGoogle {
static boolean isGoogle() {
return getGoogle() != null;
}
static String getIdTokenForActivity(Activity act) {
Class<?> google = getGoogle();
if (google == null) {
return "";
}
try {
Method method = google.getMethod("getIdTokenForActivity", Activity.class);
return (String) method.invoke(null, act);
} catch (Exception e) {
return "";
}
}
private static Class getGoogle() {
try {
return Class.forName("com.tailscale.ipn.Google");
} catch (ClassNotFoundException e) {
return null;
}
}
}

@ -105,9 +105,7 @@ type settingsFunc func(*router.Config, *dns.OSConfig) error
func (a *App) runBackend(ctx context.Context) error { func (a *App) runBackend(ctx context.Context) error {
paths.AppSharedDir.Store(a.dataDir) paths.AppSharedDir.Store(a.dataDir)
hostinfo.SetOSVersion(a.osVersion()) hostinfo.SetOSVersion(a.osVersion())
if !a.appCtx.IsPlayVersion() { hostinfo.SetPackage(a.appCtx.GetInstallSource())
hostinfo.SetPackage("nogoogle")
}
deviceModel := a.modelName() deviceModel := a.modelName()
if a.isChromeOS() { if a.isChromeOS() {
deviceModel = "ChromeOS: " + deviceModel deviceModel = "ChromeOS: " + deviceModel

@ -33,9 +33,8 @@ type AppContext interface {
// GetModelName gets the Android device's model name. // GetModelName gets the Android device's model name.
GetModelName() (string, error) GetModelName() (string, error)
// IsPlayVersion reports whether this is the Google Play version of the app // GetInstallSource gets information about how the app was installed or updated.
// (as opposed to F-droid/sideloaded). GetInstallSource() string
IsPlayVersion() bool
// ShouldUseGoogleDNSFallback reports whether or not to use Google for DNS fallback. // ShouldUseGoogleDNSFallback reports whether or not to use Google for DNS fallback.
ShouldUseGoogleDNSFallback() bool ShouldUseGoogleDNSFallback() bool

Loading…
Cancel
Save