import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import android.util.AttributeSet
import android.webkit.*
import android.widget.Toast
/**
這個WebView只是把系統WebView做了一些封裝,並沒有改變性能,只是做一次封裝,以後使用簡單點
* Note:自定義webview 支持點擊下載鏈接跳轉系統瀏覽器下載功能,
* 打開本地文件夾(在Activity的onActivityResult中調用webView.onActivityResult()方法),
* webview內部所有鏈接默認跳轉系統瀏覽器,可做js交互做其他處理
* Created by vV on 2018/4/11 8:51.
* Email:[email protected]
*
*/
class MyWebView : WebView {
var uploadMessage: android.webkit.ValueCallback<Array<Uri>>? = null
private var mUploadMessage: android.webkit.ValueCallback<Uri>? = null
val REQUEST_SELECT_FILE = 100
private val FILECHOOSER_RESULTCODE = 2
lateinit var activity : Activity
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
private fun init() {
settings.javaScriptEnabled = true
webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
var url1 = request!!.url.path
if (url1 != null)
toSysWeb(url1)
return true
}
}
setDownloadListener(object : DownloadListener {
override fun onDownloadStart(p0: String?, p1: String?, p2: String?, p3: String?, p4: Long) {
toSysWeb(p0!!)
}
})
webChromeClient = object : WebChromeClient() {
override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
if (uploadMessage != null) {
uploadMessage!!.onReceiveValue(null)
uploadMessage = null
}
uploadMessage = filePathCallback
val intent = fileChooserParams!!.createIntent()
try {
activity?.startActivityForResult(intent, REQUEST_SELECT_FILE)
} catch (e: ActivityNotFoundException) {
uploadMessage = null
Toast.makeText(context, "Cannot Open File Chooser", Toast.LENGTH_LONG).show()
return false
}
return true
}
}
}
private fun toSysWeb(url: String) {
val uri = Uri.parse(url)
val intent = Intent(Intent.ACTION_VIEW, uri)
context.startActivity(intent)
}
fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (requestCode == REQUEST_SELECT_FILE) {
if (uploadMessage == null)
return
uploadMessage!!.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent))
uploadMessage = null
}
} else if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return
// Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
// Use RESULT_OK only if you're implementing WebView inside an Activity
val result = if (intent == null || resultCode != Activity.RESULT_OK) null else intent!!.getData()
mUploadMessage!!.onReceiveValue(result)
mUploadMessage = null
} else
Toast.makeText(context, "Failed to Upload Image", Toast.LENGTH_LONG).show()
}
}