From a4f07fcd2d24a058c0dd3c26cc9086689c56a3d6 Mon Sep 17 00:00:00 2001 From: shum Date: Mon, 10 Aug 2026 09:57:34 +0000 Subject: [PATCH] android: get the Google Play account country in the google flavor --- .../foss/java/chat/simplex/app/PlayStore.kt | 4 +++ .../google/java/chat/simplex/app/PlayStore.kt | 31 +++++++++++++++++++ .../main/java/chat/simplex/app/SimplexApp.kt | 2 ++ .../chat/simplex/common/platform/AppCommon.kt | 5 +++ .../chat/simplex/common/platform/Platform.kt | 2 ++ 5 files changed, 44 insertions(+) create mode 100644 apps/multiplatform/android/src/foss/java/chat/simplex/app/PlayStore.kt create mode 100644 apps/multiplatform/android/src/google/java/chat/simplex/app/PlayStore.kt diff --git a/apps/multiplatform/android/src/foss/java/chat/simplex/app/PlayStore.kt b/apps/multiplatform/android/src/foss/java/chat/simplex/app/PlayStore.kt new file mode 100644 index 0000000000..181fe42389 --- /dev/null +++ b/apps/multiplatform/android/src/foss/java/chat/simplex/app/PlayStore.kt @@ -0,0 +1,4 @@ +package chat.simplex.app + +// Play Billing is only in the google flavor, so the Play country stays unknown here +fun loadPlayStoreCountry() {} diff --git a/apps/multiplatform/android/src/google/java/chat/simplex/app/PlayStore.kt b/apps/multiplatform/android/src/google/java/chat/simplex/app/PlayStore.kt new file mode 100644 index 0000000000..a0e7734ff0 --- /dev/null +++ b/apps/multiplatform/android/src/google/java/chat/simplex/app/PlayStore.kt @@ -0,0 +1,31 @@ +package chat.simplex.app + +import chat.simplex.common.platform.androidAppContext +import chat.simplex.common.platform.androidPlayStoreCountry +import com.android.billingclient.api.* + +// Requests the country of the Google Play account into [androidPlayStoreCountry]. +// It stays null when Play is unavailable or the user is not signed in. +fun loadPlayStoreCountry() { + val client = BillingClient.newBuilder(androidAppContext) + .setListener { _, _ -> } + .enablePendingPurchases(PendingPurchasesParams.newBuilder().enableOneTimeProducts().build()) + .build() + client.startConnection(object : BillingClientStateListener { + override fun onBillingSetupFinished(result: BillingResult) { + if (result.responseCode != BillingClient.BillingResponseCode.OK) { + client.endConnection() + return + } + client.getBillingConfigAsync(GetBillingConfigParams.newBuilder().build()) { configResult, config -> + if (configResult.responseCode == BillingClient.BillingResponseCode.OK) { + androidPlayStoreCountry.value = config?.countryCode + } + client.endConnection() + } + } + + // The connection is only used for this one request, it is not retried + override fun onBillingServiceDisconnected() = client.endConnection() + }) +} diff --git a/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt b/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt index 2772922fac..ce47d2c5de 100644 --- a/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt +++ b/apps/multiplatform/android/src/main/java/chat/simplex/app/SimplexApp.kt @@ -341,6 +341,8 @@ class SimplexApp: Application(), LifecycleEventObserver { override fun androidIsXiaomiDevice(): Boolean = setOf("xiaomi", "redmi", "poco").contains(Build.BRAND.lowercase()) + override fun androidLoadPlayStoreCountry() = loadPlayStoreCountry() + @SuppressLint("SourceLockedOrientationActivity") @Composable override fun androidLockPortraitOrientation() { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/AppCommon.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/AppCommon.kt index 7a96bd99d2..140c1951ee 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/AppCommon.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/AppCommon.kt @@ -1,5 +1,7 @@ package chat.simplex.common.platform +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.mutableStateOf import chat.simplex.common.BuildConfigCommon import chat.simplex.common.model.* import chat.simplex.common.ui.theme.DefaultTheme @@ -30,6 +32,9 @@ else val databaseBackend: String = if (appPlatform == AppPlatform.ANDROID) "sqlite" else BuildConfigCommon.DATABASE_BACKEND +// Country of the Google Play account, only set in the google flavor of the Android app +val androidPlayStoreCountry: MutableState = mutableStateOf(null) + class FifoQueue(private var capacity: Int) : LinkedList() { override fun add(element: E): Boolean { if (size > capacity) removeFirstOrNull() diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Platform.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Platform.kt index dc6bdf9b2f..b46123c9cf 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Platform.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/platform/Platform.kt @@ -29,6 +29,8 @@ interface PlatformInterface { fun androidRestartNetworkObserver() {} fun androidCreateActiveCallState(): Closeable = Closeable { } fun androidIsXiaomiDevice(): Boolean = false + // Requests the Google Play account country into [androidPlayStoreCountry] + fun androidLoadPlayStoreCountry() {} val androidApiLevel: Int? get() = null // The build distributed via Google Play, which has to follow its policies val androidIsPlayStoreBuild: Boolean get() = false