Răsfoiți Sursa

feat:添加验证码对话框

Xwite 4 ani în urmă
părinte
comite
4249898c31

+ 7 - 0
app/src/main/AndroidManifest.xml

@@ -359,6 +359,13 @@
                 <data android:scheme="yuedu" />
             </intent-filter>
         </activity>
+        <!-- 验证码输入 -->
+        <activity
+            android:name=".ui.association.VerificationCodeActivity"
+            android:configChanges="locale|keyboardHidden|orientation|screenSize|smallestScreenSize|screenLayout"
+            android:exported="true"
+            android:theme="@style/AppTheme.Transparent">
+        </activity>
         <!-- 打开文件 -->
         <activity
             android:name=".ui.association.FileAssociationActivity"

+ 4 - 0
app/src/main/assets/help/jsHelp.md

@@ -73,6 +73,10 @@ java.startBrowser(url: String, title: String)
 java.log(msg)
 java.logType(var)
 ```
+* 获取用户输入的验证码
+```
+java.getVerificationCode(imageUrl)
+```
 * 弹窗提示
 ```
 java.longToast(msg: Any?)

+ 4 - 0
app/src/main/assets/updateLog.md

@@ -11,6 +11,10 @@
 * 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。
 * 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源!
 
+**2022/05/04**
+
+* js添加 getVerificationCode
+
 **2022/05/02**
 
 * 优化强调色和文字颜色一样的情况下一些文字的显示

+ 30 - 0
app/src/main/java/io/legado/app/help/JsExtensions.kt

@@ -15,11 +15,13 @@ import io.legado.app.help.http.*
 import io.legado.app.model.Debug
 import io.legado.app.model.analyzeRule.AnalyzeUrl
 import io.legado.app.model.analyzeRule.QueryTTF
+import io.legado.app.ui.association.VerificationCodeActivity
 import io.legado.app.ui.browser.WebViewActivity
 import io.legado.app.utils.*
 import kotlinx.coroutines.Dispatchers.IO
 import kotlinx.coroutines.async
 import kotlinx.coroutines.runBlocking
+import kotlinx.coroutines.withContext
 import org.jsoup.Connection
 import org.jsoup.Jsoup
 import splitties.init.appCtx
@@ -145,6 +147,34 @@ interface JsExtensions {
         }
     }
 
+    /**
+     * 打开验证码对话框,等待输入验证码
+     */
+    fun getVerificationCode(imageUrl: String): String {
+        return runBlocking {
+            val key = "${getSource()?.getKey() ?: ""}_verificationCode"
+            CacheManager.delete(key)
+            appCtx.startActivity<VerificationCodeActivity> {
+                putExtra("imageUrl", imageUrl)
+                putExtra("sourceOrigin", getSource()?.getKey())
+            }
+            var waitUserInput: Boolean = false
+            while(CacheManager.get(key) == null) {
+                if (!waitUserInput) {
+                    log("等待输入验证码...")
+                    waitUserInput = true
+                }
+            }
+            CacheManager.get(key)!!.let {
+                if (it.isBlank()) {
+                    throw NoStackTraceException("未输入验证码或者验证码为空")
+                } else {
+                   it
+                }
+            }
+       }
+    }
+
     /**
      * 可从网络,本地文件(阅读私有缓存目录和书籍保存位置支持相对路径)导入JavaScript脚本
      */

+ 26 - 0
app/src/main/java/io/legado/app/ui/association/VerificationCodeActivity.kt

@@ -0,0 +1,26 @@
+package io.legado.app.ui.association
+
+import android.os.Bundle
+import io.legado.app.base.BaseActivity
+import io.legado.app.databinding.ActivityTranslucenceBinding
+import io.legado.app.utils.showDialogFragment
+import io.legado.app.utils.viewbindingdelegate.viewBinding
+
+/**
+ * 验证码
+ */
+class VerificationCodeActivity :
+    BaseActivity<ActivityTranslucenceBinding>() {
+
+    override val binding by viewBinding(ActivityTranslucenceBinding::inflate)
+
+    override fun onActivityCreated(savedInstanceState: Bundle?) {
+        intent.getStringExtra("imageUrl")?.let {
+            val sourceOrigin = intent.getStringExtra("sourceOrigin")
+            showDialogFragment(
+                VerificationCodeDialog(it, sourceOrigin)
+            )
+        } ?: finish()
+    }
+
+}

+ 83 - 0
app/src/main/java/io/legado/app/ui/association/VerificationCodeDialog.kt

@@ -0,0 +1,83 @@
+package io.legado.app.ui.association
+
+import android.os.Bundle
+import android.view.View
+import android.view.ViewGroup
+import android.net.Uri
+import com.bumptech.glide.request.RequestOptions
+import io.legado.app.R
+import io.legado.app.base.BaseDialogFragment
+import io.legado.app.databinding.DialogVerificationCodeViewBinding
+import io.legado.app.help.CacheManager
+import io.legado.app.help.glide.ImageLoader
+import io.legado.app.help.glide.OkHttpModelLoader
+import io.legado.app.lib.theme.primaryColor
+import io.legado.app.ui.widget.dialog.PhotoDialog
+import io.legado.app.utils.setLayout
+import io.legado.app.utils.showDialogFragment
+import io.legado.app.utils.viewbindingdelegate.viewBinding
+
+//图片验证码输入对话框
+class VerificationCodeDialog() : BaseDialogFragment(R.layout.dialog_verification_code_view) {
+
+    constructor(imageUrl: String, sourceOrigin: String? = null) : this() {
+        arguments = Bundle().apply {
+            putString("sourceOrigin", sourceOrigin)
+            putString("imageUrl", imageUrl)
+        }
+    }
+
+    val binding by viewBinding(DialogVerificationCodeViewBinding::bind)
+
+    override fun onStart() {
+        super.onStart()
+        setLayout(1f, ViewGroup.LayoutParams.WRAP_CONTENT)
+    }
+
+    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
+        binding.run {
+            toolBar.setBackgroundColor(primaryColor)
+            val sourceOrigin = arguments?.getString("sourceOrigin")
+            val key = "${sourceOrigin ?: ""}_verificationCode"
+            arguments?.getString("imageUrl")?.let { imageUrl ->
+                ImageLoader.load(requireContext(), imageUrl).apply {
+                    sourceOrigin?.let {
+                        apply(
+                            RequestOptions().set(
+                                OkHttpModelLoader.sourceOriginOption,
+                                it
+                            )
+                        )
+                    }
+                }.error(R.drawable.image_loading_error)
+                    .into(ivImage)
+                ivImage.setOnClickListener {
+                    showDialogFragment(PhotoDialog(imageUrl, sourceOrigin))
+                }
+                ivImage.setOnLongClickListener {
+                    showDialogFragment(PhotoDialog(imageUrl, sourceOrigin))
+                    true
+                }
+            }
+            tvOk.setOnClickListener {
+                val verificationCode = binding.verificationCode.text.toString()
+                verificationCode?.let {
+                    CacheManager.put(key, it)
+                    dismiss()
+                }
+            }
+            tvCancel.setOnClickListener {
+                dismiss()
+            }
+        }
+    }
+
+    override fun onDestroy() {
+        val sourceOrigin = arguments?.getString("sourceOrigin")
+        val key = "${sourceOrigin ?: ""}_verificationCode"
+        CacheManager.get(key) ?: CacheManager.put(key, "")
+        super.onDestroy()
+        activity?.finish()
+    }
+
+}

+ 70 - 0
app/src/main/res/layout/dialog_verification_code_view.xml

@@ -0,0 +1,70 @@
+<?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"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical">
+
+    <androidx.appcompat.widget.Toolbar
+        android:id="@+id/tool_bar"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:theme="?attr/actionBarStyle"
+        app:title="@string/input_verification_code"
+        app:popupTheme="@style/AppTheme.PopupOverlay"
+        app:titleTextAppearance="@style/ToolbarTitle" />
+
+    <androidx.appcompat.widget.AppCompatImageView
+        android:id="@+id/iv_image"
+        android:scaleType="fitXY"
+        android:layout_width="match_parent"
+        android:layout_height="50dp"
+        android:paddingLeft="12dp"
+        android:paddingRight="12dp"
+        android:paddingTop="3dp"
+        android:background="?android:attr/selectableItemBackgroundBorderless"
+        tools:ignore="UnusedAttribute" />
+
+    <io.legado.app.ui.widget.text.TextInputLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingLeft="12dp"
+        android:paddingRight="12dp"
+        android:paddingTop="3dp">
+
+        <io.legado.app.lib.theme.view.ThemeEditText
+            android:id="@+id/verification_code"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:hint="@string/verification_code"
+            tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />
+
+    </io.legado.app.ui.widget.text.TextInputLayout>
+
+            
+    <com.google.android.flexbox.FlexboxLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingLeft="12dp"
+        android:paddingRight="12dp"
+        app:justifyContent="flex_end">
+
+        <io.legado.app.ui.widget.text.AccentTextView
+            android:id="@+id/tv_cancel"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:padding="12dp"
+            android:text="@string/cancel"
+            tools:ignore="RtlHardcoded" />
+
+        <io.legado.app.ui.widget.text.AccentTextView
+            android:id="@+id/tv_ok"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:padding="12dp"
+            android:text="@string/ok"
+            tools:ignore="RtlHardcoded" />
+
+    </com.google.android.flexbox.FlexboxLayout>
+</LinearLayout>

+ 2 - 0
app/src/main/res/values-es-rES/strings.xml

@@ -969,5 +969,7 @@
     <string name="change_source_batch">批量换源</string>
     <string name="book_type_different">书籍类型不一样</string>
     <string name="soure_change_source">是否确认换源</string>
+    <string name="input_verification_code">输入验证码</string>
+    <string name="verification_code">验证码</string>
     <!-- string end -->
 </resources>

+ 2 - 0
app/src/main/res/values-ja-rJP/strings.xml

@@ -972,5 +972,7 @@
     <string name="change_source_batch">批量换源</string>
     <string name="book_type_different">书籍类型不一样</string>
     <string name="soure_change_source">是否确认换源</string>
+    <string name="input_verification_code">输入验证码</string>
+    <string name="verification_code">验证码</string>
     <!-- string end -->
 </resources>

+ 2 - 0
app/src/main/res/values-pt-rBR/strings.xml

@@ -972,5 +972,7 @@
     <string name="change_source_batch">批量换源</string>
     <string name="book_type_different">书籍类型不一样</string>
     <string name="soure_change_source">是否确认换源</string>
+    <string name="input_verification_code">输入验证码</string>
+    <string name="verification_code">验证码</string>
     <!-- string end -->
 </resources>

+ 2 - 0
app/src/main/res/values-zh-rHK/strings.xml

@@ -969,5 +969,7 @@
     <string name="change_source_batch">批量换源</string>
     <string name="book_type_different">书籍类型不一样</string>
     <string name="soure_change_source">是否确认换源</string>
+    <string name="input_verification_code">输入验证码</string>
+    <string name="verification_code">验证码</string>
     <!-- string end -->
 </resources>

+ 2 - 0
app/src/main/res/values-zh-rTW/strings.xml

@@ -971,5 +971,7 @@
     <string name="change_source_batch">批量换源</string>
     <string name="book_type_different">书籍类型不一样</string>
     <string name="soure_change_source">是否确认换源</string>
+    <string name="input_verification_code">输入验证码</string>
+    <string name="verification_code">验证码</string>
     <!-- string end -->
 </resources>

+ 2 - 0
app/src/main/res/values-zh/strings.xml

@@ -971,5 +971,7 @@
     <string name="change_source_batch">批量换源</string>
     <string name="book_type_different">书籍类型不一样</string>
     <string name="soure_change_source">是否确认换源</string>
+    <string name="input_verification_code">输入验证码</string>
+    <string name="verification_code">验证码</string>
     <!-- string end -->
 </resources>

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -972,5 +972,7 @@
     <string name="change_source_batch">批量换源</string>
     <string name="book_type_different">书籍类型不一样</string>
     <string name="soure_change_source">是否确认换源</string>
+    <string name="input_verification_code">输入验证码</string>
+    <string name="verification_code">验证码</string>
     <!-- string end -->
 </resources>