Skip to content

Commit

Permalink
Allow importing from ClipData
Browse files Browse the repository at this point in the history
  • Loading branch information
hardcore-sushi committed Jun 3, 2024
1 parent d1e042c commit bd60e62
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 37 deletions.
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ android {

dependencies {
implementation project(":libpdfviewer:app")
implementation 'androidx.core:core-ktx:1.12.0'
implementation "androidx.appcompat:appcompat:1.6.1"
implementation 'androidx.core:core-ktx:1.13.1'
implementation "androidx.appcompat:appcompat:1.7.0"
implementation "androidx.constraintlayout:constraintlayout:2.1.4"
def lifecycle_version = "2.6.2"
def lifecycle_version = "2.8.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"

implementation "androidx.sqlite:sqlite-ktx:2.3.1"
implementation "androidx.sqlite:sqlite-ktx:2.4.0"
implementation "androidx.preference:preference-ktx:1.2.1"
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"
implementation 'com.google.android.material:material:1.9.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'com.github.bumptech.glide:glide:4.16.0'
implementation "androidx.biometric:biometric-ktx:1.2.0-alpha05"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ open class BaseExplorerActivity : BaseActivity(), ExplorerElementAdapter.Listene
.show()
}

protected fun createNewFile(callback: (Long) -> Unit) {
EditTextDialog(this, R.string.enter_file_name) {
if (it.isEmpty()) {
Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show()
createNewFile(callback)
} else {
val filePath = PathUtils.pathJoin(currentDirectoryPath, it)
val handleID = encryptedVolume.openFileWriteMode(filePath)
if (handleID == -1L) {
CustomAlertDialogBuilder(this, theme)
.setTitle(R.string.error)
.setMessage(R.string.file_creation_failed)
.setPositiveButton(R.string.ok, null)
.show()
} else {
callback(handleID)
}
}
}.show()
}

private fun setVolumeNameTitle() {
titleText.text = getString(R.string.volume, volumeName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,11 @@ class ExplorerActivity : BaseExplorerActivity() {
pickImportDirectory.launch(null)
}
"createFile" -> {
EditTextDialog(this, R.string.enter_file_name) {
createNewFile(it)
}.show()
createNewFile {
encryptedVolume.closeFile(it)
setCurrentPath(currentDirectoryPath)
invalidateOptionsMenu()
}
}
"createFolder" -> {
openDialogCreateFolder()
Expand Down Expand Up @@ -219,26 +221,6 @@ class ExplorerActivity : BaseExplorerActivity() {
cancelItemAction()
}

private fun createNewFile(fileName: String){
if (fileName.isEmpty()) {
Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show()
} else {
val filePath = PathUtils.pathJoin(currentDirectoryPath, fileName)
val handleID = encryptedVolume.openFileWriteMode(filePath)
if (handleID == -1L) {
CustomAlertDialogBuilder(this, theme)
.setTitle(R.string.error)
.setMessage(R.string.file_creation_failed)
.setPositiveButton(R.string.ok, null)
.show()
} else {
encryptedVolume.closeFile(handleID)
setCurrentPath(currentDirectoryPath)
invalidateOptionsMenu()
}
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.explorer, menu)
val result = super.onCreateOptionsMenu(menu)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton
import sushi.hardcore.droidfs.R
import sushi.hardcore.droidfs.util.IntentUtils
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
import java.nio.CharBuffer
import java.nio.charset.StandardCharsets

class ExplorerActivityDrop : BaseExplorerActivity() {

Expand All @@ -30,15 +32,15 @@ class ExplorerActivityDrop : BaseExplorerActivity() {
return when (item.itemId) {
R.id.validate -> {
val extras = intent.extras
val errorMsg: String? = if (extras != null && extras.containsKey(Intent.EXTRA_STREAM)) {
val success = if (extras != null && extras.containsKey(Intent.EXTRA_STREAM)) {
when (intent.action) {
Intent.ACTION_SEND -> {
val uri = IntentUtils.getParcelableExtra<Uri>(intent, Intent.EXTRA_STREAM)
if (uri == null) {
getString(R.string.share_intent_parsing_failed)
false
} else {
importFilesFromUris(listOf(uri), ::onImported)
null
true
}
}
Intent.ACTION_SEND_MULTIPLE -> {
Expand All @@ -50,20 +52,34 @@ class ExplorerActivityDrop : BaseExplorerActivity() {
}
if (uris != null) {
importFilesFromUris(uris, ::onImported)
null
true
} else {
getString(R.string.share_intent_parsing_failed)
false
}
}
else -> getString(R.string.share_intent_parsing_failed)
else -> false
}
} else if ((intent.clipData?.itemCount ?: 0) > 0) {
val byteBuffer = StandardCharsets.UTF_8.encode(CharBuffer.wrap(intent.clipData!!.getItemAt(0).text))
val byteArray = ByteArray(byteBuffer.remaining())
byteBuffer.get(byteArray)
val size = byteArray.size.toLong()
createNewFile {
var offset = 0L
while (offset < size) {
offset += encryptedVolume.write(it, offset, byteArray, offset, size-offset)
}
encryptedVolume.closeFile(it)
onImported()
}
true
} else {
getString(R.string.share_intent_parsing_failed)
false
}
errorMsg?.let {
if (!success) {
CustomAlertDialogBuilder(this, theme)
.setTitle(R.string.error)
.setMessage(it)
.setMessage(R.string.share_intent_parsing_failed)
.setPositiveButton(R.string.ok, null)
.show()
}
Expand Down

0 comments on commit bd60e62

Please sign in to comment.