feat: additional clipboard method
This commit is contained in:
parent
c77338948e
commit
6f4b76353a
6 changed files with 38 additions and 10 deletions
|
@ -264,7 +264,7 @@ class FileColumn(val context: Context) {
|
||||||
f.mkdir()
|
f.mkdir()
|
||||||
}
|
}
|
||||||
ClipHelper.getInstance(context).copy(source, context)
|
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 {
|
val inputStream = try {
|
||||||
context.contentResolver.openInputStream(sourceUri)
|
context.contentResolver.openInputStream(sourceUri)
|
||||||
} catch (e: FileNotFoundException) {
|
} catch (e: FileNotFoundException) {
|
||||||
|
@ -278,7 +278,7 @@ class FileColumn(val context: Context) {
|
||||||
inputStream.close()
|
inputStream.close()
|
||||||
}
|
}
|
||||||
ClipHelper.getInstance(context).copy(target, context)
|
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)
|
val inputStream2 = context.contentResolver.openInputStream(targetUri)
|
||||||
if (inputStream2 != null) {
|
if (inputStream2 != null) {
|
||||||
val actualFile = File(f, target.name)
|
val actualFile = File(f, target.name)
|
||||||
|
@ -292,7 +292,7 @@ class FileColumn(val context: Context) {
|
||||||
).show()
|
).show()
|
||||||
} else if (source.isDirectory) {
|
} else if (source.isDirectory) {
|
||||||
ClipHelper.getInstance(context).copy(target, context)
|
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
|
dropTarget = null
|
||||||
}
|
}
|
||||||
val inputStream = try {
|
val inputStream = try {
|
||||||
|
@ -316,7 +316,7 @@ class FileColumn(val context: Context) {
|
||||||
} else if (target.isDirectory) {
|
} else if (target.isDirectory) {
|
||||||
if (source.isFile) {
|
if (source.isFile) {
|
||||||
ClipHelper.getInstance(context).copy(source, context)
|
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
|
dropTarget = null
|
||||||
}
|
}
|
||||||
val inputStream = try {
|
val inputStream = try {
|
||||||
|
@ -621,7 +621,7 @@ class FileColumn(val context: Context) {
|
||||||
onClick = {
|
onClick = {
|
||||||
AlertHelper.showOnlyPasteInfoNewAlert(context,
|
AlertHelper.showOnlyPasteInfoNewAlert(context,
|
||||||
onPaste = {
|
onPaste = {
|
||||||
val uri = ClipHelper.getInstance(context).paste()
|
val uri = ClipHelper.getInstance(context).paste(context)
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
val name = uri.path?.split('/')?.last() ?: "somePastedItem"
|
val name = uri.path?.split('/')?.last() ?: "somePastedItem"
|
||||||
val ext = name.split('.').last()
|
val ext = name.split('.').last()
|
||||||
|
|
|
@ -162,7 +162,7 @@ class document_page : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onPaste = {
|
onPaste = {
|
||||||
val uri = ClipHelper.getInstance(this).paste()
|
val uri = ClipHelper.getInstance(this).paste(this)
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
val dir = File(pasteDir)
|
val dir = File(pasteDir)
|
||||||
if (!dir.exists()) {
|
if (!dir.exists()) {
|
||||||
|
|
|
@ -143,7 +143,7 @@ class music_page : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onPaste = {
|
onPaste = {
|
||||||
val uri = ClipHelper.getInstance(this).paste()
|
val uri = ClipHelper.getInstance(this).paste(this)
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
val dir = File(pasteDir)
|
val dir = File(pasteDir)
|
||||||
if (!dir.exists()) {
|
if (!dir.exists()) {
|
||||||
|
|
|
@ -144,7 +144,7 @@ class picture_page : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onPaste = {
|
onPaste = {
|
||||||
val uri = ClipHelper.getInstance(this).paste()
|
val uri = ClipHelper.getInstance(this).paste(this)
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
val dir = File(pasteDir)
|
val dir = File(pasteDir)
|
||||||
if (!dir.exists()) {
|
if (!dir.exists()) {
|
||||||
|
|
|
@ -7,9 +7,16 @@ import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import com.dazuoye.filemanager.BuildConfig
|
import com.dazuoye.filemanager.BuildConfig
|
||||||
|
import com.dazuoye.filemanager.fileSystem.DeleteHelper.Companion.delete
|
||||||
import kotlinx.coroutines.InternalCoroutinesApi
|
import kotlinx.coroutines.InternalCoroutinesApi
|
||||||
import kotlinx.coroutines.internal.synchronized
|
import kotlinx.coroutines.internal.synchronized
|
||||||
|
import org.apache.commons.io.IOUtils
|
||||||
import java.io.File
|
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) {
|
class ClipHelper private constructor(context: Context) {
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -36,12 +43,33 @@ class ClipHelper private constructor(context: Context) {
|
||||||
)
|
)
|
||||||
val clip = ClipData.newUri(contentResolver, label, uri)
|
val clip = ClipData.newUri(contentResolver, label, uri)
|
||||||
clipboard.setPrimaryClip(clip)
|
clipboard.setPrimaryClip(clip)
|
||||||
|
|
||||||
|
// 备用的复制方法
|
||||||
|
val clipFile = File(context.cacheDir,"clipboard")
|
||||||
|
if (clipFile.exists()){
|
||||||
|
delete(clipFile.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun paste(): Uri? {
|
clipFile.createNewFile()
|
||||||
|
val fw = FileWriter(clipFile)
|
||||||
|
fw.write(uri.toString())
|
||||||
|
fw.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun paste(context: Context): Uri? {
|
||||||
val clip = clipboard.primaryClip
|
val clip = clipboard.primaryClip
|
||||||
clip?.run {
|
clip?.run {
|
||||||
val item: ClipData.Item = getItemAt(0)
|
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 item.uri
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
|
|
|
@ -143,7 +143,7 @@ class video_page : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onPaste = {
|
onPaste = {
|
||||||
val uri = ClipHelper.getInstance(this).paste()
|
val uri = ClipHelper.getInstance(this).paste(this)
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
val dir = File(pasteDir)
|
val dir = File(pasteDir)
|
||||||
if (!dir.exists()) {
|
if (!dir.exists()) {
|
||||||
|
|
Loading…
Reference in a new issue