mirror of
https://github.com/SimpleMobileTools/Simple-File-Manager.git
synced 2025-02-01 11:07:01 +01:00
fix #64, ask for SD card write permission at creating new items
This commit is contained in:
parent
c65f960704
commit
8c9f7bf050
@ -1,16 +1,16 @@
|
|||||||
package com.simplemobiletools.filemanager.dialogs
|
package com.simplemobiletools.filemanager.dialogs
|
||||||
|
|
||||||
import android.app.Activity
|
|
||||||
import android.support.v7.app.AlertDialog
|
import android.support.v7.app.AlertDialog
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.WindowManager
|
import android.view.WindowManager
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.*
|
||||||
import com.simplemobiletools.filemanager.R
|
import com.simplemobiletools.filemanager.R
|
||||||
|
import com.simplemobiletools.filemanager.activities.SimpleActivity
|
||||||
import kotlinx.android.synthetic.main.dialog_create_new.view.*
|
import kotlinx.android.synthetic.main.dialog_create_new.view.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
class CreateNewItemDialog(val activity: Activity, val path: String, val callback: () -> Unit) {
|
class CreateNewItemDialog(val activity: SimpleActivity, val path: String, val callback: () -> Unit) {
|
||||||
init {
|
init {
|
||||||
val view = activity.layoutInflater.inflate(R.layout.dialog_create_new, null)
|
val view = activity.layoutInflater.inflate(R.layout.dialog_create_new, null)
|
||||||
|
|
||||||
@ -32,12 +32,16 @@ class CreateNewItemDialog(val activity: Activity, val path: String, val callback
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_directory) {
|
if (view.dialog_radio_group.checkedRadioButtonId == R.id.dialog_radio_directory) {
|
||||||
if (!createDirectory(file, this)) {
|
createDirectory(file, this) {
|
||||||
errorOccurred()
|
if (!it) {
|
||||||
|
errorOccurred()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!createFile(file, this)) {
|
createFile(file, this) {
|
||||||
errorOccurred()
|
if (!it) {
|
||||||
|
errorOccurred()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -47,39 +51,47 @@ class CreateNewItemDialog(val activity: Activity, val path: String, val callback
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createDirectory(file: File, alertDialog: AlertDialog): Boolean {
|
private fun createDirectory(file: File, alertDialog: AlertDialog, callback: (Boolean) -> Unit) {
|
||||||
return if (activity.needsStupidWritePermissions(path)) {
|
if (activity.needsStupidWritePermissions(path)) {
|
||||||
val documentFile = activity.getFileDocument(file.absolutePath) ?: return false
|
activity.handleSAFDialog(file) {
|
||||||
documentFile.createDirectory(file.name)
|
val documentFile = activity.getFileDocument(file.absolutePath)
|
||||||
success(alertDialog)
|
if (documentFile == null) {
|
||||||
true
|
callback(false)
|
||||||
|
return@handleSAFDialog
|
||||||
|
}
|
||||||
|
documentFile.createDirectory(file.name)
|
||||||
|
success(alertDialog)
|
||||||
|
}
|
||||||
} else if (file.mkdirs()) {
|
} else if (file.mkdirs()) {
|
||||||
success(alertDialog)
|
success(alertDialog)
|
||||||
true
|
callback(true)
|
||||||
} else
|
} else
|
||||||
false
|
callback(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun errorOccurred() {
|
private fun errorOccurred() {
|
||||||
activity.toast(R.string.unknown_error_occurred)
|
activity.toast(R.string.unknown_error_occurred)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createFile(file: File, alertDialog: AlertDialog): Boolean {
|
private fun createFile(file: File, alertDialog: AlertDialog, callback: (Boolean) -> Unit) {
|
||||||
try {
|
try {
|
||||||
if (activity.needsStupidWritePermissions(path)) {
|
if (activity.needsStupidWritePermissions(path)) {
|
||||||
val documentFile = activity.getFileDocument(file.absolutePath) ?: return false
|
activity.handleSAFDialog(file) {
|
||||||
documentFile.createFile("", file.name)
|
val documentFile = activity.getFileDocument(file.absolutePath)
|
||||||
success(alertDialog)
|
if (documentFile == null) {
|
||||||
return true
|
callback(false)
|
||||||
|
return@handleSAFDialog
|
||||||
|
}
|
||||||
|
documentFile.createFile("", file.name)
|
||||||
|
success(alertDialog)
|
||||||
|
}
|
||||||
} else if (file.createNewFile()) {
|
} else if (file.createNewFile()) {
|
||||||
success(alertDialog)
|
success(alertDialog)
|
||||||
return true
|
callback(true)
|
||||||
}
|
}
|
||||||
} catch (ignored: IOException) {
|
} catch (ignored: IOException) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun success(alertDialog: AlertDialog) {
|
private fun success(alertDialog: AlertDialog) {
|
||||||
|
@ -190,7 +190,7 @@ class ItemsFragment : android.support.v4.app.Fragment(), ItemsAdapter.ItemOperat
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createNewItem() {
|
private fun createNewItem() {
|
||||||
CreateNewItemDialog(activity, mPath) {
|
CreateNewItemDialog(activity as SimpleActivity, mPath) {
|
||||||
fillItems()
|
fillItems()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user