Skip to content

Commit 380b33d

Browse files
authored
Feat/trip log drive auto-sync (#167)
1 parent 5200908 commit 380b33d

25 files changed

Lines changed: 587 additions & 218 deletions

File tree

app/src/main/java/org/obd/graphs/activity/NavigationRouter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import org.obd.graphs.preferences.PREFERENCE_SCREEN_KEY_GRAPH
3333
import org.obd.graphs.preferences.PREFERENCE_SCREEN_KEY_PERFORMANCE
3434
import org.obd.graphs.preferences.PREFERENCE_SCREEN_KEY_TRIP_INFO
3535
import org.obd.graphs.preferences.PREF_GAUGE_TRIPS
36-
import org.obd.graphs.preferences.PREF_LOGS
36+
import org.obd.graphs.preferences.PREF_TRIP_LOGS
3737
import org.obd.graphs.preferences.Prefs
3838
import org.obd.graphs.preferences.getS
3939
import org.obd.graphs.preferences.getStringSet
@@ -109,7 +109,7 @@ internal object NavigationRouter {
109109
R.id.nav_preferences -> navigateToPreferencesScreen("pref.root")
110110
R.id.navigation_adapter_connection -> navigateToPreferencesScreen("pref.adapter.connection")
111111
R.id.navigation_adapter_settings -> navigateToPreferencesScreen("pref.adapter")
112-
R.id.navigation_trip_logs -> navigateToPreferencesScreen(PREF_LOGS)
112+
R.id.navigation_trip_logs -> navigateToPreferencesScreen(PREF_TRIP_LOGS)
113113

114114
R.id.navigation_giulia -> navigateToScreen(R.id.nav_giulia)
115115
R.id.navigation_graph -> navigateToScreen(R.id.nav_graph)

app/src/main/java/org/obd/graphs/activity/Receivers.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import org.obd.graphs.ScreenLock
5454
import org.obd.graphs.TRIPS_UPLOAD_FAILED
5555
import org.obd.graphs.TRIPS_UPLOAD_NO_FILES_SELECTED
5656
import org.obd.graphs.TRIPS_UPLOAD_SUCCESSFUL
57+
import org.obd.graphs.TRIP_LOG_WRITE_COMPLETED
5758
import org.obd.graphs.bl.datalogger.DATA_LOGGER_ADAPTER_NOT_SET_EVENT
5859
import org.obd.graphs.bl.datalogger.DATA_LOGGER_CONNECTED_EVENT
5960
import org.obd.graphs.bl.datalogger.DATA_LOGGER_CONNECTING_EVENT
@@ -75,6 +76,7 @@ import org.obd.graphs.bl.extra.EVENT_VEHICLE_STATUS_VEHICLE_IDLING
7576
import org.obd.graphs.bl.extra.EVENT_VEHICLE_STATUS_VEHICLE_RUNNING
7677
import org.obd.graphs.getContext
7778
import org.obd.graphs.getSerializableCompat
79+
import org.obd.graphs.integrations.gcp.gdrive.DriveSync
7880
import org.obd.graphs.preferences.PREFS_CONNECTION_TYPE_CHANGED_EVENT
7981
import org.obd.graphs.preferences.Prefs
8082
import org.obd.graphs.preferences.isEnabled
@@ -101,6 +103,10 @@ private const val EVENT_VEHICLE_STATUS_CHANGED = "event.vehicle.status.CHANGED"
101103

102104
internal fun MainActivity.receive(intent: Intent?) {
103105
when (intent?.action) {
106+
TRIP_LOG_WRITE_COMPLETED -> {
107+
DriveSync.start(this)
108+
}
109+
104110
DATA_LOGGER_SCHEDULED_STOP_EVENT -> {
105111
Log.d(
106112
LOG_TAG,
@@ -388,6 +394,7 @@ internal fun MainActivity.registerReceiver() {
388394
it.addAction(NAVIGATION_BUTTONS_VISIBILITY_CHANGED)
389395
it.addAction(DATA_LOGGER_SCHEDULED_STOP_EVENT)
390396
it.addAction(PROFILE_NAME_CHANGED_EVENT)
397+
it.addAction(TRIP_LOG_WRITE_COMPLETED)
391398
}
392399

393400
registerReceiver(this, DataLoggerRepository.broadcastReceivers()) {

app/src/main/java/org/obd/graphs/preferences/PreferencesFragment.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const val PREFERENCE_SCREEN_KEY = "preferences.rootKey"
6060
const val PREFS_CONNECTION_TYPE_CHANGED_EVENT = "prefs.connection_type.changed.event"
6161

6262
const val PREF_GAUGE_TRIPS = "pref.gauge.recordings"
63-
const val PREF_LOGS = "pref.trip_logs"
63+
const val PREF_TRIP_LOGS = "pref.trip_logs"
6464

6565
const val PREFERENCE_CONNECTION_TYPE = "pref.adapter.connection.type"
6666
private const val LOG_KEY = "Prefs"
@@ -278,7 +278,7 @@ class PreferencesFragment : PreferenceFragmentCompat() {
278278
private fun openPreferenceDialogFor(preferenceKey: String) {
279279
when (preferenceKey) {
280280
PREF_GAUGE_TRIPS -> TripLogListDialogFragment(enableUploadCloudButton = false).show(parentFragmentManager, null)
281-
PREF_LOGS -> TripLogListDialogFragment(enableDeleteButtons = false).show(parentFragmentManager, null)
281+
PREF_TRIP_LOGS -> TripLogListDialogFragment(enableDeleteButtons = false).show(parentFragmentManager, null)
282282

283283
PREFERENCE_SCREEN_KEY_TRIP_INFO ->
284284
openPIDsDialog(

app/src/main/java/org/obd/graphs/preferences/trips/TripLogListDialogFragment.kt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.obd.graphs.preferences.trips
1818

1919
import android.annotation.SuppressLint
20+
import android.content.BroadcastReceiver
21+
import android.content.Context
22+
import android.content.Intent
2023
import android.os.Bundle
2124
import android.util.Log
2225
import android.view.LayoutInflater
@@ -33,11 +36,13 @@ import org.obd.graphs.R
3336
import org.obd.graphs.SCREEN_LOCK_PROGRESS_EVENT
3437
import org.obd.graphs.SCREEN_UNLOCK_PROGRESS_EVENT
3538
import org.obd.graphs.TRIPS_UPLOAD_NO_FILES_SELECTED
39+
import org.obd.graphs.TRIPS_UPLOAD_SUCCESSFUL
3640
import org.obd.graphs.activity.navigateToScreen
3741
import org.obd.graphs.bl.trip.TripFileDesc
3842
import org.obd.graphs.bl.trip.tripManager
3943
import org.obd.graphs.integrations.gcp.gdrive.TripLogDriveManager
4044
import org.obd.graphs.preferences.CoreDialogFragment
45+
import org.obd.graphs.registerReceiver
4146
import org.obd.graphs.sendBroadcastEvent
4247
import java.io.File
4348

@@ -52,6 +57,39 @@ class TripLogListDialogFragment(
5257
) : CoreDialogFragment() {
5358
private lateinit var tripLogDriveManager: TripLogDriveManager
5459

60+
private lateinit var adapter: TripViewAdapter
61+
62+
private var broadcastReceiver =
63+
object : BroadcastReceiver() {
64+
@SuppressLint("NotifyDataSetChanged")
65+
override fun onReceive(
66+
context: Context?,
67+
intent: Intent?
68+
) {
69+
when (intent?.action) {
70+
TRIPS_UPLOAD_SUCCESSFUL -> {
71+
if (isAdded && isVisible) {
72+
adapter.data = tripManager.findAllTripsBy().map { TripLogDetails(source = it) }.toMutableList()
73+
adapter.notifyDataSetChanged()
74+
}
75+
}
76+
}
77+
}
78+
}
79+
80+
override fun onPause() {
81+
super.onPause()
82+
requireContext().unregisterReceiver(broadcastReceiver)
83+
}
84+
85+
override fun onResume() {
86+
super.onResume()
87+
88+
registerReceiver(activity, broadcastReceiver) {
89+
it.addAction(TRIPS_UPLOAD_SUCCESSFUL)
90+
}
91+
}
92+
5593
override fun onCreate(savedInstanceState: Bundle?) {
5694
super.onCreate(savedInstanceState)
5795
tripLogDriveManager = TripLogDriveManager.instance(getString(R.string.ANDROID_WEB_CLIENT_ID), requireActivity(), this)
@@ -66,12 +104,13 @@ class TripLogListDialogFragment(
66104
requestWindowFeatures()
67105

68106
val root = inflater.inflate(R.layout.dialog_trip, container, false)
69-
val adapter =
107+
adapter =
70108
TripViewAdapter(
71109
context,
72110
tripManager.findAllTripsBy().map { TripLogDetails(source = it) }.toMutableList(),
73111
enableDeleteButtons
74112
)
113+
75114
val recyclerView: RecyclerView = root.findViewById(R.id.recycler_view)
76115
recyclerView.layoutManager = GridLayoutManager(context, 1)
77116
recyclerView.adapter = adapter
@@ -131,7 +170,7 @@ class TripLogListDialogFragment(
131170
sendBroadcastEvent(TRIPS_UPLOAD_NO_FILES_SELECTED)
132171
} else {
133172
lifecycleScope.launch {
134-
tripLogDriveManager.exportTrips(files)
173+
tripLogDriveManager.uploadTrips(files)
135174
}
136175
}
137176
}.setNegativeButton(no) { dialog, _ ->

app/src/main/java/org/obd/graphs/preferences/trips/TripViewAdapter.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ package org.obd.graphs.preferences.trips
1919
import android.content.Context
2020
import android.graphics.Color
2121
import android.graphics.Typeface
22+
import android.text.Spannable
23+
import android.text.SpannableString
24+
import android.text.style.ForegroundColorSpan
2225
import android.util.Log
2326
import android.view.LayoutInflater
2427
import android.view.View
@@ -28,6 +31,7 @@ import android.widget.CheckBox
2831
import android.widget.LinearLayout
2932
import android.widget.TextView
3033
import androidx.appcompat.app.AlertDialog
34+
import androidx.core.graphics.toColorInt
3135
import androidx.recyclerview.widget.RecyclerView
3236
import org.obd.graphs.R
3337
import org.obd.graphs.bl.trip.tripManager
@@ -80,7 +84,28 @@ class TripViewAdapter internal constructor(
8084
startTs = dateFormat.format(Date(it))
8185
}
8286

83-
holder.tripStartDate.setText(startTs, Color.GRAY, Typeface.NORMAL, 0.9f)
87+
source.startTime.toLongOrNull()?.let {
88+
startTs = dateFormat.format(Date(it))
89+
}
90+
91+
if (source.isSynced) {
92+
val syncText = " ☁️ Synced"
93+
val fullText = startTs + syncText
94+
95+
holder.tripStartDate.setText(fullText, Color.GRAY, Typeface.NORMAL, 0.9f)
96+
97+
val spannable = SpannableString(fullText)
98+
spannable.setSpan(
99+
ForegroundColorSpan("#4CAF50".toColorInt()), // A nice Material Green
100+
startTs.length,
101+
fullText.length,
102+
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
103+
)
104+
105+
holder.tripStartDate.text = spannable
106+
} else {
107+
holder.tripStartDate.setText(startTs, Color.GRAY, Typeface.NORMAL, 0.9f)
108+
}
84109

85110
holder.selected.isChecked = checked
86111
holder.selected.setOnCheckedChangeListener { buttonView, isChecked ->

app/src/main/res/menu/left_nav_menu.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258
<item
259259
android:id="@+id/navigation_graph_tripe"
260260
android:checkable="true"
261-
android:title="@string/pref.trips_recordings" />
261+
android:title="@string/pref.trips.title" />
262262

263263
<item
264264
android:id="@+id/navigation_graph_pids"

app/src/main/res/values-pl/strings.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,11 @@
424424
<string name="pref.dash_swipe_to_delete">Włącz usunięcie metryki przesunięciem</string>
425425
<string name="pref.dash_top_values_blink">Alarm dla najwyższych wartości</string>
426426
<string name="pref.trips_category">Trasy</string>
427-
<string name="pref.trips_recordings">Nagrane trasy</string>
427+
<string name="pref.trips.title">Nagrane trasy</string>
428+
429+
<string name="pref.trips.drive.auto_sync_enabled">Automatyczna synchronizacja z Dyskiem Google</string>
430+
<string name="pref.trips.drive.auto_sync_enabled_summary">Automatycznie przesyłaj nowe przejazdy na Dysk Google po połączeniu z siecią Wi-Fi.</string>
431+
428432
<string name="pref.view_category_summary">W tej sekcji możesz dostosować ustawienia związane z widokami Gauge, Wykres i Giulia.</string>
429433
<string name="pref.view_category">Widoki</string>
430434
<string name="pref.toolbar_category">Pasek narzędzi</string>

app/src/main/res/values/strings.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
<string name="nav_open">Open</string>
33
<string name="nav_close">Close</string>
44

5+
6+
7+
58
<string name="pref.performance.screen_label_y_padding"><b><i>Gauge</i></b> label top offset</string>
69
<string name="pref.performance.screen_top_margin">Top margin</string>
710

@@ -423,7 +426,10 @@
423426
<string name="pref.dash_swipe_to_delete">Enable swipe to delete metric</string>
424427
<string name="pref.dash_top_values_blink">Alarm for highest values</string>
425428
<string name="pref.trips_category">Trips</string>
426-
<string name="pref.trips_recordings">Recorded trips</string>
429+
<string name="pref.trips.title">Recorded trips</string>
430+
<string name="pref.trips.drive.auto_sync_enabled">Auto-Sync to Google Drive</string>
431+
<string name="pref.trips.drive.auto_sync_enabled_summary">Automatically upload new trips to Google Drive when connected to Wi-Fi.</string>
432+
427433
<string name="pref.view_category_summary">In this section, you can adjust settings related to the Gauge, Graph, and Giulia views.</string>
428434
<string name="pref.view_category">Views</string>
429435
<string name="pref.toolbar_category">Toolbar</string>

app/src/main/res/xml/preferences.xml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
android:summary="@string/pref.language.summary"
99
android:persistent="false" />
1010

11+
<PreferenceCategory android:title="@string/pref.trips.title">
12+
<SwitchPreferenceCompat
13+
android:defaultValue="false"
14+
android:dialogTitle="@string/pref.trips.drive.auto_sync_enabled"
15+
android:key="pref.trips.drive.auto_sync"
16+
android:title="@string/pref.trips.drive.auto_sync_enabled"
17+
android:summary="@string/pref.trips.drive.auto_sync_enabled_summary"
18+
app:singleLineTitle="false" />
19+
</PreferenceCategory>
20+
1121
<PreferenceCategory android:title="@string/pref.registry.category">
1222
<PreferenceScreen
1323
android:key="pref.registry"
@@ -1904,12 +1914,14 @@
19041914
</PreferenceScreen>
19051915
</PreferenceCategory>
19061916

1907-
<PreferenceCategory android:title="@string/pref.trips_recordings">
1917+
<PreferenceCategory android:title="@string/pref.trips.title">
1918+
1919+
19081920
<PreferenceScreen
1909-
android:summary="@string/pref.trips_recordings"
1910-
android:title="@string/pref.trips_recordings"
1921+
android:summary="@string/pref.trips.title"
1922+
android:title="@string/pref.trips.title"
19111923
app:key="pref.gauge.recordings">
1912-
<PreferenceCategory android:title="@string/pref.trips_recordings">
1924+
<PreferenceCategory android:title="@string/pref.trips.title">
19131925
<org.obd.graphs.preferences.trips.TripsListPreferences
19141926
android:defaultValue="@array/pref.empty_array"
19151927
android:key="pref.graph.trips.selected"

common/src/main/java/org/obd/graphs/Constants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.obd.graphs
1818

19+
const val TRIP_LOG_WRITE_COMPLETED = "trip.log.write.completed.event"
20+
1921
const val LANGUAGE_CHANGE_EVENT = "lang.change.event"
2022

2123
const val SCREEN_LOCK_DIALOG_CANCELLED_EVENT = "screen.lock.dialog.cancelled.event"

0 commit comments

Comments
 (0)