Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,7 @@ public fun GoogleMap(
mapView.setViewTreeLifecycleOwner(lifecycleOwner)
mapView.setViewTreeSavedStateRegistryOwner(savedStateRegistryOwner)

val componentCallbacks = object : ComponentCallbacks2 {
override fun onConfigurationChanged(newConfig: Configuration) {}

@Deprecated(
"Deprecated in Java",
ReplaceWith("onTrimMemory(level)"),
)
override fun onLowMemory() {
mapView.onLowMemory()
}

override fun onTrimMemory(level: Int) {
mapView.onLowMemory()
}
}
val componentCallbacks = mapView.componentCallbacks2()
context.registerComponentCallbacks(componentCallbacks)

val lifecycleObserver = MapLifecycleEventObserver(mapView)
Expand Down Expand Up @@ -397,6 +383,24 @@ internal class MapAttachStateChangeListener(private val lifecycleObserver: MapLi
}
}

internal fun MapView.componentCallbacks2(): ComponentCallbacks2 = object : ComponentCallbacks2 {
override fun onConfigurationChanged(newConfig: Configuration) {}

@Deprecated(
"Deprecated in Java",
ReplaceWith("onTrimMemory(level)"),
)
override fun onLowMemory() {
this@componentCallbacks2.onLowMemory()
}

override fun onTrimMemory(level: Int) {
if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
this@componentCallbacks2.onLowMemory()
}
}
}

internal val MapView.tagDataOrNull: MapTagData?
get() = tag as? MapTagData

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private fun StreetViewPanoramaView.lifecycleObserver(
previousState.value = event
}

private fun StreetViewPanoramaView.componentCallbacks2(): ComponentCallbacks2 = object : ComponentCallbacks2 {
internal fun StreetViewPanoramaView.componentCallbacks2(): ComponentCallbacks2 = object : ComponentCallbacks2 {
override fun onConfigurationChanged(config: Configuration) {}

@Deprecated("Deprecated in Java", ReplaceWith("onTrimMemory(level)"))
Expand All @@ -200,6 +200,8 @@ private fun StreetViewPanoramaView.componentCallbacks2(): ComponentCallbacks2 =
}

override fun onTrimMemory(level: Int) {
this@componentCallbacks2.onLowMemory()
if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
this@componentCallbacks2.onLowMemory()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.StreetViewPanoramaView
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.TileOverlay
import com.google.common.truth.Truth.assertThat
import com.google.maps.android.compose.streetview.componentCallbacks2
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
Expand Down Expand Up @@ -399,4 +401,40 @@ public class MapApplierCleanupTest {
unmockkStatic(Log::class)
}
}

@Suppress("DEPRECATION")
@Test
public fun `MapView componentCallbacks2 only calls onLowMemory for UI_HIDDEN and higher`() {
val mapView = mockk<MapView>(relaxed = true)
val callbacks = mapView.componentCallbacks2()

callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE)
callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW)
callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL)
verify(exactly = 0) { mapView.onLowMemory() }

callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN)
verify(exactly = 1) { mapView.onLowMemory() }

callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND)
verify(exactly = 2) { mapView.onLowMemory() }
}

@Suppress("DEPRECATION")
@Test
public fun `StreetViewPanoramaView componentCallbacks2 only calls onLowMemory for UI_HIDDEN and higher`() {
val streetView = mockk<StreetViewPanoramaView>(relaxed = true)
val callbacks = streetView.componentCallbacks2()

callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_MODERATE)
callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW)
callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL)
verify(exactly = 0) { streetView.onLowMemory() }

callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN)
verify(exactly = 1) { streetView.onLowMemory() }

callbacks.onTrimMemory(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND)
verify(exactly = 2) { streetView.onLowMemory() }
}
}
Loading