Skip to content

Commit

Permalink
feat: added support for mergeDescendants
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoso committed Nov 1, 2023
1 parent b660303 commit 6ca7fc5
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 7 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
<activity
android:name=".CustomControlsActivity"
android:exported="false"/>
<activity
android:name=".AccessibilityActivity"
android:exported="false"/>

<!-- Used by createComponentActivity() for unit testing -->
<activity android:name="androidx.activity.ComponentActivity" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2023 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

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import com.google.android.gms.maps.model.Marker

private const val TAG = "AccessibilityActivity"


class AccessibilityActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val singaporeState = rememberMarkerState(position = singapore)
val cameraPositionState = rememberCameraPositionState {
position = defaultCameraPosition
}
val uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) }
val mapProperties by remember {
mutableStateOf(MapProperties(mapType = MapType.NORMAL))
}

Box(Modifier.fillMaxSize()) {
GoogleMap(
// mergeDescendants will remove accessibility from the entire map and content inside.
mergeDescendants = true,
// alternatively, contentDescription will deactivate it for the maps, but not markers.
contentDescription = "",
cameraPositionState = cameraPositionState,
properties = mapProperties,
uiSettings = uiSettings,
onPOIClick = {
Log.d(TAG, "POI clicked: ${it.name}")
}
) {
val markerClick: (Marker) -> Boolean = {
Log.d(TAG, "${it.title} was clicked")
cameraPositionState.projection?.let { projection ->
Log.d(TAG, "The current projection is: $projection")
}
false
}

Marker(
// contentDescription overrides title for TalkBack
contentDescription = "Description of the marker",
state = singaporeState,
title = "Marker in Singapore",
onClick = markerClick
)
}
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
Expand All @@ -53,8 +54,6 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -129,17 +128,14 @@ fun GoogleMapView(

var uiSettings by remember { mutableStateOf(MapUiSettings(compassEnabled = false)) }
var shouldAnimateZoom by remember { mutableStateOf(true) }
var ticker by remember { mutableStateOf(0) }
var ticker by remember { mutableIntStateOf(0) }
var mapProperties by remember {
mutableStateOf(MapProperties(mapType = MapType.NORMAL))
}
var mapVisible by remember { mutableStateOf(true) }

if (mapVisible) {
GoogleMap(
modifier = Modifier
.semantics(mergeDescendants = true) { }
.clearAndSetSemantics { },
cameraPositionState = cameraPositionState,
properties = mapProperties,
uiSettings = uiSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ class MainActivity : ComponentActivity() {
}) {
Text(getString(R.string.custom_location_button))
}
Spacer(modifier = Modifier.padding(5.dp))
Button(
onClick = {
context.startActivity(Intent(context, AccessibilityActivity::class.java))
}) {
Text(getString(R.string.accessibility_button))
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
<string name="scale_bar_activity">Scale Bar</string>
<string name="street_view">Street View</string>
<string name="custom_location_button">Custom Location Button</string>
<string name="accessibility_button">Accessibility</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
Expand Down Expand Up @@ -72,6 +73,7 @@ import kotlinx.coroutines.awaitCancellation
*/
@Composable
public fun GoogleMap(
mergeDescendants: Boolean = false,
modifier: Modifier = Modifier,
cameraPositionState: CameraPositionState = rememberCameraPositionState(),
contentDescription: String? = null,
Expand Down Expand Up @@ -120,11 +122,11 @@ public fun GoogleMap(

val parentComposition = rememberCompositionContext()
val currentContent by rememberUpdatedState(content)

LaunchedEffect(Unit) {
disposingComposition {
mapView.newComposition(parentComposition) {
MapUpdater(
mergeDescendants = mergeDescendants,
contentDescription = contentDescription,
cameraPositionState = currentCameraPositionState,
clickListeners = mapClickListeners,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.maps.android.compose

import android.annotation.SuppressLint
import android.view.View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ComposeNode
Expand Down Expand Up @@ -112,6 +113,7 @@ internal val NoPadding = PaddingValues()
@Suppress("NOTHING_TO_INLINE")
@Composable
internal inline fun MapUpdater(
mergeDescendants: Boolean = false,
contentDescription: String?,
cameraPositionState: CameraPositionState,
clickListeners: MapClickListeners,
Expand All @@ -121,6 +123,10 @@ internal inline fun MapUpdater(
mapUiSettings: MapUiSettings,
) {
val map = (currentComposer.applier as MapApplier).map
val mapView = (currentComposer.applier as MapApplier).mapView
if (mergeDescendants) {
mapView.importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
}
val density = LocalDensity.current
val layoutDirection = LocalLayoutDirection.current
ComposeNode<MapPropertiesNode, MapApplier>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public fun rememberMarkerState(
@Composable
@GoogleMapComposable
public fun Marker(
contentDescription: String? = "",
state: MarkerState = rememberMarkerState(),
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
Expand All @@ -175,6 +176,7 @@ public fun Marker(
onInfoWindowLongClick: (Marker) -> Unit = {},
) {
MarkerImpl(
contentDescription = contentDescription,
state = state,
alpha = alpha,
anchor = anchor,
Expand Down Expand Up @@ -434,6 +436,7 @@ public fun MarkerInfoWindowContent(
@Composable
@GoogleMapComposable
private fun MarkerImpl(
contentDescription: String? = "",
state: MarkerState = rememberMarkerState(),
alpha: Float = 1.0f,
anchor: Offset = Offset(0.5f, 1.0f),
Expand All @@ -459,6 +462,7 @@ private fun MarkerImpl(
ComposeNode<MarkerNode, MapApplier>(
factory = {
val marker = mapApplier?.map?.addMarker {
contentDescription(contentDescription)
alpha(alpha)
anchor(anchor.x, anchor.y)
draggable(draggable)
Expand Down

0 comments on commit 6ca7fc5

Please sign in to comment.