From 031904283f571f0d2d3401305144ba5365c04828 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 28 May 2020 11:09:31 +0200 Subject: [PATCH] properly handle folder traversing inside a zip file --- .../pro/activities/DecompressActivity.kt | 36 ++++++++++++++----- .../pro/adapters/DecompressItemsAdapter.kt | 2 +- .../item_decompression_list_file_dir.xml | 1 + 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/DecompressActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/DecompressActivity.kt index bccef7a4..f95794a1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/DecompressActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/DecompressActivity.kt @@ -20,24 +20,32 @@ import java.util.zip.ZipInputStream class DecompressActivity : SimpleActivity() { private val allFiles = ArrayList() + private var currentPath = "" + private var uri: Uri? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_decompress) - val uri = intent.data + uri = intent.data if (uri == null) { toast(R.string.unknown_error_occurred) return } - val realPath = getRealPathFromURI(uri) + val realPath = getRealPathFromURI(uri!!) title = realPath?.getFilenameFromPath() ?: uri.toString().getFilenameFromPath() + fillAllListItems(uri!!) + updateCurrentPath("") + } + private fun updateCurrentPath(path: String) { + currentPath = path try { - fillAllListItems(uri) - val listItems = getFolderItems("") + val listItems = getFolderItems(currentPath) DecompressItemsAdapter(this, listItems, decompress_list) { - + if ((it as ListItem).isDirectory) { + updateCurrentPath(it.path) + } }.apply { decompress_list.adapter = this } @@ -60,8 +68,17 @@ class DecompressActivity : SimpleActivity() { return true } + override fun onBackPressed() { + if (currentPath.isEmpty()) { + super.onBackPressed() + } else { + val newPath = if (currentPath.contains("/")) currentPath.getParentPath() else "" + updateCurrentPath(newPath) + } + } + private fun decompressFiles() { - val defaultFolder = getRealPathFromURI(intent.data!!) ?: internalStoragePath + val defaultFolder = getRealPathFromURI(uri!!) ?: internalStoragePath FilePickerDialog(this, defaultFolder, false, config.showHidden, true, true) { destination -> handleSAFDialog(destination) { if (it) { @@ -75,7 +92,7 @@ class DecompressActivity : SimpleActivity() { private fun decompressTo(destination: String) { try { - val inputStream = contentResolver.openInputStream(intent.data!!) + val inputStream = contentResolver.openInputStream(uri!!) val zipInputStream = ZipInputStream(BufferedInputStream(inputStream!!)) val buffer = ByteArray(1024) @@ -114,7 +131,7 @@ class DecompressActivity : SimpleActivity() { } fileParent == parent - }.toMutableList() as ArrayList + }.sortedWith(compareBy({ !it.isDirectory }, { it.mName })).toMutableList() as ArrayList } @SuppressLint("NewApi") @@ -130,7 +147,8 @@ class DecompressActivity : SimpleActivity() { } val lastModified = if (isOreoPlus()) zipEntry.lastModifiedTime.toMillis() else 0 - val listItem = ListItem(zipEntry.name.removeSuffix("/"), zipEntry.name.removeSuffix("/").getFilenameFromPath(), zipEntry.isDirectory, 0, 0L, lastModified, false) + val filename = zipEntry.name.removeSuffix("/") + val listItem = ListItem(filename, filename.getFilenameFromPath(), zipEntry.isDirectory, 0, 0L, lastModified, false) allFiles.add(listItem) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/DecompressItemsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/DecompressItemsAdapter.kt index 843fc50e..ec907f67 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/DecompressItemsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/DecompressItemsAdapter.kt @@ -65,7 +65,7 @@ class DecompressItemsAdapter(activity: SimpleActivity, var listItems: MutableLis override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { val fileDirItem = listItems[position] - holder.bindView(fileDirItem, false, false) { itemView, layoutPosition -> + holder.bindView(fileDirItem, true, false) { itemView, layoutPosition -> setupView(itemView, fileDirItem) } bindViewHolder(holder) diff --git a/app/src/main/res/layout/item_decompression_list_file_dir.xml b/app/src/main/res/layout/item_decompression_list_file_dir.xml index 381d1dcb..933c810b 100644 --- a/app/src/main/res/layout/item_decompression_list_file_dir.xml +++ b/app/src/main/res/layout/item_decompression_list_file_dir.xml @@ -4,6 +4,7 @@ android:id="@+id/item_holder" android:layout_width="match_parent" android:layout_height="wrap_content" + android:background="?attr/selectableItemBackground" android:paddingEnd="@dimen/activity_margin">