Skip to content

Commit 706da69

Browse files
authored
Merge pull request #66 from FossifyOrg/fix_various_anrs
Fix various ANRs
2 parents d78f7cb + 3c93a2d commit 706da69

File tree

4 files changed

+101
-41
lines changed

4 files changed

+101
-41
lines changed

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

Lines changed: 59 additions & 17 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,25 +84,45 @@ 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

109115
private fun decompressFiles() {
110116
val defaultFolder = getRealPathFromURI(uri!!) ?: internalStoragePath
111-
FilePickerDialog(this, defaultFolder, false, config.showHidden, true, true, showFavoritesButton = true) { destination ->
117+
FilePickerDialog(
118+
activity = this,
119+
currPath = defaultFolder,
120+
pickFile = false,
121+
showHidden = config.showHidden,
122+
showFAB = true,
123+
canAddShowHiddenButton = true,
124+
showFavoritesButton = true
125+
) { destination ->
112126
handleSAFDialog(destination) {
113127
if (it) {
114128
ensureBackgroundThread {
@@ -183,13 +197,12 @@ class DecompressActivity : SimpleActivity() {
183197
}.sortedWith(compareBy({ !it.isDirectory }, { it.mName })).toMutableList() as ArrayList<ListItem>
184198
}
185199

186-
@SuppressLint("NewApi")
187-
private fun fillAllListItems(uri: Uri) {
200+
private fun fillAllListItems(uri: Uri, callback: () -> Unit) = ensureBackgroundThread {
188201
val inputStream = try {
189202
contentResolver.openInputStream(uri)
190203
} catch (e: Exception) {
191204
showErrorToast(e)
192-
return
205+
return@ensureBackgroundThread
193206
}
194207

195208
val zipInputStream = ZipInputStream(BufferedInputStream(inputStream))
@@ -206,9 +219,11 @@ class DecompressActivity : SimpleActivity() {
206219
toast(getString(R.string.invalid_password))
207220
passwordDialog?.clearPassword()
208221
} else {
209-
askForPassword()
222+
runOnUiThread {
223+
askForPassword()
224+
}
210225
}
211-
return
226+
return@ensureBackgroundThread
212227
} else {
213228
break
214229
}
@@ -220,12 +235,39 @@ class DecompressActivity : SimpleActivity() {
220235
break
221236
}
222237

238+
// Show progress bar only after password dialog is dismissed.
239+
runOnUiThread {
240+
if (binding.progressIndicator.isGone()) {
241+
binding.progressIndicator.show()
242+
}
243+
}
244+
245+
if (passwordDialog != null) {
246+
passwordDialog?.dismiss(notify = false)
247+
passwordDialog = null
248+
}
249+
223250
val lastModified = if (isOreoPlus()) zipEntry.lastModifiedTime else 0
224251
val filename = zipEntry.fileName.removeSuffix("/")
225-
val listItem = ListItem(filename, filename.getFilenameFromPath(), zipEntry.isDirectory, 0, 0L, lastModified, false, false)
226-
allFiles.add(listItem)
252+
allFiles.add(
253+
ListItem(
254+
mPath = filename,
255+
mName = filename.getFilenameFromPath(),
256+
mIsDirectory = zipEntry.isDirectory,
257+
mChildren = 0,
258+
mSize = 0L,
259+
mModified = lastModified,
260+
isSectionTitle = false,
261+
isGridTypeDivider = false
262+
)
263+
)
227264
}
228-
passwordDialog?.dismiss(notify = false)
265+
266+
runOnUiThread {
267+
binding.progressIndicator.hide()
268+
}
269+
270+
callback()
229271
}
230272

231273
private fun askForPassword() {

app/src/main/kotlin/org/fossify/filemanager/adapters/DecompressItemsAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class DecompressItemsAdapter(activity: SimpleActivity, var listItems: MutableLis
131131
}
132132

133133
private fun initDrawables() {
134-
folderDrawable = resources.getColoredDrawableWithColor(R.drawable.ic_folder_vector, textColor)
134+
folderDrawable = resources.getColoredDrawableWithColor(R.drawable.ic_folder_vector, properPrimaryColor)
135135
folderDrawable.alpha = 180
136136
fileDrawable = resources.getDrawable(R.drawable.ic_file_generic)
137137
fileDrawables = getFilePlaceholderDrawables(activity)

app/src/main/kotlin/org/fossify/filemanager/adapters/ItemsAdapter.kt

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ import java.util.LinkedList
5757
import java.util.Locale
5858

5959
class ItemsAdapter(
60-
activity: SimpleActivity, var listItems: MutableList<ListItem>, val listener: ItemOperationsListener?, recyclerView: MyRecyclerView,
61-
val isPickMultipleIntent: Boolean, val swipeRefreshLayout: SwipeRefreshLayout?, canHaveIndividualViewType: Boolean = true, itemClick: (Any) -> Unit
60+
activity: SimpleActivity,
61+
var listItems: MutableList<ListItem>,
62+
private val listener: ItemOperationsListener?,
63+
recyclerView: MyRecyclerView,
64+
private val isPickMultipleIntent: Boolean,
65+
private val swipeRefreshLayout: SwipeRefreshLayout?,
66+
canHaveIndividualViewType: Boolean = true,
67+
itemClick: (Any) -> Unit,
6268
) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), RecyclerViewFastScroller.OnPopupTextUpdate {
6369

6470
private lateinit var fileDrawable: Drawable
@@ -401,12 +407,12 @@ class ItemsAdapter(
401407
val firstFile = files[0]
402408
val source = firstFile.getParentPath()
403409
FilePickerDialog(
404-
activity,
405-
activity.getDefaultCopyDestinationPath(config.shouldShowHidden(), source),
406-
false,
407-
config.shouldShowHidden(),
408-
true,
409-
true,
410+
activity = activity,
411+
currPath = activity.getDefaultCopyDestinationPath(config.shouldShowHidden(), source),
412+
pickFile = false,
413+
showHidden = config.shouldShowHidden(),
414+
showFAB = true,
415+
canAddShowHiddenButton = true,
410416
showFavoritesButton = true
411417
) {
412418
config.lastCopyPath = it
@@ -749,28 +755,32 @@ class ItemsAdapter(
749755
return
750756
}
751757

752-
activity.handleSAFDialog(SAFPath) {
753-
if (!it) {
758+
activity.handleSAFDialog(SAFPath) { granted ->
759+
if (!granted) {
754760
return@handleSAFDialog
755761
}
756762

757763
val files = ArrayList<FileDirItem>(selectedKeys.size)
758764
val positions = ArrayList<Int>()
759-
selectedKeys.forEach {
760-
config.removeFavorite(getItemWithKey(it)?.path ?: "")
761-
val key = it
762-
val position = listItems.indexOfFirst { it.path.hashCode() == key }
763-
if (position != -1) {
764-
positions.add(position)
765-
files.add(listItems[position])
765+
766+
ensureBackgroundThread {
767+
selectedKeys.forEach { key ->
768+
config.removeFavorite(getItemWithKey(key)?.path ?: "")
769+
val position = listItems.indexOfFirst { it.path.hashCode() == key }
770+
if (position != -1) {
771+
positions.add(position)
772+
files.add(listItems[position])
773+
}
766774
}
767-
}
768775

769-
positions.sortDescending()
770-
removeSelectedItems(positions)
771-
listener?.deleteFiles(files)
772-
positions.forEach {
773-
listItems.removeAt(it)
776+
positions.sortDescending()
777+
activity.runOnUiThread {
778+
removeSelectedItems(positions)
779+
listener?.deleteFiles(files)
780+
positions.forEach {
781+
listItems.removeAt(it)
782+
}
783+
}
774784
}
775785
}
776786
}

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)