Skip to content

Commit

Permalink
Only add up to the allowed limit of list items to android auto (#3687)
Browse files Browse the repository at this point in the history
  • Loading branch information
dshokouhi committed Jul 21, 2023
1 parent f4eb29c commit c02025e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.util.Log
import androidx.annotation.RequiresApi
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.constraints.ConstraintManager
import androidx.car.app.model.Action
import androidx.car.app.model.CarColor
import androidx.car.app.model.CarIcon
Expand Down Expand Up @@ -58,8 +59,15 @@ class EntityGridVehicleScreen(
}

override fun onGetTemplate(): Template {
val manager = carContext.getCarService(ConstraintManager::class.java)
val gridLimit = manager.getContentLimit(ConstraintManager.CONTENT_LIMIT_TYPE_GRID)

val listBuilder = ItemList.Builder()
entities.forEach { entity ->
entities.forEachIndexed { index, entity ->
if (index >= gridLimit) {
Log.i(TAG, "Grid limit ($gridLimit) reached, not adding more entities (${entities.size}) for $title ")
return@forEachIndexed
}
val icon = entity.getIcon(carContext) ?: CommunityMaterial.Icon.cmd_cloud_question
val gridItem =
GridItem.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.Log
import androidx.annotation.RequiresApi
import androidx.car.app.CarContext
import androidx.car.app.Screen
import androidx.car.app.constraints.ConstraintManager
import androidx.car.app.model.Action
import androidx.car.app.model.CarColor
import androidx.car.app.model.CarIcon
Expand Down Expand Up @@ -73,6 +74,8 @@ class MapVehicleScreen(
}

override fun onGetTemplate(): Template {
val manager = carContext.getCarService(ConstraintManager::class.java)
val listLimit = manager.getContentLimit(ConstraintManager.CONTENT_LIMIT_TYPE_LIST)
val listBuilder = ItemList.Builder()
entities
.map { // Null checks handled during collection
Expand All @@ -82,11 +85,15 @@ class MapVehicleScreen(
Pair(it, listOf(lat, lon))
}
.sortedBy { it.first.friendlyName }
.forEach { (entity, location) ->
val icon = entity.getIcon(carContext) ?: CommunityMaterial.Icon.cmd_account
.forEachIndexed { index, pair ->
if (index >= listLimit) {
Log.i(TAG, "List limit ($listLimit) reached, not adding any more navigation entities (${entities.size})")
return@forEachIndexed
}
val icon = pair.first.getIcon(carContext) ?: CommunityMaterial.Icon.cmd_account
listBuilder.addItem(
Row.Builder()
.setTitle(entity.friendlyName)
.setTitle(pair.first.friendlyName)
.setImage(
CarIcon.Builder(
IconicsDrawable(carContext, icon)
Expand All @@ -98,10 +105,10 @@ class MapVehicleScreen(
.build()
)
.setOnClickListener {
Log.i(TAG, "${entity.entityId} clicked")
Log.i(TAG, "${pair.first.entityId} clicked")
val intent = Intent(
CarContext.ACTION_NAVIGATE,
Uri.parse("geo:${location[0]},${location[1]}")
Uri.parse("geo:${pair.second[0]},${pair.second[1]}")
)
carContext.startCarApp(intent)
}
Expand Down

0 comments on commit c02025e

Please sign in to comment.