Port: Allow setting padding when camera is tracking (#2165)

This commit is contained in:
Bart Louwers
2024-03-04 20:27:09 +01:00
committed by GitHub
parent d261bf54df
commit 69f983b83d
14 changed files with 399 additions and 42 deletions

View File

@@ -31,6 +31,7 @@ import org.maplibre.android.utils.BitmapUtils
import org.maplibre.android.utils.ColorUtils
import org.hamcrest.CoreMatchers.*
import org.junit.*
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.runner.RunWith
@@ -1608,6 +1609,122 @@ class LocationComponentTest : EspressoTest() {
executeComponentTest(componentAction)
}
@Test
fun animators_dontPaddingWhileNotTracking() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(
component: LocationComponent,
maplibreMap: MapLibreMap,
style: Style,
uiController: UiController,
context: Context
) {
component.activateLocationComponent(LocationComponentActivationOptions
.builder(context, style)
.useDefaultLocationEngine(false)
.build())
component.isLocationComponentEnabled = true
component.cameraMode = CameraMode.NONE
val padding = maplibreMap.cameraPosition.padding
component.paddingWhileTracking(doubleArrayOf(100.0, 200.0, 300.0, 400.0))
uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_PADDING_ANIM_DURATION)
TestingAsyncUtils.waitForLayer(uiController, mapView)
assertArrayEquals(padding, maplibreMap.cameraPosition.padding, 0.1)
}
}
executeComponentTest(componentAction)
}
@Test
fun animators_dontPaddingWhileStopped() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(
component: LocationComponent,
maplibreMap: MapLibreMap,
style: Style,
uiController: UiController,
context: Context
) {
component.activateLocationComponent(LocationComponentActivationOptions
.builder(context, style)
.useDefaultLocationEngine(false)
.build())
component.isLocationComponentEnabled = true
component.cameraMode = CameraMode.TRACKING
val padding = maplibreMap.cameraPosition.padding
component.onStop()
component.paddingWhileTracking(doubleArrayOf(100.0, 200.0, 300.0, 400.0))
uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_PADDING_ANIM_DURATION)
TestingAsyncUtils.waitForLayer(uiController, mapView)
assertArrayEquals(padding, maplibreMap.cameraPosition.padding, 0.1)
}
}
executeComponentTest(componentAction)
}
@Test
fun animators_dontPaddingWhileTransitioning() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(
component: LocationComponent,
maplibreMap: MapLibreMap,
style: Style,
uiController: UiController,
context: Context
) {
component.activateLocationComponent(LocationComponentActivationOptions
.builder(context, style)
.useDefaultLocationEngine(false)
.build())
component.isLocationComponentEnabled = true
component.forceLocationUpdate(location)
val padding = maplibreMap.cameraPosition.padding
component.setCameraMode(CameraMode.TRACKING_GPS, 500L, null, null, null, null)
component.paddingWhileTracking(doubleArrayOf(100.0, 200.0, 300.0, 400.0), 1000L)
uiController.loopMainThreadForAtLeast(1000L)
TestingAsyncUtils.waitForLayer(uiController, mapView)
assertArrayEquals(padding, maplibreMap.cameraPosition.padding, 0.1)
}
}
executeComponentTest(componentAction)
}
@Test
fun animators_paddingWhileTracking() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {
override fun onLocationComponentAction(
component: LocationComponent,
maplibreMap: MapLibreMap,
style: Style,
uiController: UiController,
context: Context
) {
component.activateLocationComponent(LocationComponentActivationOptions
.builder(context, style)
.useDefaultLocationEngine(false)
.build())
component.isLocationComponentEnabled = true
component.cameraMode = CameraMode.TRACKING
val padding = doubleArrayOf(100.0, 200.0, 300.0, 400.0)
component.paddingWhileTracking(padding)
uiController.loopMainThreadForAtLeast(DEFAULT_TRACKING_PADDING_ANIM_DURATION)
TestingAsyncUtils.waitForLayer(uiController, mapView)
assertArrayEquals(padding, maplibreMap.cameraPosition.padding, 0.01)
}
}
executeComponentTest(componentAction)
}
@Test
fun cameraPositionAdjustedToTrackingModeWhenComponentEnabled() {
val componentAction = object : LocationComponentAction.OnPerformLocationComponentAction {

View File

@@ -25,12 +25,13 @@ import org.maplibre.android.location.modes.CameraMode
import org.maplibre.android.location.modes.RenderMode
import org.maplibre.android.location.permissions.PermissionsListener
import org.maplibre.android.location.permissions.PermissionsManager
import org.maplibre.android.maps.MapView
import org.maplibre.android.maps.MapLibreMap
import org.maplibre.android.maps.MapLibreMap.CancelableCallback
import org.maplibre.android.maps.MapView
import org.maplibre.android.maps.OnMapReadyCallback
import org.maplibre.android.maps.Style
import org.maplibre.android.testapp.R
import java.util.Random
class LocationModesActivity :
AppCompatActivity(),
@@ -193,6 +194,27 @@ class LocationModesActivity :
Toast.makeText(this, "Not possible to animate - not tracking", Toast.LENGTH_SHORT)
.show()
}
} else if (id == R.id.action_component_padding_animation_while_tracking) {
val paddingRandom = Random()
locationComponent!!.paddingWhileTracking(
doubleArrayOf(
paddingRandom.nextDouble() * 500,
paddingRandom.nextDouble() * 500,
paddingRandom.nextDouble() * 500,
paddingRandom.nextDouble() * 500
), 1000L, object : CancelableCallback {
override fun onCancel() {
// No impl
}
override fun onFinish() {
locationComponent!!.zoomWhileTracking(16.0)
}
})
if (locationComponent!!.getCameraMode() == CameraMode.NONE) {
Toast.makeText(this, "Not possible to animate - not tracking", Toast.LENGTH_SHORT)
.show()
}
}
return super.onOptionsItemSelected(item)
}

View File

@@ -36,4 +36,8 @@
<item android:id="@+id/action_component_animate_while_tracking"
android:title="Animate while tracking"
app:showAsAction="never"/>
<item android:id="@+id/action_component_padding_animation_while_tracking"
android:title="PaddingAnimation while tracking"
app:showAsAction="never"/>
</menu>