Skip to content

Commit

Permalink
feat(package_info_plus): Use new API to get install source on Android…
Browse files Browse the repository at this point in the history
… >= 11
  • Loading branch information
vbuberen committed Mar 13, 2023
1 parent fe7251a commit 4b9cb0f
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class PackageInfoPlugin : MethodCallHandler, FlutterPlugin {

val buildSignature = getBuildSignature(packageManager)

val installerPackage = packageManager.getInstallerPackageName(applicationContext!!.packageName);
val installerPackage = getInstallerPackageName()

val infoMap = HashMap<String, String>()
infoMap.apply {
Expand All @@ -59,6 +59,21 @@ class PackageInfoPlugin : MethodCallHandler, FlutterPlugin {
}
}

/**
* Using initiatingPackageName on Android 11 and newer because it can't be changed
* https://developer.android.com/reference/android/content/pm/InstallSourceInfo#getInitiatingPackageName()
*/
private fun getInstallerPackageName(): String? {
val packageManager = applicationContext!!.packageManager
val packageName = applicationContext!!.packageName
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
packageManager.getInstallSourceInfo(packageName).initiatingPackageName
} else {
@Suppress("DEPRECATION")
packageManager.getInstallerPackageName(packageName)
}
}

@Suppress("deprecation")
private fun getLongVersionCode(info: PackageInfo): Long {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Expand Down

0 comments on commit 4b9cb0f

Please sign in to comment.