Skip to content

Commit

Permalink
feat: AA: Allow to configure color of buttons of virtual screens
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Sep 7, 2023
1 parent d330d89 commit cdf9f4e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 34 deletions.
52 changes: 28 additions & 24 deletions app/src/main/java/org/obd/graphs/preferences/PreferencesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import org.obd.graphs.preferences.dtc.DiagnosticTroubleCodeListPreferences
import org.obd.graphs.preferences.dtc.DiagnosticTroubleCodePreferenceDialog
import org.obd.graphs.preferences.metadata.VehicleMetadataListPreferences
import org.obd.graphs.preferences.metadata.VehicleMetadataPreferenceDialog
import org.obd.graphs.preferences.pid.PIDsListPreferences
import org.obd.graphs.preferences.pid.PIDsListPreferenceDialog
import org.obd.graphs.preferences.pid.PIDsListPreferences
import org.obd.graphs.preferences.trips.TripsListPreferences
import org.obd.graphs.preferences.trips.TripsPreferenceDialog
import org.obd.graphs.sendBroadcastEvent
Expand All @@ -36,6 +36,7 @@ const val PREF_GIULIA_DISPLAYED_PARAMETERS_IDS = "pref.giulia.displayed_paramete


private const val LOG_KEY = "Prefs"

class PreferencesFragment : PreferenceFragmentCompat() {

private val onSharedPreferenceChangeListener =
Expand All @@ -57,23 +58,21 @@ class PreferencesFragment : PreferenceFragmentCompat() {
when (preference.source) {

"graph" -> {
PIDsListPreferenceDialog(graphVirtualScreen.getVirtualScreenPrefKey(), preference.source)
.show(parentFragmentManager, null)
openPIDsDialog(graphVirtualScreen.getVirtualScreenPrefKey(),preference.source)
{ navigateToScreen(R.id.navigation_graph) }
}

"giulia" -> {
PIDsListPreferenceDialog(giuliaVirtualScreen.getVirtualScreenPrefKey(), preference.source)
.show(parentFragmentManager, null)
openPIDsDialog(giuliaVirtualScreen.getVirtualScreenPrefKey(),preference.source)
{ navigateToScreen(R.id.navigation_giulia) }
}

"gauge" -> {

PIDsListPreferenceDialog(gaugeVirtualScreen.getVirtualScreenPrefKey(), preference.source)
.show(parentFragmentManager, null)
openPIDsDialog(gaugeVirtualScreen.getVirtualScreenPrefKey(),preference.source)
{ navigateToScreen(R.id.navigation_gauge) }
}
else -> {
PIDsListPreferenceDialog(preference.key, preference.source)
.show(parentFragmentManager, null)
openPIDsDialog(preference.key, preference.source)
}
}
}
Expand All @@ -87,26 +86,29 @@ class PreferencesFragment : PreferenceFragmentCompat() {
}
}
}

override fun onNavigateToScreen(preferenceScreen: PreferenceScreen?) {
super.onNavigateToScreen(preferenceScreen)
setPreferencesFromResource(R.xml.preferences, preferenceScreen!!.key)
registerListeners()
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
if (arguments == null) {
setPreferencesFromResource(R.xml.preferences, rootKey)
} else {

val preferenceKey = requireArguments().get(PREFERENCE_SCREEN_KEY) as String
requireArguments().getString(PREFERENCE_SCREEN_KEY)?.let {

Log.d(LOG_KEY, "Loading Pref Screen for key=$preferenceKey")
Log.d(LOG_KEY, "Loading Pref Screen for key=$it")

setPreferencesFromResource(
R.xml.preferences,
preferenceKey
)
setPreferencesFromResource(
R.xml.preferences,
it
)

openPreferenceDialogFor(preferenceKey)
openPreferenceDialogFor(it)
}
}
}

Expand Down Expand Up @@ -224,19 +226,21 @@ class PreferencesFragment : PreferenceFragmentCompat() {
PREF_GAUGE_RECORDINGS -> TripsPreferenceDialog().show(parentFragmentManager, null)

PREF_GAUGE_DISPLAYED_PARAMETERS_IDS ->
PIDsListPreferenceDialog(gaugeVirtualScreen.getVirtualScreenPrefKey(),"gauge")
{ navigateToScreen(R.id.navigation_gauge) }
.show(parentFragmentManager, null)
openPIDsDialog(gaugeVirtualScreen.getVirtualScreenPrefKey(),"gauge")
{ navigateToScreen(R.id.navigation_gauge) }

PREF_GIULIA_DISPLAYED_PARAMETERS_IDS ->
PIDsListPreferenceDialog(giuliaVirtualScreen.getVirtualScreenPrefKey(), "giulia")
openPIDsDialog(giuliaVirtualScreen.getVirtualScreenPrefKey(),"giulia")
{ navigateToScreen(R.id.navigation_giulia) }
.show(parentFragmentManager, null)

PREF_GRAPH_DISPLAYED_PARAMETERS_IDS ->
PIDsListPreferenceDialog(graphVirtualScreen.getVirtualScreenPrefKey(), "graph")
openPIDsDialog(graphVirtualScreen.getVirtualScreenPrefKey(),"graph")
{ navigateToScreen(R.id.navigation_graph) }
.show(parentFragmentManager, null)
}
}

private fun openPIDsDialog(key: String, source: String, onDialogCloseListener: (() -> Unit) = {}) {
PIDsListPreferenceDialog(key = key, source = source, onDialogCloseListener = onDialogCloseListener)
.show(parentFragmentManager, null)
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@
<string name="pref.aa.theme.divider_color">Divider color</string>
<string name="pref.aa.theme.current_value_color">Current value color</string>
<string name="pref.aa.theme.inalert_value_color">In alert value color</string>
<string name="pref.aa.theme.virtual_screen_color">Virtual screen buttons color</string>


<string name="pref.aa.surface.fps.display_status">Display Head unit refresh rate (FPS)</string>
<string name="pref.aa.number_of_items_in_column">Number of OBD2 PIDs/Sensors displayed in the column</string>
Expand Down Expand Up @@ -395,6 +397,14 @@

</string-array>

<integer-array name="pref.colors">
<item name="-48060"> -48060</item>
<item name="-6697984">-6697984</item>
<item name="-17613">-17613</item>
<item name="-16737844">-16737844</item>
<item name="-1">-1</item>
</integer-array>

<string-array name="pref.numbers_1_10">
<item name="1">1</item>
<item name="2">2</item>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,13 @@
android:key="pref.aa.theme.inAlertValueColor"
android:title="@string/pref.aa.theme.inalert_value_color" />

<com.kizitonwose.colorpreferencecompat.ColorPreferenceCompat
app:colorChoices="@array/pref.colors"
android:defaultValue="0xffffffff"
android:key="pref.aa.theme.btn.virtual-screen.color"
android:title="@string/pref.aa.theme.virtual_screen_color" />


</PreferenceCategory>


Expand Down
10 changes: 0 additions & 10 deletions automotive/src/main/java/org/obd/graphs/aa/CarScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.util.Log
import androidx.car.app.CarContext
import androidx.car.app.Screen
Expand Down Expand Up @@ -298,15 +297,6 @@ internal class CarScreen(
dataLogger.stop()
}

private fun mapColor(color: Int): CarColor =
when (color) {
Color.RED -> CarColor.RED
Color.BLUE -> CarColor.BLUE
Color.GREEN -> CarColor.GREEN
Color.YELLOW -> CarColor.YELLOW
else -> CarColor.PRIMARY
}

init {

lifecycle.addObserver(this)
Expand Down
2 changes: 2 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 @@ -24,6 +24,7 @@ private const val PREF_ALERTING_ENABLED= "pref.aa.alerting.enabled"
private const val PREF_THEME_PROGRESS_BAR_COLOR= "pref.aa.theme.progressColor"
private const val PREF_THEME_DIVIDER_COLOR= "pref.aa.theme.dividerColor"
private const val PREF_THEME_CURR_VALUE_COLOR= "pref.aa.theme.currentValueColor"
private const val PREF_THEME_VIRTUAL_SCREEN_COLOR= "pref.aa.theme.btn.virtual-screen.color"

private const val PREF_CURRENT_VIRTUAL_SCREEN = "pref.aa.pids.vs.current"
private const val PREF_SELECTED_PIDS = "pref.aa.pids.selected"
Expand All @@ -48,6 +49,7 @@ internal class CarSettings(private val carContext: CarContext) : ScreenSettings
dividerColor = Prefs.getInt(PREF_THEME_DIVIDER_COLOR, Color.WHITE),
currentValueColor = Prefs.getInt(PREF_THEME_CURR_VALUE_COLOR, Color.WHITE),
currentValueInAlertColor = Prefs.getInt(PREF_THEME_IN_ALLERT_VALUE_COLOR, COLOR_DYNAMIC_SELECTOR_SPORT),
actionsBtnVirtualScreensColor = Prefs.getInt(PREF_THEME_VIRTUAL_SCREEN_COLOR, Color.GREEN)
)
}

Expand Down
22 changes: 22 additions & 0 deletions automotive/src/main/java/org/obd/graphs/aa/Colors.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.obd.graphs.aa

import android.graphics.Color
import androidx.car.app.model.CarColor
import org.obd.graphs.ui.common.COLOR_WHITE

internal fun mapColor(color: Int): CarColor {
return when (color) {
-1 -> CarColor.PRIMARY
-48060 -> CarColor.RED
-6697984 -> CarColor.GREEN
-17613 -> CarColor.YELLOW
-16737844 -> CarColor.BLUE

COLOR_WHITE -> CarColor.PRIMARY
Color.RED -> CarColor.RED
Color.BLUE -> CarColor.BLUE
Color.GREEN -> CarColor.GREEN
Color.YELLOW -> CarColor.YELLOW
else -> CarColor.PRIMARY
}
}
2 changes: 2 additions & 0 deletions common/src/main/java/org/obd/graphs/ui/common/Colors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ val COLOR_DYNAMIC_SELECTOR_ECO: Int = color(R.color.dynamic_selector_eco)
val COLOR_DYNAMIC_SELECTOR_SPORT: Int = color(R.color.dynamic_selector_sport)
val COLOR_DYNAMIC_SELECTOR_NORMAL: Int = color(R.color.dynamic_selector_normal)

val COLOR_WHITE: Int = color(R.color.white)

fun color(id: Int) = ContextCompat.getColor(getContext()!!, id)

0 comments on commit cdf9f4e

Please sign in to comment.