mirror of
				https://github.com/SimpleMobileTools/Simple-File-Manager.git
				synced 2025-06-05 22:09:15 +02:00 
			
		
		
		
	create new threads only when needed
This commit is contained in:
		| @@ -220,14 +220,14 @@ class MainActivity : SimpleActivity() { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun checkOTGPath() { |     private fun checkOTGPath() { | ||||||
|         Thread { |         ensureBackgroundThread { | ||||||
|             if (!config.wasOTGHandled && hasPermission(PERMISSION_WRITE_STORAGE) && hasOTGConnected() && config.OTGPath.isEmpty()) { |             if (!config.wasOTGHandled && hasPermission(PERMISSION_WRITE_STORAGE) && hasOTGConnected() && config.OTGPath.isEmpty()) { | ||||||
|                 getStorageDirectories().firstOrNull { it.trimEnd('/') != internalStoragePath && it.trimEnd('/') != sdCardPath }?.apply { |                 getStorageDirectories().firstOrNull { it.trimEnd('/') != internalStoragePath && it.trimEnd('/') != sdCardPath }?.apply { | ||||||
|                     config.wasOTGHandled = true |                     config.wasOTGHandled = true | ||||||
|                     config.OTGPath = trimEnd('/') |                     config.OTGPath = trimEnd('/') | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }.start() |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun openPath(path: String, forceRefresh: Boolean = false) { |     private fun openPath(path: String, forceRefresh: Boolean = false) { | ||||||
| @@ -336,24 +336,24 @@ class MainActivity : SimpleActivity() { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun checkIfRootAvailable() { |     private fun checkIfRootAvailable() { | ||||||
|         Thread { |         ensureBackgroundThread { | ||||||
|             config.isRootAvailable = RootTools.isRootAvailable() |             config.isRootAvailable = RootTools.isRootAvailable() | ||||||
|             if (config.isRootAvailable && config.enableRootAccess) { |             if (config.isRootAvailable && config.enableRootAccess) { | ||||||
|                 RootHelpers(this).askRootIfNeeded { |                 RootHelpers(this).askRootIfNeeded { | ||||||
|                     config.enableRootAccess = it |                     config.enableRootAccess = it | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }.start() |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun checkInvalidFavorites() { |     private fun checkInvalidFavorites() { | ||||||
|         Thread { |         ensureBackgroundThread { | ||||||
|             config.favorites.forEach { |             config.favorites.forEach { | ||||||
|                 if (!isPathOnOTG(it) && !isPathOnSD(it) && !File(it).exists()) { |                 if (!isPathOnOTG(it) && !isPathOnSD(it) && !File(it).exists()) { | ||||||
|                     config.removeFavorite(it) |                     config.removeFavorite(it) | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }.start() |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     fun pickedPath(path: String) { |     fun pickedPath(path: String) { | ||||||
|   | |||||||
| @@ -25,6 +25,7 @@ import com.simplemobiletools.commons.dialogs.* | |||||||
| import com.simplemobiletools.commons.extensions.* | import com.simplemobiletools.commons.extensions.* | ||||||
| import com.simplemobiletools.commons.helpers.CONFLICT_OVERWRITE | import com.simplemobiletools.commons.helpers.CONFLICT_OVERWRITE | ||||||
| import com.simplemobiletools.commons.helpers.CONFLICT_SKIP | import com.simplemobiletools.commons.helpers.CONFLICT_SKIP | ||||||
|  | import com.simplemobiletools.commons.helpers.ensureBackgroundThread | ||||||
| import com.simplemobiletools.commons.helpers.isOreoPlus | import com.simplemobiletools.commons.helpers.isOreoPlus | ||||||
| import com.simplemobiletools.commons.models.FileDirItem | import com.simplemobiletools.commons.models.FileDirItem | ||||||
| import com.simplemobiletools.commons.models.RadioItem | import com.simplemobiletools.commons.models.RadioItem | ||||||
| @@ -229,7 +230,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun toggleFileVisibility(hide: Boolean) { |     private fun toggleFileVisibility(hide: Boolean) { | ||||||
|         Thread { |         ensureBackgroundThread { | ||||||
|             getSelectedFileDirItems().forEach { |             getSelectedFileDirItems().forEach { | ||||||
|                 activity.toggleItemVisibility(it.path, hide) |                 activity.toggleItemVisibility(it.path, hide) | ||||||
|             } |             } | ||||||
| @@ -237,7 +238,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem | |||||||
|                 listener?.refreshItems() |                 listener?.refreshItems() | ||||||
|                 finishActMode() |                 finishActMode() | ||||||
|             } |             } | ||||||
|         }.start() |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @SuppressLint("NewApi") |     @SuppressLint("NewApi") | ||||||
| @@ -269,7 +270,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem | |||||||
|         if (activity.getIsPathDirectory(path)) { |         if (activity.getIsPathDirectory(path)) { | ||||||
|             callback() |             callback() | ||||||
|         } else { |         } else { | ||||||
|             Thread { |             ensureBackgroundThread { | ||||||
|                 val options = RequestOptions() |                 val options = RequestOptions() | ||||||
|                         .format(DecodeFormat.PREFER_ARGB_8888) |                         .format(DecodeFormat.PREFER_ARGB_8888) | ||||||
|                         .skipMemoryCache(true) |                         .skipMemoryCache(true) | ||||||
| @@ -296,7 +297,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem | |||||||
|                 activity.runOnUiThread { |                 activity.runOnUiThread { | ||||||
|                     callback() |                     callback() | ||||||
|                 } |                 } | ||||||
|             }.start() |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -370,7 +371,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem | |||||||
|  |  | ||||||
|     private fun copyMoveRootItems(files: ArrayList<FileDirItem>, destinationPath: String, isCopyOperation: Boolean) { |     private fun copyMoveRootItems(files: ArrayList<FileDirItem>, destinationPath: String, isCopyOperation: Boolean) { | ||||||
|         activity.toast(R.string.copying) |         activity.toast(R.string.copying) | ||||||
|         Thread { |         ensureBackgroundThread { | ||||||
|             val fileCnt = files.size |             val fileCnt = files.size | ||||||
|             RootHelpers(activity).copyMoveFiles(files, destinationPath, isCopyOperation) { |             RootHelpers(activity).copyMoveFiles(files, destinationPath, isCopyOperation) { | ||||||
|                 when (it) { |                 when (it) { | ||||||
| @@ -384,7 +385,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem | |||||||
|                     finishActMode() |                     finishActMode() | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }.start() |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun compressSelection() { |     private fun compressSelection() { | ||||||
| @@ -403,7 +404,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem | |||||||
|  |  | ||||||
|                 activity.toast(R.string.compressing) |                 activity.toast(R.string.compressing) | ||||||
|                 val paths = getSelectedFileDirItems().map { it.path } |                 val paths = getSelectedFileDirItems().map { it.path } | ||||||
|                 Thread { |                 ensureBackgroundThread { | ||||||
|                     if (compressPaths(paths, destination)) { |                     if (compressPaths(paths, destination)) { | ||||||
|                         activity.runOnUiThread { |                         activity.runOnUiThread { | ||||||
|                             activity.toast(R.string.compression_successful) |                             activity.toast(R.string.compression_successful) | ||||||
| @@ -413,7 +414,7 @@ class ItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem | |||||||
|                     } else { |                     } else { | ||||||
|                         activity.toast(R.string.compressing_failed) |                         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('/') |                 val destinationPath = fileDirItems.first().getParentPath().trimEnd('/') | ||||||
|                 activity.checkConflicts(fileDirItems, destinationPath, 0, LinkedHashMap()) { |                 activity.checkConflicts(fileDirItems, destinationPath, 0, LinkedHashMap()) { | ||||||
|                     Thread { |                     ensureBackgroundThread { | ||||||
|                         decompressPaths(sourcePaths, it, callback) |                         decompressPaths(sourcePaths, it, callback) | ||||||
|                     }.start() |                     } | ||||||
|                 } |                 } | ||||||
|             } catch (exception: Exception) { |             } catch (exception: Exception) { | ||||||
|                 activity.showErrorToast(exception) |                 activity.showErrorToast(exception) | ||||||
|   | |||||||
| @@ -10,6 +10,7 @@ import com.simplemobiletools.commons.activities.BaseSimpleActivity | |||||||
| import com.simplemobiletools.commons.dialogs.StoragePickerDialog | import com.simplemobiletools.commons.dialogs.StoragePickerDialog | ||||||
| import com.simplemobiletools.commons.extensions.* | import com.simplemobiletools.commons.extensions.* | ||||||
| import com.simplemobiletools.commons.helpers.SORT_BY_SIZE | import com.simplemobiletools.commons.helpers.SORT_BY_SIZE | ||||||
|  | import com.simplemobiletools.commons.helpers.ensureBackgroundThread | ||||||
| import com.simplemobiletools.commons.models.FileDirItem | import com.simplemobiletools.commons.models.FileDirItem | ||||||
| import com.simplemobiletools.commons.views.Breadcrumbs | import com.simplemobiletools.commons.views.Breadcrumbs | ||||||
| import com.simplemobiletools.commons.views.MyLinearLayoutManager | 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) { |     private fun getItems(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) { | ||||||
|         skipItemUpdating = false |         skipItemUpdating = false | ||||||
|         Thread { |         ensureBackgroundThread { | ||||||
|             if (activity?.isDestroyed == false && activity?.isFinishing == false) { |             if (activity?.isDestroyed == false && activity?.isFinishing == false) { | ||||||
|                 val config = context!!.config |                 val config = context!!.config | ||||||
|                 if (context!!.isPathOnOTG(path) && config.OTGTreeUri.isNotEmpty()) { |                 if (context!!.isPathOnOTG(path) && config.OTGTreeUri.isNotEmpty()) { | ||||||
| @@ -187,7 +188,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb | |||||||
|                     RootHelpers(activity!!).getFiles(path, callback) |                     RootHelpers(activity!!).getFiles(path, callback) | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }.start() |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun getRegularItemsOf(path: String, callback: (originalPath: String, items: ArrayList<ListItem>) -> Unit) { |     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) { |     fun searchQueryChanged(text: String) { | ||||||
|         val searchText = text.trim() |         val searchText = text.trim() | ||||||
|         lastSearchedText = searchText |         lastSearchedText = searchText | ||||||
|         Thread { |         ensureBackgroundThread { | ||||||
|             if (context == null) { |             if (context == null) { | ||||||
|                 return@Thread |                 return@ensureBackgroundThread | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             when { |             when { | ||||||
| @@ -292,7 +293,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb | |||||||
|                 else -> { |                 else -> { | ||||||
|                     val files = searchFiles(searchText, currentPath) |                     val files = searchFiles(searchText, currentPath) | ||||||
|                     if (lastSearchedText != searchText) { |                     if (lastSearchedText != searchText) { | ||||||
|                         return@Thread |                         return@ensureBackgroundThread | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                     val listItems = ArrayList<ListItem>() |                     val listItems = ArrayList<ListItem>() | ||||||
| @@ -317,7 +318,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb | |||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }.start() |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun searchFiles(text: String, path: String): ArrayList<ListItem> { |     private fun searchFiles(text: String, path: String): ArrayList<ListItem> { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user