Skip to content

Commit

Permalink
feat(android): implement webview_version (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Sep 5, 2022
1 parent 3624414 commit 9183de4
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changes/android-webview-version.md
@@ -0,0 +1,5 @@
---
"wry": patch
---

Implement `webview_version` on Android.
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -177,4 +177,7 @@ pub enum Error {
InvalidMethod(#[from] InvalidMethod),
#[error("Infallible error, something went really wrong: {0}")]
Infallible(#[from] std::convert::Infallible),
#[cfg(target_os = "android")]
#[error(transparent)]
JniError(#[from] tao::platform::android::ndk_glue::jni::errors::Error),
}
2 changes: 1 addition & 1 deletion src/webview/android/kotlin/PermissionHelper.kt
Expand Up @@ -78,7 +78,7 @@ object PermissionHelper {
var requestedPermissions: Array<String>? = null
try {
val pm = context.packageManager
val packageInfo = if (Build.VERSION.SDK_INT >= 33) {
val packageInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
pm.getPackageInfo(context.packageName, PackageManager.PackageInfoFlags.of(PackageManager.GET_PERMISSIONS.toLong()))
} else {
@Suppress("DEPRECATION")
Expand Down
4 changes: 2 additions & 2 deletions src/webview/android/kotlin/RustWebChromeClient.kt
Expand Up @@ -97,9 +97,9 @@ class RustWebChromeClient(appActivity: AppCompatActivity) : WebChromeClient() {
super.onHideCustomView()
}

@SuppressLint("ObsoleteSdkInt")
override fun onPermissionRequest(request: PermissionRequest) {
// val isRequestPermissionRequired = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
val isRequestPermissionRequired = true
val isRequestPermissionRequired = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
val permissionList: MutableList<String> = ArrayList()
if (listOf(*request.resources).contains("android.webkit.resource.VIDEO_CAPTURE")) {
permissionList.add(Manifest.permission.CAMERA)
Expand Down
2 changes: 2 additions & 0 deletions src/webview/android/kotlin/RustWebView.kt
Expand Up @@ -4,8 +4,10 @@

package {{app-domain-reversed}}.{{app-name-snake-case}}

import android.annotation.SuppressLint
import android.webkit.*
import android.content.Context
import android.os.Build

class RustWebView(context: Context): WebView(context) {
init {
Expand Down
39 changes: 38 additions & 1 deletion src/webview/android/kotlin/TauriActivity.kt
Expand Up @@ -4,11 +4,48 @@

package {{app-domain-reversed}}.{{app-name-snake-case}}

import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity

abstract class TauriActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {

val version: String
@SuppressLint("WebViewApiAvailability", "ObsoleteSdkInt")
get() {
// Check getCurrentWebViewPackage() directly if above Android 8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return WebView.getCurrentWebViewPackage()?.versionName ?: ""
}

// Otherwise manually check WebView versions
var webViewPackage = "com.google.android.webview"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
webViewPackage = "com.android.chrome"
}
try {
@Suppress("DEPRECATION")
val info = packageManager.getPackageInfo(webViewPackage, 0)
return info.versionName
} catch (ex: Exception) {
Logger.warn("Unable to get package info for '$webViewPackage'$ex");
}

try {
@Suppress("DEPRECATION")
val info = packageManager.getPackageInfo("com.android.webview", 0);
return info.versionName
} catch (ex: Exception) {
Logger.warn("Unable to get package info for 'com.android.webview'$ex");
}

// Could not detect any webview, return empty string
return "";
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
create(this)
}
Expand Down
15 changes: 14 additions & 1 deletion src/webview/android/main_pipe.rs
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use crate::webview::RGBA;
use crate::{webview::RGBA, Error};
use crossbeam_channel::*;
use once_cell::sync::Lazy;
use std::os::unix::prelude::*;
Expand Down Expand Up @@ -149,6 +149,18 @@ impl MainPipe<'_> {
set_background_color(env, webview.as_obj(), background_color)?;
}
}
WebViewMessage::GetWebViewVersion(tx) => {
match env
.call_method(activity, "getVersion", "()Ljava/lang/String;", &[])
.and_then(|v| v.l())
.and_then(|s| env.get_string(s.into()))
{
Ok(version) => {
tx.send(Ok(version.to_string_lossy().into())).unwrap();
}
Err(e) => tx.send(Err(e.into())).unwrap(),
}
}
}
}
Ok(())
Expand Down Expand Up @@ -181,6 +193,7 @@ pub enum WebViewMessage {
CreateWebView(CreateWebViewAttributes),
Eval(String),
SetBackgroundColor(RGBA),
GetWebViewVersion(Sender<Result<String, Error>>),
}

#[derive(Debug)]
Expand Down
5 changes: 4 additions & 1 deletion src/webview/android/mod.rs
Expand Up @@ -8,6 +8,7 @@ use crate::{
http::{header::HeaderValue, Request as HttpRequest, Response as HttpResponse},
Result,
};
use crossbeam_channel::*;
use html5ever::{interface::QualName, namespace_url, ns, tendril::TendrilSink, LocalName};
use kuchiki::NodeRef;
use once_cell::sync::OnceCell;
Expand Down Expand Up @@ -242,7 +243,9 @@ impl InnerWebView {
}

pub fn platform_webview_version() -> Result<String> {
todo!()
let (tx, rx) = bounded(1);
MainPipe::send(WebViewMessage::GetWebViewVersion(tx));
rx.recv().unwrap()
}

fn with_html_head<F: FnOnce(&NodeRef)>(document: &mut NodeRef, f: F) {
Expand Down

0 comments on commit 9183de4

Please sign in to comment.