From 6f4b76353ae75673c1ed104d4fbc746cbe5ca0e0 Mon Sep 17 00:00:00 2001 From: Kagura Date: Sun, 20 Oct 2024 16:18:39 +0800 Subject: [PATCH] feat: additional clipboard method --- .../filemanager/compose/ui/FileColumn.kt | 10 +++---- .../com/dazuoye/filemanager/document_page.kt | 2 +- .../com/dazuoye/filemanager/music_page.kt | 2 +- .../com/dazuoye/filemanager/picture_page.kt | 2 +- .../dazuoye/filemanager/utils/ClipHelper.kt | 30 ++++++++++++++++++- .../com/dazuoye/filemanager/video_page.kt | 2 +- 6 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/dazuoye/filemanager/compose/ui/FileColumn.kt b/app/src/main/java/com/dazuoye/filemanager/compose/ui/FileColumn.kt index aba2ec0..ff84452 100644 --- a/app/src/main/java/com/dazuoye/filemanager/compose/ui/FileColumn.kt +++ b/app/src/main/java/com/dazuoye/filemanager/compose/ui/FileColumn.kt @@ -264,7 +264,7 @@ class FileColumn(val context: Context) { f.mkdir() } ClipHelper.getInstance(context).copy(source, context) - val sourceUri = ClipHelper.getInstance(context).paste() ?: return false + val sourceUri = ClipHelper.getInstance(context).paste(context) ?: return false val inputStream = try { context.contentResolver.openInputStream(sourceUri) } catch (e: FileNotFoundException) { @@ -278,7 +278,7 @@ class FileColumn(val context: Context) { inputStream.close() } ClipHelper.getInstance(context).copy(target, context) - val targetUri = ClipHelper.getInstance(context).paste() ?: return false + val targetUri = ClipHelper.getInstance(context).paste(context) ?: return false val inputStream2 = context.contentResolver.openInputStream(targetUri) if (inputStream2 != null) { val actualFile = File(f, target.name) @@ -292,7 +292,7 @@ class FileColumn(val context: Context) { ).show() } else if (source.isDirectory) { ClipHelper.getInstance(context).copy(target, context) - val sourceUri = ClipHelper.getInstance(context).paste() ?: return false.also { + val sourceUri = ClipHelper.getInstance(context).paste(context) ?: return false.also { dropTarget = null } val inputStream = try { @@ -316,7 +316,7 @@ class FileColumn(val context: Context) { } else if (target.isDirectory) { if (source.isFile) { ClipHelper.getInstance(context).copy(source, context) - val sourceUri = ClipHelper.getInstance(context).paste() ?: return false.also { + val sourceUri = ClipHelper.getInstance(context).paste(context) ?: return false.also { dropTarget = null } val inputStream = try { @@ -621,7 +621,7 @@ class FileColumn(val context: Context) { onClick = { AlertHelper.showOnlyPasteInfoNewAlert(context, onPaste = { - val uri = ClipHelper.getInstance(context).paste() + val uri = ClipHelper.getInstance(context).paste(context) if (uri != null) { val name = uri.path?.split('/')?.last() ?: "somePastedItem" val ext = name.split('.').last() diff --git a/app/src/main/java/com/dazuoye/filemanager/document_page.kt b/app/src/main/java/com/dazuoye/filemanager/document_page.kt index 765f64a..2ace693 100644 --- a/app/src/main/java/com/dazuoye/filemanager/document_page.kt +++ b/app/src/main/java/com/dazuoye/filemanager/document_page.kt @@ -162,7 +162,7 @@ class document_page : AppCompatActivity() { } }, onPaste = { - val uri = ClipHelper.getInstance(this).paste() + val uri = ClipHelper.getInstance(this).paste(this) if (uri != null) { val dir = File(pasteDir) if (!dir.exists()) { diff --git a/app/src/main/java/com/dazuoye/filemanager/music_page.kt b/app/src/main/java/com/dazuoye/filemanager/music_page.kt index e9ff0f4..15eabab 100644 --- a/app/src/main/java/com/dazuoye/filemanager/music_page.kt +++ b/app/src/main/java/com/dazuoye/filemanager/music_page.kt @@ -143,7 +143,7 @@ class music_page : AppCompatActivity() { } }, onPaste = { - val uri = ClipHelper.getInstance(this).paste() + val uri = ClipHelper.getInstance(this).paste(this) if (uri != null) { val dir = File(pasteDir) if (!dir.exists()) { diff --git a/app/src/main/java/com/dazuoye/filemanager/picture_page.kt b/app/src/main/java/com/dazuoye/filemanager/picture_page.kt index 681ee04..01d8ddc 100644 --- a/app/src/main/java/com/dazuoye/filemanager/picture_page.kt +++ b/app/src/main/java/com/dazuoye/filemanager/picture_page.kt @@ -144,7 +144,7 @@ class picture_page : AppCompatActivity() { } }, onPaste = { - val uri = ClipHelper.getInstance(this).paste() + val uri = ClipHelper.getInstance(this).paste(this) if (uri != null) { val dir = File(pasteDir) if (!dir.exists()) { diff --git a/app/src/main/java/com/dazuoye/filemanager/utils/ClipHelper.kt b/app/src/main/java/com/dazuoye/filemanager/utils/ClipHelper.kt index c3e0a50..ff76be3 100644 --- a/app/src/main/java/com/dazuoye/filemanager/utils/ClipHelper.kt +++ b/app/src/main/java/com/dazuoye/filemanager/utils/ClipHelper.kt @@ -7,9 +7,16 @@ import android.content.Context import android.net.Uri import androidx.core.content.FileProvider import com.dazuoye.filemanager.BuildConfig +import com.dazuoye.filemanager.fileSystem.DeleteHelper.Companion.delete import kotlinx.coroutines.InternalCoroutinesApi import kotlinx.coroutines.internal.synchronized +import org.apache.commons.io.IOUtils import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.FileWriter +import java.io.InputStream +import java.nio.charset.CharsetDecoder class ClipHelper private constructor(context: Context) { companion object { @@ -36,12 +43,33 @@ class ClipHelper private constructor(context: Context) { ) val clip = ClipData.newUri(contentResolver, label, uri) clipboard.setPrimaryClip(clip) + + // 备用的复制方法 + val clipFile = File(context.cacheDir,"clipboard") + if (clipFile.exists()){ + delete(clipFile.path) + } + + clipFile.createNewFile() + val fw = FileWriter(clipFile) + fw.write(uri.toString()) + fw.close() } - fun paste(): Uri? { + fun paste(context: Context): Uri? { val clip = clipboard.primaryClip clip?.run { val item: ClipData.Item = getItemAt(0) + if (item.uri == null){ + // 备用粘贴方式 + val clipFile = File(context.cacheDir,"clipboard") + if (clipFile.isFile()){ + val input = FileInputStream(clipFile) + val content = IOUtils.toString(input,"UTF-8") + return Uri.parse(content) + } + } + return item.uri } return null diff --git a/app/src/main/java/com/dazuoye/filemanager/video_page.kt b/app/src/main/java/com/dazuoye/filemanager/video_page.kt index 3ba5407..cc0f22a 100644 --- a/app/src/main/java/com/dazuoye/filemanager/video_page.kt +++ b/app/src/main/java/com/dazuoye/filemanager/video_page.kt @@ -143,7 +143,7 @@ class video_page : AppCompatActivity() { } }, onPaste = { - val uri = ClipHelper.getInstance(this).paste() + val uri = ClipHelper.getInstance(this).paste(this) if (uri != null) { val dir = File(pasteDir) if (!dir.exists()) {