diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/Account.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/Account.kt index ed42dbe..6ab4e4e 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/Account.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/Account.kt @@ -6,9 +6,26 @@ import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundExce import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException import org.unifiedpush.distributor.nextpush.utils.TAG +private const val PREF_NAME = "NextPush" +private const val PREF_DEVICE_ID = "deviceId" object Account { 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? { return account ?: run { diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt deleted file mode 100644 index f15c23b..0000000 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/account/AccountUtils.kt +++ /dev/null @@ -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() - } -} diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt index 6b6323b..8fc1ccd 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt @@ -10,12 +10,8 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.sse.EventSource 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.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.Companion.mApiEndpoint import org.unifiedpush.distributor.nextpush.api.provider.ApiProviderFactory @@ -52,12 +48,11 @@ object Api { } fun Context.apiSync() { - getDeviceId(this)?.let { + deviceId?.let { syncDevice(it) } ?: run { Log.d(TAG, "No deviceId found.") - var deviceId: String? = null val parameters = mapOf("deviceName" to Build.MODEL) @@ -72,7 +67,6 @@ object Api { override fun onNext(response: ApiResponse) { response.deviceId.let { - saveDeviceId(this@apiSync, it) deviceId = it } } @@ -82,8 +76,6 @@ object Api { } override fun onComplete() { - saveUrl(this@apiSync, "$baseUrl$mApiEndpoint") - // Sync once it is registered deviceId?.let { syncDevice(it) @@ -111,7 +103,7 @@ object Api { } fun Context.apiDeleteDevice(block: () -> Unit = {}) { - val deviceId = getDeviceId(this) ?: return + val deviceId = deviceId ?: return withApiProvider { apiProvider -> apiProvider.deleteDevice(deviceId) @@ -135,11 +127,10 @@ object Api { } override fun onComplete() { - removeUrl(this@apiDeleteDevice) block() } }) - removeDeviceId(this) + this.deviceId = null } } @@ -148,7 +139,7 @@ object Api { block: (String?) -> Unit ) { // The unity of connector token must already be checked here - val parameters = getDeviceId(this)?.let { + val parameters = deviceId?.let { mutableMapOf( "deviceId" to it, "appName" to appName diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt index 30ffe93..db34fd5 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/distributor/Distributor.kt @@ -3,10 +3,11 @@ package org.unifiedpush.distributor.nextpush.distributor import android.content.Context import android.content.Intent 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.apiDeleteApp 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 /** @@ -94,7 +95,7 @@ object Distributor { private fun getEndpoint(context: Context, connectorToken: String): String { val db = getDb(context) 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 {