Avoid static SingleSignOnAccount

This commit is contained in:
sim 2023-03-05 12:39:11 +01:00
parent de6cb23556
commit f9ca9e80e8
4 changed files with 35 additions and 36 deletions

View File

@ -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) {

View File

@ -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 {

View File

@ -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()

View File

@ -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
}
}