Use class for Api
This commit is contained in:
parent
2f92edc511
commit
94b3e4d933
|
@ -19,39 +19,28 @@ import org.unifiedpush.distributor.nextpush.api.provider.ApiProvider.Companion.m
|
||||||
import org.unifiedpush.distributor.nextpush.api.response.ApiResponse
|
import org.unifiedpush.distributor.nextpush.api.response.ApiResponse
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
object Api {
|
class Api(val context: Context) {
|
||||||
|
|
||||||
private val TAG = Api::class.java.simpleName
|
private val TAG = Api::class.java.simpleName
|
||||||
private var provider: ApiProviderFactory? = null
|
|
||||||
private var source: EventSource? = null
|
|
||||||
|
|
||||||
private val Context.baseUrl: String
|
private val baseUrl: String
|
||||||
get() = getAccount(this)?.url ?: "http://0.0.0.0/"
|
get() = getAccount(context)?.url ?: "http://0.0.0.0/"
|
||||||
|
|
||||||
private fun Context.withApiProvider(block: (ApiProvider) -> Unit) {
|
private fun withApiProvider(block: (ApiProvider) -> Unit) {
|
||||||
(
|
when (context.accountType) {
|
||||||
provider ?: run {
|
AccountType.SSO -> ApiSSOFactory(context)
|
||||||
Log.d(TAG, "Setting SSOProvider")
|
AccountType.Direct -> ApiDirectFactory(context)
|
||||||
when (accountType) {
|
}.getProviderAndExecute(block)
|
||||||
AccountType.SSO -> ApiSSOFactory(this)
|
|
||||||
AccountType.Direct -> ApiDirectFactory(this)
|
|
||||||
}.apply {
|
|
||||||
provider = this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
).getProviderAndExecute(block)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun apiDestroy() {
|
fun apiDestroy() {
|
||||||
Log.d(TAG, "Destroying API")
|
Log.d(TAG, "Destroying API")
|
||||||
provider?.destroyProvider()
|
syncSource?.cancel()
|
||||||
provider = null
|
syncSource = null
|
||||||
source?.cancel()
|
|
||||||
source = null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.apiSync() {
|
fun apiSync() {
|
||||||
deviceId?.let {
|
context.deviceId?.let {
|
||||||
syncDevice(it)
|
syncDevice(it)
|
||||||
}
|
}
|
||||||
?: run {
|
?: run {
|
||||||
|
@ -70,7 +59,7 @@ object Api {
|
||||||
|
|
||||||
override fun onNext(response: ApiResponse) {
|
override fun onNext(response: ApiResponse) {
|
||||||
response.deviceId.let {
|
response.deviceId.let {
|
||||||
deviceId = it
|
context.deviceId = it
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +69,7 @@ object Api {
|
||||||
|
|
||||||
override fun onComplete() {
|
override fun onComplete() {
|
||||||
// Sync once it is registered
|
// Sync once it is registered
|
||||||
deviceId?.let {
|
context.deviceId?.let {
|
||||||
syncDevice(it)
|
syncDevice(it)
|
||||||
}
|
}
|
||||||
Log.d(TAG, "mApi register: onComplete")
|
Log.d(TAG, "mApi register: onComplete")
|
||||||
|
@ -93,7 +82,7 @@ object Api {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Context.syncDevice(deviceId: String) {
|
private fun syncDevice(deviceId: String) {
|
||||||
val client = OkHttpClient.Builder()
|
val client = OkHttpClient.Builder()
|
||||||
.readTimeout(0, TimeUnit.SECONDS)
|
.readTimeout(0, TimeUnit.SECONDS)
|
||||||
.retryOnConnectionFailure(false)
|
.retryOnConnectionFailure(false)
|
||||||
|
@ -104,12 +93,12 @@ object Api {
|
||||||
.get()
|
.get()
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
source = EventSources.createFactory(client).newEventSource(request, SSEListener(this))
|
syncSource = EventSources.createFactory(client).newEventSource(request, SSEListener(context))
|
||||||
Log.d(TAG, "cSync done.")
|
Log.d(TAG, "cSync done.")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.apiDeleteDevice(block: () -> Unit = {}) {
|
fun apiDeleteDevice(block: () -> Unit = {}) {
|
||||||
val deviceId = deviceId ?: return
|
val deviceId = context.deviceId ?: return
|
||||||
try {
|
try {
|
||||||
withApiProvider { apiProvider ->
|
withApiProvider { apiProvider ->
|
||||||
apiProvider.deleteDevice(deviceId)
|
apiProvider.deleteDevice(deviceId)
|
||||||
|
@ -136,19 +125,19 @@ object Api {
|
||||||
block()
|
block()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
this.deviceId = null
|
context.deviceId = null
|
||||||
}
|
}
|
||||||
} catch (e: NoProviderException) {
|
} catch (e: NoProviderException) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.apiCreateApp(
|
fun apiCreateApp(
|
||||||
appName: String,
|
appName: String,
|
||||||
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 = deviceId?.let {
|
val parameters = context.deviceId?.let {
|
||||||
mutableMapOf(
|
mutableMapOf(
|
||||||
"deviceId" to it,
|
"deviceId" to it,
|
||||||
"appName" to appName
|
"appName" to appName
|
||||||
|
@ -188,7 +177,7 @@ object Api {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.apiDeleteApp(nextpushToken: String, block: () -> Unit) {
|
fun apiDeleteApp(nextpushToken: String, block: () -> Unit) {
|
||||||
try {
|
try {
|
||||||
withApiProvider { apiProvider ->
|
withApiProvider { apiProvider ->
|
||||||
apiProvider.deleteApp(nextpushToken)
|
apiProvider.deleteApp(nextpushToken)
|
||||||
|
@ -220,4 +209,8 @@ object Api {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private var syncSource: EventSource? = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,15 @@
|
||||||
package org.unifiedpush.distributor.nextpush.api.provider
|
package org.unifiedpush.distributor.nextpush.api.provider
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
|
||||||
import okhttp3.* // ktlint-disable no-wildcard-imports
|
import okhttp3.* // ktlint-disable no-wildcard-imports
|
||||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||||
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.utils.TAG
|
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
|
|
||||||
class ApiDirectFactory(val context: Context) : ApiProviderFactory {
|
class ApiDirectFactory(val context: Context) : ApiProviderFactory {
|
||||||
|
|
||||||
private var apiProvider: ApiProvider? = null
|
|
||||||
|
|
||||||
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
||||||
val account = getAccount(context) ?: run {
|
val account = getAccount(context) ?: run {
|
||||||
throw NoProviderException("No account found")
|
throw NoProviderException("No account found")
|
||||||
|
@ -24,22 +20,13 @@ class ApiDirectFactory(val context: Context) : ApiProviderFactory {
|
||||||
val client = account.getAccount(context) as OkHttpClient? ?: run {
|
val client = account.getAccount(context) as OkHttpClient? ?: run {
|
||||||
throw NoProviderException("No client found")
|
throw NoProviderException("No client found")
|
||||||
}
|
}
|
||||||
apiProvider?.let(block)
|
Retrofit.Builder()
|
||||||
?: run {
|
.client(client)
|
||||||
Log.d(TAG, "Creating new provider")
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
Retrofit.Builder()
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||||
.client(client)
|
.baseUrl("$url$mApiEndpoint").build()
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
.create(ApiProvider::class.java).let {
|
||||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
block(it)
|
||||||
.baseUrl("$url$mApiEndpoint").build()
|
|
||||||
.create(ApiProvider::class.java).let {
|
|
||||||
apiProvider = it
|
|
||||||
block(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun destroyProvider() {
|
|
||||||
// Nothing
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,5 +3,4 @@ package org.unifiedpush.distributor.nextpush.api.provider
|
||||||
class NoProviderException(message: String) : Exception(message)
|
class NoProviderException(message: String) : Exception(message)
|
||||||
interface ApiProviderFactory {
|
interface ApiProviderFactory {
|
||||||
fun getProviderAndExecute(block: (ApiProvider) -> Unit)
|
fun getProviderAndExecute(block: (ApiProvider) -> Unit)
|
||||||
fun destroyProvider()
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,52 +6,40 @@ 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 nextcloudAPI: NextcloudAPI? = null
|
|
||||||
|
|
||||||
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
override fun getProviderAndExecute(block: (ApiProvider) -> Unit) {
|
||||||
|
var nextcloudAPI: NextcloudAPI? = null
|
||||||
val account = getAccount(context) ?: run {
|
val account = getAccount(context) ?: run {
|
||||||
throw NoProviderException("No account found")
|
throw NoProviderException("No account found")
|
||||||
}
|
}
|
||||||
val client = account.getAccount(context) as SingleSignOnAccount? ?: run {
|
val client = account.getAccount(context) as SingleSignOnAccount? ?: run {
|
||||||
throw NoProviderException("No client found")
|
throw NoProviderException("No client found")
|
||||||
}
|
}
|
||||||
apiProvider?.let(block)
|
val ssoApiCallback = object : NextcloudAPI.ApiConnectedListener {
|
||||||
?: run {
|
override fun onConnected() {
|
||||||
Log.d(TAG, "Creating new provider")
|
Log.d(TAG, "Api connected.")
|
||||||
val ssoApiCallback = object : NextcloudAPI.ApiConnectedListener {
|
nextcloudAPI?.let { nextcloudAPI ->
|
||||||
override fun onConnected() {
|
NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint)
|
||||||
Log.d(TAG, "Api connected.")
|
.create(ApiProvider::class.java).let {
|
||||||
nextcloudAPI?.let { nextcloudAPI ->
|
block(it)
|
||||||
NextcloudRetrofitApiBuilder(nextcloudAPI, ApiProvider.mApiEndpoint)
|
|
||||||
.create(ApiProvider::class.java).let {
|
|
||||||
apiProvider = it
|
|
||||||
block(it)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override fun onError(ex: Exception) {
|
|
||||||
Log.d(TAG, "Cannot connect to API: ex = [$ex]")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
nextcloudAPI = NextcloudAPI(
|
|
||||||
context,
|
|
||||||
client,
|
|
||||||
GsonBuilder().create(),
|
|
||||||
ssoApiCallback
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
override fun destroyProvider() {
|
override fun onError(ex: Exception) {
|
||||||
nextcloudAPI?.stop()
|
Log.d(TAG, "Cannot connect to API: ex = [$ex]")
|
||||||
nextcloudAPI = null
|
}
|
||||||
|
}
|
||||||
|
nextcloudAPI = NextcloudAPI(
|
||||||
|
context,
|
||||||
|
client,
|
||||||
|
GsonBuilder().create(),
|
||||||
|
ssoApiCallback
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,7 @@ 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.Account.getAccount
|
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||||
import org.unifiedpush.distributor.nextpush.api.Api.apiCreateApp
|
import org.unifiedpush.distributor.nextpush.api.Api
|
||||||
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.api.provider.ApiProvider.Companion.mApiEndpoint
|
||||||
import org.unifiedpush.distributor.nextpush.utils.TAG
|
import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||||
|
|
||||||
|
@ -115,11 +113,11 @@ object Distributor {
|
||||||
sendUnregistered(context, it)
|
sendUnregistered(context, it)
|
||||||
db.unregisterApp(it)
|
db.unregisterApp(it)
|
||||||
}
|
}
|
||||||
context.apiDeleteDevice(block)
|
Api(context).apiDeleteDevice(block)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createApp(context: Context, appName: String, connectorToken: String, block: () -> Unit) {
|
fun createApp(context: Context, appName: String, connectorToken: String, block: () -> Unit) {
|
||||||
context.apiCreateApp(appName) { nextpushToken ->
|
Api(context).apiCreateApp(appName) { nextpushToken ->
|
||||||
nextpushToken?.let {
|
nextpushToken?.let {
|
||||||
getDb(context).registerApp(appName, connectorToken, it)
|
getDb(context).registerApp(appName, connectorToken, it)
|
||||||
}
|
}
|
||||||
|
@ -133,7 +131,7 @@ object Distributor {
|
||||||
db.getAppToken(
|
db.getAppToken(
|
||||||
connectorToken
|
connectorToken
|
||||||
)?.let { nextpushToken ->
|
)?.let { nextpushToken ->
|
||||||
context.apiDeleteApp(nextpushToken) {
|
Api(context).apiDeleteApp(nextpushToken) {
|
||||||
db.unregisterApp(connectorToken)
|
db.unregisterApp(connectorToken)
|
||||||
block()
|
block()
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,7 @@ import android.os.IBinder
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
import org.unifiedpush.distributor.nextpush.account.Account.getAccount
|
||||||
import org.unifiedpush.distributor.nextpush.api.Api.apiDestroy
|
import org.unifiedpush.distributor.nextpush.api.Api
|
||||||
import org.unifiedpush.distributor.nextpush.api.Api.apiSync
|
|
||||||
import org.unifiedpush.distributor.nextpush.api.SSEListener.Companion.lastEventDate
|
import org.unifiedpush.distributor.nextpush.api.SSEListener.Companion.lastEventDate
|
||||||
import org.unifiedpush.distributor.nextpush.utils.NOTIFICATION_ID_FOREGROUND
|
import org.unifiedpush.distributor.nextpush.utils.NOTIFICATION_ID_FOREGROUND
|
||||||
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.createForegroundNotification
|
import org.unifiedpush.distributor.nextpush.utils.NotificationUtils.createForegroundNotification
|
||||||
|
@ -18,6 +17,7 @@ import org.unifiedpush.distributor.nextpush.utils.TAG
|
||||||
class StartService : Service() {
|
class StartService : Service() {
|
||||||
|
|
||||||
private val networkCallback = RestartNetworkCallback(this)
|
private val networkCallback = RestartNetworkCallback(this)
|
||||||
|
private var api: Api? = null
|
||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? {
|
override fun onBind(intent: Intent?): IBinder? {
|
||||||
return null
|
return null
|
||||||
|
@ -41,7 +41,7 @@ class StartService : Service() {
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
Log.d(TAG, "Destroying Service")
|
Log.d(TAG, "Destroying Service")
|
||||||
apiDestroy()
|
api?.apiDestroy()
|
||||||
wakeLock?.let {
|
wakeLock?.let {
|
||||||
while (it.isHeld) {
|
while (it.isHeld) {
|
||||||
it.release()
|
it.release()
|
||||||
|
@ -69,8 +69,9 @@ class StartService : Service() {
|
||||||
acquire(10000L /*10 secs*/)
|
acquire(10000L /*10 secs*/)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
api = Api(this).also {
|
||||||
apiSync()
|
it.apiSync()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object StartServiceCompanion {
|
companion object StartServiceCompanion {
|
||||||
|
|
Loading…
Reference in New Issue