create new threads only when needed

This commit is contained in:
tibbi 2019-12-18 11:42:49 +01:00
parent 4bee97ffd6
commit efa5f15751
3 changed files with 24 additions and 22 deletions

View File

@ -220,14 +220,14 @@ class MainActivity : SimpleActivity() {
}
private fun checkOTGPath() {
Thread {
ensureBackgroundThread {
if (!config.wasOTGHandled && hasPermission(PERMISSION_WRITE_STORAGE) && hasOTGConnected() && config.OTGPath.isEmpty()) {
getStorageDirectories().firstOrNull { it.trimEnd('/') != internalStoragePath && it.trimEnd('/') != sdCardPath }?.apply {
config.wasOTGHandled = true
config.OTGPath = trimEnd('/')
}
}
}.start()
}
}
private fun openPath(path: String, forceRefresh: Boolean = false) {
@ -336,24 +336,24 @@ class MainActivity : SimpleActivity() {
}
private fun checkIfRootAvailable() {
Thread {
ensureBackgroundThread {
config.isRootAvailable = RootTools.isRootAvailable()
if (config.isRootAvailable && config.enableRootAccess) {
RootHelpers(this).askRootIfNeeded {
config.enableRootAccess = it
}
}
}.start()
}
}
private fun checkInvalidFavorites() {
Thread {
ensureBackgroundThread {
config.favorites.forEach {
if (!isPathOnOTG(it) && !isPathOnSD(it) && !File(it).exists()) {
config.removeFavorite(it)
}
}
}.start()
}
}
fun pickedPath(path: String) {

View File

@ -25,6 +25,7 @@ import com.simplemobiletools.commons.dialogs.*
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.CONFLICT_OVERWRITE
import com.simplemobiletools.commons.helpers.CONFLICT_SKIP
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.models.RadioItem
@ -229,7 +230,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
}
private fun toggleFileVisibility(hide: Boolean) {
Thread {
ensureBackgroundThread {
getSelectedFileDirItems().forEach {
activity.toggleItemVisibility(it.path, hide)
}
@ -237,7 +238,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
listener?.refreshItems()
finishActMode()
}
}.start()
}
}
@SuppressLint("NewApi")
@ -269,7 +270,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
if (activity.getIsPathDirectory(path)) {
callback()
} else {
Thread {
ensureBackgroundThread {
val options = RequestOptions()
.format(DecodeFormat.PREFER_ARGB_8888)
.skipMemoryCache(true)
@ -296,7 +297,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
activity.runOnUiThread {
callback()
}
}.start()
}
}
}
@ -370,7 +371,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
private fun copyMoveRootItems(files: ArrayList<FileDirItem>, destinationPath: String, isCopyOperation: Boolean) {
activity.toast(R.string.copying)
Thread {
ensureBackgroundThread {
val fileCnt = files.size
RootHelpers(activity).copyMoveFiles(files, destinationPath, isCopyOperation) {
when (it) {
@ -384,7 +385,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
finishActMode()
}
}
}.start()
}
}
private fun compressSelection() {
@ -403,7 +404,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
activity.toast(R.string.compressing)
val paths = getSelectedFileDirItems().map { it.path }
Thread {
ensureBackgroundThread {
if (compressPaths(paths, destination)) {
activity.runOnUiThread {
activity.toast(R.string.compression_successful)
@ -413,7 +414,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
} else {
activity.toast(R.string.compressing_failed)
}
}.start()
}
}
}
}
@ -460,9 +461,9 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem
val destinationPath = fileDirItems.first().getParentPath().trimEnd('/')
activity.checkConflicts(fileDirItems, destinationPath, 0, LinkedHashMap()) {
Thread {
ensureBackgroundThread {
decompressPaths(sourcePaths, it, callback)
}.start()
}
}
} catch (exception: Exception) {
activity.showErrorToast(exception)

View File

@ -10,6 +10,7 @@ import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.StoragePickerDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.Breadcrumbs
import com.simplemobiletools.commons.views.MyLinearLayoutManager
@ -173,7 +174,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
private fun getItems(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) {
skipItemUpdating = false
Thread {
ensureBackgroundThread {
if (activity?.isDestroyed == false && activity?.isFinishing == false) {
val config = context!!.config
if (context!!.isPathOnOTG(path) && config.OTGTreeUri.isNotEmpty()) {
@ -187,7 +188,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
RootHelpers(activity!!).getFiles(path, callback)
}
}
}.start()
}
}
private fun getRegularItemsOf(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) {
@ -268,9 +269,9 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
fun searchQueryChanged(text: String) {
val searchText = text.trim()
lastSearchedText = searchText
Thread {
ensureBackgroundThread {
if (context == null) {
return@Thread
return@ensureBackgroundThread
}
when {
@ -292,7 +293,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
else -> {
val files = searchFiles(searchText, currentPath)
if (lastSearchedText != searchText) {
return@Thread
return@ensureBackgroundThread
}
val listItems = ArrayList<ListItem>()
@ -317,7 +318,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
}
}
}
}.start()
}
}
private fun searchFiles(text: String, path: String): ArrayList<ListItem> {