/
WebKitWebViewUIImplementation.kt
71 lines (67 loc) · 2.83 KB
/
WebKitWebViewUIImplementation.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.oursky.authgear
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class WebKitWebViewUIImplementation(val actionBarBackgroundColor: Int? = null, val actionBarButtonTintColor: Int? = null) : UIImplementation {
override fun openAuthorizationURL(
context: Context,
options: OpenAuthorizationURLOptions,
listener: OnOpenAuthorizationURLListener
) {
CoroutineScope(Dispatchers.Default).launch {
try {
val resultURLString =
this@WebKitWebViewUIImplementation.openAuthorizationURL(context, options)
listener.onSuccess(Uri.parse(resultURLString))
} catch (e: Throwable) {
listener.onFailed(e)
}
}
}
private suspend fun openAuthorizationURL(
context: Context,
options: OpenAuthorizationURLOptions
): String {
return suspendCoroutine { k ->
val action = newRandomAction()
val intentFilter = IntentFilter(action)
val br =
object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val type =
intent?.getStringExtra(AuthgearCore.KEY_OAUTH_BOARDCAST_TYPE) ?: return
when (type) {
OAuthBroadcastType.REDIRECT_URL.name -> {
context!!.unregisterReceiver(this)
val output = intent.getStringExtra(AuthgearCore.KEY_REDIRECT_URL)
if (output != null) {
k.resume(output)
} else {
k.resumeWithException(CancelException())
}
}
}
}
}
context.registerReceiver(br, intentFilter)
val webViewOptions = WebKitWebViewActivity.Options(options.url, options.redirectURI)
webViewOptions.actionBarBackgroundColor = this@WebKitWebViewUIImplementation.actionBarBackgroundColor
webViewOptions.actionBarButtonTintColor = this@WebKitWebViewUIImplementation.actionBarButtonTintColor
context.startActivity(
WebKitWebViewActivity.createIntent(
context,
action,
webViewOptions
)
)
}
}
}