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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ Their `.netrc` / `MAPBOX_DOWNLOADS_TOKEN` setup can go; this package doesn't nee
| `addVoiceInstructionListener(listener)` | Voice instruction text and SSML. |
| `setVoiceMuted(muted)` / `isVoiceMuted()` | Mutes the SDK's voice instructions. |
| `setVoiceVolume(volume)` | Volume of the voice instructions, 0 to 1. |
| `setKeepScreenOn(keepScreenOn)` | Whether guidance keeps the screen on; on by default. |
| `NAVIGATION_MAP_VIEW_IMPL` | `mapViewImpl` value for navigation maps. |
| `<NavigationViewport padding />` | Per-map camera padding. |
| `<NavigationView route \| waypoints />` | Full-screen navigation UI; see [NavigationView](#navigationview). |
Expand Down Expand Up @@ -357,6 +358,8 @@ After `stopNavigation`, the route and the puck are removed and the camera eases

The SDK speaks instructions during any guidance. `setVoiceMuted(true)` mutes it, including in `<NavigationView>`.

Guidance keeps the screen on, including in `<NavigationView>`, and puts back the app's own idle timer (iOS) or keep screen on (Android) setting when it stops. `setKeepScreenOn(false)` turns that off for apps that manage it themselves.

### NavigationView

`<NavigationView>` is a full-screen navigation UI: `NavigationViewController` from `MapboxNavigationUIKit` on iOS, and an equivalent built from the Navigation SDK UI components on Android (maneuver banner with lanes and shields, speed limit, trip progress, mute, overview, recenter and end buttons).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.rnmapbox.rnmbxnav

import com.facebook.proguard.annotations.DoNotStrip
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.bridge.WritableArray
Expand Down Expand Up @@ -29,6 +30,14 @@ class RNMBXNavigationModule(reactContext: ReactApplicationContext) :
// Read on the JS thread; the session is main thread only.
@Volatile private var voiceMuted = false

private val lifecycleListener = object : LifecycleEventListener {
override fun onHostResume() = RNMBXNavigationSession.updateKeepScreenOn()

override fun onHostPause() = Unit

override fun onHostDestroy() = Unit
}

init {
UiThreadUtil.runOnUiThread {
RNMBXNavigationSession.cameraStateObserver = { state ->
Expand Down Expand Up @@ -57,7 +66,9 @@ class RNMBXNavigationModule(reactContext: ReactApplicationContext) :
}
RNMBXNavigationCarMaps.observer = { carMap -> emitOnCarMapChange(carMapEvent(carMap)) }
RNMBXNavigationCarPreview.register(reactContext)
RNMBXNavigationSession.activity = { reactContext.currentActivity }
}
reactContext.addLifecycleEventListener(lifecycleListener)
}

override fun setAccessToken(accessToken: String) {
Expand Down Expand Up @@ -196,6 +207,10 @@ class RNMBXNavigationModule(reactContext: ReactApplicationContext) :
UiThreadUtil.runOnUiThread { RNMBXNavigationSession.simulationSpeed = multiplier }
}

override fun setKeepScreenOn(keepScreenOn: Boolean) {
UiThreadUtil.runOnUiThread { RNMBXNavigationSession.keepScreenOn = keepScreenOn }
}

override fun isCarPlayConnected(): Boolean = false

override fun getCarMap(): WritableMap = carMapEvent(RNMBXNavigationCarMaps.current)
Expand All @@ -206,6 +221,7 @@ class RNMBXNavigationModule(reactContext: ReactApplicationContext) :
}

override fun invalidate() {
reactApplicationContext.removeLifecycleEventListener(lifecycleListener)
UiThreadUtil.runOnUiThread {
RNMBXNavigationSession.cameraStateObserver = null
RNMBXNavigationSession.routeProgressObserver = null
Expand All @@ -216,6 +232,7 @@ class RNMBXNavigationModule(reactContext: ReactApplicationContext) :
RNMBXNavigationSession.routeChangeObserver = null
RNMBXNavigationCarMaps.observer = null
RNMBXNavigationSession.stopNavigation()
RNMBXNavigationSession.activity = null
}
super.invalidate()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.rnmapbox.rnmbxnav

import android.app.Activity
import android.content.Context
import android.view.View
import com.mapbox.api.directions.v5.models.VoiceInstructions
import com.mapbox.common.MapboxOptions
import com.mapbox.common.location.Location
Expand All @@ -27,6 +29,7 @@ import com.mapbox.navigation.voice.api.MapboxSpeechApi
import com.mapbox.navigation.voice.api.MapboxVoiceInstructionsPlayer
import com.mapbox.navigation.voice.model.SpeechAnnouncement
import com.mapbox.navigation.voice.model.SpeechVolume
import java.lang.ref.WeakReference
import java.util.Locale

enum class RNMBXNavigationCameraState(val jsName: String) {
Expand Down Expand Up @@ -96,6 +99,19 @@ object RNMBXNavigationSession {
private var guidanceStart: Any? = null
private var simulating = false

var keepScreenOn = true
set(value) {
field = value
updateKeepScreenOn()
}

/** Its window is kept on during guidance. */
var activity: (() -> Activity?)? = null

// The view's flag, not the window's, so apps setting `FLAG_KEEP_SCREEN_ON` themselves keep it.
private var screenOnView: WeakReference<View>? = null
private var viewKeptScreenOn = false

/** Set by Android Auto's auto drive, which Play review uses: guidance then simulates. */
var autoDrive = false

Expand Down Expand Up @@ -232,6 +248,7 @@ object RNMBXNavigationSession {
navigation.setArrivalController(arrivalController)
startVoice(navigation.navigationOptions.applicationContext, route)
guidanceRoute = route
updateKeepScreenOn()
arrivedAtFinalDestination = false
val start = Any()
guidanceStart = start
Expand Down Expand Up @@ -295,6 +312,19 @@ object RNMBXNavigationSession {
emptyList()
}

/** Also call it when [activity] may have changed. */
fun updateKeepScreenOn() {
val view = if (guidanceRoute != null && keepScreenOn) activity?.invoke()?.window?.decorView else null
val current = screenOnView?.get()
if (view === current) return
current?.keepScreenOn = viewKeptScreenOn
screenOnView = view?.let { WeakReference(it) }
if (view != null) {
viewKeptScreenOn = view.keepScreenOn
view.keepScreenOn = true
}
}

private fun startVoice(context: Context, route: RNMBXNavigationRoute) {
stopVoice()
val language = route.routes.first().directionsRoute.routeOptions()?.language() ?: Locale.getDefault().toLanguageTag()
Expand All @@ -317,6 +347,7 @@ object RNMBXNavigationSession {
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
fun stopNavigation() {
guidanceRoute = null
updateKeepScreenOn()
guidanceStart = null
offRoute = false
stopVoice()
Expand Down
6 changes: 6 additions & 0 deletions ios/RNMBXNavigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ public final class RNMBXNavigation: NSObject {
}
}

@objc public func setKeepScreenOn(_ keepScreenOn: Bool) {
Task { @MainActor in
RNMBXNavigationSession.shared.keepScreenOn = keepScreenOn
}
}

@objc public func carMap() -> [String: Any] {
stateLock.withLock { _carMap }
}
Expand Down
5 changes: 5 additions & 0 deletions ios/RNMBXNavigationModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ - (void)setSimulationSpeed:(double)multiplier
[_navigation setSimulationSpeed:multiplier];
}

- (void)setKeepScreenOn:(BOOL)keepScreenOn
{
[_navigation setKeepScreenOn:keepScreenOn];
}

- (NSNumber *)isCarPlayConnected
{
return @([_navigation isCarPlayConnected]);
Expand Down
24 changes: 23 additions & 1 deletion ios/RNMBXNavigationSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Foundation
import MapboxDirections
import MapboxNavigationCore
import rnmapbox_maps
import UIKit

/// Process-wide navigation state shared by the TurboModule and every adopted map.
/// The SDK allows one `MapboxNavigationProvider` per process; it outlives JS reloads.
Expand Down Expand Up @@ -51,7 +52,16 @@ final class RNMBXNavigationSession {
@Published private(set) var provider: MapboxNavigationProvider?
private var subscriptions = Set<AnyCancellable>()
/// Kept until navigation stops, whether or not JS releases its `Route`.
private var guidanceRoute: RNMBXNavigationRoute?
private var guidanceRoute: RNMBXNavigationRoute? {
didSet { updateIdleTimer() }
}

var keepScreenOn = true {
didSet { updateIdleTimer() }
}

/// The app's own `isIdleTimerDisabled`, restored when guidance stops.
private var appIdleTimerDisabled: Bool?

var accessToken: String? {
didSet {
Expand Down Expand Up @@ -238,6 +248,18 @@ final class RNMBXNavigationSession {
}
}

private func updateIdleTimer() {
let application = UIApplication.shared
if guidanceRoute != nil, keepScreenOn {
guard appIdleTimerDisabled == nil else { return }
appIdleTimerDisabled = application.isIdleTimerDisabled
application.isIdleTimerDisabled = true
} else if let appIdleTimerDisabled {
application.isIdleTimerDisabled = appIdleTimerDisabled
self.appIdleTimerDisabled = nil
}
}

func stop() {
provider?.mapboxNavigation.tripSession().setToIdle()
simulating = false
Expand Down
2 changes: 2 additions & 0 deletions ios/RNMBXNavigationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public final class RNMBXNavigationView: UIView {
)
let controller = RNMBXNavigationViewController(navigationRoutes: route.routes, navigationOptions: options)
controller.delegate = self
// The session keeps the screen awake; the controller would reset the idle timer to `false` when it disappears.
controller.shouldManageApplicationIdleTimer = false
parent.addChild(controller)
controller.view.frame = bounds
addSubview(controller.view)
Expand Down
8 changes: 8 additions & 0 deletions src/Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ export const Navigation = {
NativeNavigation.setVoiceVolume(Math.min(Math.max(volume, 0), 1));
},

/**
* Whether active guidance, `<NavigationView>`'s included, keeps the screen on. On by default. When guidance stops, the
* app's own idle timer or keep screen on setting is restored. Turn it off if the app manages that itself.
*/
setKeepScreenOn(keepScreenOn: boolean): void {
NativeNavigation.setKeepScreenOn(keepScreenOn);
},

/**
* Whether a CarPlay screen shows the navigation session; see the `carPlay` option of the Expo config plugin.
* Always `false` on Android.
Expand Down
1 change: 1 addition & 0 deletions src/specs/NativeRNMBXNavigationModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export interface Spec extends TurboModule {
advanceToNextLeg(): void;
setAutoAdvanceLegs(enabled: boolean): void;
setSimulationSpeed(multiplier: number): void;
setKeepScreenOn(keepScreenOn: boolean): void;
isCarPlayConnected(): boolean;
getCarMap(): CarMapEvent;

Expand Down
Loading