2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs.file_viewers
|
|
|
|
|
2023-03-13 17:02:38 +01:00
|
|
|
import android.os.Build
|
2020-07-17 16:35:39 +02:00
|
|
|
import android.os.Bundle
|
2021-12-15 09:13:30 +01:00
|
|
|
import android.view.View
|
2023-03-13 17:02:38 +01:00
|
|
|
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
|
2023-03-13 17:02:38 +01:00
|
|
|
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
|
2022-09-30 21:22:37 +02:00
|
|
|
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
|
2021-04-04 12:31:49 +02:00
|
|
|
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
|
2023-03-13 17:02:38 +01:00
|
|
|
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)
|
2022-09-30 21:22:37 +02:00
|
|
|
encryptedVolume = IntentUtils.getParcelableExtra(intent, "volume")!!
|
2021-04-04 12:31:49 +02:00
|
|
|
foldersFirst = sharedPrefs.getBoolean("folders_first", true)
|
2021-09-01 20:15:11 +02:00
|
|
|
windowInsetsController = WindowInsetsControllerCompat(window, window.decorView)
|
|
|
|
windowInsetsController.addOnControllableInsetsChangedListener { _, typeMask ->
|
|
|
|
windowTypeMask = typeMask
|
|
|
|
}
|
2023-04-18 19:47:44 +02:00
|
|
|
windowInsetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_DEFAULT
|
2020-07-17 16:35:39 +02:00
|
|
|
viewFile()
|
|
|
|
}
|
2021-03-17 21:11:14 +01:00
|
|
|
|
2023-03-13 17:02:38 +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())
|
|
|
|
}
|
2022-10-04 12:23:25 +02:00
|
|
|
}
|
|
|
|
|
2023-03-13 17:02:38 +01:00
|
|
|
open fun hideSystemUi() {
|
|
|
|
if (isLegacyFullscreen) {
|
2021-12-15 09:13:30 +01:00
|
|
|
@Suppress("Deprecation")
|
|
|
|
window.decorView.systemUiVisibility =
|
|
|
|
View.SYSTEM_UI_FLAG_LOW_PROFILE or
|
2023-03-13 17:02:38 +01:00
|
|
|
View.SYSTEM_UI_FLAG_FULLSCREEN or
|
|
|
|
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
|
|
|
|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
2021-12-15 09:13:30 +01:00
|
|
|
} else {
|
2023-03-13 17:02:38 +01:00
|
|
|
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
|
|
window.setDecorFitsSystemWindows(false)
|
|
|
|
}
|
2021-12-15 09:13:30 +01:00
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2021-03-17 21:11:14 +01:00
|
|
|
|
2023-03-13 17:02:38 +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
|
2020-08-08 14:09:34 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-17 21:11:14 +01:00
|
|
|
|
2023-03-13 17:02:38 +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
|
|
|
}
|
|
|
|
}
|
2020-08-08 14:09:34 +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"
|
2021-04-04 12:31:49 +02:00
|
|
|
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() {
|
2020-08-08 14:09:34 +02:00
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:25:17 +01:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
if (encryptedVolume.isClosed()) {
|
2020-10-23 13:21:14 +02:00
|
|
|
finish()
|
|
|
|
}
|
2020-08-08 14:09:34 +02:00
|
|
|
}
|
2020-08-12 15:14:38 +02:00
|
|
|
}
|