@@ -3,16 +3,66 @@ package io.snabble.sdk
33import android.app.Application
44import android.content.Context
55import android.content.pm.PackageManager
6+ import android.content.res.Resources
67import androidx.startup.Initializer
78import io.snabble.sdk.utils.Logger
89import okhttp3.Interceptor
10+ import java.lang.IllegalStateException
11+ import java.util.*
912
1013/* *
1114 * Initializer for the snabble SDK using androidx.startup
1215 */
1316class SnabbleInitializer : Initializer <Snabble > {
1417 override fun create (context : Context ): Snabble {
1518 val app = context.applicationContext as Application
19+
20+ // load properties created by the gradle plugin
21+ val env = UserPreferences (context).environment.name.lowercase()
22+ val resId = context.resources.getIdentifier(" snabble_${env} _config" , " raw" , app.packageName)
23+ if (resId != Resources .ID_NULL ) {
24+ val properties = Properties ()
25+ properties.load(context.resources.openRawResource(resId))
26+ val config = Config ().apply {
27+ appId = properties.getProperty(" appId" )
28+ endpointBaseUrl = properties.getProperty(" endpointBaseUrl" ) ? : endpointBaseUrl
29+ secret = properties.getProperty(" secret" )
30+ val assetPath = properties.getProperty(" bundledMetadataAssetPath" )?.let { path ->
31+ if (context.resources.assets.list(path)?.isEmpty() != false ) {
32+ null
33+ } else {
34+ path
35+ }
36+ }
37+ bundledMetadataAssetPath = assetPath
38+ bundledMetadataRawResId = context.resources.getIdentifier(" snabble_${env} _metadata" , " raw" , app.packageName)
39+ generateSearchIndex = properties.getBoolean(" generateSearchIndex" , generateSearchIndex)
40+ maxProductDatabaseAge = properties.getLong(" maxProductDatabaseAge" , maxProductDatabaseAge)
41+ maxShoppingCartAge = properties.getLong(" maxShoppingCartAge" , maxShoppingCartAge)
42+ disableCertificatePinning = properties.getBoolean(" disableCertificatePinning" , disableCertificatePinning)
43+ vibrateToConfirmCartFilled = properties.getBoolean(" vibrateToConfirmCartFilled" , vibrateToConfirmCartFilled)
44+ loadActiveShops = properties.getBoolean(" loadActiveShops" , loadActiveShops)
45+ checkInRadius = properties.getFloat(" checkInRadius" , checkInRadius)
46+ checkOutRadius = properties.getFloat(" checkOutRadius" , checkOutRadius)
47+ lastSeenThreshold = properties.getLong(" lastSeenThreshold" , lastSeenThreshold)
48+ networkInterceptor =
49+ try {
50+ Class .forName(properties.getProperty(" networkInterceptor" , null ))?.newInstance() as Interceptor ?
51+ } catch (e: Throwable ) {
52+ Logger .w(" Could not instantiate network interceptor" , e.message)
53+ null
54+ }
55+ manualProductDatabaseUpdates = properties.getBoolean(" manualProductDatabaseUpdates" , manualProductDatabaseUpdates)
56+ }
57+
58+ if (config.appId == null || config.secret == null ) {
59+ throw IllegalStateException (" Please file a bug report with our build.gradle file. This state should not be possible." )
60+ } else {
61+ Snabble .setup(app, config)
62+ return Snabble
63+ }
64+ }
65+
1666 val applicationInfo = app.packageManager.getApplicationInfo(app.packageName, PackageManager .GET_META_DATA )
1767 with (applicationInfo.metaData) {
1868 if (getBoolean(" snabble_auto_initialization_disabled" )) {
@@ -24,12 +74,10 @@ class SnabbleInitializer : Initializer<Snabble> {
2474 endpointBaseUrl = getString(" snabble_endpoint_baseurl" , endpointBaseUrl)
2575 secret = getString(" snabble_secret" , secret)
2676 bundledMetadataAssetPath = getString(" snabble_bundled_metadata_asset_path" , bundledMetadataAssetPath)
27- versionName = getString(" snabble_version_name" , versionName)
2877 generateSearchIndex = getBoolean(" snabble_generate_search_index" , generateSearchIndex)
2978 maxProductDatabaseAge = getLong(" snabble_max_product_database_age" , maxProductDatabaseAge)
3079 maxShoppingCartAge = getLong(" snabble_max_shopping_cart_age" , maxShoppingCartAge)
3180 disableCertificatePinning = getBoolean(" snabble_disable_certificate_pinning" )
32- initialSQL = getStringArrayList(" snabble_initial_sql" ) ? : initialSQL
3381 vibrateToConfirmCartFilled = getBoolean(" snabble_vibrate_to_confirm_cart_filled" , vibrateToConfirmCartFilled)
3482 loadActiveShops = getBoolean(" snabble_load_active_shops" , loadActiveShops)
3583 checkInRadius = getFloat(" snabble_check_in_radius" , checkInRadius)
@@ -56,7 +104,5 @@ class SnabbleInitializer : Initializer<Snabble> {
56104 }
57105 }
58106
59- override fun dependencies (): List <Class <out Initializer <* >>> {
60- return emptyList()
61- }
107+ override fun dependencies () = emptyList<Class <out Initializer <* >>>()
62108}
0 commit comments