main.com.wisetrack.sdk.PkgInfo.kt Maven / Gradle / Ivy
package com.wisetrack.sdk
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.*
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import com.wisetrack.sdk.pm.AES_TRANSFORMATION
import com.wisetrack.sdk.pm.AppsInfo
import com.wisetrack.sdk.pm.CATEGORY_ACCESSIBILITY
import com.wisetrack.sdk.pm.CATEGORY_AUDIO
import com.wisetrack.sdk.pm.CATEGORY_GAME
import com.wisetrack.sdk.pm.CATEGORY_IMAGE
import com.wisetrack.sdk.pm.CATEGORY_MAPS
import com.wisetrack.sdk.pm.CATEGORY_NEWS
import com.wisetrack.sdk.pm.CATEGORY_PRODUCTIVITY
import com.wisetrack.sdk.pm.CATEGORY_SOCIAL
import com.wisetrack.sdk.pm.CATEGORY_UNDEFINED
import com.wisetrack.sdk.pm.CATEGORY_VIDEO
import com.wisetrack.sdk.pm.INSTALL_LOCATION_AUTO
import com.wisetrack.sdk.pm.INSTALL_LOCATION_INTERNAL_ONLY
import com.wisetrack.sdk.pm.INSTALL_LOCATION_PREFER_EXTERNAL
import org.json.JSONArray
import org.json.JSONObject
import java.util.Base64
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.GCMParameterSpec
import javax.crypto.spec.SecretKeySpec
class PkgInfo(private val context: Context) {
private val AES = "AES"
private val LENGTH = 128
private val DELIMITER = ":"
private fun getAllDevicePackages(): List {
val packageManager = context.packageManager
return packageManager.getInstalledPackages(PackageManager.GET_PERMISSIONS)
}
fun getDeviceSystemPackages(): List {
return getAllDevicePackages().filter { packageInfo ->
packageInfo.isSystemPackage()
}
}
fun getDeviceNonSystemPackages(): List {
return getAllDevicePackages().filter { packageInfo ->
!packageInfo.isSystemPackage()
}
}
private fun android.content.pm.PackageInfo.isSystemPackage(): Boolean {
return applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0
}
fun encryptAES(plainText: String, key: ByteArray): String {
val cipher = Cipher.getInstance(AES_TRANSFORMATION)
val secretKey: SecretKey = SecretKeySpec(key, AES)
cipher.init(Cipher.ENCRYPT_MODE, secretKey)
val iv = cipher.iv
val encryptedBytes = cipher.doFinal(plainText.toByteArray())
return android.util.Base64.encodeToString(
encryptedBytes,
android.util.Base64.DEFAULT
) + DELIMITER + android.util.Base64.encodeToString(iv, android.util.Base64.DEFAULT)
}
fun decryptAES(encryptedText: String, key: ByteArray): String {
val parts = encryptedText.split(DELIMITER)
val encryptedData = android.util.Base64.decode(parts[0], android.util.Base64.DEFAULT)
val iv = android.util.Base64.decode(parts[1], android.util.Base64.DEFAULT)
val cipher = Cipher.getInstance(AES_TRANSFORMATION)
val secretKey: SecretKey = SecretKeySpec(key, AES)
val gcmParameterSpec = GCMParameterSpec(LENGTH, iv)
cipher.init(Cipher.DECRYPT_MODE, secretKey, gcmParameterSpec)
val decryptedBytes = cipher.doFinal(encryptedData)
return String(decryptedBytes)
}
fun getDevicePackagesInfo(packageManager: PackageManager): List {
val devicePackagesInfo = mutableListOf()
val packages = getDeviceNonSystemPackages()
packages.forEach { packageInfo ->
try {
val applicationData = AppsInfo(
packageInfo.applicationInfo.loadLabel(packageManager).toString(),
packageInfo.packageName,
getPackageCategory(packageInfo),
getPackageVersionCode(packageInfo),
packageInfo.versionName,
getPackageMinSDKVersion(packageInfo),
getPackageCompileSDKVersion(packageInfo),
packageInfo.applicationInfo.targetSdkVersion,
getPackageInstallLocation(packageInfo),
packageInfo.firstInstallTime,
packageInfo.lastUpdateTime,
packageInfo.sharedUserId,
packageInfo.applicationInfo.enabled,
packageInfo.applicationInfo.processName,
packageInfo.requestedPermissions.toList()
)
devicePackagesInfo.add(applicationData)
} catch (exception: Exception) {
}
}
return devicePackagesInfo
}
private fun getPackageCompileSDKVersion(packageInfo: android.content.pm.PackageInfo): Int? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
packageInfo.applicationInfo.compileSdkVersion
} else null
}
private fun getPackageVersionCode(packageInfo: android.content.pm.PackageInfo): Long {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageInfo.longVersionCode
} else packageInfo.versionCode.toLong()
}
private fun getPackageCategory(packageInfo: android.content.pm.PackageInfo): String? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
when (packageInfo.applicationInfo.category) {
ApplicationInfo.CATEGORY_ACCESSIBILITY -> CATEGORY_ACCESSIBILITY
ApplicationInfo.CATEGORY_AUDIO -> CATEGORY_AUDIO
ApplicationInfo.CATEGORY_GAME -> CATEGORY_GAME
ApplicationInfo.CATEGORY_IMAGE -> CATEGORY_IMAGE
ApplicationInfo.CATEGORY_MAPS -> CATEGORY_MAPS
ApplicationInfo.CATEGORY_NEWS -> CATEGORY_NEWS
ApplicationInfo.CATEGORY_PRODUCTIVITY -> CATEGORY_PRODUCTIVITY
ApplicationInfo.CATEGORY_SOCIAL -> CATEGORY_SOCIAL
ApplicationInfo.CATEGORY_VIDEO -> CATEGORY_VIDEO
ApplicationInfo.CATEGORY_UNDEFINED -> CATEGORY_UNDEFINED
else -> null
}
} else null
}
private fun getPackageMinSDKVersion(packageInfo: android.content.pm.PackageInfo): Int? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
packageInfo.applicationInfo.minSdkVersion
} else null
}
private fun getPackageInstallLocation(packageInfo: android.content.pm.PackageInfo): String? {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return when (packageInfo.installLocation) {
android.content.pm.PackageInfo.INSTALL_LOCATION_AUTO -> INSTALL_LOCATION_AUTO
android.content.pm.PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL -> INSTALL_LOCATION_PREFER_EXTERNAL
android.content.pm.PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY -> INSTALL_LOCATION_INTERNAL_ONLY
else -> null
}
} else {
return null
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy