11// SPDX-License-Identifier: Apache-2.0
2- // Copyright 2014-2025 Uwe Trottmann
2+ // SPDX-FileCopyrightText: Copyright © 2014 Uwe Trottmann <[email protected] > 33
44package com.battlelancer.seriesguide.backend
55
@@ -10,7 +10,6 @@ import com.battlelancer.seriesguide.backend.CloudEndpointUtils.updateBuilder
1010import com.battlelancer.seriesguide.backend.settings.HexagonSettings
1111import com.battlelancer.seriesguide.jobs.NetworkJobProcessor
1212import com.battlelancer.seriesguide.modules.ApplicationContext
13- import com.battlelancer.seriesguide.util.Errors
1413import com.battlelancer.seriesguide.util.Errors.Companion.logAndReportHexagon
1514import com.battlelancer.seriesguide.util.isRetryError
1615import com.firebase.ui.auth.AuthUI
@@ -21,7 +20,6 @@ import com.github.michaelbull.result.runCatching
2120import com.github.michaelbull.result.throwUnless
2221import com.google.android.gms.common.ConnectionResult
2322import com.google.android.gms.common.GoogleApiAvailability
24- import com.google.android.gms.tasks.Tasks
2523import com.google.api.client.http.HttpRequestInitializer
2624import com.google.api.client.http.HttpTransport
2725import com.google.api.client.http.javanet.NetHttpTransport
@@ -35,9 +33,7 @@ import com.uwetrottmann.seriesguide.backend.lists.Lists
3533import com.uwetrottmann.seriesguide.backend.movies.Movies
3634import com.uwetrottmann.seriesguide.backend.shows.Shows
3735import com.uwetrottmann.seriesguide.backend.shows.model.SgCloudShow
38- import timber.log.Timber
3936import java.io.IOException
40- import java.util.concurrent.ExecutionException
4137import javax.inject.Inject
4238import javax.inject.Singleton
4339
@@ -190,8 +186,10 @@ class HexagonTools @Inject constructor(
190186 * Make sure to check [FirebaseHttpRequestInitializer.firebaseUser] is not null (the
191187 * account might have gotten signed out).
192188 *
193- * @param checkSignInState If enabled, tries to silently sign in with Google. If it fails, sets
194- * the [HexagonSettings.setShouldValidateAccount] flag. If successful, clears the flag.
189+ * @param checkSignInState If set, tries to retrieve the signed in user from [FirebaseAuth] (if
190+ * [FirebaseHttpRequestInitializer.firebaseUser] is null and [isTimeForSignInStateCheck]).
191+ * If it fails, sets the [HexagonSettings.setShouldValidateAccount] flag.
192+ * If successful, clears the flag.
195193 */
196194 @Synchronized
197195 private fun getHttpRequestInitializer (checkSignInState : Boolean ): FirebaseHttpRequestInitializer {
@@ -216,45 +214,10 @@ class HexagonTools @Inject constructor(
216214 }
217215 lastSignInCheck = SystemClock .elapsedRealtime()
218216
219- var account = FirebaseAuth .getInstance().currentUser
217+ val account = FirebaseAuth .getInstance().currentUser
220218 if (account != null ) {
221219 // still signed in
222220 httpRequestInitializer.firebaseUser = account
223- } else {
224- // Try to silently sign in. This is fine as Cloud was enabled by the user
225- // and they reasonably expect to stay signed in.
226- val signInTask = AuthUI .getInstance().silentSignIn(context, firebaseSignInProviders)
227- try {
228- val authResult = Tasks .await(signInTask)
229- if (authResult?.user != null ) {
230- Timber .i(" %s: successful" , ACTION_SILENT_SIGN_IN )
231- authResult.user.let {
232- account = it
233- httpRequestInitializer.firebaseUser = it
234- }
235- } else {
236- Errors .logAndReportHexagonAuthError(
237- HexagonAuthError (ACTION_SILENT_SIGN_IN , " FirebaseUser is null" )
238- )
239- }
240- } catch (e: Exception ) {
241- // https://developers.google.com/android/reference/com/google/android/gms/tasks/Tasks#public-static-tresult-await-tasktresult-task
242- if (e is InterruptedException ) {
243- // Do not report thread interruptions, it's expected.
244- Timber .w(e, ACTION_SILENT_SIGN_IN )
245- } else {
246- val cause = if (e is ExecutionException ) {
247- e.cause ? : e // The Task failed, getCause returns the original exception.
248- } else {
249- e // Unexpected exception.
250- }
251- // Do not report sign in required errors, this is expected and handled below.
252- val authEx = HexagonAuthError .build(ACTION_SILENT_SIGN_IN , cause)
253- if (! authEx.isSignInRequiredError()) {
254- Errors .logAndReportHexagonAuthError(authEx)
255- }
256- }
257- }
258221 }
259222
260223 val shouldFixAccount = account == null
@@ -309,7 +272,6 @@ class HexagonTools @Inject constructor(
309272 }
310273
311274 companion object {
312- private const val ACTION_SILENT_SIGN_IN = " silent sign-in"
313275 private val JSON_FACTORY : JsonFactory = GsonFactory ()
314276 private val HTTP_TRANSPORT : HttpTransport = NetHttpTransport ()
315277 private const val SIGN_IN_CHECK_INTERVAL_MS = 5 * DateUtils .MINUTE_IN_MILLIS
0 commit comments