From 236bb140f8f4dce68b6e28dacfe6c7cf39075bc7 Mon Sep 17 00:00:00 2001 From: moreoronce Date: Mon, 7 Oct 2024 13:29:12 +0800 Subject: [PATCH] Add Functionality and Fix Issues in WebViewActivity - Implemented draggable FloatingActionButton with click functionality. - Resolved touch conflict between touch and click events on FloatingActionButton. - Improved WebView reload logic and ensured UI components are not obstructed. - Updated XML layouts for better constraint alignment and visual consistency. - Enhanced logging for user actions on the refresh button. Ensures a smoother user experience and UI operation. --- .gitignore | 3 +- app/build.gradle.kts | 2 + app/src/main/AndroidManifest.xml | 2 +- .../java/com/example/lobchat/MainActivity.kt | 20 ++--- .../com/example/lobchat/WebViewActivity.kt | 79 ++++++++++++++----- app/src/main/res/drawable/ic_refresh.xml | 11 +++ app/src/main/res/layout/activity_main.xml | 6 +- app/src/main/res/layout/activity_web_view.xml | 34 +++++--- app/src/main/res/values/colors.xml | 3 + app/src/main/res/values/strings.xml | 2 +- app/src/main/res/values/styles.xml | 5 -- app/src/main/res/values/themes.xml | 14 +++- 12 files changed, 126 insertions(+), 55 deletions(-) create mode 100644 app/src/main/res/drawable/ic_refresh.xml diff --git a/.gitignore b/.gitignore index 49463a0..fa05a99 100644 --- a/.gitignore +++ b/.gitignore @@ -15,5 +15,4 @@ local.properties /jdks /android /.tmp -/kotlin-profile - +/kotlin-profile \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 8716c94..aefa2db 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -67,6 +67,8 @@ dependencies { implementation(libs.androidx.constraintlayout) implementation("androidx.activity:activity-ktx:1.9.2") implementation(libs.material) + implementation("androidx.browser:browser:1.3.0") + testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7ae3f0f..9dcf757 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,7 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" - android:theme="@style/Theme.WebViewApp"> + android:theme="@style/Theme.WebViewApp"> >? = null private lateinit var fileChooserLauncher: ActivityResultLauncher private var isFinalUrlLoaded = false - // 用于标记是否通过 LobeChat 验证 private var isLobeChatVerified = false + private lateinit var refreshButton: FloatingActionButton - + @SuppressLint("ClickableViewAccessibility") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_web_view) + refreshButton = findViewById(R.id.refreshButton) // 初始化 WebView webView = findViewById(R.id.webView) + setupWebView() + // 刷新按钮的点击事件 + refreshButton.bringToFront() + refreshButton.setOnClickListener { + Log.i("WebViewActivity", "Refresh button clicked") + webView.reload() + } + + // 设置拖动事件监听器 + refreshButton.setOnTouchListener(DraggableTouchListener()) + // 初始化文件选择器启动器 fileChooserLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> handleFileChooserResult(result) @@ -49,29 +60,30 @@ class WebViewActivity : AppCompatActivity() { } } - // 增加处理Android返回键逻辑 + // 处理返回键 override fun onBackPressed() { if (webView.canGoBack()) { - // 如果WebView能够返回上一页 webView.goBack() } else { - // 否则调用super以处理默认的后退行为 super.onBackPressed() } } - private fun setupWebView() { val webSettings: WebSettings = webView.settings webSettings.javaScriptEnabled = true webSettings.domStorageEnabled = true - webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK + webSettings.cacheMode = WebSettings.LOAD_DEFAULT // Use default caching strategy + + // Other optimizations + webSettings.databaseEnabled = true + webSettings.mixedContentMode = WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE + + webSettings.userAgentString = "Mozilla/5.0 (Linux; Android 10; Pixel 3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Mobile Safari/537.36" webView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { - // 当 URL 变化时重置 isFinalUrlLoaded 标志 isFinalUrlLoaded = false - // 返回 false 表示允许 WebView 处理请求 return false } @@ -88,12 +100,10 @@ class WebViewActivity : AppCompatActivity() { """.trimIndent() ) { result -> if (result == "true") { - // 验证通过,不再继续验证 isLobeChatVerified = true Toast.makeText(this@WebViewActivity, "LobeChat validation passed", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this@WebViewActivity, "The webpage does not contain the required LobeChat text in the header.", Toast.LENGTH_LONG).show() - // 如果验证失败,返回 MainActivity val intent = Intent(this@WebViewActivity, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) startActivity(intent) @@ -145,4 +155,37 @@ class WebViewActivity : AppCompatActivity() { private fun showSnackbar(message: String) { Snackbar.make(findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG).show() } + + private inner class DraggableTouchListener : View.OnTouchListener { + + private var dX = 0f + private var dY = 0f + private var isClick = false + + override fun onTouch(view: View, event: MotionEvent): Boolean { + when (event.action) { + MotionEvent.ACTION_DOWN -> { + dX = view.x - event.rawX + dY = view.y - event.rawY + isClick = true + } + MotionEvent.ACTION_MOVE -> { + view.animate() + .x(event.rawX + dX) + .y(event.rawY + dY) + .setDuration(0) + .start() + isClick = false + } + MotionEvent.ACTION_UP -> { + if (isClick) { + // 处理点击事件 + Log.i("WebViewActivity", "Refresh button clicked") + webView.reload() + } + } + } + return true + } + } } diff --git a/app/src/main/res/drawable/ic_refresh.xml b/app/src/main/res/drawable/ic_refresh.xml new file mode 100644 index 0000000..47def81 --- /dev/null +++ b/app/src/main/res/drawable/ic_refresh.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0006c88..df10046 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -6,7 +6,9 @@ + android:layout_height="match_parent" + android:fitsSystemWindows="true"> + diff --git a/app/src/main/res/layout/activity_web_view.xml b/app/src/main/res/layout/activity_web_view.xml index d5e5890..8a71909 100644 --- a/app/src/main/res/layout/activity_web_view.xml +++ b/app/src/main/res/layout/activity_web_view.xml @@ -1,15 +1,31 @@ + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:fitsSystemWindows="true" + tools:context=".WebViewActivity"> + + + + android:id="@+id/webView" + android:layout_width="match_parent" + android:layout_height="match_parent" /> - \ No newline at end of file + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 5dff9d5..bd9a5fb 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -675,4 +675,7 @@ #484a4d #1b1c1d #111111 + + #00000000 + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 880fd74..9b8086e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,5 +1,5 @@ - lobchat + LobeChat Enter your server URL Send App Logo diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 58bc6fa..e5f8fdc 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,7 +1,2 @@ - - \ No newline at end of file diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index dae0348..88b3f8a 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -1,5 +1,15 @@ - - \ No newline at end of file