2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs.file_viewers
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
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
|
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
|
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
|
|
|
|
|
2020-07-17 16:35:39 +02:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
filePath = intent.getStringExtra("path")!!
|
|
|
|
val sessionID = intent.getIntExtra("sessionID", -1)
|
|
|
|
gocryptfsVolume = GocryptfsVolume(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)
|
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
|
|
|
|
2020-07-26 21:16:06 +02:00
|
|
|
open fun hideSystemUi(){
|
|
|
|
window.decorView.systemUiVisibility =
|
|
|
|
View.SYSTEM_UI_FLAG_FULLSCREEN/* or
|
2020-09-30 16:19:16 +02:00
|
|
|
View.SYSTEM_UI_FLAG_LOW_PROFILE or
|
2020-07-26 21:16:06 +02:00
|
|
|
View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
|
|
|
|
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or
|
|
|
|
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
|
|
|
|
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION*/
|
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()
|
|
|
|
if (window.decorView.systemUiVisibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 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
|
2020-07-26 21:16:06 +02:00
|
|
|
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){
|
2020-07-26 21:16:06 +02:00
|
|
|
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)
|
2020-08-08 14:09:34 +02:00
|
|
|
.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)
|
2020-07-26 21:16:06 +02:00
|
|
|
.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)
|
2020-08-08 14:09:34 +02:00
|
|
|
.setPositiveButton(R.string.ok) { _, _ -> goBackToExplorer() }
|
2020-07-17 16:35:39 +02:00
|
|
|
.show()
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2020-08-08 14:09:34 +02:00
|
|
|
|
2021-03-17 21:11:14 +01:00
|
|
|
protected fun createPlaylist() {
|
|
|
|
if (!wasMapped){
|
|
|
|
for (e in gocryptfsVolume.recursiveMapFiles(PathUtils.getParentPath(filePath))){
|
|
|
|
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
|
|
|
}
|