mirror of
https://github.com/ev-map/EVMap.git
synced 2026-04-20 22:28:21 -04:00
Rework openUrl function
- use preferBrowser only when needed (when opening charger URLs, which might otherwise open in EVMap itself) - make preferBrowser work even if the default browser does not support Custom Tabs #313
This commit is contained in:
@@ -25,6 +25,10 @@
|
|||||||
<intent>
|
<intent>
|
||||||
<action android:name="android.support.customtabs.action.CustomTabsService" />
|
<action android:name="android.support.customtabs.action.CustomTabsService" />
|
||||||
</intent>
|
</intent>
|
||||||
|
<intent>
|
||||||
|
<action android:name="android.intent.action.VIEW" />
|
||||||
|
<data android:scheme="http" />
|
||||||
|
</intent>
|
||||||
|
|
||||||
<package android:name="com.google.android.projection.gearhead" />
|
<package android:name="com.google.android.projection.gearhead" />
|
||||||
<package android:name="com.google.android.apps.automotive.templates.host" />
|
<package android:name="com.google.android.apps.automotive.templates.host" />
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package net.vonforst.evmap
|
|||||||
import android.app.PendingIntent
|
import android.app.PendingIntent
|
||||||
import android.content.ActivityNotFoundException
|
import android.content.ActivityNotFoundException
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.content.pm.ResolveInfo
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
@@ -11,7 +13,6 @@ import android.view.View
|
|||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.browser.customtabs.CustomTabColorSchemeParams
|
import androidx.browser.customtabs.CustomTabColorSchemeParams
|
||||||
import androidx.browser.customtabs.CustomTabsClient
|
|
||||||
import androidx.browser.customtabs.CustomTabsIntent
|
import androidx.browser.customtabs.CustomTabsIntent
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.splashscreen.SplashScreen
|
import androidx.core.splashscreen.SplashScreen
|
||||||
@@ -271,8 +272,7 @@ class MapsActivity : AppCompatActivity(),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun openUrl(url: String, preferBrowser: Boolean = true) {
|
fun openUrl(url: String, preferBrowser: Boolean = false) {
|
||||||
val pkg = CustomTabsClient.getPackageName(this, null)
|
|
||||||
val intent = CustomTabsIntent.Builder()
|
val intent = CustomTabsIntent.Builder()
|
||||||
.setDefaultColorSchemeParams(
|
.setDefaultColorSchemeParams(
|
||||||
CustomTabColorSchemeParams.Builder()
|
CustomTabColorSchemeParams.Builder()
|
||||||
@@ -280,13 +280,46 @@ class MapsActivity : AppCompatActivity(),
|
|||||||
.build()
|
.build()
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
pkg?.let {
|
|
||||||
// prefer to open URL in custom tab, even if native app
|
val uri = Uri.parse(url)
|
||||||
// available (such as EVMap itself)
|
val viewIntent = Intent(Intent.ACTION_VIEW, uri)
|
||||||
if (preferBrowser) intent.intent.setPackage(pkg)
|
if (preferBrowser) {
|
||||||
|
// EVMap may be set as default app for this link, but we want to open it in a browser
|
||||||
|
// try to find default web browser
|
||||||
|
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
|
||||||
|
val resolveInfo =
|
||||||
|
packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||||
|
val pkg = resolveInfo?.activityInfo?.packageName.takeIf { it != "android" }
|
||||||
|
if (pkg == null) {
|
||||||
|
// There is no default browser, fall back to app chooser
|
||||||
|
val chooserIntent = Intent.createChooser(viewIntent, null).apply {
|
||||||
|
putExtra(Intent.EXTRA_EXCLUDE_COMPONENTS, arrayOf(componentName))
|
||||||
|
}
|
||||||
|
val targets: List<ResolveInfo> = packageManager.queryIntentActivities(
|
||||||
|
viewIntent,
|
||||||
|
PackageManager.MATCH_DEFAULT_ONLY
|
||||||
|
)
|
||||||
|
|
||||||
|
// add missing browsers (if EVMap is already set as default, Android might not find other browsers with the specific intent)
|
||||||
|
val browsers = packageManager.queryIntentActivities(
|
||||||
|
browserIntent,
|
||||||
|
PackageManager.MATCH_DEFAULT_ONLY
|
||||||
|
)
|
||||||
|
val extraIntents = browsers.filter { browser ->
|
||||||
|
targets.find { it.activityInfo.packageName == browser.activityInfo.packageName } == null
|
||||||
|
}.map { browser ->
|
||||||
|
Intent(Intent.ACTION_VIEW, uri).apply {
|
||||||
|
setPackage(browser.activityInfo.packageName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toTypedArray())
|
||||||
|
startActivity(chooserIntent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
intent.intent.setPackage(pkg)
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
intent.launchUrl(this, Uri.parse(url))
|
intent.launchUrl(this, uri)
|
||||||
} catch (e: ActivityNotFoundException) {
|
} catch (e: ActivityNotFoundException) {
|
||||||
val cb = fragmentCallback ?: return
|
val cb = fragmentCallback ?: return
|
||||||
Snackbar.make(
|
Snackbar.make(
|
||||||
|
|||||||
@@ -407,7 +407,7 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
|
|||||||
binding.detailView.sourceButton.setOnClickListener {
|
binding.detailView.sourceButton.setOnClickListener {
|
||||||
val charger = vm.charger.value?.data
|
val charger = vm.charger.value?.data
|
||||||
if (charger != null) {
|
if (charger != null) {
|
||||||
(activity as? MapsActivity)?.openUrl(charger.url)
|
(activity as? MapsActivity)?.openUrl(charger.url, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
binding.detailView.btnChargeprice.setOnClickListener {
|
binding.detailView.btnChargeprice.setOnClickListener {
|
||||||
@@ -420,7 +420,7 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
|
|||||||
extras
|
extras
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
(activity as? MapsActivity)?.openUrl(ChargepriceApi.getPoiUrl(charger), false)
|
(activity as? MapsActivity)?.openUrl(ChargepriceApi.getPoiUrl(charger))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
binding.detailView.btnChargerWebsite.setOnClickListener {
|
binding.detailView.btnChargerWebsite.setOnClickListener {
|
||||||
@@ -473,7 +473,7 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
|
|||||||
R.id.menu_edit -> {
|
R.id.menu_edit -> {
|
||||||
val charger = vm.charger.value?.data
|
val charger = vm.charger.value?.data
|
||||||
if (charger?.editUrl != null) {
|
if (charger?.editUrl != null) {
|
||||||
(activity as? MapsActivity)?.openUrl(charger.editUrl)
|
(activity as? MapsActivity)?.openUrl(charger.editUrl, true)
|
||||||
if (vm.apiId.value == "goingelectric") {
|
if (vm.apiId.value == "goingelectric") {
|
||||||
// instructions specific to GoingElectric
|
// instructions specific to GoingElectric
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
@@ -827,7 +827,7 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
|
|||||||
(activity as? MapsActivity)?.showLocation(charger)
|
(activity as? MapsActivity)?.showLocation(charger)
|
||||||
}
|
}
|
||||||
R.drawable.ic_fault_report -> {
|
R.drawable.ic_fault_report -> {
|
||||||
(activity as? MapsActivity)?.openUrl(charger.url)
|
(activity as? MapsActivity)?.openUrl(charger.url, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
R.drawable.ic_payment -> {
|
R.drawable.ic_payment -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user