Skip to content

Commit 3712f9f

Browse files
committed
Move IO operation to background thread
This was causing ANRs when zip files contained more than a few entries.
1 parent cb3e393 commit 3712f9f

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

app/src/main/kotlin/org/fossify/filemanager/activities/DecompressActivity.kt

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.fossify.filemanager.activities
22

3-
import android.annotation.SuppressLint
43
import android.net.Uri
54
import android.os.Bundle
65
import net.lingala.zip4j.exception.ZipException
@@ -76,11 +75,6 @@ class DecompressActivity : SimpleActivity() {
7675
}
7776
}
7877

79-
private fun setupFilesList() {
80-
fillAllListItems(uri!!)
81-
updateCurrentPath("")
82-
}
83-
8478
override fun onBackPressed() {
8579
if (currentPath.isEmpty()) {
8680
super.onBackPressed()
@@ -90,19 +84,31 @@ class DecompressActivity : SimpleActivity() {
9084
}
9185
}
9286

87+
private fun setupFilesList() {
88+
fillAllListItems(uri!!) {
89+
updateCurrentPath("")
90+
}
91+
}
92+
9393
private fun updateCurrentPath(path: String) {
9494
currentPath = path
9595
try {
9696
val listItems = getFolderItems(currentPath)
97+
updateAdapter(listItems = listItems)
98+
} catch (e: Exception) {
99+
showErrorToast(e)
100+
}
101+
}
102+
103+
private fun updateAdapter(listItems: MutableList<ListItem>) {
104+
runOnUiThread {
97105
DecompressItemsAdapter(this, listItems, binding.decompressList) {
98106
if ((it as ListItem).isDirectory) {
99107
updateCurrentPath(it.path)
100108
}
101109
}.apply {
102110
binding.decompressList.adapter = this
103111
}
104-
} catch (e: Exception) {
105-
showErrorToast(e)
106112
}
107113
}
108114

@@ -183,13 +189,12 @@ class DecompressActivity : SimpleActivity() {
183189
}.sortedWith(compareBy({ !it.isDirectory }, { it.mName })).toMutableList() as ArrayList<ListItem>
184190
}
185191

186-
@SuppressLint("NewApi")
187-
private fun fillAllListItems(uri: Uri) {
192+
private fun fillAllListItems(uri: Uri, callback: () -> Unit) = ensureBackgroundThread {
188193
val inputStream = try {
189194
contentResolver.openInputStream(uri)
190195
} catch (e: Exception) {
191196
showErrorToast(e)
192-
return
197+
return@ensureBackgroundThread
193198
}
194199

195200
val zipInputStream = ZipInputStream(BufferedInputStream(inputStream))
@@ -206,9 +211,11 @@ class DecompressActivity : SimpleActivity() {
206211
toast(getString(R.string.invalid_password))
207212
passwordDialog?.clearPassword()
208213
} else {
209-
askForPassword()
214+
runOnUiThread {
215+
askForPassword()
216+
}
210217
}
211-
return
218+
return@ensureBackgroundThread
212219
} else {
213220
break
214221
}
@@ -220,12 +227,39 @@ class DecompressActivity : SimpleActivity() {
220227
break
221228
}
222229

230+
// Show progress bar only after password dialog is dismissed.
231+
runOnUiThread {
232+
if (binding.progressIndicator.isGone()) {
233+
binding.progressIndicator.show()
234+
}
235+
}
236+
237+
if (passwordDialog != null) {
238+
passwordDialog?.dismiss(notify = false)
239+
passwordDialog = null
240+
}
241+
223242
val lastModified = if (isOreoPlus()) zipEntry.lastModifiedTime else 0
224243
val filename = zipEntry.fileName.removeSuffix("/")
225-
val listItem = ListItem(filename, filename.getFilenameFromPath(), zipEntry.isDirectory, 0, 0L, lastModified, false, false)
226-
allFiles.add(listItem)
244+
allFiles.add(
245+
ListItem(
246+
mPath = filename,
247+
mName = filename.getFilenameFromPath(),
248+
mIsDirectory = zipEntry.isDirectory,
249+
mChildren = 0,
250+
mSize = 0L,
251+
mModified = lastModified,
252+
isSectionTitle = false,
253+
isGridTypeDivider = false
254+
)
255+
)
227256
}
228-
passwordDialog?.dismiss(notify = false)
257+
258+
runOnUiThread {
259+
binding.progressIndicator.hide()
260+
}
261+
262+
callback()
229263
}
230264

231265
private fun askForPassword() {

app/src/main/res/layout/activity_decompress.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,12 @@
3232
app:layoutManager="org.fossify.commons.views.MyLinearLayoutManager" />
3333

3434
</RelativeLayout>
35+
36+
<com.google.android.material.progressindicator.CircularProgressIndicator
37+
android:id="@+id/progress_indicator"
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
android:layout_gravity="center"
41+
android:indeterminate="true"
42+
android:visibility="gone" />
3543
</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 commit comments

Comments
 (0)