feat: select directory to save last book cover image (#2521)

This commit is contained in:
Huang Xin 2025-11-24 00:00:49 +08:00 committed by GitHub
parent c792c18e01
commit 8f22a5c570
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
44 changed files with 306 additions and 39 deletions

View file

@ -8,6 +8,7 @@ import android.util.Log
import android.os.Build
import android.os.Environment
import android.provider.Settings
import android.provider.DocumentsContract
import android.view.View
import android.view.KeyEvent
import android.view.WindowInsets
@ -25,6 +26,8 @@ import androidx.core.content.FileProvider
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.browser.customtabs.CustomTabsIntent
import app.tauri.annotation.Command
import app.tauri.annotation.InvokeArg
@ -128,11 +131,16 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
}
companion object {
var pendingInvoke: Invoke? = null
private const val REQUEST_MANAGE_STORAGE = 1001
private const val FOLDER_PICKER_REQUEST_CODE = 1002
var pendingInvoke: Invoke? = null
var pendingFolderPickerInvoke: Invoke? = null
private var instance: NativeBridgePlugin? = null
fun getInstance(): NativeBridgePlugin? = instance
}
override fun load(webView: WebView) {
instance = this
super.load(webView)
handleIntent(activity.intent)
}
@ -682,4 +690,87 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
invoke.reject("Failed to open URL: ${e.message}")
}
}
@Command
fun select_directory(invoke: Invoke) {
pendingFolderPickerInvoke = invoke
try {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
activity.startActivityForResult(intent, FOLDER_PICKER_REQUEST_CODE)
} catch (e: Exception) {
val result = JSObject()
result.put("cancelled", true)
result.put("uri", null)
result.put("path", null)
result.put("error", e.message)
invoke.resolve(result)
pendingFolderPickerInvoke = null
}
}
fun handleActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == FOLDER_PICKER_REQUEST_CODE) {
val invoke = pendingFolderPickerInvoke
if (invoke != null) {
handleDirectorySelected(data?.data, invoke)
pendingFolderPickerInvoke = null
}
}
}
private fun handleDirectorySelected(uri: Uri?, invoke: Invoke) {
val result = JSObject()
if (uri == null) {
result.put("cancelled", true)
result.put("uri", null)
result.put("path", null)
} else {
try {
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
activity.contentResolver.takePersistableUriPermission(uri, flags)
result.put("cancelled", false)
result.put("uri", uri.toString())
result.put("path", extractPathFromUri(uri))
} catch (e: SecurityException) {
result.put("cancelled", true)
result.put("uri", uri.toString())
result.put("path", extractPathFromUri(uri))
result.put("error", "Permission error: ${e.message}")
} catch (e: Exception) {
result.put("cancelled", true)
result.put("uri", null)
result.put("path", null)
result.put("error", "Error: ${e.message}")
}
}
invoke.resolve(result)
pendingInvoke = null
}
private fun extractPathFromUri(uri: Uri): String? {
val path = uri.path ?: return null
return try {
when {
DocumentsContract.isTreeUri(uri) -> {
val treeDocId = DocumentsContract.getTreeDocumentId(uri)
val split = treeDocId.split(":")
if (split[0].equals("primary", ignoreCase = true)) {
if (split.size > 1) {
Environment.getExternalStorageDirectory().path + "/" + split[1]
} else {
Environment.getExternalStorageDirectory().path
}
} else {
"/storage/${split[0]}/" + (if (split.size > 1) split[1] else "")
}
}
else -> null
}
} catch (e: Exception) {
path
}
}
}