Skip to content

Commit efa5f15

Browse files
committed
create new threads only when needed
1 parent 4bee97f commit efa5f15

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ class MainActivity : SimpleActivity() {
220220
}
221221

222222
private fun checkOTGPath() {
223-
Thread {
223+
ensureBackgroundThread {
224224
if (!config.wasOTGHandled && hasPermission(PERMISSION_WRITE_STORAGE) && hasOTGConnected() && config.OTGPath.isEmpty()) {
225225
getStorageDirectories().firstOrNull { it.trimEnd('/') != internalStoragePath && it.trimEnd('/') != sdCardPath }?.apply {
226226
config.wasOTGHandled = true
227227
config.OTGPath = trimEnd('/')
228228
}
229229
}
230-
}.start()
230+
}
231231
}
232232

233233
private fun openPath(path: String, forceRefresh: Boolean = false) {
@@ -336,24 +336,24 @@ class MainActivity : SimpleActivity() {
336336
}
337337

338338
private fun checkIfRootAvailable() {
339-
Thread {
339+
ensureBackgroundThread {
340340
config.isRootAvailable = RootTools.isRootAvailable()
341341
if (config.isRootAvailable && config.enableRootAccess) {
342342
RootHelpers(this).askRootIfNeeded {
343343
config.enableRootAccess = it
344344
}
345345
}
346-
}.start()
346+
}
347347
}
348348

349349
private fun checkInvalidFavorites() {
350-
Thread {
350+
ensureBackgroundThread {
351351
config.favorites.forEach {
352352
if (!isPathOnOTG(it) && !isPathOnSD(it) && !File(it).exists()) {
353353
config.removeFavorite(it)
354354
}
355355
}
356-
}.start()
356+
}
357357
}
358358

359359
fun pickedPath(path: String) {

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/ItemsAdapter.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.simplemobiletools.commons.dialogs.*
2525
import com.simplemobiletools.commons.extensions.*
2626
import com.simplemobiletools.commons.helpers.CONFLICT_OVERWRITE
2727
import com.simplemobiletools.commons.helpers.CONFLICT_SKIP
28+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
2829
import com.simplemobiletools.commons.helpers.isOreoPlus
2930
import com.simplemobiletools.commons.models.FileDirItem
3031
import com.simplemobiletools.commons.models.RadioItem
@@ -229,15 +230,15 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
229230
}
230231

231232
private fun toggleFileVisibility(hide: Boolean) {
232-
Thread {
233+
ensureBackgroundThread {
233234
getSelectedFileDirItems().forEach {
234235
activity.toggleItemVisibility(it.path, hide)
235236
}
236237
activity.runOnUiThread {
237238
listener?.refreshItems()
238239
finishActMode()
239240
}
240-
}.start()
241+
}
241242
}
242243

243244
@SuppressLint("NewApi")
@@ -269,7 +270,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
269270
if (activity.getIsPathDirectory(path)) {
270271
callback()
271272
} else {
272-
Thread {
273+
ensureBackgroundThread {
273274
val options = RequestOptions()
274275
.format(DecodeFormat.PREFER_ARGB_8888)
275276
.skipMemoryCache(true)
@@ -296,7 +297,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
296297
activity.runOnUiThread {
297298
callback()
298299
}
299-
}.start()
300+
}
300301
}
301302
}
302303

@@ -370,7 +371,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
370371

371372
private fun copyMoveRootItems(files: ArrayList<FileDirItem>, destinationPath: String, isCopyOperation: Boolean) {
372373
activity.toast(R.string.copying)
373-
Thread {
374+
ensureBackgroundThread {
374375
val fileCnt = files.size
375376
RootHelpers(activity).copyMoveFiles(files, destinationPath, isCopyOperation) {
376377
when (it) {
@@ -384,7 +385,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
384385
finishActMode()
385386
}
386387
}
387-
}.start()
388+
}
388389
}
389390

390391
private fun compressSelection() {
@@ -403,7 +404,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
403404

404405
activity.toast(R.string.compressing)
405406
val paths = getSelectedFileDirItems().map { it.path }
406-
Thread {
407+
ensureBackgroundThread {
407408
if (compressPaths(paths, destination)) {
408409
activity.runOnUiThread {
409410
activity.toast(R.string.compression_successful)
@@ -413,7 +414,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
413414
} else {
414415
activity.toast(R.string.compressing_failed)
415416
}
416-
}.start()
417+
}
417418
}
418419
}
419420
}
@@ -460,9 +461,9 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
460461

461462
val destinationPath = fileDirItems.first().getParentPath().trimEnd('/')
462463
activity.checkConflicts(fileDirItems, destinationPath, 0, LinkedHashMap()) {
463-
Thread {
464+
ensureBackgroundThread {
464465
decompressPaths(sourcePaths, it, callback)
465-
}.start()
466+
}
466467
}
467468
} catch (exception: Exception) {
468469
activity.showErrorToast(exception)

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/ItemsFragment.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.simplemobiletools.commons.activities.BaseSimpleActivity
1010
import com.simplemobiletools.commons.dialogs.StoragePickerDialog
1111
import com.simplemobiletools.commons.extensions.*
1212
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
13+
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
1314
import com.simplemobiletools.commons.models.FileDirItem
1415
import com.simplemobiletools.commons.views.Breadcrumbs
1516
import com.simplemobiletools.commons.views.MyLinearLayoutManager
@@ -173,7 +174,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
173174

174175
private fun getItems(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) {
175176
skipItemUpdating = false
176-
Thread {
177+
ensureBackgroundThread {
177178
if (activity?.isDestroyed == false && activity?.isFinishing == false) {
178179
val config = context!!.config
179180
if (context!!.isPathOnOTG(path) && config.OTGTreeUri.isNotEmpty()) {
@@ -187,7 +188,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
187188
RootHelpers(activity!!).getFiles(path, callback)
188189
}
189190
}
190-
}.start()
191+
}
191192
}
192193

193194
private fun getRegularItemsOf(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) {
@@ -268,9 +269,9 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
268269
fun searchQueryChanged(text: String) {
269270
val searchText = text.trim()
270271
lastSearchedText = searchText
271-
Thread {
272+
ensureBackgroundThread {
272273
if (context == null) {
273-
return@Thread
274+
return@ensureBackgroundThread
274275
}
275276

276277
when {
@@ -292,7 +293,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
292293
else -> {
293294
val files = searchFiles(searchText, currentPath)
294295
if (lastSearchedText != searchText) {
295-
return@Thread
296+
return@ensureBackgroundThread
296297
}
297298

298299
val listItems = ArrayList<ListItem>()
@@ -317,7 +318,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
317318
}
318319
}
319320
}
320-
}.start()
321+
}
321322
}
322323

323324
private fun searchFiles(text: String, path: String): ArrayList<ListItem> {

0 commit comments

Comments
 (0)