-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add locale to request context (#32)
Co-authored-by: Damian Jäger <[email protected]>
- Loading branch information
1 parent
7bd7748
commit dd34cd0
Showing
7 changed files
with
186 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,36 @@ | ||
# Gradle files | ||
.gradle/ | ||
build/ | ||
|
||
# Local configuration file (sdk path, etc) | ||
local.properties | ||
|
||
# Log/OS Files | ||
*.log | ||
|
||
# Android Studio generated files and folders | ||
captures/ | ||
.externalNativeBuild/ | ||
.cxx/ | ||
*.apk | ||
output.json | ||
|
||
# IntelliJ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/caches | ||
/.idea/libraries | ||
/.idea/modules.xml | ||
/.idea/workspace.xml | ||
/.idea/navEditor.xml | ||
/.idea/assetWizardSettings.xml | ||
.idea/ | ||
misc.xml | ||
deploymentTargetDropDown.xml | ||
render.experimental.xml | ||
|
||
# Keystore files | ||
*.jks | ||
*.keystore | ||
|
||
# Google Services (e.g. APIs or Firebase) | ||
google-services.json | ||
|
||
# Android Profiling | ||
*.hprof | ||
|
||
# macOS | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
client/src/main/java/app/kula/onlaunch/client/OnLaunchConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package app.kula.onlaunch.client | ||
|
||
import android.content.Context | ||
|
||
interface OnLaunchConfiguration { | ||
/** | ||
* Base URL where the OnLaunch API is hosted at. Change this to point to your self-hosted instance of the OnLaunch server. | ||
* | ||
* Defaults to `https://onlaunch.kula.app/api/` | ||
*/ | ||
var baseUrl: String? | ||
|
||
/** Public key used to authenticate with the API */ | ||
var publicKey: String? | ||
|
||
/** | ||
* If set to true, OnLaunch will check for messages on initialization. | ||
* | ||
* Defaults to `true` | ||
*/ | ||
var shouldCheckOnInit: Boolean? | ||
|
||
/** The package name of the app. Used by server-side rule evaluation. */ | ||
var packageName: String? | ||
|
||
/** The version code of the app. Used by server-side rule evaluation. */ | ||
var versionCode: Long? | ||
|
||
/** The version name of the app. Used by server-side rule evaluation. */ | ||
var versionName: String? | ||
|
||
/** | ||
* URL to the app store where the app can be updated. Used to open the app store. | ||
* The package name in the default value is NOT the `packageName` parameter, but the package name provided by the [Context]. | ||
* | ||
* Defaults to `https://play.google.com/store/apps/details?id=<PACKAGE_NAME>` | ||
*/ | ||
var appStoreUrl: String? | ||
|
||
/** | ||
* Set to true to use Google Play In-App Updates to check for available updates. | ||
* When using Google Play In-App Updates you have to accept the Google Play Terms of Service. | ||
* | ||
* Defaults to `false` | ||
* @see https://developer.android.com/guide/playcore/in-app-updates | ||
*/ | ||
var useInAppUpdates: Boolean? | ||
|
||
/** The locale of the app. Used by server-side rule evaluation. */ | ||
var locale: String? | ||
|
||
/** The language code of the locale. Used by server-side rule evaluation. */ | ||
var localeLanguageCode: String? | ||
|
||
/** The region code of the locale. Used by server-side rule evaluation. */ | ||
var localeRegionCode: String? | ||
} |
69 changes: 69 additions & 0 deletions
69
client/src/main/java/app/kula/onlaunch/client/OnLaunchConfigurationBulder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package app.kula.onlaunch.client | ||
|
||
import android.content.Context | ||
import android.os.Build | ||
import android.util.Log | ||
import kotlinx.coroutines.CoroutineExceptionHandler | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.plus | ||
import java.util.Locale | ||
|
||
internal class OnLaunchConfigurationBuilder : OnLaunchConfiguration { | ||
override var baseUrl: String? = null | ||
override var publicKey: String? = null | ||
override var shouldCheckOnInit: Boolean? = null | ||
override var packageName: String? = null | ||
override var versionCode: Long? = null | ||
override var versionName: String? = null | ||
override var useInAppUpdates: Boolean? = null | ||
override var appStoreUrl: String? = null | ||
override var locale: String? = null | ||
override var localeLanguageCode: String? = null | ||
override var localeRegionCode: String? = null | ||
|
||
internal fun getConfig(context: Context) = OnLaunchConfig( | ||
baseUrl = baseUrl ?: "https://onlaunch.kula.app/api/", | ||
publicKey = publicKey | ||
?: throw IllegalArgumentException("Failed to initialize OnLaunch: publicKey not set"), | ||
shouldCheckOnInit = shouldCheckOnInit ?: true, | ||
scope = (MainScope() + CoroutineExceptionHandler { _, throwable -> | ||
Log.e(OnLaunch.LOG_TAG, throwable.message, throwable) | ||
}), | ||
versionCode = versionCode ?: getVersionCode(context), | ||
versionName = versionName ?: context.packageManager.getPackageInfo( | ||
context.packageName, | ||
0 | ||
).versionName, | ||
packageName = packageName ?: context.packageName, | ||
useInAppUpdates = useInAppUpdates ?: false, | ||
appStoreUrl = appStoreUrl | ||
?: "https://play.google.com/store/apps/details?id=${context.packageName}", | ||
locale = locale ?: getLocale(context).toString(), | ||
localeLanguageCode = localeLanguageCode ?: getLocale(context).language, | ||
localeRegionCode = localeRegionCode ?: getLocale(context).country | ||
) | ||
|
||
private fun getVersionCode(context: Context): Long { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
context.packageManager.getPackageInfo( | ||
context.packageName, | ||
0 | ||
).longVersionCode | ||
} else { | ||
@Suppress("DEPRECATION") | ||
context.packageManager.getPackageInfo( | ||
context.packageName, | ||
0 | ||
).versionCode.toLong() | ||
} | ||
} | ||
|
||
private fun getLocale(context: Context): Locale { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
context.resources.configuration.locales[0] | ||
} else { | ||
@Suppress("DEPRECATION") | ||
context.resources.configuration.locale | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters