mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
Ladybird/Android: Add EditText for URL bar and attach to native WebView
This commit is contained in:
parent
a93507231c
commit
4fd915b005
Notes:
sideshowbarker
2024-07-17 18:06:52 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/4fd915b005 Pull-request: https://github.com/SerenityOS/serenity/pull/21214 Reviewed-by: https://github.com/AtkinsSJ Reviewed-by: https://github.com/bugaevc
16
Ladybird/Android/src/main/cpp/JNIHelpers.cpp
Normal file
16
Ladybird/Android/src/main/cpp/JNIHelpers.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "JNIHelpers.h"
|
||||
#include <AK/Utf16View.h>
|
||||
|
||||
namespace Ladybird {
|
||||
jstring JavaEnvironment::jstring_from_ak_string(String const& str)
|
||||
{
|
||||
auto as_utf16 = MUST(AK::utf8_to_utf16(str.code_points()));
|
||||
return m_env->NewString(as_utf16.data(), as_utf16.size());
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/String.h>
|
||||
#include <jni.h>
|
||||
|
||||
namespace Ladybird {
|
||||
@ -37,6 +38,8 @@ public:
|
||||
|
||||
JNIEnv* get() const { return m_env; }
|
||||
|
||||
jstring jstring_from_ak_string(String const& str);
|
||||
|
||||
private:
|
||||
JavaVM* m_vm = nullptr;
|
||||
JNIEnv* m_env = nullptr;
|
||||
|
@ -34,6 +34,13 @@ WebViewImplementationNative::WebViewImplementationNative(jobject thiz)
|
||||
JavaEnvironment env(global_vm);
|
||||
env.get()->CallVoidMethod(m_java_instance, invalidate_layout_method);
|
||||
};
|
||||
|
||||
on_load_start = [this](AK::URL const& url, bool is_redirect) {
|
||||
JavaEnvironment env(global_vm);
|
||||
auto url_string = env.jstring_from_ak_string(MUST(url.to_string()));
|
||||
env.get()->CallVoidMethod(m_java_instance, on_load_start_method, url_string, is_redirect);
|
||||
env.get()->DeleteLocalRef(url_string);
|
||||
};
|
||||
}
|
||||
|
||||
void WebViewImplementationNative::create_client(WebView::EnableCallgrindProfiling)
|
||||
|
@ -32,6 +32,7 @@ public:
|
||||
static jclass global_class_reference;
|
||||
static jmethodID bind_webcontent_method;
|
||||
static jmethodID invalidate_layout_method;
|
||||
static jmethodID on_load_start_method;
|
||||
|
||||
jobject java_instance() const { return m_java_instance; }
|
||||
|
||||
|
@ -12,6 +12,7 @@ using namespace Ladybird;
|
||||
jclass WebViewImplementationNative::global_class_reference;
|
||||
jmethodID WebViewImplementationNative::bind_webcontent_method;
|
||||
jmethodID WebViewImplementationNative::invalidate_layout_method;
|
||||
jmethodID WebViewImplementationNative::on_load_start_method;
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
Java_org_serenityos_ladybird_WebViewImplementation_00024Companion_nativeClassInit(JNIEnv* env, jobject /* thiz */)
|
||||
@ -31,6 +32,11 @@ Java_org_serenityos_ladybird_WebViewImplementation_00024Companion_nativeClassIni
|
||||
if (!method)
|
||||
TODO();
|
||||
WebViewImplementationNative::invalidate_layout_method = method;
|
||||
|
||||
method = env->GetMethodID(WebViewImplementationNative::global_class_reference, "onLoadStart", "(Ljava/lang/String;Z)V");
|
||||
if (!method)
|
||||
TODO();
|
||||
WebViewImplementationNative::on_load_start_method = method;
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT jlong JNICALL
|
||||
|
@ -8,15 +8,18 @@ package org.serenityos.ladybird
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.view.KeyEvent
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import org.serenityos.ladybird.databinding.ActivityMainBinding
|
||||
import java.net.URL
|
||||
import kotlin.io.path.Path
|
||||
|
||||
class LadybirdActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var binding: ActivityMainBinding
|
||||
private lateinit var resourceDir: String
|
||||
private lateinit var view: WebView
|
||||
private lateinit var urlEditText: EditText
|
||||
private var timerService = TimerExecutorService()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@ -28,16 +31,23 @@ class LadybirdActivity : AppCompatActivity() {
|
||||
binding = ActivityMainBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
setSupportActionBar(binding.toolbar)
|
||||
urlEditText = binding.urlEditText
|
||||
view = binding.webView
|
||||
view.onLoadStart = { url: String, _ ->
|
||||
urlEditText.setText(url, TextView.BufferType.EDITABLE)
|
||||
}
|
||||
urlEditText.setOnEditorActionListener { textView: TextView, actionId: Int, _: KeyEvent? ->
|
||||
when (actionId) {
|
||||
EditorInfo.IME_ACTION_GO, EditorInfo.IME_ACTION_SEARCH -> view.loadURL(textView.text.toString())
|
||||
}
|
||||
false
|
||||
}
|
||||
view.initialize(resourceDir)
|
||||
view.loadURL(intent.dataString ?: "https://ladybird.dev")
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
|
||||
// FIXME: This is not the right place to load the homepage :^)
|
||||
val initialURL = URL("https://ladybird.dev")
|
||||
view.loadURL(initialURL)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
@ -52,7 +62,10 @@ class LadybirdActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
private external fun initNativeCode(resourceDir: String, tag: String, timerService: TimerExecutorService)
|
||||
private external fun initNativeCode(
|
||||
resourceDir: String, tag: String, timerService: TimerExecutorService
|
||||
)
|
||||
|
||||
private external fun disposeNativeCode()
|
||||
private external fun execMainEventLoop()
|
||||
|
||||
|
@ -11,12 +11,12 @@ import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.util.AttributeSet
|
||||
import android.view.View
|
||||
import java.net.URL
|
||||
|
||||
// FIXME: This should (eventually) implement NestedScrollingChild3 and ScrollingView
|
||||
class WebView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {
|
||||
private val viewImpl = WebViewImplementation(this)
|
||||
private lateinit var contentBitmap: Bitmap
|
||||
var onLoadStart: (url: String, isRedirect: Boolean) -> Unit = { _, _ -> }
|
||||
|
||||
fun initialize(resourceDir: String) {
|
||||
viewImpl.initialize(resourceDir)
|
||||
@ -26,7 +26,7 @@ class WebView(context: Context, attributeSet: AttributeSet) : View(context, attr
|
||||
viewImpl.dispose()
|
||||
}
|
||||
|
||||
fun loadURL(url: URL) {
|
||||
fun loadURL(url: String) {
|
||||
viewImpl.loadURL(url)
|
||||
}
|
||||
|
||||
@ -47,4 +47,5 @@ class WebView(context: Context, attributeSet: AttributeSet) : View(context, attr
|
||||
viewImpl.drawIntoBitmap(contentBitmap);
|
||||
canvas?.drawBitmap(contentBitmap, 0f, 0f, null)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -33,8 +33,8 @@ class WebViewImplementation(private val view: WebView) {
|
||||
nativeInstance = 0
|
||||
}
|
||||
|
||||
fun loadURL(url: URL) {
|
||||
nativeLoadURL(nativeInstance, url.toString())
|
||||
fun loadURL(url: String) {
|
||||
nativeLoadURL(nativeInstance, url)
|
||||
}
|
||||
|
||||
fun drawIntoBitmap(bitmap: Bitmap) {
|
||||
@ -70,6 +70,10 @@ class WebViewImplementation(private val view: WebView) {
|
||||
view.invalidate()
|
||||
}
|
||||
|
||||
fun onLoadStart(url: String, isRedirect: Boolean) {
|
||||
view.onLoadStart(url, isRedirect)
|
||||
}
|
||||
|
||||
// Functions implemented in native code
|
||||
private external fun nativeObjectInit(): Long
|
||||
private external fun nativeObjectDispose(instance: Long)
|
||||
|
@ -16,7 +16,20 @@
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:layout_scrollFlags="scroll|snap|enterAlways"/>
|
||||
app:layout_scrollFlags="scroll|snap|enterAlways">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/urlEditText"
|
||||
style="@style/Widget.AppCompat.EditText"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:autofillHints="url"
|
||||
android:ems="10"
|
||||
android:hint="@string/url_edit_default"
|
||||
android:imeOptions="actionGo|actionSearch"
|
||||
android:inputType="textUri"
|
||||
android:singleLine="true" />
|
||||
</androidx.appcompat.widget.Toolbar>
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
||||
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||
|
@ -12,6 +12,7 @@
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="android:editTextBackground">@color/grey</item>
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowNoTitle">true</item>
|
||||
</style>
|
||||
|
@ -7,4 +7,5 @@
|
||||
<color name="teal_700">#FF018786</color>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="grey">#FF6B6B6B</color>
|
||||
</resources>
|
||||
|
@ -1,3 +1,4 @@
|
||||
<resources>
|
||||
<string name="app_name">Ladybird</string>
|
||||
<string name="url_edit_default">Enter URL...</string>
|
||||
</resources>
|
||||
|
@ -12,6 +12,7 @@
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
|
||||
<!-- Customize your theme here. -->
|
||||
<item name="android:editTextBackground">@color/white</item>
|
||||
<item name="windowActionBar">false</item>
|
||||
<item name="windowNoTitle">true</item>
|
||||
</style>
|
||||
|
@ -161,6 +161,7 @@ elseif(ANDROID)
|
||||
Android/src/main/cpp/WebViewImplementationNativeJNI.cpp
|
||||
Android/src/main/cpp/ALooperEventLoopImplementation.cpp
|
||||
Android/src/main/cpp/TimerExecutorService.cpp
|
||||
Android/src/main/cpp/JNIHelpers.cpp
|
||||
)
|
||||
target_link_libraries(ladybird PRIVATE LibArchive jnigraphics android)
|
||||
else()
|
||||
|
@ -57,6 +57,7 @@ else()
|
||||
../Android/src/main/cpp/WebContentService.cpp
|
||||
../Android/src/main/cpp/WebContentServiceJNI.cpp
|
||||
../Android/src/main/cpp/LadybirdServiceBaseJNI.cpp
|
||||
../Android/src/main/cpp/JNIHelpers.cpp
|
||||
)
|
||||
target_link_libraries(webcontent PRIVATE android)
|
||||
endif()
|
||||
|
Loading…
Reference in New Issue
Block a user