DroidFS/app/src/main/java/sushi/hardcore/droidfs/file_viewers/FileViewerActivity.kt

173 lines
6.1 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.file_viewers
import android.os.Bundle
2021-09-01 20:15:11 +02:00
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
2020-07-21 15:05:05 +02:00
import sushi.hardcore.droidfs.BaseActivity
2021-03-17 21:11:14 +01:00
import sushi.hardcore.droidfs.ConstValues
import sushi.hardcore.droidfs.GocryptfsVolume
2020-07-17 16:35:39 +02:00
import sushi.hardcore.droidfs.R
import sushi.hardcore.droidfs.content_providers.RestrictedFileProvider
2021-03-17 21:11:14 +01:00
import sushi.hardcore.droidfs.explorers.ExplorerElement
import sushi.hardcore.droidfs.util.PathUtils
2020-07-27 16:20:52 +02:00
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
2020-07-17 16:35:39 +02:00
2020-07-21 15:05:05 +02:00
abstract class FileViewerActivity: BaseActivity() {
2021-03-17 21:11:14 +01:00
protected lateinit var gocryptfsVolume: GocryptfsVolume
protected lateinit var filePath: String
2021-09-01 19:58:51 +02:00
private lateinit var originalParentPath: String
2021-09-01 20:15:11 +02:00
private lateinit var windowInsetsController: WindowInsetsControllerCompat
private var windowTypeMask = 0
private var isFinishingIntentionally = false
private var usf_keep_open = false
private var foldersFirst = true
2021-03-17 21:11:14 +01:00
private var wasMapped = false
protected val mappedPlaylist = mutableListOf<ExplorerElement>()
protected var currentPlaylistIndex = -1
2020-07-17 16:35:39 +02:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
filePath = intent.getStringExtra("path")!!
2021-09-01 19:58:51 +02:00
originalParentPath = PathUtils.getParentPath(filePath)
2020-07-17 16:35:39 +02:00
val sessionID = intent.getIntExtra("sessionID", -1)
gocryptfsVolume = GocryptfsVolume(sessionID)
usf_keep_open = sharedPrefs.getBoolean("usf_keep_open", false)
foldersFirst = sharedPrefs.getBoolean("folders_first", true)
2021-09-01 20:15:11 +02:00
windowInsetsController = WindowInsetsControllerCompat(window, window.decorView)
windowInsetsController.addOnControllableInsetsChangedListener { _, typeMask ->
windowTypeMask = typeMask
}
hideSystemUi()
2020-07-17 16:35:39 +02:00
viewFile()
}
2021-03-17 21:11:14 +01:00
open fun hideSystemUi(){
2021-09-01 20:15:11 +02:00
windowInsetsController.hide(WindowInsetsCompat.Type.statusBars())
2020-07-17 16:35:39 +02:00
}
2021-03-17 21:11:14 +01:00
abstract fun getFileType(): String
2020-07-17 16:35:39 +02:00
abstract fun viewFile()
2021-03-17 21:11:14 +01:00
override fun onUserInteraction() {
super.onUserInteraction()
2021-09-01 20:15:11 +02:00
if (windowTypeMask and WindowInsetsCompat.Type.statusBars() == 0) {
hideSystemUi()
}
}
2021-03-17 21:11:14 +01:00
protected fun loadWholeFile(path: String): ByteArray? {
2020-08-01 16:43:48 +02:00
val fileSize = gocryptfsVolume.getSize(path)
2020-07-17 16:35:39 +02:00
if (fileSize >= 0){
try {
val fileBuff = ByteArray(fileSize.toInt())
var success = false
2020-08-01 16:43:48 +02:00
val handleID = gocryptfsVolume.openReadMode(path)
2020-07-17 16:35:39 +02:00
if (handleID != -1) {
var offset: Long = 0
val ioBuffer = ByteArray(GocryptfsVolume.DefaultBS)
2020-07-17 16:35:39 +02:00
var length: Int
2020-08-01 16:43:48 +02:00
while (gocryptfsVolume.readFile(handleID, offset, ioBuffer).also { length = it } > 0){
System.arraycopy(ioBuffer, 0, fileBuff, offset.toInt(), length)
2020-07-17 16:35:39 +02:00
offset += length.toLong()
}
2020-08-01 16:43:48 +02:00
gocryptfsVolume.closeFile(handleID)
2020-07-17 16:35:39 +02:00
success = offset == fileBuff.size.toLong()
}
if (success){
return fileBuff
} else {
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.read_file_failed)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
2020-07-17 16:35:39 +02:00
.show()
}
} catch (e: OutOfMemoryError){
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.outofmemoryerror_msg)
2020-07-17 16:35:39 +02:00
.setCancelable(false)
2020-08-12 15:14:38 +02:00
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
2020-07-17 16:35:39 +02:00
.show()
}
} else {
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.get_size_failed)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
2020-07-17 16:35:39 +02:00
.show()
}
return null
}
2021-03-17 21:11:14 +01:00
protected fun createPlaylist() {
if (!wasMapped){
2021-09-01 19:58:51 +02:00
for (e in gocryptfsVolume.recursiveMapFiles(originalParentPath)) {
2021-03-17 21:11:14 +01:00
if (e.isRegularFile) {
if (ConstValues.isExtensionType(getFileType(), e.name) || filePath == e.fullPath) {
mappedPlaylist.add(e)
}
}
}
val sortOrder = intent.getStringExtra("sortOrder") ?: "name"
ExplorerElement.sortBy(sortOrder, foldersFirst, mappedPlaylist)
2021-03-17 21:11:14 +01:00
//find current index
for ((i, e) in mappedPlaylist.withIndex()){
if (filePath == e.fullPath){
currentPlaylistIndex = i
break
}
}
wasMapped = true
}
}
protected fun playlistNext(forward: Boolean) {
createPlaylist()
currentPlaylistIndex = if (forward) {
(currentPlaylistIndex+1)%mappedPlaylist.size
} else {
var x = (currentPlaylistIndex-1)%mappedPlaylist.size
if (x < 0) {
x += mappedPlaylist.size
}
x
}
filePath = mappedPlaylist[currentPlaylistIndex].fullPath
}
protected fun refreshPlaylist() {
mappedPlaylist.clear()
wasMapped = false
createPlaylist()
}
protected fun goBackToExplorer() {
isFinishingIntentionally = true
finish()
}
override fun onDestroy() {
super.onDestroy()
if (!isFinishingIntentionally) {
gocryptfsVolume.close()
RestrictedFileProvider.wipeAll(this)
}
}
override fun onPause() {
super.onPause()
if (!usf_keep_open) {
finish()
}
}
override fun onBackPressed() {
super.onBackPressed()
isFinishingIntentionally = true
}
2020-08-12 15:14:38 +02:00
}