Skip to content

Commit

Permalink
refactor: general refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
illuminati1911 committed Nov 13, 2024
1 parent 09c02ab commit f018a1b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 53 deletions.
3 changes: 2 additions & 1 deletion ANDROIDAUTO.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ To customize the Android Auto experience, override the `onGetTemplate` method in
```kotlin
override fun onGetTemplate(): Template {
/** ... */
@SuppressLint("MissingPermission") val navigationTemplateBuilder =
@SuppressLint("MissingPermission")
val navigationTemplateBuilder =
NavigationTemplate.Builder()
.setActionStrip(
ActionStrip.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ open class AndroidAutoBaseScreen(carContext: CarContext) : Screen(carContext), S
private var mPresentation: Presentation? = null
private var mNavigationView: NavigationViewForAuto? = null
private var mAutoMapView: GoogleMapsAutoMapView? = null
private var mViewRegistry: GoogleMapsViewRegistry? = null
var mGoogleMap: GoogleMap? = null

init {
Expand Down Expand Up @@ -73,28 +74,33 @@ open class AndroidAutoBaseScreen(carContext: CarContext) : Screen(carContext), S
surfaceContainer.surface,
DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY,
)
mPresentation = Presentation(carContext, mVirtualDisplay!!.display)
val virtualDisplay = mVirtualDisplay ?: return

mPresentation = Presentation(carContext, virtualDisplay.display)
val presentation = mPresentation ?: return

mNavigationView = NavigationViewForAuto(carContext)
mNavigationView!!.onCreate(null)
mNavigationView!!.onStart()
mNavigationView!!.onResume()
val navigationView = mNavigationView ?: return
navigationView.onCreate(null)
navigationView.onStart()
navigationView.onResume()

mPresentation!!.setContentView(mNavigationView!!)
mPresentation!!.show()
presentation.setContentView(navigationView)
presentation.show()

mNavigationView!!.getMapAsync { googleMap: GoogleMap ->
navigationView.getMapAsync { googleMap: GoogleMap ->
val viewRegistry = GoogleMapsNavigationPlugin.getInstance()?.viewRegistry
val imageRegistry = GoogleMapsNavigationPlugin.getInstance()?.imageRegistry
if (viewRegistry != null && imageRegistry != null) {
mGoogleMap = googleMap
mViewRegistry = viewRegistry
mAutoMapView =
GoogleMapsAutoMapView(
GoogleMapOptions(),
viewRegistry,
imageRegistry,
mNavigationView!!,
mGoogleMap!!,
navigationView,
googleMap,
)
sendAutoScreenAvailabilityChangedEvent(true)
invalidate()
Expand All @@ -105,30 +111,24 @@ open class AndroidAutoBaseScreen(carContext: CarContext) : Screen(carContext), S
override fun onSurfaceDestroyed(surfaceContainer: SurfaceContainer) {
super.onSurfaceDestroyed(surfaceContainer)
sendAutoScreenAvailabilityChangedEvent(false)
mAutoMapView?.dispose()
mNavigationView!!.onPause()
mNavigationView!!.onStop()
mNavigationView!!.onDestroy()
mViewRegistry?.unregisterAndroidAutoView()
mNavigationView?.onPause()
mNavigationView?.onStop()
mNavigationView?.onDestroy()
mGoogleMap = null

mPresentation!!.dismiss()
mVirtualDisplay!!.release()
mPresentation?.dismiss()
mVirtualDisplay?.release()
}

override fun onScroll(distanceX: Float, distanceY: Float) {
if (mGoogleMap == null) {
return
}
mGoogleMap!!.moveCamera(CameraUpdateFactory.scrollBy(distanceX, distanceY))
mGoogleMap?.moveCamera(CameraUpdateFactory.scrollBy(distanceX, distanceY))
}

override fun onScale(focusX: Float, focusY: Float, scaleFactor: Float) {
if (mGoogleMap == null) {
return
}
val update =
CameraUpdateFactory.zoomBy((scaleFactor - 1), Point(focusX.toInt(), focusY.toInt()))
mGoogleMap!!.animateCamera(update) // map is set in onSurfaceAvailable.
mGoogleMap?.animateCamera(update) // map is set in onSurfaceAvailable.
}

override fun onGetTemplate(): Template {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal constructor(
imageRegistry: ImageRegistry,
private val mapView: NavigationViewForAuto,
private val map: GoogleMap,
) : PlatformView, GoogleMapsBaseMapView(null, mapOptions, null, imageRegistry) {
) : GoogleMapsBaseMapView(null, mapOptions, null, imageRegistry) {
override fun getView(): View {
return mapView
}
Expand All @@ -44,10 +44,6 @@ internal constructor(
viewRegistry.registerAndroidAutoView(this)
}

override fun dispose() {
viewRegistry.unregisterAndroidAutoView()
}

// Handled by AndroidAutoBaseScreen.
override fun onStart() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class SampleAndroidAutoScreen(carContext: CarContext): AndroidAutoBaseScreen(car
// Suppresses the missing permission check for the followMyLocation method, which requires
// "android.permission.ACCESS_COARSE_LOCATION" or "android.permission.ACCESS_FINE_LOCATION", as
// these permissions are already handled elsewhere.
@SuppressLint("MissingPermission") val navigationTemplateBuilder =
@SuppressLint("MissingPermission")
val navigationTemplateBuilder =
NavigationTemplate.Builder()
.setActionStrip(
ActionStrip.Builder()
Expand Down
40 changes: 20 additions & 20 deletions example/lib/pages/navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,22 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
? null
: (bool value) => _setRouteTokensEnabled(value),
),
ExampleSwitch(
title: 'Turn by turn events',
initialValue: _turnByTurnNavigationEventEnabled,
onChanged: (bool newValue) async {
if (newValue) {
await GoogleMapsNavigator
.enableTurnByTurnNavigationEvents(
double.maxFinite.toInt());
} else {
await GoogleMapsNavigator
.disableTurnByTurnNavigationEvents();
}
setState(() {
_turnByTurnNavigationEventEnabled = newValue;
});
}),
],
),
const SizedBox(height: 10)
Expand Down Expand Up @@ -1466,10 +1482,10 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
child: ExpansionTile(
enabled: _isAutoScreenAvailable,
title: const Text('Auto view'),
collapsedTextColor: getExpansionTileTextColor(
!_navigatorInitialized || _navigationViewController == null),
collapsedIconColor: getExpansionTileTextColor(
!_navigatorInitialized || _navigationViewController == null),
collapsedTextColor:
getExpansionTileTextColor(!_isAutoScreenAvailable),
collapsedIconColor:
getExpansionTileTextColor(!_isAutoScreenAvailable),
children: <Widget>[
ElevatedButton(
onPressed: () => _setMapTypeForAutoToSatellite(),
Expand All @@ -1483,22 +1499,6 @@ class _NavigationPageState extends ExamplePageState<NavigationPage> {
onPressed: () => _addMarkerForAuto(),
child: const Text('Add marker'),
),
ExampleSwitch(
title: 'Turn by turn events',
initialValue: _turnByTurnNavigationEventEnabled,
onChanged: (bool newValue) async {
if (newValue) {
await GoogleMapsNavigator
.enableTurnByTurnNavigationEvents(
double.maxFinite.toInt());
} else {
await GoogleMapsNavigator
.disableTurnByTurnNavigationEvents();
}
setState(() {
_turnByTurnNavigationEventEnabled = newValue;
});
}),
]),
),
IgnorePointer(
Expand Down
2 changes: 1 addition & 1 deletion ios/Classes/BaseCarSceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ open class BaseCarSceneDelegate: UIResponder, CPTemplateApplicationSceneDelegate
viewRegistry: viewRegistry,
navigationUIEnabledPreference: NavigationUIEnabledPreference.automatic,
mapConfiguration: MapConfiguration(
cameraPosition: GMSCameraPosition(latitude: 51.51, longitude: 0.12, zoom: 5),
cameraPosition: nil,
mapType: .normal,
compassEnabled: true,
rotateGesturesEnabled: false,
Expand Down
3 changes: 2 additions & 1 deletion ios/Classes/MapConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enum NavigationUIEnabledPreference {

struct MapConfiguration {
// MapView related configurations
var cameraPosition: GMSCameraPosition
var cameraPosition: GMSCameraPosition?
var mapType: GMSMapViewType
var compassEnabled: Bool
var rotateGesturesEnabled: Bool
Expand Down Expand Up @@ -72,6 +72,7 @@ extension MapConfiguration {
//
// - Parameter to: The GMSMapView to configure.
func applyCameraPosition(to mapView: GMSMapView) {
guard let cameraPosition else { return }
mapView.camera = cameraPosition
}
}
2 changes: 1 addition & 1 deletion lib/src/google_navigation_flutter_platform_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ abstract mixin class MapViewAPIInterface {
}

abstract mixin class AutoMapViewAPIInterface {
/// Awaits the platform view to be ready for communication.
/// Awaits the auto view to be ready for communication.
Future<void> awaitMapReadyForAuto();

/// Get the preference for whether the my location should be enabled or disabled.
Expand Down

0 comments on commit f018a1b

Please sign in to comment.