Skip to content

Added Null Support in BuiltWithUtil.kt & Util.kt to make compatible with kotlin latest version. #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class BuiltWithUtil {

companion object {

fun getPlatform(applicationInfo: ApplicationInfo): String {
val apkPath = applicationInfo.sourceDir
fun getPlatform(applicationInfo: ApplicationInfo?): String {
val apkPath = applicationInfo?.sourceDir ?: return "unknown"
val zipFile = ZipFile(apkPath)
val entries: List<String> = zipFile.entries().toList().map { entry -> entry.name }
return if (isFlutterApp(entries)) {
Expand Down Expand Up @@ -49,7 +49,7 @@ class BuiltWithUtil {
}

fun getAppNameFromPackage(context: Context, packageInfo: PackageInfo): String {
return packageInfo.applicationInfo.loadLabel(context.packageManager).toString()
return packageInfo.applicationInfo?.loadLabel(context.packageManager)?.toString() ?: "Unknown"
}


Expand Down
42 changes: 30 additions & 12 deletions android/src/main/kotlin/com/sharmadhiraj/installed_apps/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,44 @@ import java.io.File

class Util {
companion object {

// Convert ApplicationInfo to a map of app details
fun convertAppToMap(
packageManager: PackageManager,
app: ApplicationInfo,
app: ApplicationInfo?,
withIcon: Boolean,
platformType: PlatformType?,
): HashMap<String, Any?> {
val map = HashMap<String, Any?>()
map["name"] = packageManager.getApplicationLabel(app)
map["package_name"] = app.packageName
map["icon"] =
if (withIcon) DrawableUtil.drawableToByteArray(app.loadIcon(packageManager))
else ByteArray(0)
val packageInfo = packageManager.getPackageInfo(app.packageName, 0)
map["version_name"] = packageInfo.versionName
map["version_code"] = getVersionCode(packageInfo)
map["built_with"] = platformType?.value ?: BuiltWithUtil.getPlatform(packageInfo.applicationInfo)
map["installed_timestamp"] = File(packageInfo.applicationInfo.sourceDir).lastModified()

// Ensure app is not null
if (app != null) {
map["name"] = packageManager.getApplicationLabel(app) ?: "Unknown"
map["package_name"] = app.packageName
map["icon"] =
if (withIcon) DrawableUtil.drawableToByteArray(app.loadIcon(packageManager))
else ByteArray(0)

val packageInfo = packageManager.getPackageInfo(app.packageName, 0)
map["version_name"] = packageInfo.versionName ?: "Unknown"
map["version_code"] = getVersionCode(packageInfo)
map["built_with"] = platformType?.value ?: BuiltWithUtil.getPlatform(app)
map["installed_timestamp"] = File(app.sourceDir).lastModified()
} else {
// Handle the case where app is null, returning a map with "null" values
map["name"] = "Unknown"
map["package_name"] = "Unknown"
map["icon"] = ByteArray(0)
map["version_name"] = "Unknown"
map["version_code"] = -1
map["built_with"] = "Unknown"
map["installed_timestamp"] = -1
}

return map
}

// Retrieve PackageManager from the context
fun getPackageManager(context: Context): PackageManager {
return context.packageManager
}
Expand All @@ -40,4 +58,4 @@ class Util {
else packageInfo.longVersionCode
}
}
}
}