Skip to content

Commit

Permalink
Fixed Build Config retrieving
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-minaiev-frontegg committed Aug 23, 2024
1 parent 966cd0a commit e705471
Showing 1 changed file with 57 additions and 30 deletions.
87 changes: 57 additions & 30 deletions android/src/main/kotlin/com/frontegg/flutter/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,64 @@ interface ActivityProvider {

val Context.constants: FronteggConstants
get() {
val packageName = this.packageName
val className = "$packageName.BuildConfig"
try {
val buildConfigClass = Class.forName(className)

// Get the field from BuildConfig class
val baseUrl = safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_DOMAIN", "")
val clientId = safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_CLIENT_ID", "")

val applicationId =
safeGetNullableValueFromBuildConfig(buildConfigClass, "FRONTEGG_APPLICATION_ID", "")

val useAssetsLinks =
safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_USE_ASSETS_LINKS", true)
val useChromeCustomTabs = safeGetValueFromBuildConfig(
buildConfigClass, "FRONTEGG_USE_CHROME_CUSTOM_TABS", true
)

return FronteggConstants(
baseUrl = baseUrl,
clientId = clientId,
applicationId = applicationId,
useAssetsLinks = useAssetsLinks,
useChromeCustomTabs = useChromeCustomTabs,
bundleId = this.packageName,
)
} catch (e: ClassNotFoundException) {
Log.e(TAG, "Class not found: $className")
throw e
val mainActivity = getLaunchActivityName(this)
Log.d(TAG, "packageName: ${packageName}, mainActivity: $mainActivity")
val buildConfigClass =
getBuildConfigClass(mainActivity?.substringBeforeLast('.') ?: this.packageName)

val baseUrl = safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_DOMAIN", "")
val clientId = safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_CLIENT_ID", "")

val applicationId =
safeGetNullableValueFromBuildConfig(buildConfigClass, "FRONTEGG_APPLICATION_ID", "")

val useAssetsLinks =
safeGetValueFromBuildConfig(buildConfigClass, "FRONTEGG_USE_ASSETS_LINKS", true)
val useChromeCustomTabs = safeGetValueFromBuildConfig(
buildConfigClass, "FRONTEGG_USE_CHROME_CUSTOM_TABS", true
)

return FronteggConstants(
baseUrl = baseUrl,
clientId = clientId,
applicationId = applicationId,
useAssetsLinks = useAssetsLinks,
useChromeCustomTabs = useChromeCustomTabs,
bundleId = this.packageName,
)
}

fun getLaunchActivityName(context: Context): String? {
val launcherIntent = context.packageManager.getLaunchIntentForPackage(context.packageName)
val launchActivityInfo = launcherIntent!!.resolveActivityInfo(context.packageManager, 0)
return try {
launchActivityInfo.name
} catch (e: Exception) {
null
}
}

fun getBuildConfigClass(packageName: String): Class<*> {
if (!packageName.contains('.')) {
throw ClassNotFoundException("Invalid package name: $packageName. Failed to retrieve BuildConfig class.")
}

val className = "$packageName.BuildConfig"

return try {
Class.forName(className)
} catch (e: ClassNotFoundException) {
Log.d(TAG, "Class not found: $className, checking parent namespace")

val parentPackageName = packageName.substringBeforeLast('.')

if (parentPackageName.isNotEmpty()) {
getBuildConfigClass(parentPackageName)
} else {
throw ClassNotFoundException("Failed to retrieve BuildConfig class for package: $packageName after checking all namespaces.")
}
}
}

fun <T> safeGetNullableValueFromBuildConfig(
buildConfigClass: Class<*>,
Expand All @@ -56,7 +83,7 @@ fun <T> safeGetNullableValueFromBuildConfig(
field.get(default) as T
} catch (e: Exception) {
Log.e(
TAG, "Field '$name' not found in BuildConfig, return default $default"
TAG, "Field '$name' not found in BuildConfig, return null"
)
null
}
Expand Down

0 comments on commit e705471

Please sign in to comment.