Skip to content

Commit

Permalink
feat: add locale to request context (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: Damian Jäger <[email protected]>
  • Loading branch information
philprime and DamianJaeger authored Aug 8, 2024
1 parent 7bd7748 commit dd34cd0
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 83 deletions.
47 changes: 34 additions & 13 deletions .gitignore
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
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,19 @@ override fun onResume() {

The OnLaunch Android client provides a couple of configuration options:

| Name | Description | Default |
|---------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------|
| `publicKey` | Public key used to authenticate with the API | |
| `baseUrl` | Base URL where the OnLaunch API is hosted at. Change this to point to your self-hosted instance of the OnLaunch server. | `https://onlaunch.kula.app/api/` |
| `shouldCheckOnInit` | Flag indicating if the client should check for new messages immediately after it has been initialized. | `true` |
| `useInAppUpdates` | Set to `true` to use Google Play In-App Updates to check for available updates. When using Google Play In-App Updates you accept the Google Play Terms of Service. See https://developer.android.com/guide/playcore/in-app-updates | `false` |
| Name | Description | Default |
|----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------|
| `publicKey` | Public key used to authenticate with the API. | |
| `baseUrl` | Base URL where the OnLaunch API is hosted at. Change this to point to your self-hosted instance of the OnLaunch server. | `https://onlaunch.kula.app/api/` |
| `shouldCheckOnInit` | Flag indicating if the client should check for new messages immediately after it has been initialized. | `true` |
| `useInAppUpdates` | Set to `true` to use Google Play In-App Updates to check for available updates. When using Google Play In-App Updates you accept the Google Play Terms of Service. See https://developer.android.com/guide/playcore/in-app-updates | `false` |
| `appStoreUrl` | 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`. | `https://play.google.com/store/apps/details?id=<PACKAGE_NAME>` |
| `packageName` | The package name of the app. Used by server-side rule evaluation. | Package name defined in the context |
| `versionCode` | The version code of the app. Used by server-side rule evaluation. | Version code defined in the package manager context |
| `versionName` | The version name of the app. Used by server-side rule evaluation. | Version name defined in the package manager context |
| `locale` | The locale of the app. Used by server-side rule evaluation. | Locale defined in `context.resources.configuration.locale`, i.e. `en_US` |
| `localeLanguageCode` | The language code of the locale. Used by server-side rule evaluation. | Language code defined in `context.resources.configuration.locale`, i.e. `en` |
| `localeRegionCode` | The region code of the locale. Used by server-side rule evaluation. | Region code defined in `context.resources.configuration.locale`, i.e. `US` |

# Contributing Guide

Expand Down
72 changes: 9 additions & 63 deletions client/src/main/java/app/kula/onlaunch/client/OnLaunch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package app.kula.onlaunch.client
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import app.kula.onlaunch.client.data.api.OnLaunchApi
import app.kula.onlaunch.client.data.dtos.toMessages
import app.kula.onlaunch.client.data.local.OnLaunchDataStore
import app.kula.onlaunch.client.ui.OnLaunchActivity
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.install.model.UpdateAvailability
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import kotlin.coroutines.resume
Expand Down Expand Up @@ -63,8 +61,11 @@ object OnLaunch {
versionCode = config.versionCode.toString(),
versionName = config.versionName,
platformName = "android",
platformVersion = android.os.Build.VERSION.SDK_INT.toString(),
platformVersion = Build.VERSION.SDK_INT.toString(),
updateAvailable = if (config.useInAppUpdates) checkUpdateAvailable(context = context) else null,
locale = config.locale,
localeLanguageCode = config.localeLanguageCode,
localeRegionCode = config.localeRegionCode,
).toMessages()
val dismissedIds = dataStore.getDismissedMessageIds()

Expand Down Expand Up @@ -128,7 +129,7 @@ object OnLaunch {
}
}

private data class OnLaunchConfig(
internal data class OnLaunchConfig(
val baseUrl: String,
val publicKey: String,
val shouldCheckOnInit: Boolean,
Expand All @@ -138,62 +139,7 @@ private data class OnLaunchConfig(
val versionName: String,
val useInAppUpdates: Boolean,
val appStoreUrl: String,
val locale: String,
val localeLanguageCode: String,
val localeRegionCode: String,
)

interface OnLaunchConfiguration {
var baseUrl: String?
var publicKey: String?

/**
* If set to true, OnLaunch will check for messages on initialization.
*
* Defaults to true
*/
var shouldCheckOnInit: Boolean?
var packageName: String?
var versionCode: Long?
var versionName: String?
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?
}

private 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

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 ?: context.packageManager.getPackageInfo(
context.packageName,
0
).versionCode.toLong(),
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}",
)
}
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?
}
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ internal interface OnLaunchApi {
@GET("v0.2/messages")
suspend fun getMessages(
@Header("x-api-key") publicKey: String,
@Header("X-ONLAUNCH-LOCALE") locale: String,
@Header("X-ONLAUNCH-LOCALE-LANGUAGE-CODE") localeLanguageCode: String,
@Header("X-ONLAUNCH-LOCALE-REGION-CODE") localeRegionCode: String,
@Header("X-ONLAUNCH-VERSION-CODE") versionCode: String,
@Header("X-ONLAUNCH-VERSION-NAME") versionName: String,
@Header("X-ONLAUNCH-PACKAGE-NAME") packageName: String,
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/java/app/kula/onlaunch/sample/SampleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class SampleApp : Application() {
super.onCreate()

OnLaunch.init(this) {
publicKey = ""
publicKey = "K2UX4fVPFyixVaeLn8Fky_uWhjMr-frADqKqpOCZW2c"
}
}
}

0 comments on commit dd34cd0

Please sign in to comment.