From 1993928bab4fde7f303610a7753a022999e6989d Mon Sep 17 00:00:00 2001 From: undefiened Date: Sun, 27 Aug 2023 00:35:37 +0200 Subject: [PATCH 1/3] Added exceptions in Google Sync process so that it fails correctly and moved the redirect url to a constant --- .../data/sync/service/GoogleDriveSyncService.kt | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt index e517a9ce8..af93dcc0e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt @@ -18,6 +18,7 @@ import com.google.api.client.json.jackson2.JacksonFactory import com.google.api.services.drive.Drive import com.google.api.services.drive.DriveScopes import com.google.api.services.drive.model.File +import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.sync.models.SyncData import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -57,7 +58,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync // Check if the Google Drive service is initialized if (drive == null) { logcat(LogPriority.ERROR) { "Google Drive service not initialized" } - return null + throw Exception(context.getString(R.string.google_drive_not_signed_in)) } val fileList = getFileList(drive) @@ -85,7 +86,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync // Check if the Google Drive service is initialized if (drive == null) { logcat(LogPriority.ERROR) { "Google Drive service not initialized" } - return + throw Exception(context.getString(R.string.google_drive_not_signed_in)) } // delete file if exists @@ -151,6 +152,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync class GoogleDriveService(private val context: Context) { var googleDriveService: Drive? = null + private val redirectUri = "eu.kanade.google.oauth:/oauth2redirect" private val syncPreferences = Injekt.get() init { @@ -208,7 +210,7 @@ class GoogleDriveService(private val context: Context) { ).setAccessType("offline").build() return flow.newAuthorizationUrl() - .setRedirectUri("eu.kanade.google.oauth:/oauth2redirect") + .setRedirectUri(redirectUri) .setApprovalPrompt("force") .build() } @@ -225,6 +227,10 @@ class GoogleDriveService(private val context: Context) { .setClientSecrets(secrets) .build() + if (syncPreferences.getGoogleDriveRefreshToken() == "") { + throw Exception(context.getString(R.string.google_drive_not_signed_in)) + } + credential.refreshToken = syncPreferences.getGoogleDriveRefreshToken() logcat(LogPriority.DEBUG) { "Refreshing access token with: ${syncPreferences.getGoogleDriveRefreshToken()}" } @@ -246,11 +252,13 @@ class GoogleDriveService(private val context: Context) { // Token refresh failed; handle this situation logcat(LogPriority.ERROR) { "Failed to refresh access token ${e.message}" } logcat(LogPriority.ERROR) { "Google Drive sync will be disabled" } + throw e.message?.let { Exception(it) } ?: Exception("Unknown error") } } catch (e: IOException) { // Token refresh failed; handle this situation logcat(LogPriority.ERROR) { "Failed to refresh access token ${e.message}" } logcat(LogPriority.ERROR) { "Google Drive sync will be disabled" } + throw e.message?.let { Exception(it) } ?: Exception("Unknown error") } } @@ -305,7 +313,7 @@ class GoogleDriveService(private val context: Context) { secrets.installed.clientId, secrets.installed.clientSecret, authorizationCode, - "eu.kanade.google.oauth:/oauth2redirect", + redirectUri, ).setGrantType("authorization_code").execute() try { From 569db9eac85cb95483105147c99d83c9e98435af Mon Sep 17 00:00:00 2001 From: undefiened Date: Sun, 27 Aug 2023 16:23:30 +0200 Subject: [PATCH 2/3] Using a proper constant instead of val Co-authored-by: KaiserBh <41852205+KaiserBh@users.noreply.github.com> --- .../tachiyomi/data/sync/service/GoogleDriveSyncService.kt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt index af93dcc0e..2340b027b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt @@ -152,7 +152,9 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync class GoogleDriveService(private val context: Context) { var googleDriveService: Drive? = null - private val redirectUri = "eu.kanade.google.oauth:/oauth2redirect" + companion object { + const val REDIRECT_URI = "eu.kanade.google.oauth:/oauth2redirect" + } private val syncPreferences = Injekt.get() init { @@ -210,7 +212,7 @@ class GoogleDriveService(private val context: Context) { ).setAccessType("offline").build() return flow.newAuthorizationUrl() - .setRedirectUri(redirectUri) + .setRedirectUri(REDIRECT_URI) .setApprovalPrompt("force") .build() } @@ -313,7 +315,7 @@ class GoogleDriveService(private val context: Context) { secrets.installed.clientId, secrets.installed.clientSecret, authorizationCode, - redirectUri, + REDIRECT_URI, ).setGrantType("authorization_code").execute() try { From 2289fb937c463430db86a5cb86da5b82b29cad91 Mon Sep 17 00:00:00 2001 From: undefiened Date: Sun, 27 Aug 2023 16:25:09 +0200 Subject: [PATCH 3/3] Fixed identation of the REDIRECT_URI constant --- .../tachiyomi/data/sync/service/GoogleDriveSyncService.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt index 2340b027b..bd800d457 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt @@ -152,7 +152,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync class GoogleDriveService(private val context: Context) { var googleDriveService: Drive? = null - companion object { + companion object { const val REDIRECT_URI = "eu.kanade.google.oauth:/oauth2redirect" } private val syncPreferences = Injekt.get()