处理game center为 本地webview加载后再跳转

This commit is contained in:
renhaoting 2026-01-12 14:31:17 +08:00
parent 1e487a4f8d
commit 5d49ce6a96
1 changed files with 69 additions and 5 deletions

View File

@ -1,6 +1,5 @@
package com.gamedog.vididin.features.game
import android.R.attr.scheme
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
@ -60,9 +59,9 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
with(webView) {
settings.javaScriptEnabled = true
webView.addJavascriptInterface(WebAppInterface(this@GameCenterActivity), "com.viddin.videos.free")
webView.addJavascriptInterface(WebAppInterface(this@GameCenterActivity, webView), "com.viddin.videos.free")
/*webViewClient = object : WebViewClient() {
webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest
@ -71,6 +70,13 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
if (!request.url.toString().startsWith("intent")) {
view?.loadUrl(url.toString())
return true
} else {
try {
handleIntentUrl(url.toString())
return true
} catch (e: Exception) {
e.printStackTrace()
}
}
}
return false
@ -85,7 +91,7 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
}
return false
}
}*/
}
}
@ -138,6 +144,63 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
//TODO("Not yet implemented")
}
private fun handleIntentUrl(url: String) {
var intent: Intent? = null
if (url.startsWith("intent")) {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME)
}
if (intent != null) {
if (isHw()) {
intent.setPackage(getDefaultBrowser())
}
intent.addCategory(Intent.CATEGORY_BROWSABLE)
intent.setComponent(null)
intent.setFlags(FLAG_ACTIVITY_NEW_TASK)
}
this.startActivity(intent)
}
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
}
companion object {
internal fun startActivity(activity: Activity) {
@ -148,7 +211,7 @@ class GameCenterActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(),
}
class WebAppInterface(private val context: Activity) {
class WebAppInterface(private val context: Activity, private val webView: WebView) {
@JavascriptInterface
fun openBrowser(url: String) {
try {
@ -168,6 +231,7 @@ class WebAppInterface(private val context: Activity) {
}
context.startActivity(intent)
} catch (e: Exception) {
e.printStackTrace()
}
}