From f9ca9e80e82c3e7b2376d8556eee3504fcff9633 Mon Sep 17 00:00:00 2001 From: sim Date: Sun, 5 Mar 2023 12:39:11 +0100 Subject: [PATCH] Avoid static SingleSignOnAccount --- .../nextpush/account/SSOAccountFactory.kt | 35 ++++++++----------- .../nextpush/api/provider/ApiDirectFactory.kt | 9 ++--- .../api/provider/ApiProviderFactory.kt | 1 + .../nextpush/api/provider/ApiSSOFactory.kt | 26 ++++++++------ 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/SSOAccountFactory.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/SSOAccountFactory.kt index bd834fa..97acb49 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/SSOAccountFactory.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/SSOAccountFactory.kt @@ -14,22 +14,20 @@ import androidx.appcompat.app.AlertDialog import com.nextcloud.android.sso.AccountImporter import com.nextcloud.android.sso.exceptions.* // ktlint-disable no-wildcard-imports import com.nextcloud.android.sso.helper.SingleAccountHelper -import com.nextcloud.android.sso.model.SingleSignOnAccount import com.nextcloud.android.sso.ui.UiExceptionManager import org.unifiedpush.distributor.nextpush.R import org.unifiedpush.distributor.nextpush.utils.TAG class SSOAccountFactory : AccountFactory { override var name: String? = null - get() = ssoAccount?.name override var url: String? = null - get() = ssoAccount?.url - - private var ssoAccount: SingleSignOnAccount? = null override fun initAccount(context: Context): Boolean { try { - ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context) + SingleAccountHelper.getCurrentSingleSignOnAccount(context).let { + name = it.name + url = it.url + } } catch (e: NextcloudFilesAppAccountNotFoundException) { return false } catch (e: NoCurrentAccountSelectedException) { @@ -65,26 +63,23 @@ class SSOAccountFactory : AccountFactory { data, activity ) { account -> - val context = activity.applicationContext - - SingleAccountHelper.setCurrentAccount(context, account.name) - - // Get the "default" account - try { - ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context) - success = true - } catch (e: NextcloudFilesAppAccountNotFoundException) { - nextcloudAppNotInstalledDialog(context) - } catch (e: NoCurrentAccountSelectedException) { - UiExceptionManager.showDialogForException(context, e) - } + SingleAccountHelper.setCurrentAccount(activity.applicationContext, account.name) + success = true } } catch (_: AccountImportCancelledException) {} block(success) } override fun getAccount(context: Context): Any? { - return ssoAccount + return try { + SingleAccountHelper.getCurrentSingleSignOnAccount(context) + } catch (e: NextcloudFilesAppAccountNotFoundException) { + Log.w(TAG, "Nextcloud File is not found") + null + } catch (e: NoCurrentAccountSelectedException) { + Log.w(TAG, "No account selected") + null + } } override fun logout(context: Context) { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiDirectFactory.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiDirectFactory.kt index 212cff5..9e0b6eb 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiDirectFactory.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiDirectFactory.kt @@ -16,16 +16,13 @@ class ApiDirectFactory(val context: Context) : ApiProviderFactory { override fun getProviderAndExecute(block: (ApiProvider) -> Unit) { val account = getAccount(context) ?: run { - Log.w(TAG, "No account found") - return + throw NoProviderException("No account found") } val url = account.url ?: run { - Log.w(TAG, "No url found") - return + throw NoProviderException("No url found") } val client = account.getAccount(context) as OkHttpClient? ?: run { - Log.w(TAG, "No client found") - return + throw NoProviderException("No client found") } apiProvider?.let(block) ?: run { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiProviderFactory.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiProviderFactory.kt index 7353aba..2bad7fd 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiProviderFactory.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiProviderFactory.kt @@ -1,5 +1,6 @@ package org.unifiedpush.distributor.nextpush.api.provider +class NoProviderException(message: String) : Exception(message) interface ApiProviderFactory { fun getProviderAndExecute(block: (ApiProvider) -> Unit) fun destroyProvider() diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiSSOFactory.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiSSOFactory.kt index 92d4992..96cc117 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiSSOFactory.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/provider/ApiSSOFactory.kt @@ -6,18 +6,21 @@ import com.google.gson.GsonBuilder import com.nextcloud.android.sso.api.NextcloudAPI import com.nextcloud.android.sso.model.SingleSignOnAccount import org.unifiedpush.distributor.nextpush.account.Account.getAccount +import org.unifiedpush.distributor.nextpush.utils.TAG import retrofit2.NextcloudRetrofitApiBuilder class ApiSSOFactory(val context: Context) : ApiProviderFactory { private val TAG = ApiSSOFactory::class.java.simpleName private var apiProvider: ApiProvider? = null - private lateinit var nextcloudAPI: NextcloudAPI + private var nextcloudAPI: NextcloudAPI? = null override fun getProviderAndExecute(block: (ApiProvider) -> Unit) { val account = getAccount(context) ?: run { - Log.w(TAG, "No account found") - return + throw NoProviderException("No account found") + } + val client = account.getAccount(context) as SingleSignOnAccount? ?: run { + throw NoProviderException("No client found") } apiProvider?.let(block) ?: run { @@ -25,11 +28,13 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory { val ssoApiCallback = object : NextcloudAPI.ApiConnectedListener { override fun onConnected() { Log.d(TAG, "Api connected.") - NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint) - .create(ApiProvider::class.java).let { - apiProvider = it - block(it) - } + nextcloudAPI?.let { nextcloudAPI -> + NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint) + .create(ApiProvider::class.java).let { + apiProvider = it + block(it) + } + } } override fun onError(ex: Exception) { @@ -38,7 +43,7 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory { } nextcloudAPI = NextcloudAPI( context, - account.getAccount(context) as SingleSignOnAccount, + client, GsonBuilder().create(), ssoApiCallback ) @@ -46,6 +51,7 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory { } override fun destroyProvider() { - nextcloudAPI.stop() + nextcloudAPI?.stop() + nextcloudAPI = null } }