Skip to content

Commit 4f0fddd

Browse files
authored
feat(navigation-sample): integrate Layout Customization Framework delegate selector (#40)
1 parent 8afe25b commit 4f0fddd

13 files changed

Lines changed: 437 additions & 83 deletions

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ import com.google.android.gms.maps.GoogleMap.OnCameraFollowLocationCallback
3535
import com.google.android.gms.maps.model.CameraPosition
3636
import com.google.android.gms.maps.model.LatLng
3737
import com.google.android.libraries.navigation.ForceNightMode
38+
import com.google.android.libraries.navigation.NavigationFragment
3839
import com.google.android.libraries.navigation.Navigator
3940
import com.google.android.libraries.navigation.OnNavigationUiChangedListener
40-
import com.google.android.libraries.navigation.SupportNavigationFragment
4141
import com.google.common.base.MoreObjects
4242

4343
/**
@@ -46,8 +46,8 @@ import com.google.common.base.MoreObjects
4646
*
4747
* Users are initially presented with a simple overlay of toggles that allow them to select which
4848
* type of runtime behavior to stress. "Nav methods" refer to behaviors on [Navigator]. "View
49-
* methods" refer to behaviors on [NavigationView] or [SupportNavigationFragment]. And "Map methods"
50-
* refer to behaviors on [GoogleMap].
49+
* methods" refer to behaviors on [NavigationView] or [NavigationFragment]. And "Map methods" refer
50+
* to behaviors on [GoogleMap].
5151
*
5252
* Clicking one of the toggles will expand a list of corresponding behaviors, presented as a panel
5353
* of buttons, spinners etc for the user to play with. The user can always hide/show the entire
@@ -246,6 +246,12 @@ internal object CustomizationPanelsDelegate {
246246
googleMap.isMyLocationEnabled = toggleButton.isChecked
247247
}
248248

249+
/** Toggles whether 3D buildings are enabled. */
250+
fun toggleBuildingsEnabled(activity: Activity, googleMap: GoogleMap) {
251+
val toggleButton = activity.findViewById<ToggleButton>(R.id.btn_set_buildings_enabled)
252+
googleMap.isBuildingsEnabled = toggleButton.isChecked
253+
}
254+
249255
///////////////////////////////////////////////////////////////////////////////////////
250256
// Handlers for "Nav methods" control panel interactions (other than spinners).
251257
///////////////////////////////////////////////////////////////////////////////////////

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,100 @@ import android.content.Intent
2020
import android.os.Bundle
2121
import android.widget.AdapterView.OnItemClickListener
2222
import android.widget.ArrayAdapter
23+
import android.widget.Button
2324
import android.widget.ListView
25+
import androidx.appcompat.app.AlertDialog
2426
import androidx.appcompat.app.AppCompatActivity
2527
import com.example.navigationapidemo.EdgeToEdgeUtil.EdgeToEdgeMarginConfig
28+
import com.example.navigationapidemo.layoutdelegate.LayoutDelegateType
2629

2730
/** Main activity that lets the user choose a demo to launch. */
2831
class MainActivity : AppCompatActivity() {
32+
private var selectedLayoutDelegateType: LayoutDelegateType = LayoutDelegateType.NONE
33+
private var layoutDelegateDialog: AlertDialog? = null
34+
2935
public override fun onCreate(savedInstanceState: Bundle?) {
3036
super.onCreate(savedInstanceState)
3137
setContentView(R.layout.main)
3238

39+
selectedLayoutDelegateType =
40+
if (savedInstanceState != null) {
41+
LayoutDelegateType.IntentHelper.parseType(
42+
savedInstanceState.getString(KEY_SELECTED_LAYOUT_DELEGATE)
43+
)
44+
} else {
45+
LayoutDelegateType.IntentHelper.fromIntent(intent)
46+
}
47+
3348
// Margins are only set if the edge-to-edge mode is enabled, it's enabled by default for Android
3449
// V+ devices.
3550
// No margins are set for pre-Android V devices.
3651
EdgeToEdgeUtil.setMarginForEdgeToEdgeSupport(
3752
listOf(EdgeToEdgeMarginConfig(view = findViewById(R.id.main_layout_container)))
3853
)
3954

55+
val configureLayoutDelegateButton = findViewById<Button>(R.id.configure_layout_delegate_button)
56+
updateLayoutDelegateButtonText(configureLayoutDelegateButton)
57+
configureLayoutDelegateButton.setOnClickListener {
58+
showLayoutDelegateSelectionDialog(configureLayoutDelegateButton)
59+
}
60+
4061
val listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, DEMOS.keys.toList())
4162
val listView = findViewById<ListView>(R.id.list_view)
4263
listView.adapter = listAdapter
4364
listView.onItemClickListener = OnItemClickListener { parent, view, position, _ ->
4465
val demoName = parent.getItemAtPosition(position) as String
45-
startActivity(Intent(view.context, DEMOS[demoName]))
66+
val intent =
67+
Intent(view.context, DEMOS[demoName]).apply {
68+
putExtra(
69+
LayoutDelegateType.IntentHelper.EXTRA_LAYOUT_DELEGATE_TYPE,
70+
selectedLayoutDelegateType.name,
71+
)
72+
}
73+
startActivity(intent)
4674
}
4775
}
4876

77+
public override fun onSaveInstanceState(outState: Bundle) {
78+
super.onSaveInstanceState(outState)
79+
outState.putString(KEY_SELECTED_LAYOUT_DELEGATE, selectedLayoutDelegateType.name)
80+
}
81+
82+
public override fun onDestroy() {
83+
layoutDelegateDialog?.dismiss()
84+
layoutDelegateDialog = null
85+
super.onDestroy()
86+
}
87+
88+
private fun updateLayoutDelegateButtonText(configureButton: Button) {
89+
configureButton.text =
90+
if (selectedLayoutDelegateType == LayoutDelegateType.NONE) {
91+
getString(R.string.select_layout_delegate_prompt)
92+
} else {
93+
selectedLayoutDelegateType.displayName
94+
}
95+
}
96+
97+
private fun showLayoutDelegateSelectionDialog(configureButton: Button) {
98+
val types = LayoutDelegateType.entries
99+
val displayNames = types.map { it.displayName }.toTypedArray()
100+
val currentIndex = types.indexOf(selectedLayoutDelegateType).coerceAtLeast(0)
101+
102+
layoutDelegateDialog?.dismiss()
103+
layoutDelegateDialog =
104+
AlertDialog.Builder(this)
105+
.setTitle(R.string.select_layout_delegate_title)
106+
.setSingleChoiceItems(displayNames, currentIndex) { dialog, which ->
107+
selectedLayoutDelegateType = types[which]
108+
updateLayoutDelegateButtonText(configureButton)
109+
dialog.dismiss()
110+
}
111+
.create()
112+
.also { it.show() }
113+
}
114+
49115
companion object {
116+
private const val KEY_SELECTED_LAYOUT_DELEGATE = "selected_layout_delegate"
50117
private val DEMOS =
51118
mapOf<String, Class<*>>(
52119
"NavViewActivity" to NavViewActivity::class.java,

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

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ import androidx.appcompat.app.AppCompatActivity
2929
import androidx.fragment.app.Fragment
3030
import com.example.navigationapidemo.CustomizationPanelsDelegate.logDebugInfo
3131
import com.example.navigationapidemo.EdgeToEdgeUtil.EdgeToEdgeMarginConfig
32+
import com.example.navigationapidemo.layoutdelegate.LayoutDelegateApplier
3233
import com.google.android.libraries.navigation.NavigationApi
3334
import com.google.android.libraries.navigation.NavigationApi.NavigatorListener
35+
import com.google.android.libraries.navigation.NavigationFragment
3436
import com.google.android.libraries.navigation.Navigator
3537
import com.google.android.libraries.navigation.Navigator.RouteStatus
3638
import com.google.android.libraries.navigation.SimulationOptions
37-
import com.google.android.libraries.navigation.SupportNavigationFragment
3839
import com.google.android.libraries.navigation.Waypoint
3940
import com.google.android.libraries.navigation.Waypoint.UnsupportedPlaceIdException
4041
import com.google.android.libraries.places.api.model.Place
@@ -52,7 +53,7 @@ class NavFragmentActivity : AppCompatActivity() {
5253
private var arrivalListener: Navigator.ArrivalListener? = null
5354
private var routeChangedListener: Navigator.RouteChangedListener? = null
5455

55-
private lateinit var navFragment: SupportNavigationFragment
56+
private lateinit var navFragment: NavigationFragment
5657
private var navInfoDisplayFragment: Fragment? = null
5758

5859
@SuppressLint("MissingPermission") // TODO: requestPermissions(...) in here or earlier
@@ -67,9 +68,18 @@ class NavFragmentActivity : AppCompatActivity() {
6768
listOf(EdgeToEdgeMarginConfig(view = findViewById(R.id.nav_fragment_layout_container)))
6869
)
6970

70-
// Obtain a reference to the NavigationFragment
71-
navFragment =
72-
supportFragmentManager.findFragmentById(R.id.navigation_fragment) as SupportNavigationFragment
71+
// Obtain a reference to the NavigationFragment, creating it if needed so the layout delegate
72+
// can be applied before the fragment creates its NavigationView.
73+
val existingFragment =
74+
supportFragmentManager.findFragmentById(R.id.navigation_fragment) as? NavigationFragment
75+
navFragment = existingFragment ?: NavigationFragment.newInstance()
76+
LayoutDelegateApplier.applyLayoutDelegateFromIntent(intent, navFragment)
77+
if (existingFragment == null) {
78+
supportFragmentManager
79+
.beginTransaction()
80+
.add(R.id.navigation_fragment, navFragment)
81+
.commitNow()
82+
}
7383

7484
// Set up the UI that allows the user to control some NavSDK behaviors in the demo app.
7585
// These panels set up all the users' selectable options, like whether to show the trip
@@ -177,18 +187,17 @@ class NavFragmentActivity : AppCompatActivity() {
177187
*/
178188
private fun registerNavigationListeners() {
179189
withNavigatorAsync {
180-
arrivalListener =
181-
Navigator.ArrivalListener { // Show an onscreen message
182-
showToast("User has arrived at the destination!")
190+
arrivalListener = Navigator.ArrivalListener { // Show an onscreen message
191+
showToast("User has arrived at the destination!")
183192

184-
// Stop turn-by-turn guidance and return to TOP_DOWN perspective of the map
185-
navigator.stopGuidance()
193+
// Stop turn-by-turn guidance and return to TOP_DOWN perspective of the map
194+
navigator.stopGuidance()
186195

187-
// Stop simulating vehicle movement.
188-
if (BuildConfig.DEBUG) {
189-
navigator.simulator.unsetUserLocation()
190-
}
196+
// Stop simulating vehicle movement.
197+
if (BuildConfig.DEBUG) {
198+
navigator.simulator.unsetUserLocation()
191199
}
200+
}
192201
navigator.addArrivalListener(arrivalListener)
193202

194203
routeChangedListener =
@@ -324,6 +333,13 @@ class NavFragmentActivity : AppCompatActivity() {
324333
}
325334
}
326335

336+
/** Toggles whether 3D buildings are enabled. */
337+
fun toggleBuildingsEnabled(unused: View?) {
338+
withMapAsync {
339+
CustomizationPanelsDelegate.toggleBuildingsEnabled(this@NavFragmentActivity, map)
340+
}
341+
}
342+
327343
/** Moves the position of the camera to hover over Melbourne. */
328344
fun moveCameraToMelbourne(unused: View?) {
329345
withMapAsync {

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import androidx.appcompat.app.AppCompatActivity
3030
import androidx.fragment.app.Fragment
3131
import com.example.navigationapidemo.CustomizationPanelsDelegate.logDebugInfo
3232
import com.example.navigationapidemo.EdgeToEdgeUtil.EdgeToEdgeMarginConfig
33+
import com.example.navigationapidemo.layoutdelegate.LayoutDelegateApplier
3334
import com.google.android.libraries.navigation.NavigationApi
3435
import com.google.android.libraries.navigation.NavigationApi.NavigatorListener
3536
import com.google.android.libraries.navigation.NavigationView
@@ -72,6 +73,7 @@ class NavViewActivity : AppCompatActivity() {
7273
)
7374

7475
navView = findViewById(R.id.navigation_view)
76+
LayoutDelegateApplier.applyLayoutDelegateFromIntent(intent, navView)
7577

7678
navView.onCreate(savedInstanceState)
7779

@@ -181,16 +183,15 @@ class NavViewActivity : AppCompatActivity() {
181183
*/
182184
private fun registerNavigationListeners() {
183185
withNavigatorAsync {
184-
arrivalListener =
185-
Navigator.ArrivalListener { // Show an onscreen message
186-
showToast("User has arrived at the destination!")
187-
navigator.clearDestinations()
188-
189-
// Stop simulating vehicle movement.
190-
if (BuildConfig.DEBUG) {
191-
navigator.simulator?.unsetUserLocation()
192-
}
186+
arrivalListener = Navigator.ArrivalListener { // Show an onscreen message
187+
showToast("User has arrived at the destination!")
188+
navigator.clearDestinations()
189+
190+
// Stop simulating vehicle movement.
191+
if (BuildConfig.DEBUG) {
192+
navigator.simulator?.unsetUserLocation()
193193
}
194+
}
194195
navigator.addArrivalListener(arrivalListener)
195196

196197
routeChangedListener =
@@ -391,6 +392,11 @@ class NavViewActivity : AppCompatActivity() {
391392
CustomizationPanelsDelegate.toggleTripProgressBarUI(this, navView::setTripProgressBarEnabled)
392393
}
393394

395+
/** Toggles whether 3D buildings are enabled. */
396+
fun toggleBuildingsEnabled(unused: View?) {
397+
withMapAsync { CustomizationPanelsDelegate.toggleBuildingsEnabled(this@NavViewActivity, map) }
398+
}
399+
394400
/** Logs some debug information to the logcat from the Navigator, upon user request. */
395401
fun logDebugInfo(unused: View?) {
396402
withNavigatorAsync {

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ import androidx.appcompat.app.AppCompatActivity
2626
import androidx.fragment.app.Fragment
2727
import androidx.fragment.app.FragmentTransaction
2828
import com.example.navigationapidemo.EdgeToEdgeUtil.EdgeToEdgeMarginConfig
29+
import com.example.navigationapidemo.layoutdelegate.LayoutDelegateApplier
2930
import com.google.android.gms.maps.SupportMapFragment
3031
import com.google.android.libraries.navigation.NavigationApi
3132
import com.google.android.libraries.navigation.NavigationApi.NavigatorListener
33+
import com.google.android.libraries.navigation.NavigationFragment
3234
import com.google.android.libraries.navigation.Navigator
3335
import com.google.android.libraries.navigation.Navigator.RouteStatus
3436
import com.google.android.libraries.navigation.SimulationOptions
35-
import com.google.android.libraries.navigation.SupportNavigationFragment
3637
import com.google.android.libraries.navigation.Waypoint
3738
import com.google.android.libraries.navigation.Waypoint.UnsupportedPlaceIdException
3839
import com.google.android.libraries.places.api.model.Place
@@ -46,17 +47,17 @@ import java.lang.ref.WeakReference
4647
* Customers of the v1 Navigation SDK historically have integrated the Google Play Services Maps
4748
* client into their builds side-by-side with the Navigation SDK. In that usage pattern, the
4849
* adopting app would show a SupportMapFragment while the driver is out-of-navigation, and switch
49-
* over to a SupportNavigationFragment once the driver has selected a destination. At the end of the
50-
* trip, or upon user cancellation, the customer would return to showing a SupportMapFragment.
50+
* over to a NavigationFragment once the driver has selected a destination. At the end of the trip,
51+
* or upon user cancellation, the customer would return to showing a SupportMapFragment.
5152
*
5253
* This activity mimics that behavior to support any customers who want to temporarily continue this
5354
* usage pattern. We recommend that v1 customers eventually transition to exclusively using a
54-
* NavigationView or SupportNavigationFragment, since these classes support the GoogleMap interface.
55+
* NavigationView or NavigationFragment, since these classes support the GoogleMap interface.
5556
*/
5657
class SwappingMapAndNavActivity : AppCompatActivity() {
5758
private var navigator: Navigator? = null
5859
private lateinit var mapFragment: SupportMapFragment
59-
private lateinit var navigationFragment: SupportNavigationFragment
60+
private lateinit var navigationFragment: NavigationFragment
6061
private var arrivalListener: Navigator.ArrivalListener? = null
6162

6263
override fun onCreate(savedInstanceState: Bundle?) {
@@ -70,8 +71,13 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
7071
listOf(EdgeToEdgeMarginConfig(view = findViewById(R.id.container)))
7172
)
7273

73-
mapFragment = SupportMapFragment()
74-
navigationFragment = SupportNavigationFragment()
74+
mapFragment =
75+
supportFragmentManager.findFragmentByTag(MAP_FRAGMENT_TAG) as? SupportMapFragment
76+
?: SupportMapFragment.newInstance()
77+
navigationFragment =
78+
supportFragmentManager.findFragmentByTag(NAVIGATION_FRAGMENT_TAG) as? NavigationFragment
79+
?: NavigationFragment.newInstance()
80+
LayoutDelegateApplier.applyLayoutDelegateFromIntent(intent, navigationFragment)
7581

7682
// Start with a basic map on the screen.
7783
if (savedInstanceState == null) {
@@ -198,7 +204,7 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
198204
*/
199205
fun stopTripAndShowMapFragment(unused: MenuItem?) {
200206
navigator?.clearDestinations()
201-
if (supportFragmentManager.findFragmentById(R.id.container) is SupportNavigationFragment) {
207+
if (supportFragmentManager.findFragmentById(R.id.container) is NavigationFragment) {
202208
detachOldFragmentAndAddOrAttachNewFragment(
203209
/*fragmentToDetach=*/ navigationFragment,
204210
/*fragmentToAttach=*/ mapFragment,
@@ -208,19 +214,18 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
208214
}
209215

210216
private fun registerArrivalListener() {
211-
arrivalListener =
212-
Navigator.ArrivalListener {
213-
showToast("User has arrived at the destination!")
214-
navigator?.stopGuidance()
217+
arrivalListener = Navigator.ArrivalListener {
218+
showToast("User has arrived at the destination!")
219+
navigator?.stopGuidance()
215220

216-
// Stop simulating vehicle movement.
217-
if (BuildConfig.DEBUG) {
218-
navigator?.simulator?.unsetUserLocation()
219-
}
220-
221-
// Switch back to the MapView.
222-
stopTripAndShowMapFragment(/* unused= */ null)
221+
// Stop simulating vehicle movement.
222+
if (BuildConfig.DEBUG) {
223+
navigator?.simulator?.unsetUserLocation()
223224
}
225+
226+
// Switch back to the MapView.
227+
stopTripAndShowMapFragment(/* unused= */ null)
228+
}
224229
navigator?.addArrivalListener(arrivalListener)
225230
}
226231

0 commit comments

Comments
 (0)