HapeLee 1 год назад
Родитель
Сommit
3516af6a9f

+ 26 - 1
app/src/main/java/io/legato/kazusa/help/AppWebDav.kt

@@ -93,7 +93,7 @@ object AppWebDav {
     @Throws(WebDavException::class)
     private suspend fun checkAuthorization(authorization: Authorization) {
         if (!WebDav(rootWebDavUrl, authorization).check()) {
-            appCtx.removePref(PreferKey.webDavPassword)
+            //appCtx.removePref(PreferKey.webDavPassword)
             appCtx.toastOnUi(R.string.webdav_application_authorization_error)
             throw WebDavException(appCtx.getString(R.string.webdav_application_authorization_error))
         }
@@ -154,6 +154,31 @@ object AppWebDav {
         }
     }
 
+    suspend fun testWebDav(): Boolean {
+        return kotlin.runCatching {
+            val account = appCtx.getPrefString(PreferKey.webDavAccount)
+            val password = appCtx.getPrefString(PreferKey.webDavPassword)
+            if (account.isNullOrEmpty() || password.isNullOrEmpty()) {
+                appCtx.toastOnUi("账号或密码为空")
+                return false
+            }
+
+            val auth = Authorization(account, password)
+            checkAuthorization(auth)
+
+            appCtx.toastOnUi("WebDAV 服务正常")
+            true
+        }.getOrElse {
+            it.printStackTrace()
+            if (it !is WebDavException) {
+                appCtx.toastOnUi(it.message ?: "未知错误")
+            }
+            false
+        }
+    }
+
+
+
     /**
      * webDav备份
      * @param fileName 备份文件名

+ 47 - 23
app/src/main/java/io/legato/kazusa/ui/config/BackupConfigFragment.kt

@@ -1,24 +1,32 @@
 package io.legato.kazusa.ui.config
 
+import android.app.AlertDialog
 import android.content.Context
 import android.content.SharedPreferences
 import android.os.Bundle
 import android.text.InputType
+import android.view.LayoutInflater
 import android.view.Menu
 import android.view.MenuInflater
 import android.view.MenuItem
 import android.view.View
+import android.widget.EditText
 import androidx.core.view.MenuProvider
 import androidx.fragment.app.activityViewModels
 import androidx.lifecycle.lifecycleScope
 import androidx.preference.EditTextPreference
 import androidx.preference.ListPreference
 import androidx.preference.Preference
+import androidx.preference.PreferenceManager
+import com.google.android.material.dialog.MaterialAlertDialogBuilder
+import com.google.android.material.dialog.MaterialDialogs
 import io.legato.kazusa.R
 import io.legato.kazusa.constant.AppLog
 import io.legato.kazusa.constant.PreferKey
+import io.legato.kazusa.databinding.DialogWebdavAuthBinding
 import io.legato.kazusa.exception.NoStackTraceException
 import io.legato.kazusa.help.AppWebDav
+import io.legato.kazusa.help.AppWebDav.testWebDav
 import io.legato.kazusa.help.config.AppConfig
 import io.legato.kazusa.help.config.LocalConfig
 import io.legato.kazusa.help.coroutine.Coroutine
@@ -53,6 +61,7 @@ import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 import splitties.init.appCtx
 import kotlin.coroutines.coroutineContext
+import androidx.core.content.edit
 
 class BackupConfigFragment : PreferenceFragment(),
     SharedPreferences.OnSharedPreferenceChangeListener,
@@ -107,12 +116,6 @@ class BackupConfigFragment : PreferenceFragment(),
 
     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
         addPreferencesFromResource(R.xml.pref_config_backup)
-        findPreference<EditTextPreference>(PreferKey.webDavPassword)?.let {
-            it.setOnBindEditTextListener { editText ->
-                editText.inputType =
-                    InputType.TYPE_TEXT_VARIATION_PASSWORD or InputType.TYPE_CLASS_TEXT
-            }
-        }
         findPreference<EditTextPreference>(PreferKey.webDavDir)?.let {
             it.setOnBindEditTextListener { editText ->
                 editText.text = AppConfig.webDavDir?.toEditable()
@@ -124,8 +127,6 @@ class BackupConfigFragment : PreferenceFragment(),
             }
         }
         upPreferenceSummary(PreferKey.webDavUrl, getPrefString(PreferKey.webDavUrl))
-        upPreferenceSummary(PreferKey.webDavAccount, getPrefString(PreferKey.webDavAccount))
-        upPreferenceSummary(PreferKey.webDavPassword, getPrefString(PreferKey.webDavPassword))
         upPreferenceSummary(PreferKey.webDavDir, AppConfig.webDavDir)
         upPreferenceSummary(PreferKey.webDavDeviceName, AppConfig.webDavDeviceName)
         upPreferenceSummary(PreferKey.backupPath, getPrefString(PreferKey.backupPath))
@@ -149,7 +150,6 @@ class BackupConfigFragment : PreferenceFragment(),
 
     override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
         menuInflater.inflate(R.menu.backup_restore, menu)
-        menu.applyTint(requireContext())
     }
 
     override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
@@ -194,20 +194,6 @@ class BackupConfigFragment : PreferenceFragment(),
                     preference.summary = value
                 }
 
-            PreferKey.webDavAccount ->
-                if (value.isNullOrBlank()) {
-                    preference.summary = getString(R.string.web_dav_account_s)
-                } else {
-                    preference.summary = value
-                }
-
-            PreferKey.webDavPassword ->
-                if (value.isNullOrEmpty()) {
-                    preference.summary = getString(R.string.web_dav_pw_s)
-                } else {
-                    preference.summary = "*".repeat(value.length)
-                }
-
             PreferKey.webDavDir -> preference.summary = when (value) {
                 null -> "legado"
                 else -> value
@@ -229,6 +215,17 @@ class BackupConfigFragment : PreferenceFragment(),
         when (preference.key) {
             PreferKey.backupPath -> selectBackupPath.launch()
             PreferKey.restoreIgnore -> backupIgnore()
+            "web_dav_test" -> {
+                lifecycleScope.launch {
+                    testWebDav()
+                }
+            }
+            "web_dav_auth" -> {
+                showWebDavAuthDialog()
+                lifecycleScope.launch {
+                    testWebDav()
+                }
+            }
             "web_dav_backup" -> backup()
             "web_dav_restore" -> restore()
             "import_old" -> restoreOld.launch()
@@ -236,6 +233,33 @@ class BackupConfigFragment : PreferenceFragment(),
         return super.onPreferenceTreeClick(preference)
     }
 
+    private fun showWebDavAuthDialog() {
+        val binding = DialogWebdavAuthBinding.inflate(layoutInflater).apply {
+            editAccount.setText(PreferenceManager.getDefaultSharedPreferences(requireContext()).getString(PreferKey.webDavAccount, ""))
+            editPassword.setText(PreferenceManager.getDefaultSharedPreferences(requireContext()).getString(PreferKey.webDavPassword, ""))
+            editPassword.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
+        }
+
+        alert(titleResource = R.string.web_dav_account) {
+            customView { binding.root }
+
+            okButton {
+                val account = binding.editAccount.text.toString()
+                val password = binding.editPassword.text.toString()
+
+                PreferenceManager.getDefaultSharedPreferences(requireContext()).edit {
+                    putString(PreferKey.webDavAccount, account)
+                        .putString(PreferKey.webDavPassword, password)
+                }
+
+                upPreferenceSummary(PreferKey.webDavAccount, account)
+                upPreferenceSummary(PreferKey.webDavPassword, password)
+            }
+
+            cancelButton()
+        }
+    }
+
     /**
      * 备份忽略设置
      */

+ 1 - 0
app/src/main/java/io/legato/kazusa/ui/config/ConfigActivity.kt

@@ -17,6 +17,7 @@ class ConfigActivity : VMBaseActivity<ActivityConfigBinding, ConfigViewModel>()
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
+        setSupportActionBar(binding.topBar)
         when (val configTag = intent.getStringExtra("configTag")) {
             ConfigTag.OTHER_CONFIG -> replaceFragment<OtherConfigFragment>(configTag)
             ConfigTag.THEME_CONFIG -> replaceFragment<ThemeConfigFragment>(configTag)

+ 31 - 0
app/src/main/res/layout/dialog_webdav_auth.xml

@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:orientation="vertical"
+    android:padding="24dp"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content">
+    <com.google.android.material.textfield.TextInputLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginBottom="8dp">
+        <com.google.android.material.textfield.TextInputEditText
+            android:id="@+id/edit_account"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:hint="@string/web_dav_account" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+    <com.google.android.material.textfield.TextInputLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:endIconMode="password_toggle">
+        <com.google.android.material.textfield.TextInputEditText
+            android:id="@+id/edit_password"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:hint="@string/web_dav_pw"
+            android:inputType="textPassword" />
+    </com.google.android.material.textfield.TextInputLayout>
+
+</LinearLayout>

+ 0 - 5
app/src/main/res/values/themes.xml

@@ -8,11 +8,6 @@
         <item name="android:windowBackground">@android:color/transparent</item>
     </style>
 
-    <!-- Welcome主题 -->
-    <style name="AppTheme.Welcome" parent="Base.AppTheme">
-        <item name="android:windowNoTitle">true</item>
-    </style>
-
     <style name="Base.AppTheme" parent="Theme.Material3.DynamicColors.DayNight.NoActionBar">
         <item name="android:windowDrawsSystemBarBackgrounds">true</item>
         <item name="android:windowLightStatusBar">true</item>

+ 9 - 9
app/src/main/res/xml/pref_config_backup.xml

@@ -15,16 +15,10 @@
             android:summary="@string/web_dav_url_s"
             app:iconSpaceReserved="false" />
 
-        <io.legato.kazusa.lib.prefs.EditTextPreference
-            android:key="web_dav_account"
+        <io.legato.kazusa.lib.prefs.Preference
+            android:key="web_dav_auth"
             android:title="@string/web_dav_account"
-            android:summary="@string/web_dav_account_s"
-            app:iconSpaceReserved="false" />
-
-        <io.legato.kazusa.lib.prefs.EditTextPreference
-            android:key="web_dav_password"
-            android:title="@string/web_dav_pw"
-            android:summary="@string/web_dav_pw_s"
+            android:summary="设置 WebDAV 账号和密码"
             app:iconSpaceReserved="false" />
 
         <io.legato.kazusa.lib.prefs.EditTextPreference
@@ -41,6 +35,12 @@
             app:allowDividerBelow="false"
             app:iconSpaceReserved="false" />
 
+        <io.legato.kazusa.lib.prefs.Preference
+            android:key="web_dav_test"
+            android:title="测试 WebDav 配置"
+            android:summary="测试 WebDav 服务工作状态"
+            app:iconSpaceReserved="false"/>
+
         <io.legato.kazusa.lib.prefs.SwitchPreference
             android:key="syncBookProgress"
             android:defaultValue="true"