Skip to content

Commit c43249a

Browse files
committed
Bump navigation-sample to NavSDK 7.0.
1 parent 9af1149 commit c43249a

15 files changed

+291
-119
lines changed

navigation-sample/app/build.gradle

100755100644
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ apply plugin: 'com.google.android.libraries.mapsplatform.secrets-gradle-plugin'
2121
ext {
2222
androidxLifecycleVersion = "2.6.2"
2323
glideVersion = "4.13.2"
24-
navSdkVersion = "6.3.0"
24+
navSdkVersion = "7.0.0"
2525
}
2626

2727
android {
2828
namespace 'com.example.navigationapidemo'
29-
compileSdkVersion 34
29+
compileSdk 36
3030

3131
buildFeatures {
3232
buildConfig true
3333
}
3434

3535
defaultConfig {
3636
applicationId "com.example.navigationapidemo"
37-
// Navigation SDK supports a minimum of SDK 23.
38-
minSdkVersion 23
39-
targetSdkVersion 34
37+
// Navigation SDK supports a minimum of SDK 24.
38+
minSdkVersion 24
39+
targetSdkVersion 36
4040
versionCode 1
4141
versionName "1.0"
4242
// Set this to the languages you actually use, otherwise you'll include resource strings
@@ -93,7 +93,7 @@ dependencies {
9393
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.14'
9494

9595
// And dependencies.
96-
api "androidx.appcompat:appcompat:1.6.1"
96+
api "androidx.appcompat:appcompat:1.7.1"
9797
api "androidx.cardview:cardview:1.0.0"
9898
api "androidx.constraintlayout:constraintlayout:2.1.4"
9999
api "androidx.customview:customview:1.1.0"
@@ -114,7 +114,7 @@ dependencies {
114114
api "joda-time:joda-time:2.10.14"
115115
// Also include the Google Places SDK, which is used by this example, but
116116
// isn't required by the Navigation SDK.
117-
api "com.google.android.libraries.places:places:2.5.0"
117+
api "com.google.android.libraries.places:places:4.0.0"
118118
api "com.google.android.material:material:1.12.0"
119119
api 'org.jetbrains.kotlin:kotlin-reflect:2.1.10'
120120
api 'org.jetbrains.kotlin:kotlin-stdlib:2.1.10'

navigation-sample/app/src/main/AndroidManifest.xml

100644100755
File mode changed.

navigation-sample/app/src/main/java/com/example/navigationapidemo/CustomizationPanelsDelegate.kt

100755100644
Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,16 @@ internal object CustomizationPanelsDelegate {
273273
}
274274

275275
fun Navigator.logDebugInfo() {
276-
if (currentRouteSegment != null) {
277-
val stringifiedCurrentRouteSegment =
276+
currentRouteSegment?.let {
277+
Log.i(
278+
TAG,
278279
MoreObjects.toStringHelper("RouteSegment")
279-
.add("Destination LatLng", currentRouteSegment.destinationLatLng)
280-
.add("Destination Waypoint", currentRouteSegment.destinationWaypoint)
281-
.add("Traffic Data", currentRouteSegment.trafficData)
282-
.add("List<LatLng> for segment", currentRouteSegment.latLngs)
283-
.toString()
284-
Log.i(TAG, stringifiedCurrentRouteSegment)
280+
.add("Destination LatLng", it.destinationLatLng)
281+
.add("Destination Waypoint", it.destinationWaypoint)
282+
.add("Traffic Data", it.trafficData)
283+
.add("List<LatLng> for segment", it.latLngs)
284+
.toString(),
285+
)
285286
}
286287

287288
val stringifiedTraveledRoute =
@@ -290,14 +291,15 @@ internal object CustomizationPanelsDelegate {
290291
.toString()
291292
Log.i(TAG, stringifiedTraveledRoute)
292293

293-
if (currentTimeAndDistance != null) {
294-
val stringifiedCurrentTimeAndDistance =
295-
MoreObjects.toStringHelper(currentTimeAndDistance)
296-
.add("Delay severity", currentTimeAndDistance.delaySeverity)
297-
.add("Meters", currentTimeAndDistance.meters)
298-
.add("Seconds", currentTimeAndDistance.seconds)
299-
.toString()
300-
Log.i(TAG, stringifiedCurrentTimeAndDistance)
294+
currentTimeAndDistance?.let {
295+
Log.i(
296+
TAG,
297+
MoreObjects.toStringHelper(it)
298+
.add("Delay severity", it.delaySeverity)
299+
.add("Meters", it.meters)
300+
.add("Seconds", it.seconds)
301+
.toString(),
302+
)
301303
}
302304
}
303305

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.example.navigationapidemo
2+
3+
import android.view.View
4+
import android.view.ViewGroup.MarginLayoutParams
5+
import androidx.core.graphics.Insets
6+
import androidx.core.view.ViewCompat
7+
import androidx.core.view.WindowInsetsCompat
8+
import com.google.auto.value.AutoValue
9+
10+
/** Utility to support Edge-To-Edge mode for devices running Android V+. */
11+
object EdgeToEdgeUtil {
12+
/**
13+
* Configuration to set up margins for {@link View}s supporting Edge-To-Edge mode with Android V+.
14+
*/
15+
data class EdgeToEdgeMarginConfig(
16+
/** The {@link View} the margins need to be applied to. */
17+
val view: View,
18+
19+
/** If the top margin should be applied, {@code false} otherwise. */
20+
val shouldSetTopMargin: Boolean = true,
21+
22+
/** If the bottom margin should be applied, {@code false} otherwise. */
23+
val shouldSetBottomMargin: Boolean = true,
24+
25+
/** If the left margin should be applied, {@code false} otherwise. */
26+
val shouldSetLeftMargin: Boolean = true,
27+
28+
/** If the right margin should be applied, {@code false} otherwise. */
29+
val shouldSetRightMargin: Boolean = true,
30+
)
31+
32+
/**
33+
* Edge-To-Edge mode is enabled by default on devices running Android V and above. As a result,
34+
* margins should be set for devices targeting Android V and higher so that they are not hidden
35+
* behind the system bars and remain accessible.
36+
*
37+
* <p>Note: The margins are applied when the edge-to-edge is enabled and because we are not
38+
* manually enabling it, they'll be applied only for Android V+ devices.
39+
*
40+
* @param edgeToEdgeMarginConfig, the configuration that requires to set margins.
41+
*/
42+
fun setMarginForEdgeToEdgeSupport(edgeToEdgeMarginConfig: EdgeToEdgeMarginConfig): Unit {
43+
applyMargins(listOf(edgeToEdgeMarginConfig))
44+
}
45+
46+
/**
47+
* Edge-To-Edge mode is enabled by default on devices running Android V and above. As a result,
48+
* margins should be set for devices targeting Android V and higher so that they are not hidden
49+
* behind the system bars and remain accessible.
50+
*
51+
* <p>Note: The margins are applied when the edge-to-edge is enabled and because we are not
52+
* manually enabling it, they'll be applied only for Android V+ devices.
53+
*
54+
* @param edgeToEdgeMarginConfigs, the list of configurations that requires to set margins.
55+
*/
56+
fun setMarginForEdgeToEdgeSupport(edgeToEdgeMarginConfigs: List<EdgeToEdgeMarginConfig>): Unit {
57+
applyMargins(edgeToEdgeMarginConfigs)
58+
}
59+
60+
/**
61+
* Applies margins for the provided configs.
62+
*
63+
* <p>A callback is provided to the window inset listener to adjust margins, ensuring that views
64+
* are not obscured by the system bars.
65+
*
66+
* <p>Note: The callbacks are only invoked when the edge-to-edge is enabled and because we are not
67+
* manually enabling it, they'll be invoked for Android V+ devices.
68+
*
69+
* @param edgeToEdgeMarginConfigs, the list of configurations that requires to set margins.
70+
*/
71+
fun applyMargins(edgeToEdgeMarginConfigs: List<EdgeToEdgeMarginConfig>): Unit {
72+
for (edgeToEdgeMarginConfig in edgeToEdgeMarginConfigs) {
73+
ViewCompat.setOnApplyWindowInsetsListener(
74+
edgeToEdgeMarginConfig.view,
75+
{ v, windowInsets ->
76+
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
77+
// Apply the insets as a margin to the view.
78+
val mlp = v.layoutParams as MarginLayoutParams
79+
if (edgeToEdgeMarginConfig.shouldSetLeftMargin) {
80+
mlp.leftMargin = insets.left
81+
}
82+
if (edgeToEdgeMarginConfig.shouldSetRightMargin) {
83+
mlp.rightMargin = insets.right
84+
}
85+
if (edgeToEdgeMarginConfig.shouldSetTopMargin) {
86+
mlp.topMargin = insets.top
87+
}
88+
if (edgeToEdgeMarginConfig.shouldSetBottomMargin) {
89+
mlp.bottomMargin = insets.bottom
90+
}
91+
v.layoutParams = mlp
92+
93+
// Return the window insets to keep passing down to descendant views.
94+
windowInsets
95+
},
96+
)
97+
}
98+
}
99+
}

navigation-sample/app/src/main/java/com/example/navigationapidemo/MainActivity.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,21 @@ import android.widget.AdapterView.OnItemClickListener
2222
import android.widget.ArrayAdapter
2323
import android.widget.ListView
2424
import androidx.appcompat.app.AppCompatActivity
25+
import com.example.navigationapidemo.EdgeToEdgeUtil.EdgeToEdgeMarginConfig
2526

2627
/** Main activity that lets the user choose a demo to launch. */
2728
class MainActivity : AppCompatActivity() {
2829
public override fun onCreate(savedInstanceState: Bundle?) {
2930
super.onCreate(savedInstanceState)
3031
setContentView(R.layout.main)
32+
33+
// Margins are only set if the edge-to-edge mode is enabled, it's enabled by default for Android
34+
// V+ devices.
35+
// No margins are set for pre-Android V devices.
36+
EdgeToEdgeUtil.setMarginForEdgeToEdgeSupport(
37+
listOf(EdgeToEdgeMarginConfig(view = findViewById(R.id.main_layout_container)))
38+
)
39+
3140
val listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, DEMOS.keys.toList())
3241
val listView = findViewById<ListView>(R.id.list_view)
3342
listView.adapter = listAdapter

navigation-sample/app/src/main/java/com/example/navigationapidemo/NavFragmentActivity.kt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import android.widget.Toast
2828
import androidx.appcompat.app.AppCompatActivity
2929
import androidx.fragment.app.Fragment
3030
import com.example.navigationapidemo.CustomizationPanelsDelegate.logDebugInfo
31+
import com.example.navigationapidemo.EdgeToEdgeUtil.EdgeToEdgeMarginConfig
3132
import com.google.android.libraries.navigation.NavigationApi
3233
import com.google.android.libraries.navigation.NavigationApi.NavigatorListener
3334
import com.google.android.libraries.navigation.Navigator
@@ -37,6 +38,7 @@ import com.google.android.libraries.navigation.SupportNavigationFragment
3738
import com.google.android.libraries.navigation.Waypoint
3839
import com.google.android.libraries.navigation.Waypoint.UnsupportedPlaceIdException
3940
import com.google.android.libraries.places.api.model.Place
41+
import com.google.android.libraries.places.api.model.PlaceTypes
4042
import java.lang.Exception
4143

4244
/**
@@ -59,6 +61,13 @@ class NavFragmentActivity : AppCompatActivity() {
5961
super.onCreate(savedInstanceState)
6062
setContentView(R.layout.activity_nav_fragment)
6163

64+
// Margins are only set if the edge-to-edge mode is enabled, it's enabled by default for Android
65+
// V+ devices.
66+
// No margins are set for pre-Android V devices.
67+
EdgeToEdgeUtil.setMarginForEdgeToEdgeSupport(
68+
listOf(EdgeToEdgeMarginConfig(view = findViewById(R.id.nav_fragment_layout_container)))
69+
)
70+
6271
// Obtain a reference to the NavigationFragment
6372
navFragment =
6473
supportFragmentManager.findFragmentById(R.id.navigation_fragment) as SupportNavigationFragment
@@ -145,21 +154,21 @@ class NavFragmentActivity : AppCompatActivity() {
145154
)
146155

147156
withMapAsync {
148-
CustomizationPanelsDelegate.setUpCameraPerspectiveSpinner(
149-
this@NavFragmentActivity,
150-
map::followMyLocation,
151-
)
152-
// The logic below simply helps keep the UI in tune with the underlying SDK
153-
// state.
154-
CustomizationPanelsDelegate.registerOnCameraFollowLocationCallback(
155-
this@NavFragmentActivity,
156-
map,
157-
)
157+
CustomizationPanelsDelegate.setUpCameraPerspectiveSpinner(
158+
this@NavFragmentActivity,
159+
map::followMyLocation,
160+
)
161+
// The logic below simply helps keep the UI in tune with the underlying SDK
162+
// state.
163+
CustomizationPanelsDelegate.registerOnCameraFollowLocationCallback(
164+
this@NavFragmentActivity,
165+
map,
166+
)
158167

159-
CustomizationPanelsDelegate.registerOnNavigationUiChangedListener(
160-
this@NavFragmentActivity,
161-
navFragment::addOnNavigationUiChangedListener,
162-
)
168+
CustomizationPanelsDelegate.registerOnNavigationUiChangedListener(
169+
this@NavFragmentActivity,
170+
navFragment::addOnNavigationUiChangedListener,
171+
)
163172
}
164173
}
165174

@@ -205,7 +214,7 @@ class NavFragmentActivity : AppCompatActivity() {
205214
*/
206215
private fun navigateToPlace(place: Place) {
207216
val waypoint: Waypoint? =
208-
if (place.types?.contains(Place.Type.GEOCODE) == true) {
217+
if (place.placeTypes?.contains(PlaceTypes.GEOCODE) == true) {
209218
// An example of setting a destination via Lat-Lng.
210219
// Note: Setting LatLng destinations can result in poor routing quality/ETA calculation.
211220
// Wherever possible you should use a Place ID to describe the destination accurately.

0 commit comments

Comments
 (0)