Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/last visited screen #28

Merged
merged 9 commits into from
Aug 4, 2024
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ android {
dimension "version"
resValue "string", "DEFAULT_PROFILE", "profile_8"
applicationId "org.obd.graphs.my.giulia.aa"
versionCode 108
versionCode 138
}

giuliaPerformanceMonitor {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<string name="pref.aa.display_pid_history">Display PIDs min and max value collected</string>
<string name="pref.aa.display_pid_scale">Display PIDs scale</string>

<string name="pref.aa.screen.load_last_visited.enabled">Load automatically last visited screen</string>
<string name="pref.aa.display_status_panel">Display status panel</string>
<string name="pref.aa.connection_auto">Automatic connection to the OBD2 adapter when enter AA mode</string>
<string name="pref.aa.virtual_screens.sort_order.enabled">Enable PIDs sort order</string>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,12 @@
android:title="@string/pref.aa.connection_auto"
app:singleLineTitle="false" />

<CheckBoxPreference
android:defaultValue="false"
android:key="pref.aa.screen.load_last_visited.enabled"
android:title="@string/pref.aa.screen.load_last_visited.enabled"
app:singleLineTitle="false" />

<org.obd.graphs.preferences.aa.AACheckBoxPreference
android:defaultValue="true"
android:key="pref.aa.virtual_screens.status_panel.enabled"
Expand Down
9 changes: 9 additions & 0 deletions automotive/src/main/java/org/obd/graphs/aa/CarSettings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.obd.graphs.ui.common.COLOR_DYNAMIC_SELECTOR_NORMAL
import org.obd.graphs.ui.common.COLOR_DYNAMIC_SELECTOR_RACE
import org.obd.graphs.ui.common.COLOR_DYNAMIC_SELECTOR_SPORT

private const val LAST_USER_SCREEN = "pref.aa.screen.last_used"

private const val PREF_PIDS_HISTORY_ENABLED = "pref.aa.pids.history.enabled"

Expand Down Expand Up @@ -114,6 +115,14 @@ class CarSettings(private val carContext: CarContext) : ScreenSettings {

fun isAutomaticConnectEnabled(): Boolean = Prefs.getBoolean("pref.aa.connection.auto.enabled", false)

fun isLoadLastVisitedScreenEnabled(): Boolean = Prefs.getBoolean("pref.aa.screen.load_last_visited.enabled", false)

fun getLastVisitedScreen(): Int = Prefs.getInt(LAST_USER_SCREEN, 0)

fun setLastVisitedScreen(newScreen: Int){
Prefs.updateInt(LAST_USER_SCREEN, newScreen)
}

override fun getGaugeRendererSetting(): GaugeRendererSettings = gaugeRendererSettings.apply {
gaugeProgressBarType = GaugeProgressBarType.valueOf(Prefs.getS("pref.aa.virtual_screens.screen.gauge.progress_type", GaugeProgressBarType.LONG.name))
topOffset = Prefs.getS("pref.aa.virtual_screens.gauge.top_offset","0").toInt()
Expand Down
4 changes: 2 additions & 2 deletions automotive/src/main/java/org/obd/graphs/aa/Toast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ package org.obd.graphs.aa
import android.util.Log
import androidx.car.app.CarContext
import androidx.car.app.CarToast
import org.obd.graphs.aa.screen.LOG_KEY
import org.obd.graphs.aa.screen.LOG_TAG


internal val toast = Toast()
Expand All @@ -38,7 +38,7 @@ internal class Toast {
msg, CarToast.LENGTH_LONG
).show()
} catch (e: Exception){
Log.w(LOG_KEY,"Failed to show toast",e)
Log.w(LOG_TAG,"Failed to show toast",e)
}
}
}
101 changes: 52 additions & 49 deletions automotive/src/main/java/org/obd/graphs/aa/screen/CarScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import androidx.car.app.model.CarColor
import androidx.lifecycle.DefaultLifecycleObserver
import org.obd.graphs.AA_EDIT_PREF_SCREEN
import org.obd.graphs.RenderingThread
import org.obd.graphs.aa.*
import org.obd.graphs.aa.CarSettings
import org.obd.graphs.aa.R
import org.obd.graphs.aa.mapColor
import org.obd.graphs.aa.screen.nav.*
import org.obd.graphs.aa.screen.nav.CHANGE_SCREEN_EVENT
import org.obd.graphs.aa.toast
import org.obd.graphs.bl.collector.MetricsCollector
import org.obd.graphs.bl.datalogger.WorkflowStatus
Expand All @@ -40,25 +41,30 @@ import org.obd.graphs.bl.query.QueryStrategyType
import org.obd.graphs.renderer.Fps
import org.obd.graphs.sendBroadcastEvent


const val VIRTUAL_SCREEN_1_SETTINGS_CHANGED = "pref.aa.pids.profile_1.event.changed"
const val VIRTUAL_SCREEN_2_SETTINGS_CHANGED = "pref.aa.pids.profile_2.event.changed"
const val VIRTUAL_SCREEN_3_SETTINGS_CHANGED
= "pref.aa.pids.profile_3.event.changed"
const val VIRTUAL_SCREEN_3_SETTINGS_CHANGED = "pref.aa.pids.profile_3.event.changed"
const val VIRTUAL_SCREEN_4_SETTINGS_CHANGED = "pref.aa.pids.profile_4.event.changed"
const val LOG_KEY = "CarScreen"
const val LOG_TAG = "CarScreen"

internal abstract class CarScreen(
carContext: CarContext,
protected val settings: CarSettings,
protected val metricsCollector: MetricsCollector,
protected val fps: Fps = Fps()
) : Screen(carContext), DefaultLifecycleObserver {
) : Screen(carContext), DefaultLifecycleObserver {

protected val query = Query.instance()

protected open fun gotoScreen(newScreen: Int) {}
protected open fun updateLastVisitedScreen(newScreen: Int) {
settings.setLastVisitedScreen(newScreen)
}

protected open fun renderAction() {}

open fun onCarConfigurationChanged(){}
open fun onCarConfigurationChanged() {}

protected val renderingThread: RenderingThread = RenderingThread(
id = "CarScreenRenderingThread",
Expand All @@ -74,6 +80,22 @@ internal abstract class CarScreen(
CarConnection(carContext).type.observe(this, ::onConnectionStateUpdated)
}

protected fun actionStopDataLogging() {
Log.i(LOG_TAG, "Stopping data logging process")
dataLogger.stop()
cancelRenderingTask()
}

protected open fun actionStartDataLogging() {
if (dataLoggerPreferences.instance.individualQueryStrategyEnabled) {
query.setStrategy(QueryStrategyType.INDIVIDUAL_QUERY_FOR_EACH_VIEW)
query.update(metricsCollector.getMetrics().map { p -> p.source.command.pid.id }.toSet())
} else {
query.setStrategy(QueryStrategyType.SHARED_QUERY)
}
dataLogger.start(query)
}

protected open fun getHorizontalActionStrip(
preferencesEnabled: Boolean = true,
exitEnabled: Boolean = true,
Expand All @@ -83,40 +105,24 @@ internal abstract class CarScreen(
var builder = ActionStrip.Builder()

builder = if (dataLogger.status() == WorkflowStatus.Connecting || dataLogger.status() == WorkflowStatus.Connected) {
builder.addAction(createAction(carContext, R.drawable.action_disconnect, mapColor(settings.getColorTheme().actionsBtnDisconnectColor)) {
stopDataLogging()
toast.show(carContext, R.string.toast_connection_disconnect)
})
builder.addAction(
createAction(
carContext,
R.drawable.action_disconnect,
mapColor(settings.getColorTheme().actionsBtnDisconnectColor)
) {
actionStopDataLogging()
toast.show(carContext, R.string.toast_connection_disconnect)
})
} else {
builder.addAction(createAction(carContext, R.drawable.actions_connect, mapColor(settings.getColorTheme().actionsBtnConnectColor)) {
when (screenId) {
GIULIA_SCREEN_ID -> {
if (dataLoggerPreferences.instance.individualQueryStrategyEnabled) {
query.setStrategy(QueryStrategyType.INDIVIDUAL_QUERY_FOR_EACH_VIEW)
query.update(metricsCollector.getMetrics().map { p -> p.source.command.pid.id }.toSet())
} else {
query.setStrategy(QueryStrategyType.SHARED_QUERY)
}
dataLogger.start(query)
}

DRAG_RACING_SCREEN_ID ->
dataLogger.start(query.apply{
setStrategy(QueryStrategyType.DRAG_RACING_QUERY)
})

TRIP_INFO_SCREEN_ID ->
dataLogger.start(query.apply{
setStrategy(QueryStrategyType.TRIP_INFO_QUERY)
})

ROUTINES_SCREEN_ID -> {
dataLogger.start(query.apply{
setStrategy(QueryStrategyType.ROUTINES_QUERY)
})
}
}
})
builder.addAction(
createAction(
carContext,
R.drawable.actions_connect,
mapColor(settings.getColorTheme().actionsBtnConnectColor)
) {
actionStartDataLogging()
})
}

if (featureListsEnabledSetting) {
Expand All @@ -135,9 +141,9 @@ internal abstract class CarScreen(
if (exitEnabled) {
builder = builder.addAction(createAction(carContext, R.drawable.action_exit, CarColor.RED) {
try {
stopDataLogging()
actionStopDataLogging()
} finally {
Log.i(LOG_KEY, "Exiting the app. Closing the context")
Log.i(LOG_TAG, "Exiting the app. Closing the context")
carContext.finishCarApp()
}
})
Expand All @@ -158,21 +164,18 @@ internal abstract class CarScreen(
}
}


private fun onConnectionStateUpdated(connectionState: Int) {
when (connectionState) {
CarConnection.CONNECTION_TYPE_PROJECTION -> {
if (settings.isAutomaticConnectEnabled() && !dataLogger.isRunning()) {
query.setStrategy(QueryStrategyType.SHARED_QUERY)
dataLogger.start(query)
}

if (settings.isLoadLastVisitedScreenEnabled()) {
gotoScreen(settings.getLastVisitedScreen())
}
}
}
}

protected fun stopDataLogging() {
Log.i(LOG_KEY, "Stopping data logging process")
dataLogger.stop()
cancelRenderingTask()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ internal class DynamicSelectorModeEventBroadcaster: MetricsProcessor {
override fun postValue(obdMetric: ObdMetric) {
if (obdMetric.isDynamicSelector()) {

if (Log.isLoggable(LOG_KEY, Log.VERBOSE)) {
Log.v(LOG_KEY, "Received=${obdMetric.value.toInt()}, current=${currentMode} ")
if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
Log.v(LOG_TAG, "Received=${obdMetric.value.toInt()}, current=${currentMode} ")
}

if (currentMode != obdMetric.value) {

if (Log.isLoggable(LOG_KEY, Log.VERBOSE)) {
Log.v(LOG_KEY, "Broadcasting Dynamic Selector Mode Change, new=${obdMetric.value.toInt()}")
if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
Log.v(LOG_TAG, "Broadcasting Dynamic Selector Mode Change, new=${obdMetric.value.toInt()}")
}

currentMode = obdMetric.value.toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ internal class IotTemplateCarScreen(
AA_VIRTUAL_SCREEN_REFRESH_EVENT -> invalidate()

MAIN_ACTIVITY_EVENT_DESTROYED -> {
Log.v(LOG_KEY, "Main activity has been destroyed.")
Log.v(LOG_TAG, "Main activity has been destroyed.")
invalidate()
}
MAIN_ACTIVITY_EVENT_PAUSE -> {
Log.v(LOG_KEY, "Main activity is going to the background.")
Log.v(LOG_TAG, "Main activity is going to the background.")
invalidate()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ import org.obd.graphs.aa.R
import org.obd.graphs.aa.screen.createAction
import org.obd.graphs.bl.datalogger.*

const val GIULIA_SCREEN_ID = 0
const val DRAG_RACING_SCREEN_ID = 1
const val LOG_TAG = "AvailableFeaturesScreen"
const val ROUTINES_SCREEN_ID = 2
const val TRIP_INFO_SCREEN_ID = 3


data class FeatureDescription(val id: Int, val iconId: Int, val title: String)

internal class AvailableFeaturesScreen(
carContext: CarContext,
private val screenMapping: List<FeatureDescription>,
private val carSettings: CarSettings
) : Screen(carContext) {

Expand All @@ -50,7 +52,7 @@ internal class AvailableFeaturesScreen(
listTemplate()
}
} catch (e: Exception) {
Log.e(org.obd.graphs.aa.screen.LOG_KEY, "Failed to build template", e)
Log.e(LOG_TAG, "Failed to build template", e)
PaneTemplate.Builder(Pane.Builder().setLoading(true).build())
.setHeaderAction(Action.BACK)
.setTitle(carContext.getString(R.string.pref_aa_car_error))
Expand All @@ -59,30 +61,10 @@ internal class AvailableFeaturesScreen(
private fun listTemplate(): ListTemplate {
val items = ItemList.Builder().apply {

if (carSettings.getTripInfoScreenSettings().viewEnabled) {
addItem(
row(
TRIP_INFO_SCREEN_ID, R.drawable.action_giulia,
carContext.getString(R.string.available_features_trip_info_screen_title)
)
)
screenMapping.forEach {
addItem(row(it.id, it.iconId, it.title))
}


addItem(
row(
DRAG_RACING_SCREEN_ID, R.drawable.action_drag_race_screen,
carContext.getString(R.string.available_features_drag_race_screen_title)
)
)

addItem(
row(
GIULIA_SCREEN_ID, R.drawable.action_giulia,
carContext.getString(R.string.available_features_giulia_screen_title)
)
)

if (carSettings.getRoutinesScreenSettings().viewEnabled){
addItem(
row(
Expand Down Expand Up @@ -122,7 +104,7 @@ internal class AvailableFeaturesScreen(
private fun getHorizontalActionStrip(): ActionStrip {
var builder = ActionStrip.Builder()
builder = builder.addAction(createAction(carContext, R.drawable.action_exit, CarColor.RED) {
Log.i(org.obd.graphs.aa.screen.LOG_KEY, "Exiting the app. Closing the context")
Log.i(org.obd.graphs.aa.screen.LOG_TAG, "Exiting the app. Closing the context")
carContext.finishCarApp()
})
return builder.build()
Expand Down
Loading
Loading