check proper permission at saving file in ReadTextActivity

This commit is contained in:
tibbi 2023-01-15 10:49:00 +01:00
parent ce4952751d
commit 4baca371f0
3 changed files with 44 additions and 36 deletions

View File

@ -345,15 +345,6 @@ class MainActivity : SimpleActivity() {
} }
} }
@SuppressLint("NewApi")
private fun hasStoragePermission(): Boolean {
return if (isRPlus()) {
Environment.isExternalStorageManager()
} else {
hasPermission(PERMISSION_WRITE_STORAGE)
}
}
private fun initFileManager(refreshRecents: Boolean) { private fun initFileManager(refreshRecents: Boolean) {
if (intent.action == Intent.ACTION_VIEW && intent.data != null) { if (intent.action == Intent.ACTION_VIEW && intent.data != null) {
val data = intent.data val data = intent.data

View File

@ -15,7 +15,10 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog import com.simplemobiletools.commons.dialogs.ConfirmationAdvancedDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.NavigationIcon
import com.simplemobiletools.commons.helpers.REAL_FILE_PATH
import com.simplemobiletools.commons.helpers.SAVE_DISCARD_PROMPT_INTERVAL
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.views.MyEditText import com.simplemobiletools.commons.views.MyEditText
import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.dialogs.SaveAsDialog import com.simplemobiletools.filemanager.pro.dialogs.SaveAsDialog
@ -170,14 +173,14 @@ class ReadTextActivity : SimpleActivity() {
} }
} else { } else {
SaveAsDialog(this, filePath, false) { path, _ -> SaveAsDialog(this, filePath, false) { path, _ ->
handlePermission(PERMISSION_WRITE_STORAGE) { isPermissionGranted -> if (hasStoragePermission()) {
if (isPermissionGranted) {
val file = File(path) val file = File(path)
getFileOutputStream(file.toFileDirItem(this), true) { getFileOutputStream(file.toFileDirItem(this), true) {
val shouldOverwriteOriginalText = path == filePath val shouldOverwriteOriginalText = path == filePath
saveTextContent(it, shouldExitAfterSaving, shouldOverwriteOriginalText) saveTextContent(it, shouldExitAfterSaving, shouldOverwriteOriginalText)
} }
} } else {
toast(R.string.no_storage_permissions)
} }
} }
} }

View File

@ -1,6 +1,11 @@
package com.simplemobiletools.filemanager.pro.activities package com.simplemobiletools.filemanager.pro.activities
import android.annotation.SuppressLint
import android.os.Environment
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.hasPermission
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.isRPlus
import com.simplemobiletools.filemanager.pro.R import com.simplemobiletools.filemanager.pro.R
open class SimpleActivity : BaseSimpleActivity() { open class SimpleActivity : BaseSimpleActivity() {
@ -27,4 +32,13 @@ open class SimpleActivity : BaseSimpleActivity() {
) )
override fun getAppLauncherName() = getString(R.string.app_launcher_name) override fun getAppLauncherName() = getString(R.string.app_launcher_name)
@SuppressLint("NewApi")
fun hasStoragePermission(): Boolean {
return if (isRPlus()) {
Environment.isExternalStorageManager()
} else {
hasPermission(PERMISSION_WRITE_STORAGE)
}
}
} }