js 交互接口定义
This commit is contained in:
parent
eb85079523
commit
e0938dac56
|
|
@ -2,14 +2,22 @@ package com.gamedog.vididin.features.game
|
|||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.view.LayoutInflater
|
||||
import android.webkit.JavascriptInterface
|
||||
import android.webkit.WebResourceRequest
|
||||
import android.webkit.WebView
|
||||
import android.webkit.WebViewClient
|
||||
import androidx.activity.addCallback
|
||||
import androidx.activity.viewModels
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import com.ama.core.architecture.BaseApp
|
||||
import com.ama.core.architecture.appBase.AppViewsActivity
|
||||
import com.ama.core.architecture.util.AndroidUtil
|
||||
import com.gamedog.vididin.VidiConst
|
||||
|
|
@ -44,12 +52,13 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
|
|||
if (webView.canGoBack()) {
|
||||
webView.goBack()
|
||||
} else {
|
||||
super.onBackPressed()
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
with(webView) {
|
||||
settings.javaScriptEnabled = true
|
||||
webView.addJavascriptInterface(WebAppInterface(this@GameCenterActivity), "com.viddin.videos.free")
|
||||
|
||||
webViewClient = object : WebViewClient() {
|
||||
override fun shouldOverrideUrlLoading(
|
||||
|
|
@ -58,8 +67,9 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
|
|||
): Boolean {
|
||||
request.url?.let { url ->
|
||||
view?.loadUrl(url.toString())
|
||||
return true
|
||||
}
|
||||
return true
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -67,8 +77,9 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
|
|||
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
|
||||
if (!url.isNullOrBlank()) {
|
||||
view?.loadUrl(url)
|
||||
return true
|
||||
}
|
||||
return true
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -128,4 +139,82 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class WebAppInterface(private val context: Activity) {
|
||||
@JavascriptInterface
|
||||
fun openBrowser(url: String) {
|
||||
try {
|
||||
var intent: Intent? = null
|
||||
if (url.startsWith("intent")) {
|
||||
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME)
|
||||
} else {
|
||||
intent = Intent("android.intent.action.VIEW", Uri.parse(url))
|
||||
}
|
||||
if (intent != null) {
|
||||
if (isHw()) {
|
||||
intent.setPackage(getDefaultBrowser())
|
||||
}
|
||||
intent.addCategory(Intent.CATEGORY_BROWSABLE)
|
||||
intent.setComponent(null)
|
||||
intent.setFlags(FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
context.startActivity(intent)
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
//点击互动广告关闭按钮时调用该方法。
|
||||
@JavascriptInterface
|
||||
fun close() {
|
||||
//TODO close the ad page or activity.
|
||||
}
|
||||
|
||||
//接入Gspace-Fully广告时需实现此方法。该方法用于打开新的Webview页面。
|
||||
@JavascriptInterface
|
||||
fun openWebview(url: String?) {
|
||||
//TODO open a new page to display landingpage.
|
||||
//TODO 使用展示GSpace的Activity新实例打开当前url.
|
||||
}
|
||||
|
||||
fun isHw(): Boolean {
|
||||
return "huawei".equals(Build.MANUFACTURER, ignoreCase = true)
|
||||
}
|
||||
|
||||
fun getDefaultBrowser(): String? {
|
||||
var packageName: String? = null
|
||||
var systemApp: String? = null
|
||||
var userApp: String? = null
|
||||
val userAppList: MutableList<String?> = ArrayList<String?>()
|
||||
val context: Context = BaseApp.appContext()
|
||||
val browserIntent = Intent("android.intent.action.VIEW", Uri.parse("https://"))
|
||||
val resolveInfo = context.getPackageManager()
|
||||
.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||
if (resolveInfo != null && resolveInfo.activityInfo != null) {
|
||||
packageName = resolveInfo.activityInfo.packageName
|
||||
}
|
||||
if (packageName == null || packageName == "android") {
|
||||
val lists = context.getPackageManager().queryIntentActivities(browserIntent, 0)
|
||||
for (app in lists) {
|
||||
if ((app.activityInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0) {
|
||||
systemApp = app.activityInfo.packageName
|
||||
} else {
|
||||
userApp = app.activityInfo.packageName
|
||||
userAppList.add(userApp)
|
||||
}
|
||||
}
|
||||
if (userAppList.contains("com.android.chrome")) {
|
||||
packageName = "com.android.chrome"
|
||||
} else {
|
||||
if (systemApp != null) {
|
||||
packageName = systemApp
|
||||
}
|
||||
if (userApp != null) {
|
||||
packageName = userApp
|
||||
}
|
||||
}
|
||||
}
|
||||
return packageName
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue