Skip to content

Commit

Permalink
feat: add support for accessibility (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
kikoso authored Nov 13, 2023
1 parent 52d0342 commit f89e071
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 8 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 Down Expand Up @@ -85,7 +86,6 @@ class BasicMapActivity : ComponentActivity() {

Box(Modifier.fillMaxSize()) {
GoogleMapView(
modifier = Modifier.matchParentSize(),
cameraPositionState = cameraPositionState,
onMapLoaded = {
isMapLoaded = true
Expand Down Expand Up @@ -129,7 +129,7 @@ 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))
}
Expand Down Expand Up @@ -223,7 +223,8 @@ fun GoogleMapView(
MapButton(
text = "Toggle Map",
onClick = { mapVisible = !mapVisible },
modifier = Modifier.testTag("toggleMapVisibility"),
modifier = Modifier
.testTag("toggleMapVisibility")
)
}
val coroutineScope = rememberCoroutineScope()
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
@@ -1,4 +1,4 @@
// Copyright 2021 Google LLC
// 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.
Expand Down 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 All @@ -50,6 +51,7 @@ import kotlinx.coroutines.awaitCancellation
/**
* A compose container for a [MapView].
*
* @param mergeDescendants deactivates the map for accessibility purposes
* @param modifier Modifier to be applied to the GoogleMap
* @param cameraPositionState the [CameraPositionState] to be used to control or observe the map's
* camera state
Expand All @@ -72,6 +74,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 @@ -115,18 +118,16 @@ public fun GoogleMap(
val currentLocationSource by rememberUpdatedState(locationSource)
val currentCameraPositionState by rememberUpdatedState(cameraPositionState)
val currentContentPadding by rememberUpdatedState(contentPadding)

// If we pass a custom location button, the native one is deactivated.
val currentUiSettings by rememberUpdatedState(uiSettings)
val currentMapProperties by rememberUpdatedState(properties)

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 Expand Up @@ -206,13 +207,15 @@ private fun MapView.lifecycleObserver(previousState: MutableState<Lifecycle.Even
this.onCreate(Bundle())
}
}

Lifecycle.Event.ON_START -> this.onStart()
Lifecycle.Event.ON_RESUME -> this.onResume()
Lifecycle.Event.ON_PAUSE -> this.onPause()
Lifecycle.Event.ON_STOP -> this.onStop()
Lifecycle.Event.ON_DESTROY -> {
//handled in onDispose
}

else -> throw IllegalStateException()
}
previousState.value = event
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 @@ -133,7 +133,7 @@ public fun rememberMarkerState(

/**
* A composable for a marker on the map.
*
* @param contentDescription the content description for accessibility purposes
* @param state the [MarkerState] to be used to control or observe the marker
* state such as its position and info window
* @param alpha the alpha (opacity) of the marker
Expand All @@ -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 f89e071

Please sign in to comment.