-
Notifications
You must be signed in to change notification settings - Fork 173
feat: Introduce a robust Google Maps initializer #758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
7b23a41
cdac9aa
6aefeda
06d200a
69984e7
0cd0f49
3e6ba8c
e150f37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package com.google.maps.android.compose | ||
|
|
||
| import android.content.Context | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import com.google.common.truth.Truth.assertThat | ||
| import com.google.maps.android.compose.internal.GoogleMapsInitializer | ||
| import com.google.maps.android.compose.internal.InitializationState | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.After | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| @RunWith(AndroidJUnit4::class) | ||
| class GoogleMapsInitializerTest { | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a random idea: since one of the motivations to do some work on the Maps Initializer was the positive we got on the StrictMode, wondering if it could make sense to test this with the StrictMode activated: @Before
fun setUp() {
StrictMode.setThreadPolicy(
StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectAll()
.penaltyLog()
.build()
)
StrictMode.setVmPolicy(
StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build()
)
}
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good idea. Thanks for the suggestion! |
||
| @After | ||
| fun tearDown() = runTest { | ||
| GoogleMapsInitializer.reset() | ||
| } | ||
|
|
||
| @Test | ||
| fun testInitializationSuccess() = runTest { | ||
| // In an instrumentation test environment, Google Play services are available. | ||
| // Therefore, we expect the initialization to succeed. | ||
| val context: Context = InstrumentationRegistry.getInstrumentation().targetContext | ||
|
|
||
| GoogleMapsInitializer.initialize(context) | ||
|
|
||
| assertThat(GoogleMapsInitializer.state.value).isEqualTo(InitializationState.SUCCESS) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package com.google.maps.android.compose | ||
|
|
||
| import android.content.Context | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import com.google.common.truth.Truth.assertThat | ||
| import com.google.maps.android.compose.internal.GoogleMapsInitializer | ||
| import com.google.maps.android.compose.internal.InitializationState | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.runTest | ||
| import org.junit.After | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| @RunWith(RobolectricTestRunner::class) | ||
| class GoogleMapsInitializerTest { | ||
|
|
||
| @After | ||
| fun tearDown() = runTest { | ||
| GoogleMapsInitializer.reset() | ||
| } | ||
|
|
||
| @Test | ||
| fun testInitializationFailure() = runTest { | ||
| // In a unit test environment, Google Play services are not available. | ||
| // Therefore, we expect the initialization to fail. | ||
| val context: Context = ApplicationProvider.getApplicationContext() | ||
|
|
||
| GoogleMapsInitializer.initialize(context) | ||
|
|
||
| // The initialization is now synchronous within the test scope, so we don't need to wait. | ||
| assertThat(GoogleMapsInitializer.state.value).isEqualTo(InitializationState.FAILURE) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package com.google.maps.android.compose.internal | ||
|
|
||
| import android.content.Context | ||
| import androidx.compose.runtime.State | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import com.google.android.gms.common.ConnectionResult | ||
| import com.google.android.gms.maps.MapsInitializer | ||
| import com.google.android.gms.maps.MapsApiSettings | ||
| import com.google.maps.android.compose.meta.AttributionId | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.sync.withLock | ||
| import kotlinx.coroutines.withContext | ||
|
|
||
| /** | ||
| * Enum representing the initialization state of the Google Maps SDK. | ||
| */ | ||
| public enum class InitializationState { | ||
| /** | ||
| * The SDK has not been initialized. | ||
| */ | ||
| UNINITIALIZED, | ||
|
|
||
| /** | ||
| * The SDK is currently being initialized. | ||
| */ | ||
| INITIALIZING, | ||
|
|
||
| /** | ||
| * The SDK has been successfully initialized. | ||
| */ | ||
| SUCCESS, | ||
|
|
||
| /** | ||
| * The SDK initialization failed. | ||
| */ | ||
| FAILURE | ||
| } | ||
|
|
||
| /** | ||
| * A singleton object to manage the initialization of the Google Maps SDK. | ||
| * | ||
| * This object provides a state machine to track the initialization process and ensures that | ||
| * the initialization is performed only once. It also provides a mechanism to reset the | ||
| * initialization state, which can be useful in test environments. | ||
| * | ||
| * The initialization process consists of two main steps: | ||
| * 1. Calling `MapsInitializer.initialize(context)` to initialize the Google Maps SDK. | ||
| * 2. Calling `MapsApiSettings.addInternalUsageAttributionId(context, attributionId)` to add | ||
| * the library's attribution ID to the Maps API settings. | ||
| * | ||
| * The state of the initialization is exposed via the `state` property, which is a [State] object | ||
| * that can be observed for changes. | ||
| */ | ||
| public object GoogleMapsInitializer { | ||
| private val _state = mutableStateOf(InitializationState.UNINITIALIZED) | ||
| public val state: State<InitializationState> = _state | ||
|
|
||
| private val mutex = Mutex() | ||
|
|
||
| /** | ||
| * The value of the attribution ID. Set this to the empty string to opt out of attribution. | ||
| * | ||
| * This must be set before calling the `initialize` function. | ||
| */ | ||
| public var attributionId: String = AttributionId.VALUE | ||
|
|
||
| /** | ||
| * Initializes the Google Maps SDK. | ||
| * | ||
| * This function starts the initialization process on a background thread. The process is | ||
| * performed only once. If the initialization is already in progress or has completed, | ||
| * this function does nothing. | ||
| * | ||
| * The initialization state can be observed via the `state` property. | ||
| * | ||
| * @param context The context to use for initialization. | ||
| */ | ||
| public suspend fun initialize(context: Context) { | ||
| // 1. Quick exit if already initialized or in progress. | ||
| if (_state.value != InitializationState.UNINITIALIZED) { | ||
| return | ||
| } | ||
|
|
||
| // 2. Acquire the mutex, perform a double-check (in case another | ||
| // coroutine was also waiting), and set the state. | ||
| // This block is synchronous and ensures only one coroutine | ||
| // proceeds to the IO operation. | ||
| mutex.withLock { | ||
| if (_state.value != InitializationState.UNINITIALIZED) { | ||
| return // Another coroutine won the race while this one was suspended on the lock. | ||
| } | ||
| _state.value = InitializationState.INITIALIZING | ||
| } | ||
|
|
||
| // The lock is now released. | ||
|
|
||
| // 3. Run the blocking initialization code on the IO dispatcher. | ||
| // This function will SUSPEND until the withContext(Dispatchers.IO) block completes. | ||
| // If the calling scope is cancelled while waiting, withContext will throw | ||
| // a CancellationException, and the state will remain INITIALIZING | ||
| // (which the catch block will update to FAILURE). | ||
| try { | ||
| withContext(Dispatchers.IO) { | ||
| // This is the blocking call. The thread will be blocked here. | ||
| // If cancellation happens, the thread STILL finishes this call, | ||
| // but the coroutine will immediately throw CancellationException | ||
| // *after* this call returns, skipping the state assignments below. | ||
| if (MapsInitializer.initialize(context) == ConnectionResult.SUCCESS) { | ||
| MapsApiSettings.addInternalUsageAttributionId(context, attributionId) | ||
| _state.value = InitializationState.SUCCESS | ||
| } else { | ||
| // Handle cases where initialize() returns a non-SUCCESS code | ||
| _state.value = InitializationState.FAILURE | ||
| } | ||
| } | ||
| } catch (_: Exception) { | ||
| // This will catch any exceptions from the init process (like from mocks in tests) | ||
| // Note: By default, this does NOT catch CancellationException. | ||
| _state.value = InitializationState.FAILURE | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resets the initialization state. | ||
| * | ||
| * This function cancels any ongoing initialization and resets the state to `UNINITIALIZED`. | ||
| * This is useful in test environments where you might need to re-initialize the SDK | ||
| * multiple times. | ||
| */ | ||
| public suspend fun reset() { | ||
| mutex.withLock { | ||
| _state.value = InitializationState.UNINITIALIZED | ||
| } | ||
| } | ||
| } |
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.