forked from authgear/authgear-sdk-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebKitWebViewActivity.kt
More file actions
338 lines (298 loc) · 12.7 KB
/
WebKitWebViewActivity.kt
File metadata and controls
338 lines (298 loc) · 12.7 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package com.authgear.flutter
import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Message
import android.view.Menu
import android.view.MenuItem
import android.webkit.*
import android.webkit.WebView.HitTestResult.SRC_ANCHOR_TYPE
import android.webkit.WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE
import androidx.annotation.*
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
class WebKitWebViewActivity: AppCompatActivity() {
companion object {
private const val MENU_ID_CANCEL = 1
private const val KEY_OPTIONS = "KEY_OPTIONS"
private const val KEY_WEB_VIEW_STATE = "KEY_WEB_VIEW_STATE"
private const val TAG_FILE_CHOOSER = 1
internal const val KEY_WECHAT_REDIRECT_URI = "KEY_WECHAT_REDIRECT_URI"
fun createIntent(ctx: Context, options: Options): Intent {
val intent = Intent(ctx, WebKitWebViewActivity::class.java)
intent.putExtra(KEY_OPTIONS, options.toBundle())
return intent
}
}
private lateinit var mWebView: WebView
private var result: Uri? = null
private val handles = StartActivityHandles<ValueCallback<Array<Uri>>>()
class Options {
var url: Uri
var redirectURI: Uri
var actionBarBackgroundColor: Int? = null
var actionBarButtonTintColor: Int? = null
var wechatRedirectURI: Uri? = null
var wechatRedirectURIIntentAction: String? = null
constructor(url: Uri, redirectURI: Uri) {
this.url = url
this.redirectURI = redirectURI
}
internal constructor(bundle: Bundle) {
this.url = bundle.getParcelable("url")!!
this.redirectURI = bundle.getParcelable("redirectURI")!!
if (bundle.containsKey("actionBarBackgroundColor")) {
this.actionBarBackgroundColor = bundle.getInt("actionBarBackgroundColor")
}
if (bundle.containsKey("actionBarButtonTintColor")) {
this.actionBarButtonTintColor = bundle.getInt("actionBarButtonTintColor")
}
if (bundle.containsKey("wechatRedirectURI")) {
this.wechatRedirectURI = bundle.getParcelable("wechatRedirectURI")
}
if (bundle.containsKey("wechatRedirectURIIntentAction")) {
this.wechatRedirectURIIntentAction = bundle.getString("wechatRedirectURIIntentAction")
}
}
fun toBundle(): Bundle {
val bundle = Bundle()
bundle.putParcelable("url", this.url)
bundle.putParcelable("redirectURI", this.redirectURI)
this.actionBarBackgroundColor?.let {
bundle.putInt("actionBarBackgroundColor", it)
}
this.actionBarButtonTintColor?.let {
bundle.putInt("actionBarButtonTintColor", it)
}
this.wechatRedirectURI?.let {
bundle.putParcelable("wechatRedirectURI", it)
}
this.wechatRedirectURIIntentAction?.let {
bundle.putString("wechatRedirectURIIntentAction", it)
}
return bundle
}
}
private class MyWebViewClient constructor(private val activity: WebKitWebViewActivity) :
WebViewClient() {
companion object {
private const val USERSCRIPT_USER_SELECT_NONE = "document.documentElement.style.webkitUserSelect='none';document.documentElement.style.userSelect='none';";
}
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
// android.webkit.view does not have WKUserContentController that allows us to inject userscript.
// onPageFinished will be called for each navigation.
// So it can be used as a replacement of WKUserContentController to allow us to
// run a script for every page.
// The caveat is that the script is run in the main frame only.
// But we do not actually use iframes so it does not matter.
view?.evaluateJavascript(USERSCRIPT_USER_SELECT_NONE, null);
}
@TargetApi(Build.VERSION_CODES.N)
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
val uri = request?.url!!
if (this.shouldOverrideUrlLoading(uri)) {
return true
}
return super.shouldOverrideUrlLoading(view, request)
}
@SuppressWarnings("deprecation")
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
val uri = Uri.parse(url!!)!!
if (this.shouldOverrideUrlLoading(uri)) {
return true
}
return super.shouldOverrideUrlLoading(view, url)
}
private fun shouldOverrideUrlLoading(uri: Uri): Boolean {
val withoutQuery = this.removeQueryAndFragment(uri)
val redirectURI = this.activity.getOptions().redirectURI
if (withoutQuery.toString() == redirectURI.toString()) {
this.activity.result = uri
this.activity.callSetResult()
this.activity.finish()
return true
}
val wechatRedirectURI = this.activity.getOptions().wechatRedirectURI
if (wechatRedirectURI != null) {
if (withoutQuery.toString() == wechatRedirectURI.toString()) {
val intent = Intent(this.activity.getOptions().wechatRedirectURIIntentAction)
intent.setPackage(this.activity.applicationContext.packageName)
intent.putExtra(KEY_WECHAT_REDIRECT_URI, uri)
this.activity.applicationContext.sendBroadcast(intent)
return true
}
}
return false;
}
private fun removeQueryAndFragment(uri: Uri): Uri {
return uri.buildUpon().query(null).fragment(null).build()
}
}
private class MyWebChromeClient constructor(private val activity: WebKitWebViewActivity) :
WebChromeClient() {
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
override fun onShowFileChooser(
webView: WebView?,
filePathCallback: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
val handle = StartActivityHandle(TAG_FILE_CHOOSER, filePathCallback!!)
val requestCode = this.activity.handles.push(handle)
val intent = fileChooserParams!!.createIntent()
this.activity.startActivityForResult(intent, requestCode)
return true
}
override fun onCreateWindow(
view: WebView?,
isDialog: Boolean,
isUserGesture: Boolean,
resultMsg: Message?
): Boolean {
if (view == null) return false
val result = view.hitTestResult
return when (result.type) {
SRC_IMAGE_ANCHOR_TYPE -> {
// ref: https://pacheco.dev/posts/android/webview-image-anchor/
val handler = view.handler
val message = handler.obtainMessage()
view.requestFocusNodeHref(message)
val url = message.data.getString("url") ?: return false
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
view.context.startActivity(browserIntent)
return false
}
SRC_ANCHOR_TYPE -> {
val data = result.extra ?: return false
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(data))
view.context.startActivity(browserIntent)
return false
}
else -> false
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val options = this.getOptions()
// Do not show title.
supportActionBar?.setDisplayShowTitleEnabled(false)
// Configure navigation bar background color.
options.actionBarBackgroundColor?.let {
supportActionBar?.setBackgroundDrawable(ColorDrawable(it))
}
// Show back button.
supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_HOME or ActionBar.DISPLAY_HOME_AS_UP
// Configure the back button.
var backButtonDrawable = getDrawableCompat(R.drawable.ic_arrow_back)
if (options.actionBarButtonTintColor != null) {
backButtonDrawable =
tintDrawable(backButtonDrawable, options.actionBarButtonTintColor!!)
}
supportActionBar?.setHomeAsUpIndicator(backButtonDrawable)
// Configure web view.
this.mWebView = WebView(this)
this.mWebView.settings.setSupportMultipleWindows(true)
this.mWebView.settings.domStorageEnabled = true
this.mWebView.settings.javaScriptEnabled = true
this.setContentView(this.mWebView)
this.mWebView.setWebViewClient(MyWebViewClient(this))
this.mWebView.setWebChromeClient(MyWebChromeClient(this))
if (savedInstanceState == null) {
this.mWebView.loadUrl(options.url.toString())
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
val webViewBundle = Bundle()
this.mWebView.saveState(webViewBundle)
outState.putBundle(KEY_WEB_VIEW_STATE, webViewBundle)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
val bundle = savedInstanceState.getBundle(KEY_WEB_VIEW_STATE)
if (bundle != null) {
this.mWebView.restoreState(bundle)
}
}
override fun onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack()
} else {
callSetResult()
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val options = getOptions()
// Configure the close button.
var drawable = getDrawableCompat(R.drawable.ic_close)
if (options.actionBarButtonTintColor != null) {
drawable = tintDrawable(drawable, options.actionBarButtonTintColor!!)
}
menu.add(Menu.NONE, MENU_ID_CANCEL, Menu.NONE, android.R.string.cancel)
.setIcon(drawable)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(@NonNull item: MenuItem): Boolean {
if (item.getItemId() === android.R.id.home) {
onBackPressed()
return true
}
if (item.getItemId() === MENU_ID_CANCEL) {
callSetResult()
finish()
return true
}
return super.onOptionsItemSelected(item)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, @Nullable data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val handle = handles.pop(requestCode)
?: return
when (handle.tag) {
TAG_FILE_CHOOSER -> when (resultCode) {
Activity.RESULT_CANCELED -> handle.value.onReceiveValue(null)
Activity.RESULT_OK -> if (data != null && data.data != null) {
handle.value.onReceiveValue(arrayOf(data.data!!))
} else {
handle.value.onReceiveValue(null)
}
}
}
}
private fun getOptions(): Options {
val bundle: Bundle = this.intent.getParcelableExtra(KEY_OPTIONS)!!
return Options(bundle)
}
private fun callSetResult() {
if (this.result == null) {
this.setResult(Activity.RESULT_CANCELED)
} else {
val intent = Intent()
intent.data = this.result
this.setResult(Activity.RESULT_OK, intent)
}
}
private fun getDrawableCompat(@DrawableRes id: Int): Drawable {
return ResourcesCompat.getDrawable(resources, id, null)!!
}
private fun tintDrawable(drawable: Drawable, @ColorInt color: Int): Drawable {
val newDrawable: Drawable =
DrawableCompat.wrap(drawable).constantState!!.newDrawable().mutate()
DrawableCompat.setTint(newDrawable, color)
return newDrawable
}
}