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

153 lines
5.2 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.file_viewers
import android.os.Bundle
import android.view.View
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
2021-11-09 11:12:09 +01:00
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
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
private val legacyMod by lazy {
sharedPrefs.getBoolean("legacyMod", false)
}
2021-03-17 21:11:14 +01:00
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)
2021-11-11 15:05:33 +01:00
gocryptfsVolume = GocryptfsVolume(applicationContext, 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() {
if (legacyMod) {
@Suppress("Deprecation")
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LOW_PROFILE or
View.SYSTEM_UI_FLAG_FULLSCREEN
} else {
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
2022-02-18 15:53:48 +01:00
protected fun loadWholeFile(path: String, fileSize: Long? = null): ByteArray? {
val result = gocryptfsVolume.loadWholeFile(path, size = fileSize)
2021-11-11 15:05:33 +01:00
if (result.second != 0) {
val dialog = CustomAlertDialogBuilder(this, themeValue)
2020-07-17 16:35:39 +02:00
.setTitle(R.string.error)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
2021-11-11 15:05:33 +01:00
when (result.second) {
1 -> dialog.setMessage(R.string.get_size_failed)
2 -> dialog.setMessage(R.string.outofmemoryerror_msg)
else -> dialog.setMessage(R.string.read_file_failed)
}
dialog.show()
2020-07-17 16:35:39 +02:00
}
2021-11-11 15:05:33 +01:00
return result.first
2020-07-17 16:35:39 +02:00
}
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
}