-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from hotwired/webview-info
Provide the WebView system information in an easy to access way.
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
core/src/main/kotlin/dev/hotwire/core/turbo/webview/WebViewInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package dev.hotwire.core.turbo.webview | ||
|
||
import android.content.Context | ||
import android.content.pm.PackageInfo | ||
import android.net.Uri | ||
import android.webkit.WebSettings | ||
import androidx.webkit.WebViewCompat | ||
|
||
private const val PACKAGE_SYSTEM_WEBVIEW = "com.google.android.webview" | ||
private const val PACKAGE_CHROME_WEBVIEW = "com.android.chrome" | ||
private const val PACKAGE_CHROME_PRE_RELEASE_WEBVIEW = "com.chrome" | ||
|
||
class WebViewInfo internal constructor(context: Context) { | ||
enum class WebViewType { | ||
ANDROID_SYSTEM, | ||
CHROME, | ||
UNKNOWN | ||
} | ||
|
||
/** | ||
* The system WebView's package info (corresponds to Chrome or Android System WebView). | ||
*/ | ||
val packageInfo: PackageInfo? = WebViewCompat.getCurrentWebViewPackage(context) | ||
|
||
/** | ||
* The system WebView's major version (corresponds to Chrome or Android System WebView). | ||
* Returns null if the major version cannot be determined. | ||
*/ | ||
val majorVersion: Int? = packageInfo?.versionName?.substringBefore(".")?.toIntOrNull() | ||
|
||
/** | ||
* The default User-Agent provided by the system WebView before a call is made | ||
* to WebView.settings.setUserAgentString(String). | ||
*/ | ||
val defaultUserAgent: String = WebSettings.getDefaultUserAgent(context) | ||
|
||
/** | ||
* The system WebView's origin type. Different OS versions have the WebView component | ||
* backed by either Google Chrome or the Android System WebView component. These are updatable | ||
* through the Play Store. | ||
*/ | ||
val webViewType = when { | ||
packageInfo?.packageName?.contains(PACKAGE_CHROME_WEBVIEW) == true -> WebViewType.CHROME | ||
packageInfo?.packageName?.contains(PACKAGE_CHROME_PRE_RELEASE_WEBVIEW) == true -> WebViewType.CHROME | ||
packageInfo?.packageName?.contains(PACKAGE_SYSTEM_WEBVIEW) == true -> WebViewType.ANDROID_SYSTEM | ||
else -> WebViewType.UNKNOWN | ||
} | ||
|
||
/** | ||
* The system WebView's origin type as a human readable string. | ||
*/ | ||
val webViewTypeName = when (webViewType) { | ||
WebViewType.ANDROID_SYSTEM -> "Android System WebView" | ||
WebViewType.CHROME -> "Google Chrome" | ||
WebViewType.UNKNOWN -> "Unknown" | ||
} | ||
|
||
/** | ||
* The Play Store app Uri for the system WebView type. This is useful to point users to the | ||
* Play Store if their WebView version is outdated. | ||
*/ | ||
val playStoreWebViewAppUri = when (webViewType) { | ||
WebViewType.ANDROID_SYSTEM -> Uri.parse("market://details?id=${PACKAGE_SYSTEM_WEBVIEW}") | ||
WebViewType.CHROME -> Uri.parse("market://details?id=${PACKAGE_CHROME_WEBVIEW}") | ||
else -> null | ||
} | ||
} |