Rename Acount/AccountFactory
This commit is contained in:
parent
dcc4d301aa
commit
0373d0eb63
@ -1,108 +1,15 @@
|
||||
package org.unifiedpush.distributor.nextpush.account
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException
|
||||
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
import android.content.Intent
|
||||
|
||||
internal const val PREF_NAME = "NextPush"
|
||||
private const val PREF_DEVICE_ID = "deviceId"
|
||||
private const val PREF_ACCOUNT_TYPE = "account::type"
|
||||
|
||||
enum class AccountType {
|
||||
SSO,
|
||||
Direct;
|
||||
fun toInt(): Int {
|
||||
return this.ordinal
|
||||
}
|
||||
}
|
||||
|
||||
private fun Int.toAccountType(): AccountType {
|
||||
return AccountType.entries.getOrNull(this) ?: AccountType.SSO
|
||||
}
|
||||
|
||||
object Account {
|
||||
private var account: AccountFactory? = null
|
||||
|
||||
var Context.accountType: AccountType
|
||||
get() = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.getInt(PREF_ACCOUNT_TYPE, 0).toAccountType()
|
||||
private set(value) = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.edit().putInt(PREF_ACCOUNT_TYPE, value.toInt())
|
||||
.apply()
|
||||
|
||||
var Context.npDeviceId: String?
|
||||
get() = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.getString(PREF_DEVICE_ID, null)
|
||||
set(value) {
|
||||
this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.edit()
|
||||
.apply {
|
||||
value?.let {
|
||||
putString(PREF_DEVICE_ID, it)
|
||||
} ?: run {
|
||||
remove(PREF_DEVICE_ID)
|
||||
}
|
||||
}.apply()
|
||||
}
|
||||
|
||||
fun getAccount(context: Context, uninitialized: Boolean = false): AccountFactory? {
|
||||
return account
|
||||
?: run {
|
||||
Log.d(TAG, "New account, type=${context.accountType}")
|
||||
when (context.accountType) {
|
||||
AccountType.SSO -> {
|
||||
try {
|
||||
SSOAccountFactory().apply {
|
||||
initAccount(context)
|
||||
account = this
|
||||
}
|
||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"Nextcloud application not found",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
Log.w(TAG, "Nextcloud application not found")
|
||||
null
|
||||
} catch (e: NoCurrentAccountSelectedException) {
|
||||
if (uninitialized) {
|
||||
SSOAccountFactory()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
AccountType.Direct -> {
|
||||
DirectAccountFactory().apply {
|
||||
initAccount(context)
|
||||
account = this
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun logout(context: Context) {
|
||||
getAccount(context)?.logout(context)
|
||||
context.npDeviceId = null
|
||||
}
|
||||
|
||||
fun Context.setTypeSSO() {
|
||||
account = null
|
||||
accountType = AccountType.SSO
|
||||
DirectAccountFactory.setCredentials(this, null, null, null)
|
||||
}
|
||||
|
||||
fun Context.setTypeDirect(url: String, username: String, password: String) {
|
||||
account = null
|
||||
accountType = AccountType.Direct
|
||||
DirectAccountFactory.setCredentials(this, url, username, password)
|
||||
}
|
||||
|
||||
fun isConnected(context: Context): Boolean {
|
||||
return getAccount(context)?.initAccount(context) == true
|
||||
}
|
||||
interface Account {
|
||||
var name: String?
|
||||
var url: String?
|
||||
fun initAccount(context: Context): Boolean
|
||||
fun connect(activity: Activity)
|
||||
fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?, block: (success: Boolean) -> Unit)
|
||||
fun getAccount(context: Context): Any?
|
||||
fun logout(context: Context)
|
||||
}
|
||||
|
@ -1,15 +1,108 @@
|
||||
package org.unifiedpush.distributor.nextpush.account
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import android.widget.Toast
|
||||
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException
|
||||
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
|
||||
interface AccountFactory {
|
||||
var name: String?
|
||||
var url: String?
|
||||
fun initAccount(context: Context): Boolean
|
||||
fun connect(activity: Activity)
|
||||
fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?, block: (success: Boolean) -> Unit)
|
||||
fun getAccount(context: Context): Any?
|
||||
fun logout(context: Context)
|
||||
internal const val PREF_NAME = "NextPush"
|
||||
private const val PREF_DEVICE_ID = "deviceId"
|
||||
private const val PREF_ACCOUNT_TYPE = "account::type"
|
||||
|
||||
enum class AccountType {
|
||||
SSO,
|
||||
Direct;
|
||||
fun toInt(): Int {
|
||||
return this.ordinal
|
||||
}
|
||||
}
|
||||
|
||||
private fun Int.toAccountType(): AccountType {
|
||||
return AccountType.entries.getOrNull(this) ?: AccountType.SSO
|
||||
}
|
||||
|
||||
object AccountFactory {
|
||||
private var account: Account? = null
|
||||
|
||||
var Context.accountType: AccountType
|
||||
get() = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.getInt(PREF_ACCOUNT_TYPE, 0).toAccountType()
|
||||
private set(value) = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.edit().putInt(PREF_ACCOUNT_TYPE, value.toInt())
|
||||
.apply()
|
||||
|
||||
var Context.npDeviceId: String?
|
||||
get() = this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.getString(PREF_DEVICE_ID, null)
|
||||
set(value) {
|
||||
this.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
|
||||
.edit()
|
||||
.apply {
|
||||
value?.let {
|
||||
putString(PREF_DEVICE_ID, it)
|
||||
} ?: run {
|
||||
remove(PREF_DEVICE_ID)
|
||||
}
|
||||
}.apply()
|
||||
}
|
||||
|
||||
fun getAccount(context: Context, uninitialized: Boolean = false): Account? {
|
||||
return account
|
||||
?: run {
|
||||
Log.d(TAG, "New account, type=${context.accountType}")
|
||||
when (context.accountType) {
|
||||
AccountType.SSO -> {
|
||||
try {
|
||||
SSOAccount().apply {
|
||||
initAccount(context)
|
||||
account = this
|
||||
}
|
||||
} catch (e: NextcloudFilesAppAccountNotFoundException) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"Nextcloud application not found",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
Log.w(TAG, "Nextcloud application not found")
|
||||
null
|
||||
} catch (e: NoCurrentAccountSelectedException) {
|
||||
if (uninitialized) {
|
||||
SSOAccount()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
AccountType.Direct -> {
|
||||
DirectAccount().apply {
|
||||
initAccount(context)
|
||||
account = this
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun logout(context: Context) {
|
||||
getAccount(context)?.logout(context)
|
||||
context.npDeviceId = null
|
||||
}
|
||||
|
||||
fun Context.setTypeSSO() {
|
||||
account = null
|
||||
accountType = AccountType.SSO
|
||||
DirectAccount.setCredentials(this, null, null, null)
|
||||
}
|
||||
|
||||
fun Context.setTypeDirect(url: String, username: String, password: String) {
|
||||
account = null
|
||||
accountType = AccountType.Direct
|
||||
DirectAccount.setCredentials(this, url, username, password)
|
||||
}
|
||||
|
||||
fun isConnected(context: Context): Boolean {
|
||||
return getAccount(context)?.initAccount(context) == true
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ private const val PREF_URL = "direct_account::url"
|
||||
private const val PREF_USERNAME = "direct_account::username"
|
||||
private const val PREF_PASSWORD = "direct_account::password"
|
||||
|
||||
class DirectAccountFactory : AccountFactory {
|
||||
class DirectAccount : Account {
|
||||
override var name: String? = null
|
||||
override var url: String? = null
|
||||
|
@ -18,7 +18,7 @@ import com.nextcloud.android.sso.ui.UiExceptionManager
|
||||
import org.unifiedpush.distributor.nextpush.R
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
|
||||
class SSOAccountFactory : AccountFactory {
|
||||
class SSOAccount : Account {
|
||||
override var name: String? = null
|
||||
override var url: String? = null
|
||||
|
@ -29,9 +29,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import org.unifiedpush.distributor.nextpush.Database.Companion.getDb
|
||||
import org.unifiedpush.distributor.nextpush.LocalNotification
|
||||
import org.unifiedpush.distributor.nextpush.R
|
||||
import org.unifiedpush.distributor.nextpush.account.Account
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.isConnected
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.isConnected
|
||||
import org.unifiedpush.distributor.nextpush.activities.PermissionsRequest.requestAppPermissions
|
||||
import org.unifiedpush.distributor.nextpush.activities.StartActivity.Companion.goToStartActivity
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor
|
||||
@ -157,7 +157,7 @@ class MainActivity : AppCompatActivity() {
|
||||
StartService.stopService()
|
||||
FailureHandler.clearFails()
|
||||
}
|
||||
Account.logout(this)
|
||||
AccountFactory.logout(this)
|
||||
finish()
|
||||
goToStartActivity(this)
|
||||
}
|
||||
|
@ -14,9 +14,9 @@ import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.isGone
|
||||
import org.unifiedpush.distributor.nextpush.R
|
||||
import org.unifiedpush.distributor.nextpush.account.Account
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.setTypeDirect
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.setTypeSSO
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.setTypeDirect
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.setTypeSSO
|
||||
import org.unifiedpush.distributor.nextpush.activities.MainActivity.Companion.goToMainActivity
|
||||
import org.unifiedpush.distributor.nextpush.activities.PermissionsRequest.requestAppPermissions
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
@ -29,7 +29,7 @@ class StartActivity : AppCompatActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_start)
|
||||
this.requestAppPermissions()
|
||||
if (Account.isConnected(this)) {
|
||||
if (AccountFactory.isConnected(this)) {
|
||||
goToMainActivity(this)
|
||||
finish()
|
||||
}
|
||||
@ -74,7 +74,7 @@ class StartActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun login() {
|
||||
Account.getAccount(this, uninitialized = true)?.let {
|
||||
AccountFactory.getAccount(this, uninitialized = true)?.let {
|
||||
onResult = { activity: Activity, i: Int, i1: Int, intent: Intent?, block: (success: Boolean) -> Unit ->
|
||||
it.onActivityResult(activity, i, i1, intent, block)
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.sse.EventSource
|
||||
import okhttp3.sse.EventSources
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.accountType
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.npDeviceId
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.accountType
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.npDeviceId
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountType
|
||||
import org.unifiedpush.distributor.nextpush.api.provider.* // ktlint-disable no-wildcard-imports
|
||||
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
|
||||
|
@ -2,7 +2,7 @@ package org.unifiedpush.distributor.nextpush.api.provider
|
||||
|
||||
import android.content.Context
|
||||
import okhttp3.* // ktlint-disable no-wildcard-imports
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
|
||||
|
@ -5,7 +5,7 @@ import android.util.Log
|
||||
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.account.AccountFactory.getAccount
|
||||
import retrofit2.NextcloudRetrofitApiBuilder
|
||||
|
||||
class ApiSSOFactory(val context: Context) : ApiProviderFactory {
|
||||
|
@ -5,7 +5,7 @@ import android.content.Intent
|
||||
import android.util.Log
|
||||
import org.unifiedpush.distributor.nextpush.Database.Companion.getDb
|
||||
import org.unifiedpush.distributor.nextpush.LocalNotification
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.api.Api
|
||||
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
|
@ -11,7 +11,7 @@ import androidx.annotation.RequiresApi
|
||||
import org.unifiedpush.distributor.nextpush.AppCompanion
|
||||
import org.unifiedpush.distributor.nextpush.Database.Companion.getDb
|
||||
import org.unifiedpush.distributor.nextpush.WakeLock
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.isConnected
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.isConnected
|
||||
import org.unifiedpush.distributor.nextpush.distributor.* // ktlint-disable no-wildcard-imports
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.checkToken
|
||||
import org.unifiedpush.distributor.nextpush.distributor.Distributor.createApp
|
||||
|
@ -4,7 +4,7 @@ import android.content.Context
|
||||
import android.util.Log
|
||||
import androidx.work.* // ktlint-disable no-wildcard-imports
|
||||
import org.unifiedpush.distributor.nextpush.AppCompanion
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||
import java.util.Calendar
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
@ -8,7 +8,7 @@ import android.os.IBinder
|
||||
import android.util.Log
|
||||
import org.unifiedpush.distributor.nextpush.AppCompanion
|
||||
import org.unifiedpush.distributor.nextpush.WakeLock
|
||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount
|
||||
import org.unifiedpush.distributor.nextpush.api.Api
|
||||
import org.unifiedpush.distributor.nextpush.utils.ForegroundNotification
|
||||
import org.unifiedpush.distributor.nextpush.utils.NOTIFICATION_ID_FOREGROUND
|
||||
|
Loading…
x
Reference in New Issue
Block a user