-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleApiHelperRx.kt
51 lines (40 loc) · 1.8 KB
/
GoogleApiHelperRx.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package de.fintasys.jobcantouch.helper
import android.content.Context
import android.os.Bundle
import android.util.Log
import com.google.android.gms.common.api.GoogleApiClient
import com.google.android.gms.location.LocationServices
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
class GoogleApiHelperRx {
companion object {
private val TAG = this::class.java.simpleName
}
fun build(context: Context, callbackRx: GoogleApiClientCallbackRx): Observable<GoogleApiClient> {
return Observable.create<GoogleApiClient> { emitter: ObservableEmitter<GoogleApiClient> ->
val builder = GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
val client = builder.build()
val connCallback = object : GoogleApiClient.ConnectionCallbacks {
override fun onConnected(bundle: Bundle?) {
Log.v(TAG, "Connected")
callbackRx.onConnected(client, emitter)
}
override fun onConnectionSuspended(status: Int) {
Log.e(TAG, "Connection suspended")
emitter.onError(RuntimeException("Connection failed"))
}
}
val onConnectionFailedListener = GoogleApiClient.OnConnectionFailedListener { result ->
Log.e(TAG, "Connection failed")
emitter.onError(RuntimeException("Connection failed " + result.errorMessage))
}
client.registerConnectionCallbacks(connCallback)
client.registerConnectionFailedListener(onConnectionFailedListener)
client.connect()
}
}
interface GoogleApiClientCallbackRx {
fun onConnected(client: GoogleApiClient, emitter: ObservableEmitter<GoogleApiClient>)
}
}