Skip to content

Commit

Permalink
Tweaked rendering and Template refreshing conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ixam97 committed Apr 29, 2024
1 parent f6a5e59 commit a0bb8f9
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,33 @@ class CarStatsViewerScreen(

private val CID_TRIP_DATA = "cid_trip_data"
private val CID_MENU = "cid_menu"
private val CID_CANVAS = "cid_nav_test"
private val CID_CANVAS = "cid_canvas"
private val CID_STATUS = "cid_status"

internal var dataUpdate = false
internal var apiState: Map<String, Int> = mapOf()
internal var session : DrivingSession? = null

internal val carDataSurfaceCallback = CarDataSurfaceCallback(carContext)

internal val appPreferences = CarStatsViewer.appPreferences

internal var drivingSession: DrivingSession? = null

internal val colorDisconnected = CarColor.createCustom(carContext.getColor(R.color.inactive_text_color), carContext.getColor(R.color.disabled_tint))
internal val colorConnected = CarColor.createCustom(carContext.getColor(R.color.connected_blue), carContext.getColor(R.color.connected_blue))
internal val colorLimited = CarColor.createCustom(carContext.getColor(R.color.limited_yellow), carContext.getColor(R.color.limited_yellow))
internal val colorError = CarColor.createCustom(carContext.getColor(R.color.bad_red), carContext.getColor(R.color.bad_red))

internal var resetFlag = false

internal var selectedTabContentID = CID_TRIP_DATA
internal var selectedTabContentID = CID_CANVAS
set(value) {
if (field != value) {
carDataSurfaceCallback.invalidatePlot()
}
field = value

}

private val settingsActivityIntent = Intent(carContext, SettingsActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
Expand All @@ -88,6 +96,10 @@ class CarStatsViewerScreen(
lifecycleScope.launch {
CarStatsViewer.dataProcessor.selectedSessionDataFlow.throttle(1000).collect {
carDataSurfaceCallback.updateSession()
drivingSession = it
if (selectedTabContentID != CID_CANVAS) {
invalidate()
}
}
}
setupListeners()
Expand Down Expand Up @@ -116,20 +128,19 @@ class CarStatsViewerScreen(

override fun onPause(owner: LifecycleOwner) {
super.onPause(owner)
carDataSurfaceCallback.pause()
// carDataSurfaceCallback.pause()
}

override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
carDataSurfaceCallback.resume()
// carDataSurfaceCallback.resume()
}

override fun onGetTemplate(): Template {
// updateLiveData()
if (dataUpdate) {
InAppLogger.i("[AAOS] Data Update")
this.apiState = CarStatsViewer.watchdog.getCurrentWatchdogState().apiState
session = CarStatsViewer.dataProcessor.selectedSessionData
dataUpdate = false
}

Expand All @@ -148,18 +159,25 @@ class CarStatsViewerScreen(
invalidate()
}
}).apply {
val tripType = when (appPreferences.mainViewTrip + 1) {
1 -> R.string.CurrentTripData
2 -> R.string.SinceChargeData
3 -> R.string.AutoTripData
4 -> R.string.CurrentMonthData
else -> R.string.car_app_unknown
}
setHeaderAction(Action.APP_ICON)
addTab(createTab(R.string.car_app_trip_data, CID_TRIP_DATA, R.drawable.ic_car_app_list))
addTab(createTab(R.string.car_app_dashboard, CID_CANVAS, R.drawable.ic_car_app_dashboard))
addTab(createTab(tripType, CID_TRIP_DATA, R.drawable.ic_car_app_list))
addTab(createTab(R.string.car_app_status, CID_STATUS, R.drawable.ic_car_app_status))
addTab(createTab(R.string.car_app_canvas, CID_CANVAS, R.drawable.ic_car_app_canvas))
addTab(createTab(R.string.car_app_menu, CID_MENU, R.drawable.ic_car_app_menu))
setTabContents(TabContents.Builder(
// TripDataList()
// createTripDataPane()
when (selectedTabContentID) {
CID_TRIP_DATA -> {
carDataSurfaceCallback.pause()
TripDataList()
TripDataList(drivingSession)
}
CID_STATUS -> {
carDataSurfaceCallback.pause()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ internal fun CarStatsViewerScreen.NavigationTest() = NavigationTemplate.Builder(
val currentDistance = appPreferences.mainPrimaryDimensionRestriction
appPreferences.mainPrimaryDimensionRestriction = if (currentDistance >= 2) 0 else currentDistance + 1
invalidate()
carDataSurfaceCallback.renderFrame()
carDataSurfaceCallback.invalidatePlot()
// carDataSurfaceCallback.renderFrame()
}
}.build())
}.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.car.app.model.SectionedItemList
import androidx.core.graphics.drawable.IconCompat
import com.ixam97.carStatsViewer.CarStatsViewer
import com.ixam97.carStatsViewer.R
import com.ixam97.carStatsViewer.database.tripData.DrivingSession
import com.ixam97.carStatsViewer.database.tripData.TripType
import com.ixam97.carStatsViewer.utils.InAppLogger
import com.ixam97.carStatsViewer.utils.StringFormatters
Expand All @@ -26,134 +27,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@OptIn(ExperimentalCarApi::class)
internal fun CarStatsViewerScreen.TripDataPane() = PaneTemplate.Builder(Pane.Builder().apply {
session?.let {
val tripType = when (it.session_type) {
1 -> carContext.getString(R.string.CurrentTripData)
2 -> carContext.getString(R.string.SinceChargeData)
3 -> carContext.getString(R.string.AutoTripData)
4 -> carContext.getString(R.string.CurrentMonthData)
else -> "unknown"
}
val tripTypeIcon = when (it.session_type) {
1 -> CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_hand)).build()
2 -> CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_charger)).build()
3 -> CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_day)).build()
4 -> CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_month)).build()
else -> CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_help)).build()
}
addRow(createDataRow(
StringFormatters.getTraveledDistanceString(it.driven_distance.toFloat()),
carContext.getString(R.string.summary_traveled_distance),
R.drawable.ic_distance_large
))
addRow(createDataRow(
StringFormatters.getEnergyString(it.used_energy.toFloat()),
carContext.getString(R.string.summary_used_energy),
R.drawable.ic_energy_large
))
addRow(createDataRow(
StringFormatters.getAvgConsumptionString(it.used_energy.toFloat(), it.driven_distance.toFloat()),
carContext.getString(R.string.summary_average_consumption),
R.drawable.ic_diagram
))
addRow(createDataRow(
StringFormatters.getAvgSpeedString(it.driven_distance.toFloat(), it.drive_time),
carContext.getString(R.string.summary_speed),
R.drawable.ic_speed_large
))
try {
addRow(
createDataRow(
StringFormatters.getElapsedTimeString(it.drive_time, true),
carContext.getString(R.string.summary_travel_time),
R.drawable.ic_time_large
)
)
} catch (e: Exception) {
InAppLogger.e("Row failed: ${e.stackTraceToString()}")
}

var statusString = ""
var index = 0
var apiIconColor = colorDisconnected

apiState.forEach { apiName, status ->
statusString += "$apiName: $status "
if (index == CarStatsViewer.appPreferences.mainViewConnectionApi) {
apiIconColor = when (status) {
0 -> colorDisconnected
1 -> colorConnected
2 -> colorLimited
else -> colorError
}
}

index++
}

addRow(createDataRow(
statusString,
"Api Status",
R.drawable.ic_connected,
apiIconColor
))

if (it.session_type == 1) {
addAction(Action.Builder().apply {
setIcon(CarIcon.Builder(IconCompat.createWithResource(carContext, R.drawable.ic_reset)).build())
setTitle(carContext.getString(R.string.dialog_reset_confirm))
setOnClickListener {
if (resetFlag) {
CoroutineScope(Dispatchers.IO).launch {
CarStatsViewer.dataProcessor.resetTrip(
TripType.MANUAL,
CarStatsViewer.dataProcessor.realTimeData.drivingState
)
}
resetFlag = false
CarToast.makeText(carContext, carContext.getString(R.string.car_app_toast_reset_confirmation), CarToast.LENGTH_LONG).show()
} else {
CoroutineScope(Dispatchers.IO).launch {
delay(4_000)
resetFlag = false
invalidate()
}
resetFlag = true
CarToast.makeText(carContext, carContext.getString(R.string.car_app_toast_reset_hint), CarToast.LENGTH_LONG).show()
}
invalidate()
}
if (resetFlag) {
setFlags(Action.FLAG_PRIMARY)
}
}.build())
}

addAction(Action.Builder().apply {
setIcon(tripTypeIcon)
setTitle(tripType)
setOnClickListener {
val newTripType = if (it.session_type >= 4 ) {
1
} else {
it.session_type + 1
}
CarStatsViewer.dataProcessor.changeSelectedTrip(newTripType)
CarStatsViewer.appPreferences.mainViewTrip = newTripType - 1
// session = null
// invalidate()
}
// setFlags(Action.FLAG_PRIMARY)
}.build())

} ?: setLoading(true)
}.build()).apply {
setTitle("Dummy")
}.build()

@OptIn(ExperimentalCarApi::class)
internal fun CarStatsViewerScreen.TripDataList() = ListTemplate.Builder().apply {
internal fun CarStatsViewerScreen.TripDataList(session: DrivingSession?) = ListTemplate.Builder().apply {

if (session == null) {
setLoading(true)
Expand All @@ -165,7 +39,7 @@ internal fun CarStatsViewerScreen.TripDataList() = ListTemplate.Builder().apply
4 -> carContext.getString(R.string.CurrentMonthData)
else -> "unknown"
}
addSectionedList(SectionedItemList.create(ItemList.Builder().apply {
setSingleList(ItemList.Builder().apply {
session?.let {

addItem(createDataRow(
Expand Down Expand Up @@ -219,7 +93,7 @@ internal fun CarStatsViewerScreen.TripDataList() = ListTemplate.Builder().apply
apiIconColor
))
}
}.build(), tripType))
}.build())

addAction(Action.Builder().apply {
val backgroundColor = carContext.getColor(R.color.default_button_color)
Expand Down Expand Up @@ -253,7 +127,8 @@ internal fun CarStatsViewerScreen.TripDataList() = ListTemplate.Builder().apply

}.build()

private fun CarStatsViewerScreen.createDataRow(value: String, name: String, iconResId: Int, iconColor: CarColor = CarColor.DEFAULT) = Row.Builder().apply {
@OptIn(ExperimentalCarApi::class) private
fun CarStatsViewerScreen.createDataRow(value: String, name: String, iconResId: Int, iconColor: CarColor = CarColor.DEFAULT) = Row.Builder().apply {
setTitle(value)
setImage(CarIcon.Builder(IconCompat.createWithResource(carContext, iconResId)).setTint(iconColor).build())
addText(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,44 +29,58 @@ class CarDataSurfaceCallback(val carContext: CarContext): SurfaceCallback {


override fun onSurfaceAvailable(surfaceContainer: SurfaceContainer) {
Log.d(TAG, "Surface available")
InAppLogger.d("[$TAG] Surface available")
super.onSurfaceAvailable(surfaceContainer)
surface = surfaceContainer.surface
renderFrame()
}

override fun onVisibleAreaChanged(visibleArea: Rect) {
Log.i(TAG, "Visible area changed " + surface + ". stableArea: "
InAppLogger.i("[$TAG] Visible area changed " + surface + ". stableArea: "
+ stableArea + " visibleArea:" + visibleArea)
this.visibleArea = visibleArea
renderFrame()
}

override fun onStableAreaChanged(stableArea: Rect) {
Log.i(TAG, "Stable area changed " + surface + ". stableArea: "
InAppLogger.i("[$TAG] Stable area changed " + surface + ". stableArea: "
+ stableArea + " visibleArea:" + visibleArea)
super.onStableAreaChanged(stableArea)
this.stableArea = stableArea
renderFrame()
}

fun pause() {
renderFrame(clearFrame = true)
rendererEnabled = false
}

fun resume() {
rendererEnabled = true
invalidatePlot()
}

fun renderFrame() {
fun renderFrame(clearFrame: Boolean = false) {
if (!rendererEnabled) return

Log.d(TAG, "Rendering Frame")
InAppLogger.d("[$TAG] Rendering Frame")
defaultRenderer.setData(CarStatsViewer.dataProcessor.realTimeData)

surface?.apply {
if(!isValid) return
val canvas: Canvas = lockCanvas(null)
val canvas: Canvas = try {
lockCanvas(null)
} catch (e: Exception) {
InAppLogger.w("[$TAG] Failed to get canvas:\n${e.printStackTrace()}")
return
}

if (clearFrame) {
canvas.drawColor(Color.BLACK)
unlockCanvasAndPost(canvas)
return
}

canvas.drawColor(carContext.getColor(R.color.slideup_activity_background))
// canvas.drawColor(if (carContext.isDarkMode) Color.BLACK else Color.WHITE)

Expand All @@ -83,4 +97,9 @@ class CarDataSurfaceCallback(val carContext: CarContext): SurfaceCallback {
defaultRenderer.updateSession()
renderFrame()
}

fun invalidatePlot() {
defaultRenderer.refreshConsumptionPlot()
renderFrame()
}
}
Loading

0 comments on commit a0bb8f9

Please sign in to comment.