Skip to content

Commit 8d939dd

Browse files
committed
feat: force user to google sigin when enable auto-sync
1 parent 5e2c1e4 commit 8d939dd

10 files changed

Lines changed: 97 additions & 6 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import org.obd.graphs.bl.drag.dragRacingMetricsProcessor
5050
import org.obd.graphs.bl.extra.vehicleStatusMetricsProcessor
5151
import org.obd.graphs.bl.gps.gpsMetricsEmitter
5252
import org.obd.graphs.bl.trip.tripManager
53+
import org.obd.graphs.integrations.gcp.gdrive.TripLogDriveManager
5354
import org.obd.graphs.language.LanguageManager
5455
import org.obd.graphs.preferences.setPreferencesContext
5556
import org.obd.graphs.profile.profile
@@ -69,6 +70,9 @@ class MainActivity :
6970

7071
internal val screenLockManager = ScreenLockManager(this)
7172
internal lateinit var backupManager: BackupManager
73+
74+
internal lateinit var tripLogDriveManager: TripLogDriveManager
75+
7276
internal lateinit var appBarConfiguration: AppBarConfiguration
7377

7478
val drawerLayout: DrawerLayout by lazy { findViewById(R.id.drawer_layout) }
@@ -121,6 +125,8 @@ class MainActivity :
121125

122126
supportActionBar?.hide()
123127
backupManager = BackupManager(this)
128+
tripLogDriveManager = TripLogDriveManager.instance(getString(R.string.ANDROID_WEB_CLIENT_ID), activity = this, null)
129+
124130
setupFabButtons()
125131

126132
if (savedInstanceState == null) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import org.obd.graphs.BACKUP_START
3939
import org.obd.graphs.BACKUP_SUCCESSFUL
4040
import org.obd.graphs.GOOGLE_SIGN_IN_GENERAL_FAILURE
4141
import org.obd.graphs.GOOGLE_SIGN_IN_NO_CREDENTIAL_FAILURE
42+
import org.obd.graphs.GOOGLE_SIGN_IN_REQUEST
4243
import org.obd.graphs.MODULES_LIST_CHANGED_EVENT
4344
import org.obd.graphs.Notifications
4445
import org.obd.graphs.Permissions
@@ -127,7 +128,13 @@ internal fun MainActivity.receive(intent: Intent?) {
127128
TRIPS_UPLOAD_SUCCESSFUL -> toast(org.obd.graphs.commons.R.string.main_activity_toast_trips_upload_successful)
128129
TRIPS_UPLOAD_NO_FILES_SELECTED -> toast(org.obd.graphs.commons.R.string.main_activity_toast_trips_upload_no_files_selected)
129130

130-
TRIPS_UPLOAD_FAILED_NO_TOKEN -> {
131+
GOOGLE_SIGN_IN_REQUEST -> {
132+
lifecycleScope.launch {
133+
tripLogDriveManager.authenticate()
134+
}
135+
}
136+
137+
TRIPS_UPLOAD_FAILED_NO_TOKEN ->
131138
Notifications.show(
132139
context = applicationContext,
133140
notificationId = 9999,
@@ -146,7 +153,6 @@ internal fun MainActivity.receive(intent: Intent?) {
146153
},
147154
isOngoing = false
148155
)
149-
}
150156

151157
BACKUP_FAILED -> toast(org.obd.graphs.commons.R.string.main_activity_toast_backup_failed)
152158
BACKUP_SUCCESSFUL -> toast(org.obd.graphs.commons.R.string.main_activity_toast_backup_successful)
@@ -420,6 +426,7 @@ internal fun MainActivity.registerReceiver() {
420426
it.addAction(PROFILE_NAME_CHANGED_EVENT)
421427
it.addAction(TRIP_LOG_WRITE_COMPLETED)
422428
it.addAction(TRIPS_UPLOAD_FAILED_NO_TOKEN)
429+
it.addAction(GOOGLE_SIGN_IN_REQUEST)
423430
}
424431

425432
registerReceiver(this, DataLoggerRepository.broadcastReceivers()) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2019-2026, Tomasz Żebrowski
3+
*
4+
* <p>Licensed to the Apache Software Foundation (ASF) under one or more contributor license
5+
* agreements. See the NOTICE file distributed with this work for additional information regarding
6+
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance with the License. You may obtain a
8+
* copy of the License at
9+
*
10+
* <p>http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
13+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14+
* express or implied. See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.obd.graphs.preferences
18+
19+
import android.content.Context
20+
import android.util.AttributeSet
21+
import android.util.Log
22+
import androidx.appcompat.app.AlertDialog
23+
import androidx.preference.SwitchPreferenceCompat
24+
import org.obd.graphs.GOOGLE_SIGN_IN_REQUEST
25+
import org.obd.graphs.R
26+
import org.obd.graphs.sendBroadcastEvent
27+
28+
private const val LOG_TAG = "DriveAutoSyncEnablerCheckBoxPreference"
29+
30+
class DriveAutoSyncEnablerCheckBoxPreference(
31+
context: Context,
32+
attrs: AttributeSet?
33+
) : SwitchPreferenceCompat(context, attrs) {
34+
35+
init {
36+
setOnPreferenceChangeListener { _, newValue ->
37+
val isEnabling = newValue as Boolean
38+
if (isEnabling) {
39+
AlertDialog
40+
.Builder(context)
41+
.setTitle(context.getString(R.string.pref_trips_drive_auto_sync_enabled_sign_in_dialog_title))
42+
.setMessage(context.getString(R.string.pref_trips_drive_auto_sync_enabled_sign_in_dialog_summary))
43+
.setCancelable(false)
44+
.setPositiveButton(context.getString(R.string.dialog_ask_question_yes)) { _, _ ->
45+
sendBroadcastEvent(GOOGLE_SIGN_IN_REQUEST)
46+
isChecked = true
47+
}.setNegativeButton(context.getString(R.string.dialog_ask_question_no)) { dialog, _ ->
48+
Log.i(LOG_TAG, "Disabling google drive auto sync")
49+
dialog.dismiss()
50+
}.show()
51+
false
52+
} else {
53+
true
54+
}
55+
}
56+
}
57+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@
428428

429429
<string name="pref.trips.drive.auto_sync_enabled">Automatyczna synchronizacja z Dyskiem Google</string>
430430
<string name="pref.trips.drive.auto_sync_enabled_summary">Automatycznie przesyłaj nowe przejazdy na Dysk Google po połączeniu z siecią Wi-Fi. Uwaga: Najpierw należy ręcznie przesłać przejazd w aplikacji, aby przyznać uprawnienia.</string>
431+
<string name="pref.trips.drive.auto_sync_enabled.sign_in.dialog_title">Wymagane logowanie do Dysku Google</string>
432+
<string name="pref.trips.drive.auto_sync_enabled.sign_in.dialog_summary">Aby automatycznie tworzyć kopie zapasowe przejazdów w tle, MyGiulia potrzebuje uprawnień do zapisywania plików na Twoim Dysku Google.\n\nZaloguj się na swoje konto Google na następnym ekranie, aby włączyć automatyczną synchronizację.</string>
433+
431434

432435
<string name="pref.view_category_summary">W tej sekcji możesz dostosować ustawienia związane z widokami Gauge, Wykres i Giulia.</string>
433436
<string name="pref.view_category">Widoki</string>

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

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

5-
6-
7-
85
<string name="pref.performance.screen_label_y_padding"><b><i>Gauge</i></b> label top offset</string>
96
<string name="pref.performance.screen_top_margin">Top margin</string>
107

@@ -425,10 +422,14 @@
425422
<string name="pref.dash_enable_drag_and_drop">Enable drag and drop</string>
426423
<string name="pref.dash_swipe_to_delete">Enable swipe to delete metric</string>
427424
<string name="pref.dash_top_values_blink">Alarm for highest values</string>
425+
428426
<string name="pref.trips_category">Trips</string>
429427
<string name="pref.trips.title">Recorded trips</string>
430428
<string name="pref.trips.drive.auto_sync_enabled">Auto-Sync to Google Drive</string>
431429
<string name="pref.trips.drive.auto_sync_enabled_summary">Automatically upload new trips to Google Drive when connected to Wi-Fi. Note: You should upload a trip manually in the app first to grant permissions.</string>
430+
<string name="pref.trips.drive.auto_sync_enabled.sign_in.dialog_title">Google Drive Sign-In Required</string>
431+
<string name="pref.trips.drive.auto_sync_enabled.sign_in.dialog_summary">To automatically back up your trips in the background, MyGiulia needs permission to save files to your Google Drive. \n\nPlease sign in with your Google account on the next screen to enable auto-sync.</string>
432+
432433

433434
<string name="pref.view_category_summary">In this section, you can adjust settings related to the Gauge, Graph, and Giulia views.</string>
434435
<string name="pref.view_category">Views</string>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
android:persistent="false" />
1010

1111
<PreferenceCategory android:title="@string/pref.trips.title">
12-
<SwitchPreferenceCompat
12+
<org.obd.graphs.preferences.DriveAutoSyncEnablerCheckBoxPreference
1313
android:defaultValue="false"
1414
android:dialogTitle="@string/pref.trips.drive.auto_sync_enabled"
1515
android:key="pref.trips.drive.auto_sync"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const val TRIPS_UPLOAD_NO_FILES_SELECTED = "trips.upload.no_files"
5454

5555
const val TRIPS_UPLOAD_FAILED_NO_TOKEN = "trips.upload.no_token"
5656

57+
const val GOOGLE_SIGN_IN_REQUEST = "gdrive.authorization.token_requests"
58+
5759
const val GOOGLE_SIGN_IN_GENERAL_FAILURE = "gdrive.authorization.failed"
5860
const val GOOGLE_SIGN_IN_NO_CREDENTIAL_FAILURE = "gdrive.authorization.no_credentials.failed"
5961

integrations/src/main/java/org/obd/graphs.integrations/gcp/gdrive/ManualTripLogUpload.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.obd.graphs.integrations.gcp.gdrive
1818

1919
import android.app.Activity
20+
import android.util.Log
2021
import androidx.fragment.app.Fragment
2122
import org.obd.graphs.SCREEN_UNLOCK_PROGRESS_EVENT
2223
import org.obd.graphs.TRIPS_UPLOAD_FAILED
@@ -33,6 +34,13 @@ internal open class ManualTripLogUpload(
3334
fragment: Fragment?
3435
) : AbstractDriveManager(webClientId, activity, fragment),
3536
TripLogDriveManager {
37+
38+
override suspend fun authenticate() =
39+
signInAndExecute("authenticate_for_auto_sync") { token ->
40+
Log.i("ManualTripLogUpload", "Successfully authenticated user for background Auto-Sync!")
41+
sendBroadcastEvent(SCREEN_UNLOCK_PROGRESS_EVENT)
42+
}
43+
3644
override suspend fun uploadTrips(files: List<File>) =
3745
signInAndExecute("exportTrips") { token ->
3846
executeDriveOperation(

integrations/src/main/java/org/obd/graphs.integrations/gcp/gdrive/TripLogDriveManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import java.io.File
2323
interface TripLogDriveManager {
2424
suspend fun uploadTrips(files: List<File>)
2525

26+
suspend fun authenticate()
2627
companion object {
2728
fun instance(
2829
webClientId: String,

integrations/src/test/java/org/obd/graphs/integrations/gcp/gdrive/TripLogDriveManagerTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class TripLogDriveManagerTest {
3434

3535
// Subclass to expose logic wrapped in the executeDriveOperation block
3636
private inner class TestableTripLogManager : ManualTripLogUpload("client", activity, null) {
37+
override suspend fun authenticate() =
38+
signInAndExecute("authenticate_for_auto_sync") { token ->
39+
// If we get here, the user successfully signed in and the token is cached!
40+
// No need to do anything else. The background worker will now work.
41+
}
42+
3743
fun testUploadLogic(files: List<File>) {
3844
if (files.isEmpty()) {
3945
sendBroadcastEvent(TRIPS_UPLOAD_NO_FILES_SELECTED)

0 commit comments

Comments
 (0)