2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs.file_viewers
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2021-12-15 09:13:30 +01:00
|
|
|
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
|
2020-12-31 13:15:13 +01:00
|
|
|
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
|
2020-08-09 17:01:47 +02:00
|
|
|
private var isFinishingIntentionally = false
|
2020-12-31 16:24:04 +01:00
|
|
|
private var usf_keep_open = false
|
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
|
2021-12-15 09:13:30 +01:00
|
|
|
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)
|
2020-10-23 13:21:14 +02:00
|
|
|
usf_keep_open = sharedPrefs.getBoolean("usf_keep_open", false)
|
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
|
|
|
|
}
|
2020-07-26 21:16:06 +02:00
|
|
|
hideSystemUi()
|
2020-07-17 16:35:39 +02:00
|
|
|
viewFile()
|
|
|
|
}
|
2021-03-17 21:11:14 +01:00
|
|
|
|
2021-12-15 09:13:30 +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
|
|
|
|
2020-08-08 14:09:34 +02:00
|
|
|
override fun onUserInteraction() {
|
|
|
|
super.onUserInteraction()
|
2021-09-01 20:15:11 +02:00
|
|
|
if (windowTypeMask and WindowInsetsCompat.Type.statusBars() == 0) {
|
2020-08-08 14:09:34 +02:00
|
|
|
hideSystemUi()
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 21:11:14 +01:00
|
|
|
|
|
|
|
protected fun loadWholeFile(path: String): ByteArray? {
|
2021-11-11 15:05:33 +01:00
|
|
|
val result = gocryptfsVolume.loadWholeFile(path)
|
|
|
|
if (result.second != 0) {
|
|
|
|
val dialog = CustomAlertDialogBuilder(this, themeValue)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(R.string.error)
|
|
|
|
.setCancelable(false)
|
2020-08-08 14:09:34 +02:00
|
|
|
.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
|
|
|
}
|
2020-08-08 14:09:34 +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"
|
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-09 17:01:47 +02:00
|
|
|
isFinishingIntentionally = true
|
2020-08-08 14:09:34 +02:00
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
2020-08-09 17:01:47 +02:00
|
|
|
if (!isFinishingIntentionally) {
|
2020-08-08 14:09:34 +02:00
|
|
|
gocryptfsVolume.close()
|
|
|
|
RestrictedFileProvider.wipeAll(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
2020-10-23 13:21:14 +02:00
|
|
|
if (!usf_keep_open) {
|
|
|
|
finish()
|
|
|
|
}
|
2020-08-08 14:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBackPressed() {
|
|
|
|
super.onBackPressed()
|
2020-08-09 17:01:47 +02:00
|
|
|
isFinishingIntentionally = true
|
2020-08-08 14:09:34 +02:00
|
|
|
}
|
2020-08-12 15:14:38 +02:00
|
|
|
}
|