2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs.explorers
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
2020-08-07 19:25:16 +02:00
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
|
|
|
import android.os.Message
|
2020-07-17 16:35:39 +02:00
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.view.WindowManager
|
|
|
|
import android.widget.AdapterView.OnItemClickListener
|
|
|
|
import android.widget.AdapterView.OnItemLongClickListener
|
|
|
|
import android.widget.EditText
|
|
|
|
import android.widget.Toast
|
2020-08-04 11:44:29 +02:00
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import androidx.lifecycle.ViewModelProvider
|
2020-07-19 21:46:29 +02:00
|
|
|
import kotlinx.android.synthetic.main.activity_explorer_base.*
|
2020-07-17 16:35:39 +02:00
|
|
|
import kotlinx.android.synthetic.main.explorer_info_bar.*
|
|
|
|
import kotlinx.android.synthetic.main.toolbar.*
|
2020-07-21 15:05:05 +02:00
|
|
|
import sushi.hardcore.droidfs.BaseActivity
|
2020-07-17 16:35:39 +02:00
|
|
|
import sushi.hardcore.droidfs.ConstValues
|
|
|
|
import sushi.hardcore.droidfs.ConstValues.Companion.isAudio
|
|
|
|
import sushi.hardcore.droidfs.ConstValues.Companion.isImage
|
|
|
|
import sushi.hardcore.droidfs.ConstValues.Companion.isText
|
|
|
|
import sushi.hardcore.droidfs.ConstValues.Companion.isVideo
|
|
|
|
import sushi.hardcore.droidfs.R
|
2020-07-27 16:20:52 +02:00
|
|
|
import sushi.hardcore.droidfs.adapters.DialogSingleChoiceAdapter
|
2020-07-17 16:35:39 +02:00
|
|
|
import sushi.hardcore.droidfs.adapters.ExplorerElementAdapter
|
2020-08-05 14:06:54 +02:00
|
|
|
import sushi.hardcore.droidfs.adapters.OpenAsDialogAdapter
|
2020-07-17 16:35:39 +02:00
|
|
|
import sushi.hardcore.droidfs.file_viewers.AudioPlayer
|
|
|
|
import sushi.hardcore.droidfs.file_viewers.ImageViewer
|
|
|
|
import sushi.hardcore.droidfs.file_viewers.TextEditor
|
|
|
|
import sushi.hardcore.droidfs.file_viewers.VideoPlayer
|
2020-07-26 21:16:06 +02:00
|
|
|
import sushi.hardcore.droidfs.provider.RestrictedFileProvider
|
2020-07-17 16:35:39 +02:00
|
|
|
import sushi.hardcore.droidfs.util.ExternalProvider
|
|
|
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
2020-08-05 14:06:54 +02:00
|
|
|
import sushi.hardcore.droidfs.util.PathUtils
|
2020-07-27 16:20:52 +02:00
|
|
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
|
2020-08-07 19:25:16 +02:00
|
|
|
import java.io.File
|
2020-07-17 16:35:39 +02:00
|
|
|
|
2020-07-21 15:05:05 +02:00
|
|
|
open class BaseExplorerActivity : BaseActivity() {
|
2020-08-04 11:44:29 +02:00
|
|
|
private lateinit var sortOrderEntries: Array<String>
|
|
|
|
private lateinit var sortOrderValues: Array<String>
|
|
|
|
private var currentSortOrderIndex = 0
|
2020-07-17 16:35:39 +02:00
|
|
|
protected lateinit var gocryptfsVolume: GocryptfsVolume
|
2020-07-26 21:16:06 +02:00
|
|
|
private lateinit var volumeName: String
|
2020-08-04 11:44:29 +02:00
|
|
|
private lateinit var explorerViewModel: ExplorerViewModel
|
|
|
|
protected var currentDirectoryPath: String = ""
|
|
|
|
set(value) {
|
|
|
|
field = value
|
|
|
|
explorerViewModel.currentDirectoryPath = value
|
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
protected lateinit var explorerElements: MutableList<ExplorerElement>
|
|
|
|
protected lateinit var explorerAdapter: ExplorerElementAdapter
|
2020-08-05 14:06:54 +02:00
|
|
|
private var isCreating = true
|
2020-08-08 14:09:34 +02:00
|
|
|
protected var isStartingActivity = false
|
2020-07-17 16:35:39 +02:00
|
|
|
private var usf_open = false
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
usf_open = sharedPrefs.getBoolean("usf_open", false)
|
|
|
|
val intent = intent
|
2020-07-26 21:16:06 +02:00
|
|
|
volumeName = intent.getStringExtra("volume_name") ?: ""
|
2020-07-17 16:35:39 +02:00
|
|
|
val sessionID = intent.getIntExtra("sessionID", -1)
|
|
|
|
gocryptfsVolume = GocryptfsVolume(sessionID)
|
2020-08-04 11:44:29 +02:00
|
|
|
sortOrderEntries = resources.getStringArray(R.array.sort_orders_entries)
|
|
|
|
sortOrderValues = resources.getStringArray(R.array.sort_orders_values)
|
|
|
|
currentSortOrderIndex = resources.getStringArray(R.array.sort_orders_values).indexOf(sharedPrefs.getString(ConstValues.sort_order_key, "name"))
|
2020-07-17 16:35:39 +02:00
|
|
|
init()
|
|
|
|
setSupportActionBar(toolbar)
|
|
|
|
title = ""
|
2020-07-26 21:16:06 +02:00
|
|
|
title_text.text = getString(R.string.volume, volumeName)
|
|
|
|
explorerAdapter = ExplorerElementAdapter(this)
|
2020-08-04 11:44:29 +02:00
|
|
|
explorerViewModel= ViewModelProvider(this).get(ExplorerViewModel::class.java)
|
|
|
|
currentDirectoryPath = explorerViewModel.currentDirectoryPath
|
2020-07-26 21:16:06 +02:00
|
|
|
setCurrentPath(currentDirectoryPath)
|
|
|
|
list_explorer.adapter = explorerAdapter
|
2020-07-17 16:35:39 +02:00
|
|
|
list_explorer.onItemClickListener = OnItemClickListener { _, _, position, _ -> onExplorerItemClick(position) }
|
2020-08-01 15:39:00 +02:00
|
|
|
list_explorer.onItemLongClickListener = OnItemLongClickListener { _, _, position, _ -> onExplorerItemLongClick(position); true }
|
2020-07-17 16:35:39 +02:00
|
|
|
refresher.setOnRefreshListener {
|
2020-07-26 21:16:06 +02:00
|
|
|
setCurrentPath(currentDirectoryPath)
|
2020-07-17 16:35:39 +02:00
|
|
|
refresher.isRefreshing = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:44:29 +02:00
|
|
|
class ExplorerViewModel: ViewModel() {
|
|
|
|
var currentDirectoryPath = ""
|
|
|
|
}
|
|
|
|
|
2020-07-17 16:35:39 +02:00
|
|
|
protected open fun init() {
|
2020-07-19 21:46:29 +02:00
|
|
|
setContentView(R.layout.activity_explorer_base)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:44:29 +02:00
|
|
|
private fun startFileViewer(cls: Class<*>, filePath: String, sortOrder: String = ""){
|
2020-07-17 16:35:39 +02:00
|
|
|
val intent = Intent(this, cls)
|
|
|
|
intent.putExtra("path", filePath)
|
|
|
|
intent.putExtra("sessionID", gocryptfsVolume.sessionID)
|
2020-08-04 11:44:29 +02:00
|
|
|
if (sortOrder.isNotEmpty()){
|
|
|
|
intent.putExtra("sortOrder", sortOrder)
|
|
|
|
}
|
2020-08-08 14:09:34 +02:00
|
|
|
isStartingActivity = true
|
2020-07-17 16:35:39 +02:00
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
|
2020-09-05 11:37:47 +02:00
|
|
|
private fun openWithExternalApp(fullPath: String){
|
|
|
|
isStartingActivity = true
|
|
|
|
ExternalProvider.open(this, gocryptfsVolume, fullPath)
|
|
|
|
}
|
|
|
|
|
2020-07-17 16:35:39 +02:00
|
|
|
protected open fun onExplorerItemClick(position: Int) {
|
2020-07-26 21:16:06 +02:00
|
|
|
val wasSelecting = explorerAdapter.selectedItems.isNotEmpty()
|
|
|
|
explorerAdapter.onItemClick(position)
|
|
|
|
if (explorerAdapter.selectedItems.isEmpty()) {
|
2020-07-17 16:35:39 +02:00
|
|
|
if (!wasSelecting) {
|
2020-08-05 14:06:54 +02:00
|
|
|
val fullPath = explorerElements[position].fullPath
|
2020-07-17 16:35:39 +02:00
|
|
|
when {
|
2020-07-26 21:16:06 +02:00
|
|
|
explorerElements[position].isDirectory -> {
|
|
|
|
setCurrentPath(fullPath)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
explorerElements[position].isParentFolder -> {
|
2020-08-01 15:39:00 +02:00
|
|
|
setCurrentPath(PathUtils.getParentPath(currentDirectoryPath))
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
isImage(fullPath) -> {
|
2020-08-04 11:44:29 +02:00
|
|
|
startFileViewer(ImageViewer::class.java, fullPath, sortOrderValues[currentSortOrderIndex])
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
isVideo(fullPath) -> {
|
|
|
|
startFileViewer(VideoPlayer::class.java, fullPath)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
isText(fullPath) -> {
|
|
|
|
startFileViewer(TextEditor::class.java, fullPath)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
isAudio(fullPath) -> {
|
|
|
|
startFileViewer(AudioPlayer::class.java, fullPath)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
else -> {
|
2020-09-05 11:37:47 +02:00
|
|
|
val adapter = OpenAsDialogAdapter(this, usf_open)
|
2020-07-27 16:20:52 +02:00
|
|
|
ColoredAlertDialogBuilder(this)
|
|
|
|
.setSingleChoiceItems(adapter, -1){ dialog, which ->
|
|
|
|
when (adapter.getItem(which)){
|
|
|
|
"image" -> startFileViewer(ImageViewer::class.java, fullPath)
|
|
|
|
"video" -> startFileViewer(VideoPlayer::class.java, fullPath)
|
|
|
|
"audio" -> startFileViewer(AudioPlayer::class.java, fullPath)
|
|
|
|
"text" -> startFileViewer(TextEditor::class.java, fullPath)
|
2020-09-05 11:37:47 +02:00
|
|
|
"external" -> if (usf_open){
|
|
|
|
openWithExternalApp(fullPath)
|
|
|
|
}
|
2020-07-27 16:20:52 +02:00
|
|
|
}
|
|
|
|
dialog.dismiss()
|
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(getString(R.string.open_as))
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
2020-07-27 16:20:52 +02:00
|
|
|
.show()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 20:48:12 +02:00
|
|
|
invalidateOptionsMenu()
|
2020-08-01 15:39:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected open fun onExplorerItemLongClick(position: Int) {
|
|
|
|
explorerAdapter.onItemLongClick(position)
|
2020-07-17 16:35:39 +02:00
|
|
|
invalidateOptionsMenu()
|
|
|
|
}
|
|
|
|
|
2020-07-28 22:25:10 +02:00
|
|
|
protected fun unselectAll(){
|
|
|
|
explorerAdapter.unSelectAll()
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:44:53 +02:00
|
|
|
private fun sortExplorerElements() {
|
2020-08-04 11:44:29 +02:00
|
|
|
ExplorerElement.sortBy(sortOrderValues[currentSortOrderIndex], explorerElements)
|
2020-07-26 21:16:06 +02:00
|
|
|
val sharedPrefsEditor = sharedPrefs.edit()
|
2020-08-04 11:44:29 +02:00
|
|
|
sharedPrefsEditor.putString(ConstValues.sort_order_key, sortOrderValues[currentSortOrderIndex])
|
2020-07-26 21:16:06 +02:00
|
|
|
sharedPrefsEditor.apply()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected fun setCurrentPath(path: String) {
|
2020-08-01 16:43:48 +02:00
|
|
|
explorerElements = gocryptfsVolume.listDir(path)
|
2020-07-26 21:16:06 +02:00
|
|
|
text_dir_empty.visibility = if (explorerElements.size == 0) View.VISIBLE else View.INVISIBLE
|
2020-07-18 18:44:53 +02:00
|
|
|
sortExplorerElements()
|
2020-07-17 16:35:39 +02:00
|
|
|
if (path.isNotEmpty()) { //not root
|
2020-08-01 15:39:00 +02:00
|
|
|
explorerElements.add(0, ExplorerElement("..", (-1).toShort(), -1, -1, currentDirectoryPath))
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
explorerAdapter.setExplorerElements(explorerElements)
|
|
|
|
currentDirectoryPath = path
|
|
|
|
current_path_text.text = getString(R.string.location, currentDirectoryPath)
|
2020-08-05 14:06:54 +02:00
|
|
|
Thread{
|
|
|
|
var totalSize: Long = 0
|
2020-08-11 20:40:14 +02:00
|
|
|
for (element in explorerElements){
|
|
|
|
if (element.isDirectory){
|
|
|
|
var dirSize: Long = 0
|
|
|
|
for (subFile in gocryptfsVolume.recursiveMapFiles(element.fullPath)){
|
|
|
|
if (subFile.isRegularFile){
|
|
|
|
dirSize += subFile.size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
element.size = dirSize
|
|
|
|
totalSize += dirSize
|
|
|
|
} else if (element.isRegularFile) {
|
|
|
|
totalSize += element.size
|
2020-08-05 14:06:54 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 14:10:46 +02:00
|
|
|
runOnUiThread {
|
2020-08-25 16:36:48 +02:00
|
|
|
total_size_text.text = getString(R.string.total_size, PathUtils.formatSize(totalSize))
|
2020-08-25 14:10:46 +02:00
|
|
|
explorerAdapter.notifyDataSetChanged()
|
|
|
|
}
|
2020-08-05 14:06:54 +02:00
|
|
|
}.start()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun askCloseVolume() {
|
2020-07-27 16:20:52 +02:00
|
|
|
ColoredAlertDialogBuilder(this)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(R.string.warning)
|
|
|
|
.setMessage(R.string.ask_close_volume)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ -> closeVolumeOnUserExit() }
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBackPressed() {
|
2020-07-26 21:16:06 +02:00
|
|
|
if (explorerAdapter.selectedItems.isEmpty()) {
|
2020-08-01 15:39:00 +02:00
|
|
|
val parentPath = PathUtils.getParentPath(currentDirectoryPath)
|
2020-07-26 21:16:06 +02:00
|
|
|
if (parentPath == currentDirectoryPath) {
|
2020-07-17 16:35:39 +02:00
|
|
|
askCloseVolume()
|
|
|
|
} else {
|
2020-08-01 15:39:00 +02:00
|
|
|
setCurrentPath(PathUtils.getParentPath(currentDirectoryPath))
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-28 22:25:10 +02:00
|
|
|
unselectAll()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 18:44:53 +02:00
|
|
|
private fun createFolder(folder_name: String){
|
2020-07-17 16:35:39 +02:00
|
|
|
if (folder_name.isEmpty()) {
|
|
|
|
Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show()
|
|
|
|
} else {
|
2020-07-26 21:16:06 +02:00
|
|
|
if (!gocryptfsVolume.mkdir(PathUtils.path_join(currentDirectoryPath, folder_name))) {
|
2020-07-27 16:20:52 +02:00
|
|
|
ColoredAlertDialogBuilder(this)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(R.string.error)
|
|
|
|
.setMessage(R.string.error_mkdir)
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show()
|
|
|
|
} else {
|
2020-07-26 21:16:06 +02:00
|
|
|
setCurrentPath(currentDirectoryPath)
|
2020-07-17 16:35:39 +02:00
|
|
|
invalidateOptionsMenu()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:06:54 +02:00
|
|
|
protected fun openDialogCreateFolder() {
|
2020-07-19 21:46:29 +02:00
|
|
|
val dialogEditTextView = layoutInflater.inflate(R.layout.dialog_edit_text, null)
|
|
|
|
val dialogEditText = dialogEditTextView.findViewById<EditText>(R.id.dialog_edit_text)
|
2020-07-27 16:20:52 +02:00
|
|
|
val dialog = ColoredAlertDialogBuilder(this)
|
2020-07-19 21:46:29 +02:00
|
|
|
.setView(dialogEditTextView)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(R.string.enter_folder_name)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ ->
|
2020-07-19 21:46:29 +02:00
|
|
|
val folderName = dialogEditText.text.toString()
|
|
|
|
createFolder(folderName)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.create()
|
2020-07-19 21:46:29 +02:00
|
|
|
dialogEditText.setOnEditorActionListener { _, _, _ ->
|
|
|
|
val folderName = dialogEditText.text.toString()
|
2020-07-17 16:35:39 +02:00
|
|
|
dialog.dismiss()
|
2020-07-19 21:46:29 +02:00
|
|
|
createFolder(folderName)
|
2020-07-17 16:35:39 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
|
|
|
dialog.show()
|
|
|
|
}
|
|
|
|
|
2020-08-09 14:34:42 +02:00
|
|
|
protected fun checkPathOverwrite(path: String, isDirectory: Boolean): String? {
|
2020-08-07 19:25:16 +02:00
|
|
|
var outputPath: String? = null
|
|
|
|
if (gocryptfsVolume.pathExists(path)){
|
|
|
|
val fileName = File(path).name
|
|
|
|
val handler = Handler{ msg ->
|
|
|
|
outputPath = msg.obj as String?
|
|
|
|
throw RuntimeException()
|
|
|
|
}
|
|
|
|
runOnUiThread {
|
|
|
|
val dialog = ColoredAlertDialogBuilder(this)
|
|
|
|
.setTitle(R.string.warning)
|
2020-08-09 14:34:42 +02:00
|
|
|
.setMessage(getString(if (isDirectory){R.string.dir_overwrite_question} else {R.string.file_overwrite_question}, path))
|
2020-08-07 19:25:16 +02:00
|
|
|
.setNegativeButton(R.string.no) { _, _ ->
|
|
|
|
val dialogEditTextView = layoutInflater.inflate(R.layout.dialog_edit_text, null)
|
|
|
|
val dialogEditText = dialogEditTextView.findViewById<EditText>(R.id.dialog_edit_text)
|
|
|
|
dialogEditText.setText(fileName)
|
|
|
|
dialogEditText.selectAll()
|
|
|
|
val dialog = ColoredAlertDialogBuilder(this)
|
|
|
|
.setView(dialogEditTextView)
|
2020-09-30 16:19:16 +02:00
|
|
|
.setTitle(R.string.enter_new_name)
|
2020-08-07 19:25:16 +02:00
|
|
|
.setPositiveButton(R.string.ok) { _, _ ->
|
2020-08-09 14:34:42 +02:00
|
|
|
handler.sendMessage(Message().apply { obj = checkPathOverwrite(PathUtils.path_join(PathUtils.getParentPath(path), dialogEditText.text.toString()), isDirectory) })
|
2020-08-07 19:25:16 +02:00
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel) { _, _ -> handler.sendMessage(Message().apply { obj = null }) }
|
|
|
|
.create()
|
|
|
|
dialogEditText.setOnEditorActionListener { _, _, _ ->
|
|
|
|
dialog.dismiss()
|
2020-08-09 14:34:42 +02:00
|
|
|
handler.sendMessage(Message().apply { obj = checkPathOverwrite(PathUtils.path_join(PathUtils.getParentPath(path), dialogEditText.text.toString()), isDirectory) })
|
2020-08-07 19:25:16 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
dialog.setOnCancelListener { handler.sendMessage(Message().apply { obj = null }) }
|
|
|
|
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
|
|
|
dialog.show()
|
|
|
|
}
|
2020-09-30 16:19:16 +02:00
|
|
|
.setPositiveButton(R.string.yes) { _, _ -> handler.sendMessage(Message().apply { obj = path }) }
|
2020-08-07 19:25:16 +02:00
|
|
|
.create()
|
|
|
|
dialog.setOnCancelListener { handler.sendMessage(Message().apply { obj = null }) }
|
|
|
|
dialog.show()
|
|
|
|
}
|
|
|
|
try { Looper.loop() }
|
|
|
|
catch (e: RuntimeException) {}
|
|
|
|
} else {
|
|
|
|
outputPath = path
|
|
|
|
}
|
|
|
|
return outputPath
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:06:54 +02:00
|
|
|
protected fun rename(old_name: String, new_name: String){
|
2020-07-17 16:35:39 +02:00
|
|
|
if (new_name.isEmpty()) {
|
|
|
|
Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show()
|
|
|
|
} else {
|
2020-07-26 21:16:06 +02:00
|
|
|
if (!gocryptfsVolume.rename(PathUtils.path_join(currentDirectoryPath, old_name), PathUtils.path_join(currentDirectoryPath, new_name))) {
|
2020-07-27 16:20:52 +02:00
|
|
|
ColoredAlertDialogBuilder(this)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(R.string.error)
|
|
|
|
.setMessage(getString(R.string.rename_failed, old_name))
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show()
|
|
|
|
} else {
|
2020-07-26 21:16:06 +02:00
|
|
|
setCurrentPath(currentDirectoryPath)
|
2020-07-17 16:35:39 +02:00
|
|
|
invalidateOptionsMenu()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:06:54 +02:00
|
|
|
protected fun handleMenuItems(menu: Menu){
|
2020-08-01 15:39:00 +02:00
|
|
|
menu.findItem(R.id.rename).isVisible = false
|
2020-07-17 16:35:39 +02:00
|
|
|
if (usf_open){
|
2020-08-01 15:39:00 +02:00
|
|
|
menu.findItem(R.id.external_open)?.isVisible = false
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-08-01 15:39:00 +02:00
|
|
|
val noItemSelected = explorerAdapter.selectedItems.isEmpty()
|
|
|
|
menu.findItem(R.id.sort).isVisible = noItemSelected
|
|
|
|
menu.findItem(R.id.close).isVisible = noItemSelected
|
|
|
|
if (noItemSelected){
|
2020-07-17 16:35:39 +02:00
|
|
|
toolbar.navigationIcon = null
|
|
|
|
} else {
|
|
|
|
toolbar.setNavigationIcon(R.drawable.icon_arrow_back)
|
2020-08-01 15:39:00 +02:00
|
|
|
if (explorerAdapter.selectedItems.size == 1) {
|
|
|
|
menu.findItem(R.id.rename).isVisible = true
|
|
|
|
if (usf_open && explorerElements[explorerAdapter.selectedItems[0]].isRegularFile) {
|
|
|
|
menu.findItem(R.id.external_open)?.isVisible = true
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
return when (item.itemId) {
|
|
|
|
android.R.id.home -> {
|
2020-07-28 22:25:10 +02:00
|
|
|
unselectAll()
|
2020-07-17 16:35:39 +02:00
|
|
|
true
|
|
|
|
}
|
2020-08-01 15:39:00 +02:00
|
|
|
R.id.sort -> {
|
2020-07-27 16:20:52 +02:00
|
|
|
ColoredAlertDialogBuilder(this)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(R.string.sort_order)
|
2020-08-04 11:44:29 +02:00
|
|
|
.setSingleChoiceItems(DialogSingleChoiceAdapter(this, sortOrderEntries), currentSortOrderIndex) { dialog, which ->
|
|
|
|
currentSortOrderIndex = which
|
2020-07-26 21:16:06 +02:00
|
|
|
setCurrentPath(currentDirectoryPath)
|
2020-07-17 16:35:39 +02:00
|
|
|
dialog.dismiss()
|
2020-07-27 16:20:52 +02:00
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel) { dialog, _ -> dialog.dismiss() }
|
|
|
|
.show()
|
2020-07-17 16:35:39 +02:00
|
|
|
true
|
|
|
|
}
|
2020-08-01 15:39:00 +02:00
|
|
|
R.id.rename -> {
|
2020-07-26 21:16:06 +02:00
|
|
|
val dialogEditTextView = layoutInflater.inflate(R.layout.dialog_edit_text, null)
|
|
|
|
val oldName = explorerElements[explorerAdapter.selectedItems[0]].name
|
|
|
|
val dialogEditText = dialogEditTextView.findViewById<EditText>(R.id.dialog_edit_text)
|
|
|
|
dialogEditText.setText(oldName)
|
|
|
|
dialogEditText.selectAll()
|
2020-07-27 16:20:52 +02:00
|
|
|
val dialog = ColoredAlertDialogBuilder(this)
|
2020-07-26 21:16:06 +02:00
|
|
|
.setView(dialogEditTextView)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(R.string.rename_title)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ ->
|
2020-07-26 21:16:06 +02:00
|
|
|
val newName = dialogEditText.text.toString()
|
|
|
|
rename(oldName, newName)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.create()
|
2020-07-26 21:16:06 +02:00
|
|
|
dialogEditText.setOnEditorActionListener { _, _, _ ->
|
|
|
|
val newName = dialogEditText.text.toString()
|
2020-07-17 16:35:39 +02:00
|
|
|
dialog.dismiss()
|
2020-07-26 21:16:06 +02:00
|
|
|
rename(oldName, newName)
|
2020-07-17 16:35:39 +02:00
|
|
|
true
|
|
|
|
}
|
|
|
|
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
|
|
|
dialog.show()
|
|
|
|
true
|
|
|
|
}
|
2020-08-01 15:39:00 +02:00
|
|
|
R.id.external_open -> {
|
2020-07-17 16:35:39 +02:00
|
|
|
if (usf_open){
|
2020-09-05 11:37:47 +02:00
|
|
|
openWithExternalApp(PathUtils.path_join(currentDirectoryPath, explorerElements[explorerAdapter.selectedItems[0]].name))
|
2020-07-28 22:25:10 +02:00
|
|
|
unselectAll()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2020-08-01 15:39:00 +02:00
|
|
|
R.id.close -> {
|
2020-07-17 16:35:39 +02:00
|
|
|
askCloseVolume()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:09:34 +02:00
|
|
|
protected open fun closeVolumeOnUserExit() {
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
protected open fun closeVolumeOnDestroy() {
|
|
|
|
if (!gocryptfsVolume.isClosed()){
|
|
|
|
gocryptfsVolume.close()
|
|
|
|
}
|
|
|
|
RestrictedFileProvider.wipeAll(this) //additional security
|
|
|
|
}
|
|
|
|
|
2020-07-17 16:35:39 +02:00
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
if (!isChangingConfigurations) { //activity won't be recreated
|
|
|
|
closeVolumeOnDestroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-08 14:09:34 +02:00
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
2020-08-29 20:48:12 +02:00
|
|
|
if (!isChangingConfigurations){
|
|
|
|
if (isStartingActivity){
|
|
|
|
isStartingActivity = false
|
|
|
|
} else {
|
|
|
|
finish()
|
|
|
|
}
|
2020-08-08 14:09:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 16:35:39 +02:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2020-08-05 14:06:54 +02:00
|
|
|
if (isCreating){
|
|
|
|
isCreating = false
|
|
|
|
} else {
|
2020-08-08 14:09:34 +02:00
|
|
|
if (gocryptfsVolume.isClosed()){
|
|
|
|
finish()
|
|
|
|
} else {
|
|
|
|
isStartingActivity = false
|
|
|
|
ExternalProvider.removeFiles(this)
|
|
|
|
}
|
2020-08-05 14:06:54 +02:00
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-08-11 20:40:14 +02:00
|
|
|
}
|