Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/package_info_plus/package_info_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.4.0

- Android: Migrate to Kotlin
- Android: Update dependencies, build config updates
- Android: Fix project title

## 1.3.1

- Fix embedding issue in example
Expand Down
8 changes: 4 additions & 4 deletions packages/package_info_plus/package_info_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

[![Flutter Community: package_info_plus](https://fluttercommunity.dev/_github/header/package_info_plus)](https://github.com/fluttercommunity/community)

[![package_info_plus](https://github.com/fluttercommunity/plus_plugins/actions/workflows/package_info_plus.yaml/badge.svg)](https://github.com/fluttercommunity/plus_plugins/actions/workflows/package_info_plus.yaml)
[![pub package](https://img.shields.io/pub/v/package_info_plus.svg)](https://pub.dev/packages/package_info_plus)

<p class="center">
<center><a href="https://flutter.dev/docs/development/packages-and-plugins/favorites" target="_blank" rel="noreferrer noopener"><img src="../../website/static/img/flutter-favorite-badge.png" width="100" alt="build"></a></center>
<center><a href="https://flutter.dev/docs/development/packages-and-plugins/favorites" target="_blank" rel="noreferrer noopener"><img src="../../../website/static/img/flutter-favorite-badge.png" width="100" alt="build"></a></center>
</p>

This Flutter plugin provides an API for querying information about an
application package.

Expand All @@ -24,7 +26,7 @@ application package. This works both on iOS and Android.
```dart
import 'package:package_info_plus/package_info_plus.dart';

...
...

// Be sure to add this line if `PackageInfo.fromPlatform()` is called before runApp()
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -54,5 +56,3 @@ Check out our documentation website to learn more. [Plus plugins documentation](

Calling to `PackageInfo.fromPlatform()` before the `runApp()` call will cause an exception.
See https://github.com/fluttercommunity/plus_plugins/issues/309

**Important:** As of January 2021, the Flutter team is no longer accepting non-critical PRs for the original set of plugins in `flutter/plugins`, and instead they should be submitted in this project. [You can read more about this announcement here.](https://github.com/flutter/plugins/blob/master/CONTRIBUTING.md#important-note) as well as [in the Flutter 2 announcement blog post.](https://medium.com/flutter/whats-new-in-flutter-2-0-fe8e95ecc65)
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ group 'io.flutter.plugins.packageinfo'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.6.10'

repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.android.tools.build:gradle:7.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

Expand All @@ -20,6 +23,7 @@ rootProject.allprojects {
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 31
Expand All @@ -31,4 +35,8 @@ android {
lintOptions {
disable 'InvalidPackage'
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = 'package_info'
rootProject.name = 'package_info_plus'

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package dev.fluttercommunity.plus.packageinfo

import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Build
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

/** PackageInfoPlugin */
class PackageInfoPlugin : MethodCallHandler, FlutterPlugin {
private var applicationContext: Context? = null
private var methodChannel: MethodChannel? = null

/** Plugin registration. */
override fun onAttachedToEngine(binding: FlutterPluginBinding) {
applicationContext = binding.applicationContext
methodChannel = MethodChannel(binding.binaryMessenger, CHANNEL_NAME)
methodChannel!!.setMethodCallHandler(this)
}

override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
applicationContext = null
methodChannel!!.setMethodCallHandler(null)
methodChannel = null
}

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
try {
if (call.method == "getAll") {
val packageManager = applicationContext!!.packageManager
val info = packageManager.getPackageInfo(applicationContext!!.packageName, 0)

val buildSignature = getBuildSignature(packageManager)

val infoMap = HashMap<String, String>()
infoMap.apply {
put("appName", info.applicationInfo.loadLabel(packageManager).toString())
put("packageName", applicationContext!!.packageName)
put("version", info.versionName)
put("buildNumber", getLongVersionCode(info).toString())
if (buildSignature != null) put("buildSignature", buildSignature)
}.also { resultingMap ->
result.success(resultingMap)
}
} else {
result.notImplemented()
}
} catch (ex: PackageManager.NameNotFoundException) {
result.error("Name not found", ex.message, null)
}
}

@Suppress("deprecation")
private fun getLongVersionCode(info: PackageInfo): Long {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
info.longVersionCode
} else {
info.versionCode.toLong()
}
}

@Suppress("deprecation", "PackageManagerGetSignatures")
private fun getBuildSignature(pm: PackageManager): String? {
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val packageInfo = pm.getPackageInfo(
applicationContext!!.packageName,
PackageManager.GET_SIGNING_CERTIFICATES
)
val signingInfo = packageInfo.signingInfo ?: return null

if (signingInfo.hasMultipleSigners()) {
signatureToSha1(signingInfo.apkContentsSigners.first().toByteArray())
} else {
signatureToSha1(signingInfo.signingCertificateHistory.first().toByteArray())
}
} else {
val packageInfo = pm.getPackageInfo(
applicationContext!!.packageName,
PackageManager.GET_SIGNATURES
)
val signatures = packageInfo.signatures

if (signatures.isNullOrEmpty() || packageInfo.signatures.first() == null) {
null
} else {
signatureToSha1(signatures.first().toByteArray())
}
}
} catch (e: PackageManager.NameNotFoundException) {
null
} catch (e: NoSuchAlgorithmException) {
null
}
}

// Credits https://gist.github.com/scottyab/b849701972d57cf9562e
@Throws(NoSuchAlgorithmException::class)
private fun signatureToSha1(sig: ByteArray): String {
val digest = MessageDigest.getInstance("SHA1")
digest.update(sig)
val hashText = digest.digest()
return bytesToHex(hashText)
}

// Credits https://gist.github.com/scottyab/b849701972d57cf9562e
private fun bytesToHex(bytes: ByteArray): String {
val hexArray = charArrayOf(
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
)
val hexChars = CharArray(bytes.size * 2)
var v: Int
for (j in bytes.indices) {
v = bytes[j].toInt() and 0xFF
hexChars[j * 2] = hexArray[v ushr 4]
hexChars[j * 2 + 1] = hexArray[v and 0x0F]
}
return String(hexChars)
}

companion object {
private const val CHANNEL_NAME = "dev.fluttercommunity.plus/package_info"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
2 changes: 1 addition & 1 deletion packages/package_info_plus/package_info_plus/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: package_info_plus
description: Flutter plugin for querying information about the application package, such as CFBundleVersion on iOS or versionCode on Android.
version: 1.3.1
version: 1.4.0
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/

Expand Down