Avoid static SingleSignOnAccount
This commit is contained in:
parent
de6cb23556
commit
f9ca9e80e8
|
@ -14,22 +14,20 @@ import androidx.appcompat.app.AlertDialog
|
||||||
import com.nextcloud.android.sso.AccountImporter
|
import com.nextcloud.android.sso.AccountImporter
|
||||||
import com.nextcloud.android.sso.exceptions.* // ktlint-disable no-wildcard-imports
|
import com.nextcloud.android.sso.exceptions.* // ktlint-disable no-wildcard-imports
|
||||||
import com.nextcloud.android.sso.helper.SingleAccountHelper
|
import com.nextcloud.android.sso.helper.SingleAccountHelper
|
||||||
import com.nextcloud.android.sso.model.SingleSignOnAccount
|
|
||||||
import com.nextcloud.android.sso.ui.UiExceptionManager
|
import com.nextcloud.android.sso.ui.UiExceptionManager
|
||||||
import org.unifiedpush.distributor.nextpush.R
|
import org.unifiedpush.distributor.nextpush.R
|
||||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||||
|
|
||||||
class SSOAccountFactory : AccountFactory {
|
class SSOAccountFactory : AccountFactory {
|
||||||
override var name: String? = null
|
override var name: String? = null
|
||||||
get() = ssoAccount?.name
|
|
||||||
override var url: String? = null
|
override var url: String? = null
|
||||||
get() = ssoAccount?.url
|
|
||||||
|
|
||||||
private var ssoAccount: SingleSignOnAccount? = null
|
|
||||||
|
|
||||||
override fun initAccount(context: Context): Boolean {
|
override fun initAccount(context: Context): Boolean {
|
||||||
try {
|
try {
|
||||||
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context)
|
SingleAccountHelper.getCurrentSingleSignOnAccount(context).let {
|
||||||
|
name = it.name
|
||||||
|
url = it.url
|
||||||
|
}
|
||||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||||
return false
|
return false
|
||||||
} catch (e: NoCurrentAccountSelectedException) {
|
} catch (e: NoCurrentAccountSelectedException) {
|
||||||
|
@ -65,26 +63,23 @@ class SSOAccountFactory : AccountFactory {
|
||||||
data,
|
data,
|
||||||
activity
|
activity
|
||||||
) { account ->
|
) { account ->
|
||||||
val context = activity.applicationContext
|
SingleAccountHelper.setCurrentAccount(activity.applicationContext, account.name)
|
||||||
|
|
||||||
SingleAccountHelper.setCurrentAccount(context, account.name)
|
|
||||||
|
|
||||||
// Get the "default" account
|
|
||||||
try {
|
|
||||||
ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context)
|
|
||||||
success = true
|
success = true
|
||||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
|
||||||
nextcloudAppNotInstalledDialog(context)
|
|
||||||
} catch (e: NoCurrentAccountSelectedException) {
|
|
||||||
UiExceptionManager.showDialogForException(context, e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} catch (_: AccountImportCancelledException) {}
|
} catch (_: AccountImportCancelledException) {}
|
||||||
block(success)
|
block(success)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getAccount(context: Context): Any? {
|
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) {
|
override fun logout(context: Context) {
|
||||||
|
|
|
@ -16,16 +16,13 @@ class ApiDirectFactory(val context: Context) : ApiProviderFactory {
|
||||||
|
|
||||||
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
||||||
val account = getAccount(context) ?: run {
|
val account = getAccount(context) ?: run {
|
||||||
Log.w(TAG, "No account found")
|
throw NoProviderException("No account found")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
val url = account.url ?: run {
|
val url = account.url ?: run {
|
||||||
Log.w(TAG, "No url found")
|
throw NoProviderException("No url found")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
val client = account.getAccount(context) as OkHttpClient? ?: run {
|
val client = account.getAccount(context) as OkHttpClient? ?: run {
|
||||||
Log.w(TAG, "No client found")
|
throw NoProviderException("No client found")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
apiProvider?.let(block)
|
apiProvider?.let(block)
|
||||||
?: run {
|
?: run {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.unifiedpush.distributor.nextpush.api.provider
|
package org.unifiedpush.distributor.nextpush.api.provider
|
||||||
|
|
||||||
|
class NoProviderException(message: String) : Exception(message)
|
||||||
interface ApiProviderFactory {
|
interface ApiProviderFactory {
|
||||||
fun getProviderAndExecute(block: (ApiProvider) -> Unit)
|
fun getProviderAndExecute(block: (ApiProvider) -> Unit)
|
||||||
fun destroyProvider()
|
fun destroyProvider()
|
||||||
|
|
|
@ -6,18 +6,21 @@ import com.google.gson.GsonBuilder
|
||||||
import com.nextcloud.android.sso.api.NextcloudAPI
|
import com.nextcloud.android.sso.api.NextcloudAPI
|
||||||
import com.nextcloud.android.sso.model.SingleSignOnAccount
|
import com.nextcloud.android.sso.model.SingleSignOnAccount
|
||||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||||
|
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||||
import retrofit2.NextcloudRetrofitApiBuilder
|
import retrofit2.NextcloudRetrofitApiBuilder
|
||||||
|
|
||||||
class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
||||||
|
|
||||||
private val TAG = ApiSSOFactory::class.java.simpleName
|
private val TAG = ApiSSOFactory::class.java.simpleName
|
||||||
private var apiProvider: ApiProvider? = null
|
private var apiProvider: ApiProvider? = null
|
||||||
private lateinit var nextcloudAPI: NextcloudAPI
|
private var nextcloudAPI: NextcloudAPI? = null
|
||||||
|
|
||||||
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
||||||
val account = getAccount(context) ?: run {
|
val account = getAccount(context) ?: run {
|
||||||
Log.w(TAG, "No account found")
|
throw NoProviderException("No account found")
|
||||||
return
|
}
|
||||||
|
val client = account.getAccount(context) as SingleSignOnAccount? ?: run {
|
||||||
|
throw NoProviderException("No client found")
|
||||||
}
|
}
|
||||||
apiProvider?.let(block)
|
apiProvider?.let(block)
|
||||||
?: run {
|
?: run {
|
||||||
|
@ -25,12 +28,14 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
||||||
val ssoApiCallback = object : NextcloudAPI.ApiConnectedListener {
|
val ssoApiCallback = object : NextcloudAPI.ApiConnectedListener {
|
||||||
override fun onConnected() {
|
override fun onConnected() {
|
||||||
Log.d(TAG, "Api connected.")
|
Log.d(TAG, "Api connected.")
|
||||||
|
nextcloudAPI?.let { nextcloudAPI ->
|
||||||
NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint)
|
NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint)
|
||||||
.create(ApiProvider::class.java).let {
|
.create(ApiProvider::class.java).let {
|
||||||
apiProvider = it
|
apiProvider = it
|
||||||
block(it)
|
block(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onError(ex: Exception) {
|
override fun onError(ex: Exception) {
|
||||||
Log.d(TAG, "Cannot connect to API: ex = [$ex]")
|
Log.d(TAG, "Cannot connect to API: ex = [$ex]")
|
||||||
|
@ -38,7 +43,7 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
||||||
}
|
}
|
||||||
nextcloudAPI = NextcloudAPI(
|
nextcloudAPI = NextcloudAPI(
|
||||||
context,
|
context,
|
||||||
account.getAccount(context) as SingleSignOnAccount,
|
client,
|
||||||
GsonBuilder().create(),
|
GsonBuilder().create(),
|
||||||
ssoApiCallback
|
ssoApiCallback
|
||||||
)
|
)
|
||||||
|
@ -46,6 +51,7 @@ class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun destroyProvider() {
|
override fun destroyProvider() {
|
||||||
nextcloudAPI.stop()
|
nextcloudAPI?.stop()
|
||||||
|
nextcloudAPI = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue