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

184 lines
6.9 KiB
Kotlin
Raw Normal View History

2020-07-17 16:35:39 +02:00
package sushi.hardcore.droidfs.file_viewers
import android.os.Build
2020-07-17 16:35:39 +02:00
import android.os.Bundle
import android.view.View
import android.view.WindowInsets
import android.widget.FrameLayout
import androidx.core.view.ViewCompat
2021-09-01 20:15:11 +02:00
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.core.view.updateLayoutParams
import androidx.core.view.updateMargins
2022-10-04 13:30:51 +02:00
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
2020-07-21 15:05:05 +02:00
import sushi.hardcore.droidfs.BaseActivity
2023-02-06 10:52:51 +01:00
import sushi.hardcore.droidfs.FileTypes
2020-07-17 16:35:39 +02:00
import sushi.hardcore.droidfs.R
2021-03-17 21:11:14 +01:00
import sushi.hardcore.droidfs.explorers.ExplorerElement
2022-06-18 21:13:16 +02:00
import sushi.hardcore.droidfs.filesystems.EncryptedVolume
import sushi.hardcore.droidfs.util.IntentUtils
2021-03-17 21:11:14 +01:00
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() {
2022-06-18 21:13:16 +02:00
protected lateinit var encryptedVolume: EncryptedVolume
2021-03-17 21:11:14 +01:00
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 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 isLegacyFullscreen = Build.VERSION.SDK_INT <= Build.VERSION_CODES.R
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)
encryptedVolume = IntentUtils.getParcelableExtra(intent, "volume")!!
foldersFirst = sharedPrefs.getBoolean("folders_first", true)
2021-09-01 20:15:11 +02:00
windowInsetsController = WindowInsetsControllerCompat(window, window.decorView)
windowInsetsController.addOnControllableInsetsChangedListener { _, typeMask ->
windowTypeMask = typeMask
}
windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_SWIPE
2020-07-17 16:35:39 +02:00
viewFile()
}
2021-03-17 21:11:14 +01:00
open fun showPartialSystemUi() {
if (isLegacyFullscreen) {
@Suppress("Deprecation")
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
} else {
windowInsetsController.hide(WindowInsetsCompat.Type.statusBars())
windowInsetsController.show(WindowInsetsCompat.Type.navigationBars())
}
}
open fun hideSystemUi() {
if (isLegacyFullscreen) {
@Suppress("Deprecation")
window.decorView.systemUiVisibility =
View.SYSTEM_UI_FLAG_LOW_PROFILE or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
} else {
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setDecorFitsSystemWindows(false)
}
}
2020-07-17 16:35:39 +02:00
}
2021-03-17 21:11:14 +01:00
protected fun applyNavigationBarMargin(root: View) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
ViewCompat.setOnApplyWindowInsetsListener(root) { _, insets ->
root.updateLayoutParams<FrameLayout.LayoutParams> {
val newInsets = insets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars())
this.updateMargins(
left = newInsets.left,
top = newInsets.top,
right = newInsets.right,
bottom = newInsets.bottom
)
}
insets
}
} else {
root.fitsSystemWindows = true
}
}
2021-03-17 21:11:14 +01:00
abstract fun getFileType(): String
abstract fun viewFile()
2022-10-04 13:30:51 +02:00
protected fun loadWholeFile(path: String, fileSize: Long? = null, callback: (ByteArray) -> Unit) {
lifecycleScope.launch(Dispatchers.IO) {
val result = encryptedVolume.loadWholeFile(path, size = fileSize)
if (isActive) {
withContext(Dispatchers.Main) {
if (result.second == 0) {
callback(result.first!!)
} else {
2023-02-28 22:50:59 +01:00
val dialog = CustomAlertDialogBuilder(this@FileViewerActivity, theme)
2022-10-04 13:30:51 +02:00
.setTitle(R.string.error)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
when (result.second) {
1 -> dialog.setMessage(R.string.get_size_failed)
2 -> dialog.setMessage(R.string.outofmemoryerror_msg)
3 -> dialog.setMessage(R.string.read_file_failed)
4 -> dialog.setMessage(R.string.io_error)
}
dialog.show()
}
}
2021-11-11 15:05:33 +01:00
}
2020-07-17 16:35:39 +02:00
}
}
2021-03-17 21:11:14 +01:00
protected fun createPlaylist() {
if (!wasMapped){
2022-06-18 21:13:16 +02:00
encryptedVolume.recursiveMapFiles(originalParentPath)?.let { elements ->
for (e in elements) {
if (e.isRegularFile) {
2023-02-06 10:52:51 +01:00
if (FileTypes.isExtensionType(getFileType(), e.name) || filePath == e.fullPath) {
2022-06-18 21:13:16 +02:00
mappedPlaylist.add(e)
}
2021-03-17 21:11:14 +01:00
}
}
}
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() {
finish()
}
2023-03-07 23:25:17 +01:00
override fun onResume() {
super.onResume()
if (encryptedVolume.isClosed()) {
finish()
}
}
2020-08-12 15:14:38 +02:00
}