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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package dev.nova.browser
|
| 2 |
+
|
| 3 |
+
import android.os.Bundle
|
| 4 |
+
import android.util.Log
|
| 5 |
+
import androidx.activity.ComponentActivity
|
| 6 |
+
import androidx.activity.OnBackPressedCallback
|
| 7 |
+
import androidx.activity.compose.setContent
|
| 8 |
+
import androidx.compose.foundation.layout.*
|
| 9 |
+
import androidx.compose.foundation.lazy.LazyRow
|
| 10 |
+
import androidx.compose.foundation.lazy.items
|
| 11 |
+
import androidx.compose.material3.*
|
| 12 |
+
import androidx.compose.runtime.*
|
| 13 |
+
import androidx.compose.ui.Modifier
|
| 14 |
+
import androidx.compose.ui.unit.dp
|
| 15 |
+
import androidx.compose.ui.viewinterop.AndroidView
|
| 16 |
+
import org.mozilla.geckoview.GeckoRuntime
|
| 17 |
+
import org.mozilla.geckoview.GeckoView
|
| 18 |
+
import org.mozilla.geckoview.WebExtension
|
| 19 |
+
|
| 20 |
+
class BrowserActivity : ComponentActivity() {
|
| 21 |
+
|
| 22 |
+
companion object {
|
| 23 |
+
// GeckoRuntime must be created exactly once per process (see GeckoView quick-start).
|
| 24 |
+
private var runtime: GeckoRuntime? = null
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
private lateinit var tabManager: TabManager
|
| 28 |
+
|
| 29 |
+
override fun onCreate(savedInstanceState: Bundle?) {
|
| 30 |
+
super.onCreate(savedInstanceState)
|
| 31 |
+
|
| 32 |
+
if (runtime == null) {
|
| 33 |
+
runtime = GeckoRuntime.create(this)
|
| 34 |
+
// Layer 2 of the dev console: desktop Firefox DevTools can attach
|
| 35 |
+
// via about:debugging over USB / Wi-Fi.
|
| 36 |
+
runtime!!.settings.setDebugSupportEnabled(true)
|
| 37 |
+
}
|
| 38 |
+
tabManager = TabManager(runtime!!)
|
| 39 |
+
|
| 40 |
+
installDevConsoleExtension()
|
| 41 |
+
|
| 42 |
+
// Hardware back button: navigate the page back before leaving the app.
|
| 43 |
+
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
|
| 44 |
+
override fun handleOnBackPressed() {
|
| 45 |
+
val session = tabManager.activeSession()
|
| 46 |
+
if (session != null && session.canGoBack()) session.goBack() else finish()
|
| 47 |
+
}
|
| 48 |
+
})
|
| 49 |
+
|
| 50 |
+
setContent { NovaUI() }
|
| 51 |
+
|
| 52 |
+
tabManager.open("https://example.com")
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
/**
|
| 56 |
+
* Installs the bundled dev-console extension (idempotent) and wires the
|
| 57 |
+
* background-script native messaging channel.
|
| 58 |
+
*/
|
| 59 |
+
private fun installDevConsoleExtension() {
|
| 60 |
+
val controller = runtime!!.webExtensionController
|
| 61 |
+
controller.ensureBuiltIn("resource://android/assets/devconsole/", "devconsole@nova")
|
| 62 |
+
.accept(
|
| 63 |
+
{ ext ->
|
| 64 |
+
Log.i("Nova", "Dev-console extension installed: ${ext?.id}")
|
| 65 |
+
ext?.let {
|
| 66 |
+
// Background script -> app channel.
|
| 67 |
+
it.setMessageDelegate(NovaBackgroundMessageDelegate(), "browser")
|
| 68 |
+
tabManager.wireExtension(it)
|
| 69 |
+
}
|
| 70 |
+
},
|
| 71 |
+
{ e -> Log.e("Nova", "Failed to install dev-console extension", e) }
|
| 72 |
+
)
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
/** Receives messages from the extension's background script. */
|
| 76 |
+
private inner class NovaBackgroundMessageDelegate : WebExtension.MessageDelegate {
|
| 77 |
+
override fun onMessage(
|
| 78 |
+
nativeApp: String,
|
| 79 |
+
message: Any,
|
| 80 |
+
sender: WebExtension.MessageSender
|
| 81 |
+
): GeckoResult<Any>? {
|
| 82 |
+
tabManager.recordConsoleMessage(message)
|
| 83 |
+
return null
|
| 84 |
+
}
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
// ------------------------------------------------------------------
|
| 88 |
+
// UI (Compose shell — M0 scope; toolbar/tabs polish are later milestones)
|
| 89 |
+
// ------------------------------------------------------------------
|
| 90 |
+
@Composable
|
| 91 |
+
private fun NovaUI() {
|
| 92 |
+
var urlInput by remember { mutableStateOf("https://example.com") }
|
| 93 |
+
val geckoView = remember { GeckoView(this) }
|
| 94 |
+
val activeId by tabManager.activeTabId
|
| 95 |
+
|
| 96 |
+
// Switch the engine view whenever the active tab changes.
|
| 97 |
+
LaunchedEffect(activeId) {
|
| 98 |
+
activeId?.let { id ->
|
| 99 |
+
tabManager.tabs.firstOrNull { it.id == id }?.let { geckoView.setSession(it.session) }
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
Column(Modifier.fillMaxSize()) {
|
| 104 |
+
OutlinedTextField(
|
| 105 |
+
value = urlInput,
|
| 106 |
+
onValueChange = { urlInput = it },
|
| 107 |
+
modifier = Modifier.fillMaxWidth().padding(horizontal = 8.dp, vertical = 4.dp),
|
| 108 |
+
label = { Text("Search or enter address") },
|
| 109 |
+
singleLine = true,
|
| 110 |
+
)
|
| 111 |
+
Row(Modifier.padding(horizontal = 8.dp)) {
|
| 112 |
+
TextButton(onClick = { tabManager.activeSession()?.goBack() }) { Text("◀") }
|
| 113 |
+
TextButton(onClick = { tabManager.activeSession()?.goForward() }) { Text("▶") }
|
| 114 |
+
TextButton(onClick = { tabManager.activeSession()?.reload() }) { Text("⟳") }
|
| 115 |
+
TextButton(onClick = { tabManager.activeSession()?.loadUri(normalize(urlInput)) }) { Text("Go") }
|
| 116 |
+
TextButton(onClick = { tabManager.open("about:blank") }) { Text("+") }
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
// Tab strip
|
| 120 |
+
LazyRow(Modifier.padding(horizontal = 8.dp)) {
|
| 121 |
+
items(tabManager.tabs, key = { it.id }) { tab ->
|
| 122 |
+
AssistChip(
|
| 123 |
+
onClick = { tabManager.activate(tab.id) },
|
| 124 |
+
label = {
|
| 125 |
+
Text(
|
| 126 |
+
"Tab ${tab.id}" + (if (tab.id == activeId) " ●" else ""),
|
| 127 |
+
maxLines = 1,
|
| 128 |
+
)
|
| 129 |
+
},
|
| 130 |
+
trailingIcon = {
|
| 131 |
+
TextButton(onClick = { tabManager.close(tab.id) }) { Text("✕") }
|
| 132 |
+
},
|
| 133 |
+
modifier = Modifier.padding(end = 4.dp),
|
| 134 |
+
)
|
| 135 |
+
}
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// Console output (native layer of the dev console — M4 grows this into a screen)
|
| 139 |
+
val lines = tabManager.consoleLines.takeLast(3)
|
| 140 |
+
if (lines.isNotEmpty()) {
|
| 141 |
+
Surface(
|
| 142 |
+
tonalElevation = 2.dp,
|
| 143 |
+
modifier = Modifier
|
| 144 |
+
.fillMaxWidth()
|
| 145 |
+
.heightIn(max = 72.dp),
|
| 146 |
+
) {
|
| 147 |
+
Column(Modifier.padding(6.dp)) {
|
| 148 |
+
lines.forEach { Text(it, style = MaterialTheme.typography.labelSmall, maxLines = 1) }
|
| 149 |
+
}
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
AndroidView(
|
| 154 |
+
factory = { geckoView },
|
| 155 |
+
modifier = Modifier.fillMaxWidth().weight(1f),
|
| 156 |
+
)
|
| 157 |
+
}
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
private fun normalize(input: String): String = when {
|
| 161 |
+
input.startsWith("http://") || input.startsWith("https://") -> input
|
| 162 |
+
input.startsWith("about:") -> input
|
| 163 |
+
input.contains(".") && !input.contains(" ") -> "https://$input"
|
| 164 |
+
else -> "https://duckduckgo.com/?q=" + android.net.Uri.encode(input)
|
| 165 |
+
}
|
| 166 |
+
}
|