android: use PackageManager to determine install AppSourceChecker (#517)
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/518/head
parent
9654bb5d9d
commit
9731afd44c
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue