Skip to content

Commit

Permalink
Migrate PackageManager Utilities to :ceres:core:foundation
Browse files Browse the repository at this point in the history
- Migrated the PackageManager utilities from the current location to the `ceres:core:foundation` module.
- Updated the usage of these utilities throughout the codebase to use the new methods provided by the `ceres:core:foundation` module.
  • Loading branch information
teogor committed Oct 1, 2023
1 parent b104f01 commit ad36844
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 34 deletions.
2 changes: 1 addition & 1 deletion core/runtime/api/runtime.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public final class dev/teogor/ceres/core/runtime/AppMetadataManager {
public final fun getPackageManager ()Landroid/content/pm/PackageManager;
public final fun getPackageName ()Ljava/lang/String;
public final fun getSanitizedPackageName ()Ljava/lang/String;
public final fun getVersionCode ()I
public final fun getVersionCode ()J
public final fun getVersionName ()Ljava/lang/String;
public final fun getZoneOffset ()Ljava/time/ZoneOffset;
public final fun isDebuggable ()Z
Expand Down
1 change: 1 addition & 0 deletions core/runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ android {

dependencies {
api(project(":core:startup"))
api(project(":core:foundation"))
}

ceresLibrary {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ package dev.teogor.ceres.core.runtime

import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.util.Base64
import dev.teogor.ceres.core.android.config.BuildConfig
import dev.teogor.ceres.core.runtime.AppMetadataManager.AppMetadataManagerImpl.getPackageInfoCompat
import dev.teogor.ceres.core.foundation.packageManagerUtils
import dev.teogor.ceres.core.startup.ApplicationContextProvider
import java.io.File
import java.io.FileInputStream
Expand Down Expand Up @@ -55,21 +52,16 @@ object AppMetadataManager {
}

val packageManager: PackageManager
get() = ApplicationContextProvider.context.packageManager
get() = ApplicationContextProvider.context.packageManagerUtils().packageManager

val packageInfo: PackageInfo
get() = packageManager.getPackageInfoCompat(packageName)
get() = ApplicationContextProvider.context.packageManagerUtils().packageInfo

val versionName: String
get() = packageInfo.versionName
get() = ApplicationContextProvider.context.packageManagerUtils().versionName

val versionCode: Int
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
packageInfo.longVersionCode.toInt()
} else {
@Suppress("DEPRECATION")
packageInfo.versionCode
}
val versionCode: Long
get() = ApplicationContextProvider.context.packageManagerUtils().versionCode

val zoneOffset = ZoneId.systemDefault().rules.getOffset(LocalDateTime.now())
val buildDateTime = LocalDateTime.ofEpochSecond(
Expand Down Expand Up @@ -110,16 +102,4 @@ object AppMetadataManager {
}
return null
}

private object AppMetadataManagerImpl {
fun PackageManager.getPackageInfoCompat(
packageName: String,
flags: Int = 0,
): PackageInfo = if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(flags.toLong()))
} else {
// @Suppress("DEPRECATION")
getPackageInfo(packageName, flags)
}
}
}
2 changes: 2 additions & 0 deletions firebase/crashlytics/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ android {
}

dependencies {
implementation(project(":core:foundation"))

implementation(libs.firebase.crashlytics)
implementation(platform(libs.firebase.bom))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.google.firebase.crashlytics.CustomKeysAndValues.Builder
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import dev.teogor.ceres.core.foundation.packageManagerUtils
import java.util.Locale

internal class CrashlyticsManager(context: Context) {
Expand Down Expand Up @@ -212,14 +213,12 @@ private fun Builder.packageInstaller(context: Context) {
}

private fun Builder.app(context: Context) {
val packageManager = context.packageManager
val packageName = context.packageName
val packageManagerUtils = context.packageManagerUtils()
val packageManager = packageManagerUtils.packageManager
val packageName = packageManagerUtils.packageName
val packageSignatures = packageManagerUtils.packageSignatures

val packageInfoSignatures = packageManager.getPackageInfo(
packageName,
PackageManager.GET_SIGNATURES,
)
val signatures = packageInfoSignatures.signatures.joinToString(", ") { signature ->
val signatures = packageSignatures.apkContentsSigners.joinToString(", ") { signature ->
signature.toCharsString()
}
putString("app_signatures", signatures)
Expand Down

0 comments on commit ad36844

Please sign in to comment.