diff --git a/app/src/main/java/com/gamedog/vididin/features/game/GameCenterActivity.kt b/app/src/main/java/com/gamedog/vididin/features/game/GameCenterActivity.kt index ed0b482..b6fa112 100644 --- a/app/src/main/java/com/gamedog/vididin/features/game/GameCenterActivity.kt +++ b/app/src/main/java/com/gamedog/vididin/features/game/GameCenterActivity.kt @@ -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(), 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(), 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(), } return false } - }*/ + } } @@ -138,6 +144,63 @@ class GameCenterActivity : AppViewsActivity(), //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 = ArrayList() + 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(), } -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() } }