mirror of
https://github.com/awfixers-stuff/OwnDroid.git
synced 2026-03-28 04:45:59 +00:00
app installer: get basic apk info
This commit is contained in:
52
app/src/main/java/com/github/fishb1/apkinfo/ApkInfo.kt
Normal file
52
app/src/main/java/com/github/fishb1/apkinfo/ApkInfo.kt
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright (C) 2022 fishbone
|
||||
*
|
||||
* This code is licensed under MIT license (see LICENSE file for details)
|
||||
*/
|
||||
|
||||
package com.github.fishb1.apkinfo
|
||||
|
||||
import com.android.apksig.internal.apk.AndroidBinXmlParser
|
||||
import com.android.apksig.internal.apk.AndroidBinXmlParser.XmlParserException
|
||||
import java.io.InputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipInputStream
|
||||
|
||||
data class ApkInfo(
|
||||
val compileSdkVersion: Int = 0,
|
||||
val compileSdkVersionCodename: String = "",
|
||||
val installLocation: String = "",
|
||||
val packageName: String = "",
|
||||
val platformBuildVersionCode: Int = 0,
|
||||
val platformBuildVersionName: String = "",
|
||||
val versionCode: Int = 0,
|
||||
val versionName: String = "",
|
||||
) {
|
||||
|
||||
companion object {
|
||||
|
||||
private val EMPTY = ApkInfo()
|
||||
|
||||
private const val MANIFEST_FILE_NAME = "AndroidManifest.xml"
|
||||
|
||||
fun fromInputStream(stream: InputStream): ApkInfo {
|
||||
ZipInputStream(stream).use { zip ->
|
||||
var entry: ZipEntry?
|
||||
do {
|
||||
entry = zip.nextEntry
|
||||
if (entry?.name == MANIFEST_FILE_NAME) {
|
||||
val data = ByteBuffer.wrap(zip.readBytes())
|
||||
return try {
|
||||
val parser = AndroidBinXmlParser(data)
|
||||
ManifestUtils.readApkInfo(parser)
|
||||
} catch (e: XmlParserException) {
|
||||
EMPTY
|
||||
}
|
||||
}
|
||||
} while (entry != null)
|
||||
}
|
||||
return EMPTY
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2022 fishbone
|
||||
*
|
||||
* This code is licensed under MIT license (see LICENSE file for details)
|
||||
*/
|
||||
|
||||
package com.github.fishb1.apkinfo
|
||||
|
||||
internal class ApkInfoBuilder {
|
||||
|
||||
private var compileSdkVersion: Int = 0
|
||||
private var compileSdkVersionCodename: String = ""
|
||||
private var installLocation: String = ""
|
||||
private var packageName: String = ""
|
||||
private var platformBuildVersionCode: Int = 0
|
||||
private var platformBuildVersionName: String = ""
|
||||
private var versionCode: Int = 0
|
||||
private var versionName: String = ""
|
||||
|
||||
fun compileSdkVersion(value: Int) = apply {
|
||||
compileSdkVersion = value
|
||||
}
|
||||
|
||||
fun compileSdkVersionCodename(value: String) = apply {
|
||||
compileSdkVersionCodename = value
|
||||
}
|
||||
|
||||
fun installLocation(value: String) = apply {
|
||||
installLocation = value
|
||||
}
|
||||
|
||||
fun packageName(value: String) = apply {
|
||||
packageName = value
|
||||
}
|
||||
|
||||
fun platformBuildVersionCode(value: Int) = apply {
|
||||
platformBuildVersionCode = value
|
||||
}
|
||||
|
||||
fun platformBuildVersionName(value: String) = apply {
|
||||
platformBuildVersionName = value
|
||||
}
|
||||
|
||||
fun versionCode(value: Int) = apply {
|
||||
versionCode = value
|
||||
}
|
||||
|
||||
fun versionName(value: String) = apply {
|
||||
versionName = value
|
||||
}
|
||||
|
||||
fun build(): ApkInfo {
|
||||
return ApkInfo(
|
||||
compileSdkVersion = compileSdkVersion,
|
||||
compileSdkVersionCodename =compileSdkVersionCodename,
|
||||
installLocation = installLocation,
|
||||
packageName = packageName,
|
||||
platformBuildVersionCode = platformBuildVersionCode,
|
||||
platformBuildVersionName = platformBuildVersionName,
|
||||
versionCode = versionCode,
|
||||
versionName = versionName,
|
||||
)
|
||||
}
|
||||
}
|
||||
55
app/src/main/java/com/github/fishb1/apkinfo/ManifestUtils.kt
Normal file
55
app/src/main/java/com/github/fishb1/apkinfo/ManifestUtils.kt
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 2022 fishbone
|
||||
*
|
||||
* This code is licensed under MIT license (see LICENSE file for details)
|
||||
*/
|
||||
|
||||
package com.github.fishb1.apkinfo
|
||||
|
||||
import com.android.apksig.internal.apk.AndroidBinXmlParser
|
||||
|
||||
internal object ManifestUtils {
|
||||
|
||||
private const val TAG_MANIFEST = "manifest"
|
||||
|
||||
private const val ATTR_COMPILE_SDK_VERSION = "compileSdkVersion"
|
||||
private const val ATTR_COMPILE_SDK_VERSION_CODENAME = "compileSdkVersionCodename"
|
||||
private const val ATTR_INSTALL_LOCATION = "installLocation"
|
||||
private const val ATTR_PACKAGE = "package"
|
||||
private const val ATTR_PLATFORM_BUILD_VERSION_CODE = "platformBuildVersionCode"
|
||||
private const val ATTR_PLATFORM_BUILD_VERSION_NAME = "platformBuildVersionName"
|
||||
private const val ATTR_VERSION_CODE = "versionCode"
|
||||
private const val ATTR_VERSION_NAME = "versionName"
|
||||
|
||||
fun readApkInfo(parser: AndroidBinXmlParser): ApkInfo {
|
||||
val builder = ApkInfoBuilder()
|
||||
var eventType = parser.eventType
|
||||
while (eventType != AndroidBinXmlParser.EVENT_END_DOCUMENT) {
|
||||
if (eventType == AndroidBinXmlParser.EVENT_START_ELEMENT && parser.name == TAG_MANIFEST) {
|
||||
|
||||
for (i in 0 until parser.attributeCount) {
|
||||
when (parser.getAttributeName(i)) {
|
||||
ATTR_COMPILE_SDK_VERSION ->
|
||||
builder.compileSdkVersion(parser.getAttributeIntValue(i))
|
||||
ATTR_COMPILE_SDK_VERSION_CODENAME ->
|
||||
builder.compileSdkVersionCodename(parser.getAttributeStringValue(i))
|
||||
ATTR_INSTALL_LOCATION ->
|
||||
builder.installLocation(parser.getAttributeStringValue(i))
|
||||
ATTR_PACKAGE ->
|
||||
builder.packageName(parser.getAttributeStringValue(i))
|
||||
ATTR_PLATFORM_BUILD_VERSION_CODE ->
|
||||
builder.platformBuildVersionCode(parser.getAttributeIntValue(i))
|
||||
ATTR_PLATFORM_BUILD_VERSION_NAME ->
|
||||
builder.platformBuildVersionName(parser.getAttributeStringValue(i))
|
||||
ATTR_VERSION_CODE ->
|
||||
builder.versionCode(parser.getAttributeIntValue(i))
|
||||
ATTR_VERSION_NAME ->
|
||||
builder.versionName(parser.getAttributeStringValue(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
eventType = parser.next()
|
||||
}
|
||||
return builder.build()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user