Remove AccountUtils

This commit is contained in:
sim 2023-02-26 16:38:28 +01:00
parent 4fd0099302
commit 3485a77e95
4 changed files with 25 additions and 64 deletions

View File

@ -6,9 +6,26 @@ import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundExce
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException
import org.unifiedpush.distributor.nextpush.utils.TAG import org.unifiedpush.distributor.nextpush.utils.TAG
private const val PREF_NAME = "NextPush"
private const val PREF_DEVICE_ID = "deviceId"
object Account { object Account {
private var account: AccountFactory? = null private var account: AccountFactory? = null
var Context.deviceId: 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? { fun getAccount(context: Context, uninitialized: Boolean = false): AccountFactory? {
return account return account
?: run { ?: run {

View File

@ -1,48 +0,0 @@
package org.unifiedpush.distributor.nextpush.account
import android.content.Context
private const val PREF_NAME = "NextPush"
private const val PREF_DEVICE_ID = "deviceId"
private const val PREF_URL = "url"
object AccountUtils {
fun saveDeviceId(context: Context, deviceId: String) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.edit()
.putString(PREF_DEVICE_ID, deviceId)
.apply()
}
fun getDeviceId(context: Context): String? {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getString(PREF_DEVICE_ID, null)
}
fun removeDeviceId(context: Context) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.edit()
.remove(PREF_DEVICE_ID)
.apply()
}
fun saveUrl(context: Context, url: String) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.edit()
.putString(PREF_URL, url)
.apply()
}
fun getUrl(context: Context): String? {
return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.getString(PREF_URL, null)
}
fun removeUrl(context: Context) {
context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
.edit()
.remove(PREF_URL)
.apply()
}
}

View File

@ -10,12 +10,8 @@ import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
import okhttp3.sse.EventSource import okhttp3.sse.EventSource
import okhttp3.sse.EventSources import okhttp3.sse.EventSources
import org.unifiedpush.distributor.nextpush.account.Account.deviceId
import org.unifiedpush.distributor.nextpush.account.Account.getAccount import org.unifiedpush.distributor.nextpush.account.Account.getAccount
import org.unifiedpush.distributor.nextpush.account.AccountUtils.getDeviceId
import org.unifiedpush.distributor.nextpush.account.AccountUtils.removeDeviceId
import org.unifiedpush.distributor.nextpush.account.AccountUtils.removeUrl
import org.unifiedpush.distributor.nextpush.account.AccountUtils.saveDeviceId
import org.unifiedpush.distributor.nextpush.account.AccountUtils.saveUrl
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
import org.unifiedpush.distributor.nextpush.api.provider.ApiProviderFactory import org.unifiedpush.distributor.nextpush.api.provider.ApiProviderFactory
@ -52,12 +48,11 @@ object Api {
} }
fun Context.apiSync() { fun Context.apiSync() {
getDeviceId(this)?.let { deviceId?.let {
syncDevice(it) syncDevice(it)
} }
?: run { ?: run {
Log.d(TAG, "No deviceId found.") Log.d(TAG, "No deviceId found.")
var deviceId: String? = null
val parameters = mapOf("deviceName" to Build.MODEL) val parameters = mapOf("deviceName" to Build.MODEL)
@ -72,7 +67,6 @@ object Api {
override fun onNext(response: ApiResponse) { override fun onNext(response: ApiResponse) {
response.deviceId.let { response.deviceId.let {
saveDeviceId(this@apiSync, it)
deviceId = it deviceId = it
} }
} }
@ -82,8 +76,6 @@ object Api {
} }
override fun onComplete() { override fun onComplete() {
saveUrl(this@apiSync, "$baseUrl$mApiEndpoint")
// Sync once it is registered // Sync once it is registered
deviceId?.let { deviceId?.let {
syncDevice(it) syncDevice(it)
@ -111,7 +103,7 @@ object Api {
} }
fun Context.apiDeleteDevice(block: () -> Unit = {}) { fun Context.apiDeleteDevice(block: () -> Unit = {}) {
val deviceId = getDeviceId(this) ?: return val deviceId = deviceId ?: return
withApiProvider { apiProvider -> withApiProvider { apiProvider ->
apiProvider.deleteDevice(deviceId) apiProvider.deleteDevice(deviceId)
@ -135,11 +127,10 @@ object Api {
} }
override fun onComplete() { override fun onComplete() {
removeUrl(this@apiDeleteDevice)
block() block()
} }
}) })
removeDeviceId(this) this.deviceId = null
} }
} }
@ -148,7 +139,7 @@ object Api {
block: (String?) -> Unit block: (String?) -> Unit
) { ) {
// The unity of connector token must already be checked here // The unity of connector token must already be checked here
val parameters = getDeviceId(this)?.let { val parameters = deviceId?.let {
mutableMapOf( mutableMapOf(
"deviceId" to it, "deviceId" to it,
"appName" to appName "appName" to appName

View File

@ -3,10 +3,11 @@ package org.unifiedpush.distributor.nextpush.distributor
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.Log import android.util.Log
import org.unifiedpush.distributor.nextpush.account.AccountUtils.getUrl import org.unifiedpush.distributor.nextpush.account.Account.getAccount
import org.unifiedpush.distributor.nextpush.api.Api.apiCreateApp import org.unifiedpush.distributor.nextpush.api.Api.apiCreateApp
import org.unifiedpush.distributor.nextpush.api.Api.apiDeleteApp import org.unifiedpush.distributor.nextpush.api.Api.apiDeleteApp
import org.unifiedpush.distributor.nextpush.api.Api.apiDeleteDevice import org.unifiedpush.distributor.nextpush.api.Api.apiDeleteDevice
import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.mApiEndpoint
import org.unifiedpush.distributor.nextpush.utils.TAG import org.unifiedpush.distributor.nextpush.utils.TAG
/** /**
@ -94,7 +95,7 @@ object Distributor {
private fun getEndpoint(context: Context, connectorToken: String): String { private fun getEndpoint(context: Context, connectorToken: String): String {
val db = getDb(context) val db = getDb(context)
val appToken = db.getAppToken(connectorToken) val appToken = db.getAppToken(connectorToken)
return "${getUrl(context)}/push/$appToken" return "${getAccount(context)?.url}$mApiEndpoint/push/$appToken"
} }
fun checkToken(context: Context, connectorToken: String, app: String): String { fun checkToken(context: Context, connectorToken: String, app: String): String {