Avoid a null pointer exception
This commit is contained in:
parent
08ac9e38bf
commit
2495c78bce
@ -32,22 +32,22 @@ object ApiUtils {
|
|||||||
val createQueue = emptyList<String>().toMutableList()
|
val createQueue = emptyList<String>().toMutableList()
|
||||||
val delQueue = emptyList<String>().toMutableList()
|
val delQueue = emptyList<String>().toMutableList()
|
||||||
|
|
||||||
private lateinit var mApi: ProviderApi
|
private var mApi: ProviderApi? = null
|
||||||
private lateinit var nextcloudAPI: NextcloudAPI
|
private var nextcloudAPI: NextcloudAPI? = null
|
||||||
private lateinit var factory: EventSource.Factory
|
private var source: EventSource? = null
|
||||||
private lateinit var source: EventSource
|
|
||||||
|
|
||||||
fun apiDestroy() {
|
fun apiDestroy() {
|
||||||
if (::nextcloudAPI.isInitialized)
|
nextcloudAPI?.stop()
|
||||||
nextcloudAPI.stop()
|
source?.cancel()
|
||||||
if (::source.isInitialized)
|
nextcloudAPI = null
|
||||||
source.cancel()
|
source = null
|
||||||
|
mApi = null
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun cApi(context: Context, callback: () -> Unit) {
|
private fun cApi(context: Context, callback: () -> Unit) {
|
||||||
if (::mApi.isInitialized and ::nextcloudAPI.isInitialized) {
|
mApi?.let {
|
||||||
callback()
|
callback()
|
||||||
} else {
|
} ?: run {
|
||||||
val nCallback = object : NextcloudAPI.ApiConnectedListener {
|
val nCallback = object : NextcloudAPI.ApiConnectedListener {
|
||||||
override fun onConnected() {
|
override fun onConnected() {
|
||||||
Log.d(TAG, "Api connected.")
|
Log.d(TAG, "Api connected.")
|
||||||
@ -58,11 +58,13 @@ object ApiUtils {
|
|||||||
Log.d(TAG, "Cannot connect to API: ex = [$ex]")
|
Log.d(TAG, "Cannot connect to API: ex = [$ex]")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nextcloudAPI = NextcloudAPI(context, ssoAccount, GsonBuilder().create(), nCallback)
|
NextcloudAPI(context, ssoAccount, GsonBuilder().create(), nCallback).let {
|
||||||
mApi = NextcloudRetrofitApiBuilder(nextcloudAPI, mApiEndpoint)
|
nextcloudAPI = it
|
||||||
|
mApi = NextcloudRetrofitApiBuilder(it, mApiEndpoint)
|
||||||
.create(ProviderApi::class.java)
|
.create(ProviderApi::class.java)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun apiSync(context: Context) {
|
fun apiSync(context: Context) {
|
||||||
cApi(context) { cSync(context) }
|
cApi(context) { cSync(context) }
|
||||||
@ -75,7 +77,7 @@ object ApiUtils {
|
|||||||
Log.d(TAG, "No deviceId found.")
|
Log.d(TAG, "No deviceId found.")
|
||||||
val parameters: MutableMap<String, String> = HashMap()
|
val parameters: MutableMap<String, String> = HashMap()
|
||||||
parameters["deviceName"] = Build.MODEL
|
parameters["deviceName"] = Build.MODEL
|
||||||
mApi.createDevice(parameters)
|
mApi?.createDevice(parameters)
|
||||||
?.subscribeOn(Schedulers.newThread())
|
?.subscribeOn(Schedulers.newThread())
|
||||||
?.observeOn(Schedulers.newThread())
|
?.observeOn(Schedulers.newThread())
|
||||||
?.subscribe(object : Observer<ApiResponse?> {
|
?.subscribe(object : Observer<ApiResponse?> {
|
||||||
@ -124,8 +126,7 @@ object ApiUtils {
|
|||||||
.get()
|
.get()
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
factory = EventSources.createFactory(client)
|
source = EventSources.createFactory(client).newEventSource(request, SSEListener(context))
|
||||||
source = factory.newEventSource(request, SSEListener(context))
|
|
||||||
Log.d(TAG, "cSync done.")
|
Log.d(TAG, "cSync done.")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,9 +140,9 @@ object ApiUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun cDeleteDevice(context: Context) {
|
private fun cDeleteDevice(context: Context) {
|
||||||
val deviceId = getDeviceId(context)
|
val deviceId = getDeviceId(context) ?: return
|
||||||
|
|
||||||
mApi.deleteDevice(deviceId)
|
mApi?.deleteDevice(deviceId)
|
||||||
?.subscribeOn(Schedulers.newThread())
|
?.subscribeOn(Schedulers.newThread())
|
||||||
?.observeOn(Schedulers.newThread())
|
?.observeOn(Schedulers.newThread())
|
||||||
?.subscribe(object : Observer<ApiResponse?> {
|
?.subscribe(object : Observer<ApiResponse?> {
|
||||||
@ -195,7 +196,7 @@ object ApiUtils {
|
|||||||
)
|
)
|
||||||
} ?: return
|
} ?: return
|
||||||
|
|
||||||
mApi.createApp(parameters)
|
mApi?.createApp(parameters)
|
||||||
?.subscribeOn(Schedulers.newThread())
|
?.subscribeOn(Schedulers.newThread())
|
||||||
?.observeOn(Schedulers.newThread())
|
?.observeOn(Schedulers.newThread())
|
||||||
?.subscribe(object : Observer<ApiResponse?> {
|
?.subscribe(object : Observer<ApiResponse?> {
|
||||||
@ -240,7 +241,7 @@ object ApiUtils {
|
|||||||
|
|
||||||
private fun cDeleteApp(context: Context, connectorToken: String, callback: () -> Unit) {
|
private fun cDeleteApp(context: Context, connectorToken: String, callback: () -> Unit) {
|
||||||
val appToken = getDb(context).getAppToken(connectorToken)
|
val appToken = getDb(context).getAppToken(connectorToken)
|
||||||
mApi.deleteApp(appToken)
|
mApi?.deleteApp(appToken)
|
||||||
?.subscribeOn(Schedulers.newThread())
|
?.subscribeOn(Schedulers.newThread())
|
||||||
?.observeOn(Schedulers.newThread())
|
?.observeOn(Schedulers.newThread())
|
||||||
?.subscribe(object : Observer<ApiResponse?> {
|
?.subscribe(object : Observer<ApiResponse?> {
|
||||||
|
@ -10,19 +10,19 @@ interface ProviderApi {
|
|||||||
|
|
||||||
@PUT("/device/")
|
@PUT("/device/")
|
||||||
fun createDevice(
|
fun createDevice(
|
||||||
@Body subscribeMap: MutableMap<String, String>?
|
@Body subscribeMap: MutableMap<String, String>
|
||||||
): Observable<ApiResponse>?
|
): Observable<ApiResponse>?
|
||||||
|
|
||||||
@DELETE("/device/{deviceId}")
|
@DELETE("/device/{deviceId}")
|
||||||
fun deleteDevice(@Path("deviceId") devideId: String?): Observable<ApiResponse>?
|
fun deleteDevice(@Path("deviceId") deviceId: String): Observable<ApiResponse>?
|
||||||
|
|
||||||
@PUT("/app/")
|
@PUT("/app/")
|
||||||
fun createApp(
|
fun createApp(
|
||||||
@Body authorizeMap: MutableMap<String, String>?
|
@Body authorizeMap: MutableMap<String, String>
|
||||||
): Observable<ApiResponse>?
|
): Observable<ApiResponse>?
|
||||||
|
|
||||||
@DELETE("/app/{token}")
|
@DELETE("/app/{token}")
|
||||||
fun deleteApp(@Path("token") token: String?): Observable<ApiResponse>?
|
fun deleteApp(@Path("token") token: String): Observable<ApiResponse>?
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val mApiEndpoint = "/index.php/apps/uppush"
|
const val mApiEndpoint = "/index.php/apps/uppush"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user