Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -20,8 +22,10 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.key
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -46,75 +50,63 @@ class ComposeWebActivity : ComponentActivity() {
val context = LocalContext.current
val webView = remember(context) { WebView(context) }
val customWebView = remember(context) { CustomWebView(context) }
val geckoView = remember(context) { GeckoView(context) }
val customGeckoView = remember(context) { CustomGeckoView(context) }

Column(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.verticalScroll(rememberScrollState())
) {
Text(
text = "android.webkit.WebView",
fontSize = 16.sp,
modifier = Modifier
.background(Color.Yellow)
.align(Alignment.CenterHorizontally)
)
WebViewItem(
url = "https://www.google.com",
webView = webView,
Column(
modifier = Modifier
.fillMaxWidth()
.height(450.dp)
)
.weight(1f)
.verticalScroll(rememberScrollState())
) {
Text(
text = "android.webkit.WebView",
fontSize = 16.sp,
modifier = Modifier
.background(Color.Yellow)
.align(Alignment.CenterHorizontally)
)
WebViewItem(
url = "https://www.google.com",
webView = webView,
modifier = Modifier
.fillMaxWidth()
.height(450.dp)
)

Text(
text = "CustomWebView",
fontSize = 16.sp,
modifier = Modifier
.background(Color.Yellow)
.align(Alignment.CenterHorizontally)
)
WebViewItem(
url = "https://www.google.com",
webView = customWebView,
modifier = Modifier
.fillMaxWidth()
.height(450.dp)
)
Text(
text = "CustomWebView",
fontSize = 16.sp,
modifier = Modifier
.background(Color.Yellow)
.align(Alignment.CenterHorizontally)
)
WebViewItem(
url = "https://www.google.com",
webView = customWebView,
modifier = Modifier
.fillMaxWidth()
.height(450.dp)
)
}

Text(
text = "org.mozilla.geckoview.GeckoView",
fontSize = 16.sp,
modifier = Modifier
.align(Alignment.CenterHorizontally)
.background(Color.Yellow)
.padding(top = 8.dp)
)
GeckoViewItem(
LazyGeckoViewItem(
label = "org.mozilla.geckoview.GeckoView (device)",
url = "https://www.google.com",
geckoView = geckoView,
geckoViewFactory = { GeckoView(it) },
modifier = Modifier
.fillMaxWidth()
.height(450.dp)
.height(200.dp)
)

Text(
text = "CustomGeckoView",
fontSize = 16.sp,
modifier = Modifier
.align(Alignment.CenterHorizontally)
.background(Color.Yellow)
.padding(top = 8.dp)
)
GeckoViewItem(
LazyGeckoViewItem(
label = "CustomGeckoView (device)",
url = "https://www.google.com",
geckoView = customGeckoView,
geckoViewFactory = { CustomGeckoView(it) },
modifier = Modifier
.fillMaxWidth()
.height(450.dp)
.height(200.dp)
)
}
}
Expand Down Expand Up @@ -152,36 +144,52 @@ fun WebViewItem(url: String, webView: WebView, modifier: Modifier = Modifier) {
}

@Composable
fun GeckoViewItem(url: String, geckoView: GeckoView, modifier: Modifier = Modifier) {
val context = LocalContext.current
val runtime = remember {
GeckoRuntime.getDefault(context.applicationContext)
}
val session = remember(runtime) {
GeckoSession().apply {
setContentDelegate(object : ContentDelegate {})
open(runtime)
fun LazyGeckoViewItem(
label: String,
url: String,
geckoViewFactory: (android.content.Context) -> GeckoView,
modifier: Modifier = Modifier
) {
var loaded by remember { mutableStateOf(false) }

Text(
text = if (loaded) label else "Tap to load $label",
fontSize = 16.sp,
modifier = Modifier
.background(Color.Yellow)
.padding(top = 8.dp)
.then(if (!loaded) Modifier.clickable { loaded = true } else Modifier)
)

if (loaded) {
val context = LocalContext.current
val geckoView = remember(context) { geckoViewFactory(context) }
val runtime = remember { GeckoRuntime.getDefault(context.applicationContext) }
val session = remember(runtime) {
GeckoSession().apply {
setContentDelegate(object : ContentDelegate {})
open(runtime)
}
}
}

DisposableEffect(session) {
onDispose {
session.close()
DisposableEffect(session) {
onDispose { session.close() }
}
}

key(geckoView) {
AndroidView(
modifier = modifier,
factory = { _ ->
geckoView.apply {
setSession(session)
}
}
factory = { _ -> geckoView.apply { setSession(session) } }
)
}

LaunchedEffect(url) {
session.loadUri(url)
LaunchedEffect(url) { session.loadUri(url) }
} else {
Box(
modifier = modifier
.background(Color.LightGray)
.clickable { loaded = true },
contentAlignment = Alignment.Center
) {
Text("Tap to load", color = Color.DarkGray)
}
}
}
1 change: 1 addition & 0 deletions e2e/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.AndroidObservability"
android:usesCleartextTraffic="true"
android:extractNativeLibs="true"
tools:targetApi="31" >
<activity
android:name=".masking.XMLUserFormActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.FrameLayout
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.annotation.IdRes
import com.example.androidobservability.R
Expand All @@ -22,8 +24,19 @@ class XMLWebActivity : ComponentActivity() {

setupWebView(R.id.webview)
setupWebView(R.id.customWebView)
setupGeckoWebView(R.id.geckoview)
setupGeckoWebView(R.id.customGeckoView)

setupLazyGeckoView(
labelId = R.id.geckoLabel,
containerId = R.id.geckoContainer,
factory = { GeckoView(this) },
label = "org.mozilla.geckoview.GeckoView (device)"
)
setupLazyGeckoView(
labelId = R.id.customGeckoLabel,
containerId = R.id.customGeckoContainer,
factory = { CustomGeckoView(this) },
label = "CustomGeckoView (device)"
)
}

@SuppressLint("SetJavaScriptEnabled")
Expand All @@ -34,16 +47,41 @@ class XMLWebActivity : ComponentActivity() {
webView.loadUrl(url)
}

private fun setupGeckoWebView(@IdRes geckoViewId: Int) {
val view = findViewById<GeckoView?>(geckoViewId)
val session = GeckoSession()
private fun setupLazyGeckoView(
@IdRes labelId: Int,
@IdRes containerId: Int,
factory: () -> GeckoView,
label: String
) {
val labelView = findViewById<TextView>(labelId)
val container = findViewById<FrameLayout>(containerId)

session.setContentDelegate(object : ContentDelegate {})
labelView.setOnClickListener { loadGeckoView(container, factory(), label, labelView) }
container.setOnClickListener { loadGeckoView(container, factory(), label, labelView) }
}

GeckoRuntime.getDefault(application).let {
session.open(it)
}
view?.setSession(session)
private fun loadGeckoView(
container: FrameLayout,
geckoView: GeckoView,
label: String,
labelView: TextView
) {
container.setOnClickListener(null)
labelView.setOnClickListener(null)
labelView.text = label

val session = GeckoSession()
session.setContentDelegate(object : ContentDelegate {})
GeckoRuntime.getDefault(application).let { session.open(it) }
geckoView.setSession(session)
session.loadUri(url)

container.addView(
geckoView,
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
)
)
}
}
Loading
Loading