Skip to content

Commit

Permalink
[MOBILE-4147] Update locale targeting (#1371)
Browse files Browse the repository at this point in the history
* update locale targeting

* spotless
  • Loading branch information
Ulrico972 authored Feb 15, 2024
1 parent 6740736 commit 202e789
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,9 @@ private void init() {
components.add(this.remoteConfigManager);

DeviceInfoProvider infoProvider = new DeviceInfoProviderImpl(
pushManager::areNotificationsOptedIn, privacyManager::isEnabled,
channel::getTags, channel::getId, applicationMetrics::getCurrentAppVersion,
permissionsManager, contact::getStableContactId, PlatformUtils.asString(getPlatformType()));
pushManager::areNotificationsOptedIn, privacyManager::isEnabled, channel::getTags,
channel::getId, applicationMetrics::getCurrentAppVersion, permissionsManager,
contact::getStableContactId, PlatformUtils.asString(getPlatformType()), localeManager);

// Experiments
this.experimentManager = new ExperimentManager(application, preferenceDataStore,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,7 @@ public class AudienceSelector private constructor(builder: Builder) : JsonSerial
return true
}

val locale = dataProvider.getUserLocals(context).getFirstMatch(languageTags.toTypedArray())
?: return false
val locale = dataProvider.getUserLocale(context)

// getFirstMatch will return the default language if none of the specified locales are found,
// so we still have to verify the locale exists in the audience conditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ package com.urbanairship.audience

import android.content.Context
import androidx.annotation.RestrictTo
import androidx.core.os.ConfigurationCompat
import androidx.core.os.LocaleListCompat
import com.urbanairship.PrivacyManager
import com.urbanairship.UAirship
import com.urbanairship.locale.LocaleManager
import com.urbanairship.permission.Permission
import com.urbanairship.permission.PermissionStatus
import com.urbanairship.permission.PermissionsManager
import com.urbanairship.util.PlatformUtils
import java.util.Locale
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.sync.Mutex
Expand All @@ -23,7 +23,7 @@ import kotlinx.coroutines.sync.withLock
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public interface DeviceInfoProvider {
public fun userCutOffDate(context: Context): Long
public fun getUserLocals(context: Context): LocaleListCompat
public fun getUserLocale(context: Context): Locale
public fun isFeatureEnabled(@PrivacyManager.Feature feature: Int): Boolean
public suspend fun getPermissionStatuses(): Map<Permission, PermissionStatus>
public suspend fun getStableContactId(): String
Expand All @@ -45,7 +45,8 @@ public interface DeviceInfoProvider {
versionFetcher = UAirship.shared().applicationMetrics::getCurrentAppVersion,
permissionsManager = UAirship.shared().permissionsManager,
contactIdFetcher = UAirship.shared().contact::stableContactId,
platform = PlatformUtils.asString(UAirship.shared().platformType)
platform = PlatformUtils.asString(UAirship.shared().platformType),
localeManager = UAirship.shared().localeManager
)
}
}
Expand All @@ -58,7 +59,8 @@ internal class DeviceInfoProviderImpl(
private val versionFetcher: () -> Long,
private val permissionsManager: PermissionsManager,
private val contactIdFetcher: suspend () -> String,
override val platform: String
override val platform: String,
private val localeManager: LocaleManager
) : DeviceInfoProvider {

override fun userCutOffDate(context: Context): Long {
Expand All @@ -82,8 +84,8 @@ internal class DeviceInfoProviderImpl(
return privacyFeatureFetcher.invoke(feature)
}

override fun getUserLocals(context: Context): LocaleListCompat {
return ConfigurationCompat.getLocales(context.resources.configuration)
override fun getUserLocale(context: Context): Locale {
return localeManager.locale
}

override suspend fun getPermissionStatuses(): Map<Permission, PermissionStatus> {
Expand All @@ -102,7 +104,7 @@ internal class DeviceInfoProviderImpl(
override suspend fun snapshot(context: Context): DeviceInfoProvider {
return CachedDeviceInfoProvider(
cutOffTime = OneTimeValue { userCutOffDate(context) },
localeList = OneTimeValue { getUserLocals(context) },
locale = OneTimeValue { getUserLocale(context) },
privacyFeatureFetcher = privacyFeatureFetcher,
permissionStatuses = OneTimeValueSus { getPermissionStatuses() },
stableContactId = OneTimeValueSus { getStableContactId() },
Expand All @@ -117,7 +119,7 @@ internal class DeviceInfoProviderImpl(

internal class CachedDeviceInfoProvider(
private val cutOffTime: OneTimeValue<Long>,
private val localeList: OneTimeValue<LocaleListCompat>,
private val locale: OneTimeValue<Locale>,
private val privacyFeatureFetcher: (Int) -> Boolean,
private val permissionStatuses: OneTimeValueSus<Map<Permission, PermissionStatus>>,
private val stableContactId: OneTimeValueSus<String>,
Expand All @@ -128,7 +130,7 @@ internal class CachedDeviceInfoProvider(
cachedPlatform: OneTimeValue<String>
) : DeviceInfoProvider {
override fun userCutOffDate(context: Context): Long = cutOffTime.getValue()
override fun getUserLocals(context: Context): LocaleListCompat = localeList.getValue()
override fun getUserLocale(context: Context): Locale = locale.getValue()
override fun isFeatureEnabled(feature: Int): Boolean = privacyFeatureFetcher(feature)
override suspend fun getPermissionStatuses(): Map<Permission, PermissionStatus> = permissionStatuses.getValue()
override suspend fun getStableContactId(): String = stableContactId.getValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.urbanairship.json.JsonMatcher
import com.urbanairship.json.JsonPredicate
import com.urbanairship.json.JsonValue
import com.urbanairship.json.ValueMatcher
import com.urbanairship.locale.LocaleManager
import com.urbanairship.permission.Permission
import com.urbanairship.permission.PermissionStatus
import com.urbanairship.permission.PermissionsManager
Expand All @@ -22,10 +23,10 @@ import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import java.util.Arrays
import java.util.Locale
import junit.framework.TestCase
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.test.TestResult
import kotlinx.coroutines.test.runTest
import org.junit.Before
Expand Down Expand Up @@ -71,9 +72,12 @@ public class AudienceSelectorTest {
result
}

val localeManager: LocaleManager = mockk()
every { localeManager.locale } answers { Locale.US }

infoProvider = DeviceInfoProviderImpl(
{ notificationStatus }, { privacyFeatures[it] ?: false }, { channelTags },
{ channelId }, { version }, permissionManager, contactIdGetter, "android")
{ channelId }, { version }, permissionManager, contactIdGetter, "android", localeManager)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DeviceInfoProviderTest {
public fun setUp() {
infoProvider = DeviceInfoProviderImpl(
{ true }, { true }, { emptySet() },
{ "channel-id" }, { 1 }, mockk(), { "contact-id" }, "android"
{ "channel-id" }, { 1 }, mockk(), { "contact-id" }, "android", mockk()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public class ExperimentManagerTest {
versionFetcher = { 1 },
permissionsManager = permissionManager,
contactIdFetcher = { contactId },
platform = "android"
platform = "android",
mockk()
)

val clock: Clock = mockk()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public class FeatureFlagManager
is DeferredPayload -> {
val contactId = deviceInfoSnapshot.getStableContactId()
val chanelId = deviceInfoSnapshot.channelId ?: return Result.failure(FeatureFlagException.FailedToFetch())
val locale = deviceInfoSnapshot.getUserLocals(context).get(0) ?: return Result.failure(FeatureFlagException.FailedToFetch())
val locale = deviceInfoSnapshot.getUserLocale(context)
val request = DeferredRequest(
uri = payload.url,
channelID = chanelId,
Expand Down

0 comments on commit 202e789

Please sign in to comment.