diff --git a/app/build.gradle b/app/build.gradle index 591f7345c..7ee3799c6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -61,7 +61,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.14.0' + implementation 'com.simplemobiletools:commons:5.14.2' implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0' implementation 'androidx.multidex:multidex:2.0.1' implementation 'it.sephiroth.android.exif:library:1.0.1' diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/EditActivity.kt index 6a395f3e0..f65b800fe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/EditActivity.kt @@ -26,10 +26,7 @@ import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.target.Target import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE -import com.simplemobiletools.commons.helpers.REAL_FILE_PATH -import com.simplemobiletools.commons.helpers.SIDELOADING_TRUE -import com.simplemobiletools.commons.helpers.isNougatPlus +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.gallery.pro.BuildConfig import com.simplemobiletools.gallery.pro.R @@ -259,9 +256,9 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener if (!wasDrawCanvasPositioned) { wasDrawCanvasPositioned = true editor_draw_canvas.onGlobalLayout { - Thread { + ensureBackgroundThread { fillCanvasBackground() - }.start() + } } } } @@ -336,7 +333,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener bottom_actions_filter_list.adapter = null bottom_actions_filter_list.beGone() - Thread { + ensureBackgroundThread { try { val originalBitmap = Glide.with(applicationContext).asBitmap().load(uri).submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get() currentFilter.filter.processFilter(originalBitmap) @@ -344,23 +341,23 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } catch (e: OutOfMemoryError) { toast(R.string.out_of_memory_error) } - }.start() + } } } } private fun shareImage() { - Thread { + ensureBackgroundThread { when { default_image_view.isVisible() -> { val currentFilter = getFiltersAdapter()?.getCurrentFilter() if (currentFilter == null) { toast(R.string.unknown_error_occurred) - return@Thread + return@ensureBackgroundThread } val originalBitmap = Glide.with(applicationContext).asBitmap().load(uri).submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get() - currentFilter!!.filter.processFilter(originalBitmap) + currentFilter.filter.processFilter(originalBitmap) shareBitmap(originalBitmap) } crop_image_view.isVisible() -> { @@ -371,12 +368,12 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } editor_draw_canvas.isVisible() -> shareBitmap(editor_draw_canvas.getBitmap()) } - }.start() + } } private fun getTempImagePath(bitmap: Bitmap, callback: (path: String?) -> Unit) { val bytes = ByteArrayOutputStream() - bitmap.compress(Bitmap.CompressFormat.PNG, 0, bytes) + bitmap.compress(CompressFormat.PNG, 0, bytes) val folder = File(cacheDir, TEMP_FOLDER_NAME) if (!folder.exists()) { @@ -581,7 +578,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener bottom_editor_draw_actions.beVisibleIf(currPrimaryAction == PRIMARY_ACTION_DRAW) if (currPrimaryAction == PRIMARY_ACTION_FILTER && bottom_actions_filter_list.adapter == null) { - Thread { + ensureBackgroundThread { val thumbnailSize = resources.getDimension(R.dimen.bottom_filters_thumbnail_size).toInt() val bitmap = try { @@ -600,7 +597,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener } catch (e: GlideException) { showErrorToast(e) finish() - return@Thread + return@ensureBackgroundThread } runOnUiThread { @@ -630,7 +627,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener bottom_actions_filter_list.adapter = adapter adapter.notifyDataSetChanged() } - }.start() + } } if (currPrimaryAction != PRIMARY_ACTION_CROP_ROTATE) { @@ -811,7 +808,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener private fun saveBitmapToFile(bitmap: Bitmap, path: String, showSavingToast: Boolean) { try { - Thread { + ensureBackgroundThread { val file = File(path) val fileDirItem = FileDirItem(path, path.getFilenameFromPath()) getFileOutputStream(fileDirItem, true) { @@ -821,7 +818,7 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener toast(R.string.image_editing_failed) } } - }.start() + } } catch (e: Exception) { showErrorToast(e) } catch (e: OutOfMemoryError) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/HiddenFoldersActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/HiddenFoldersActivity.kt index 98d5eb9d9..a7a5002ba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/HiddenFoldersActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/HiddenFoldersActivity.kt @@ -5,6 +5,7 @@ import android.view.Menu import android.view.MenuItem import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.extensions.beVisibleIf +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.adapters.ManageHiddenFoldersAdapter @@ -55,11 +56,11 @@ class HiddenFoldersActivity : SimpleActivity(), RefreshRecyclerViewListener { private fun addFolder() { FilePickerDialog(this, config.lastFilepickerPath, false, config.shouldShowHidden, false, true) { config.lastFilepickerPath = it - Thread { + ensureBackgroundThread { addNoMedia(it) { updateFolders() } - }.start() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt index fcf7f04de..4b25b4bb0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MainActivity.kt @@ -346,7 +346,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } 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 @@ -364,7 +364,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } } } - }.start() + } } private fun checkDefaultSpamFolders() { @@ -434,9 +434,9 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { if (config.directorySorting and SORT_BY_DATE_MODIFIED > 0 || config.directorySorting and SORT_BY_DATE_TAKEN > 0) { getDirectories() } else { - Thread { + ensureBackgroundThread { gotDirectories(getCurrentlyDisplayedDirs()) - }.start() + } } } } @@ -540,11 +540,11 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { refreshItems() } - Thread { + ensureBackgroundThread { folders.filter { !it.exists() }.forEach { mDirectoryDao.deleteDirPath(it.absolutePath) } - }.start() + } } } @@ -629,22 +629,22 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { private fun toggleRecycleBin(show: Boolean) { config.showRecycleBinAtFolders = show invalidateOptionsMenu() - Thread { + ensureBackgroundThread { var dirs = getCurrentlyDisplayedDirs() if (!show) { dirs = dirs.filter { it.path != RECYCLE_BIN } as ArrayList } gotDirectories(dirs) - }.start() + } } private fun createNewFolder() { FilePickerDialog(this, internalStoragePath, false, config.shouldShowHidden, false, true) { CreateNewFolderDialog(this, it) { config.tempFolderPath = it - Thread { + ensureBackgroundThread { gotDirectories(addTempFolderIfNeeded(getCurrentlyDisplayedDirs())) - }.start() + } } } } @@ -1098,12 +1098,12 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { ?: "" private fun setupLatestMediaId() { - Thread { + ensureBackgroundThread { if (hasPermission(PERMISSION_READ_STORAGE)) { mLatestMediaId = getLatestMediaId() mLatestMediaDateId = getLatestMediaByDateId() } - }.start() + } } private fun checkLastMediaChanged() { @@ -1112,7 +1112,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } mLastMediaHandler.postDelayed({ - Thread { + ensureBackgroundThread { val mediaId = getLatestMediaId() val mediaDateId = getLatestMediaByDateId() if (mLatestMediaId != mediaId || mLatestMediaDateId != mediaDateId) { @@ -1125,7 +1125,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { mLastMediaHandler.removeCallbacksAndMessages(null) checkLastMediaChanged() } - }.start() + } }, LAST_MEDIA_CHECK_PERIOD) } @@ -1133,12 +1133,12 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { if (config.useRecycleBin && config.lastBinCheck < System.currentTimeMillis() - DAY_SECONDS * 1000) { config.lastBinCheck = System.currentTimeMillis() Handler().postDelayed({ - Thread { + ensureBackgroundThread { try { mMediumDao.deleteOldRecycleBinItems(System.currentTimeMillis() - MONTH_MILLISECONDS) } catch (e: Exception) { } - }.start() + } }, 3000L) } } @@ -1147,7 +1147,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { // /storage/emulated/0/Android/data/com.facebook.orca/files/stickers/175139712676531/209575122566323 // /storage/emulated/0/Android/data/com.facebook.orca/files/stickers/497837993632037/499671223448714 private fun excludeSpamFolders() { - Thread { + ensureBackgroundThread { try { val internalPath = internalStoragePath val checkedPaths = ArrayList() @@ -1184,7 +1184,7 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } } catch (e: Exception) { } - }.start() + } } override fun refreshItems() { @@ -1192,16 +1192,16 @@ class MainActivity : SimpleActivity(), DirectoryOperationsListener { } override fun recheckPinnedFolders() { - Thread { + ensureBackgroundThread { gotDirectories(movePinnedDirectoriesToFront(getCurrentlyDisplayedDirs())) - }.start() + } } override fun updateDirectories(directories: ArrayList) { - Thread { + ensureBackgroundThread { storeDirectoryItems(directories, mDirectoryDao) removeInvalidDBDirectories() - }.start() + } } private fun checkWhatsNewDialog() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt index dfae84429..f79e0a1f5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/MediaActivity.kt @@ -27,6 +27,7 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.REQUEST_EDIT_IMAGE import com.simplemobiletools.commons.helpers.SORT_BY_RANDOM +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.views.MyGridLayoutManager import com.simplemobiletools.commons.views.MyRecyclerView @@ -327,7 +328,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } private fun searchQueryChanged(text: String) { - Thread { + ensureBackgroundThread { try { val filtered = mMedia.filter { it is Medium && it.name.contains(text, true) } as ArrayList filtered.sortBy { it is Medium && !it.name.startsWith(text, true) } @@ -345,7 +346,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } } catch (ignored: Exception) { } - }.start() + } } private fun tryLoadGallery() { @@ -435,7 +436,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { mLastMediaHandler.removeCallbacksAndMessages(null) mLastMediaHandler.postDelayed({ - Thread { + ensureBackgroundThread { val mediaId = getLatestMediaId() val mediaDateId = getLatestMediaByDateId() if (mLatestMediaId != mediaId || mLatestMediaDateId != mediaDateId) { @@ -447,7 +448,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } else { checkLastMediaChanged() } - }.start() + } }, LAST_MEDIA_CHECK_PERIOD) } @@ -487,9 +488,9 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { private fun restoreAllFiles() { val paths = mMedia.filter { it is Medium }.map { (it as Medium).path } as ArrayList restoreRecycleBinPaths(paths, mMediumDao) { - Thread { + ensureBackgroundThread { mDirectoryDao.deleteDirPath(RECYCLE_BIN) - }.start() + } finish() } } @@ -593,7 +594,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { private fun startAsyncTask() { mCurrAsyncTask?.stopFetching() mCurrAsyncTask = GetMediaAsynctask(applicationContext, mPath, mIsGetImageIntent, mIsGetVideoIntent, mShowAll) { - Thread { + ensureBackgroundThread { val oldMedia = mMedia.clone() as ArrayList val newMedia = it gotMedia(newMedia, false) @@ -603,7 +604,7 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } } catch (e: Exception) { } - }.start() + } } mCurrAsyncTask!!.execute() @@ -617,9 +618,9 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } if (mPath == FAVORITES) { - Thread { + ensureBackgroundThread { mDirectoryDao.deleteDirPath(FAVORITES) - }.start() + } } finish() @@ -630,9 +631,9 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { } private fun deleteDBDirectory() { - Thread { + ensureBackgroundThread { mDirectoryDao.deleteDirPath(mPath) - }.start() + } } private fun createNewFolder() { @@ -910,14 +911,14 @@ class MediaActivity : SimpleActivity(), MediaOperationsListener { mMedia.removeAll { filtered.map { it.path }.contains((it as? Medium)?.path) } - Thread { + ensureBackgroundThread { val useRecycleBin = config.useRecycleBin filtered.forEach { if (it.path.startsWith(recycleBinPath) || !useRecycleBin) { deleteDBPath(mMediumDao, it.path) } } - }.start() + } if (mMedia.isEmpty()) { deleteDirectoryIfEmpty() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/PanoramaPhotoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/PanoramaPhotoActivity.kt index fa8dcfb54..c69cd5271 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/PanoramaPhotoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/PanoramaPhotoActivity.kt @@ -15,6 +15,7 @@ import com.simplemobiletools.commons.extensions.onGlobalLayout import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.helpers.PATH @@ -94,7 +95,7 @@ open class PanoramaPhotoActivity : SimpleActivity() { try { val options = VrPanoramaView.Options() options.inputType = VrPanoramaView.Options.TYPE_MONO - Thread { + ensureBackgroundThread { val bitmap = getBitmapToLoad(path) runOnUiThread { panorama_view.apply { @@ -120,7 +121,7 @@ open class PanoramaPhotoActivity : SimpleActivity() { }) } } - }.start() + } } catch (e: Exception) { showErrorToast(e) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt index 663f1b9bd..33901c1d4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SearchActivity.kt @@ -13,6 +13,7 @@ import androidx.core.view.MenuItemCompat import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.views.MyGridLayoutManager import com.simplemobiletools.gallery.pro.R @@ -91,7 +92,7 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener { } private fun textChanged(text: String) { - Thread { + ensureBackgroundThread { try { val filtered = mAllMedia.filter { it is Medium && it.name.contains(text, true) } as ArrayList filtered.sortBy { it is Medium && !it.name.startsWith(text, true) } @@ -109,7 +110,7 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener { } } catch (ignored: Exception) { } - }.start() + } } private fun setupAdapter() { @@ -323,14 +324,14 @@ class SearchActivity : SimpleActivity(), MediaOperationsListener { mAllMedia.removeAll { filtered.map { it.path }.contains((it as? Medium)?.path) } - Thread { + ensureBackgroundThread { val useRecycleBin = config.useRecycleBin filtered.forEach { if (it.path.startsWith(recycleBinPath) || !useRecycleBin) { deleteDBPath(galleryDB.MediumDao(), it.path) } } - }.start() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SetWallpaperActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SetWallpaperActivity.kt index 946e0176f..2ceafc06b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SetWallpaperActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SetWallpaperActivity.kt @@ -11,6 +11,7 @@ import android.view.Menu import android.view.MenuItem import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.isNougatPlus import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.gallery.pro.R @@ -117,7 +118,7 @@ class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageComplete if (result.error == null) { toast(R.string.setting_wallpaper) - Thread { + ensureBackgroundThread { val bitmap = result.bitmap val wantedHeight = wallpaperManager.desiredMinimumHeight val ratio = wantedHeight / bitmap.height.toFloat() @@ -135,7 +136,7 @@ class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageComplete setResult(Activity.RESULT_CANCELED) } finish() - }.start() + } } else { toast("${getString(R.string.image_editing_failed)}: ${result.error.message}") } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt index a8df6dc58..c1e278770 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SettingsActivity.kt @@ -572,7 +572,7 @@ class SettingsActivity : SimpleActivity() { } private fun setupEmptyRecycleBin() { - Thread { + ensureBackgroundThread { try { mRecycleBinContentSize = galleryDB.MediumDao().getDeletedMedia().sumByLong { it.size } } catch (ignored: Exception) { @@ -580,7 +580,7 @@ class SettingsActivity : SimpleActivity() { runOnUiThread { settings_empty_recycle_bin_size.text = mRecycleBinContentSize.formatSize() } - }.start() + } settings_empty_recycle_bin_holder.setOnClickListener { if (mRecycleBinContentSize == 0L) { @@ -686,13 +686,13 @@ class SettingsActivity : SimpleActivity() { private fun setupImportSettings() { settings_import_holder.setOnClickListener { FilePickerDialog(this) { - Thread { + ensureBackgroundThread { try { parseFile(it) } catch (e: Exception) { showErrorToast(e) } - }.start() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SimpleActivity.kt index 8b3505972..da2a95982 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SimpleActivity.kt @@ -10,6 +10,7 @@ import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.extensions.getParentPath import com.simplemobiletools.commons.extensions.getRealPathFromURI import com.simplemobiletools.commons.extensions.scanPathRecursively +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.isPiePlus import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.extensions.addPathToDB @@ -87,9 +88,9 @@ open class SimpleActivity : BaseSimpleActivity() { config.lastFilepickerPath = it config.addIncludedFolder(it) callback() - Thread { + ensureBackgroundThread { scanPathRecursively(it) - }.start() + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt index a1f0c225e..bed9bc018 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/VideoPlayerActivity.kt @@ -25,6 +25,7 @@ import com.google.android.exoplayer2.upstream.DataSpec import com.google.android.exoplayer2.video.VideoListener import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.helpers.* @@ -566,10 +567,10 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen private fun releaseExoPlayer() { mExoPlayer?.stop() - Thread { + ensureBackgroundThread { mExoPlayer?.release() mExoPlayer = null - }.start() + } } override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { @@ -602,9 +603,9 @@ open class VideoPlayerActivity : SimpleActivity(), SeekBar.OnSeekBarChangeListen override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?) = false override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) { - Thread { + ensureBackgroundThread { mExoPlayer?.setVideoSurface(Surface(video_surface!!.surfaceTexture)) - }.start() + } } override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index eedd54868..e6418911f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -335,7 +335,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } if (intent.action == "com.android.camera.action.REVIEW") { - Thread { + ensureBackgroundThread { val mediumDao = galleryDB.MediumDao() if (mediumDao.getMediaFromPath(mPath).isEmpty()) { val type = when { @@ -352,7 +352,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val medium = Medium(null, mPath.getFilenameFromPath(), mPath, mPath.getParentPath(), ts, ts, File(mPath).length(), type, duration, isFavorite, 0) mediumDao.insert(medium) } - }.start() + } } } @@ -362,9 +362,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } private fun initFavorites() { - Thread { + ensureBackgroundThread { mFavoritePaths = getFavoritePaths() - }.start() + } } private fun setupOrientation() { @@ -635,14 +635,14 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View val newPath = it handleSAFDialog(it) { toast(R.string.saving) - Thread { - val photoFragment = getCurrentPhotoFragment() ?: return@Thread + ensureBackgroundThread { + val photoFragment = getCurrentPhotoFragment() ?: return@ensureBackgroundThread saveRotatedImageToFile(currPath, newPath, photoFragment.mCurrentRotationDegrees, true) { toast(R.string.file_saved) getCurrentPhotoFragment()?.mCurrentRotationDegrees = 0 invalidateOptionsMenu() } - }.start() + } } } } @@ -874,7 +874,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun toggleFavorite() { val medium = getCurrentMedium() ?: return medium.isFavorite = !medium.isFavorite - Thread { + ensureBackgroundThread { galleryDB.MediumDao().updateFavorite(medium.path, medium.isFavorite) if (medium.isFavorite) { mFavoritePaths.add(medium.path) @@ -882,7 +882,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View mFavoritePaths.remove(medium.path) } invalidateOptionsMenu() - }.start() + } } private fun printFile() { @@ -1053,9 +1053,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View name = it.getFilenameFromPath() } - Thread { + ensureBackgroundThread { updateDBMediaPath(oldPath, it) - }.start() + } updateActionbarTitle() } } @@ -1156,8 +1156,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } override fun launchViewVideoIntent(path: String) { - Thread { - val newUri = getFinalUriFromPath(path, BuildConfig.APPLICATION_ID) ?: return@Thread + ensureBackgroundThread { + val newUri = getFinalUriFromPath(path, BuildConfig.APPLICATION_ID) ?: return@ensureBackgroundThread val mimeType = getUriMimeType(path, newUri) Intent().apply { action = Intent.ACTION_VIEW @@ -1180,7 +1180,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View } } } - }.start() + } } private fun checkSystemUI() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt index 20feaa695..f34a23e9b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/WidgetConfigureActivity.kt @@ -10,6 +10,7 @@ import android.widget.RelativeLayout import android.widget.RemoteViews import com.simplemobiletools.commons.dialogs.ColorPickerDialog import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.pro.extensions.* @@ -89,9 +90,9 @@ class WidgetConfigureActivity : SimpleActivity() { AppWidgetManager.getInstance(this).updateAppWidget(mWidgetId, views) config.showWidgetFolderName = folder_picker_show_folder_name.isChecked val widget = Widget(null, mWidgetId, mFolderPath) - Thread { + ensureBackgroundThread { widgetsDB.insertOrUpdate(widget) - }.start() + } storeWidgetColors() requestWidgetUpdate() @@ -161,14 +162,14 @@ class WidgetConfigureActivity : SimpleActivity() { config_folder_name.text = getFolderNameFromPath(folderPath) } - Thread { + ensureBackgroundThread { val path = directoryDB.getDirectoryThumbnail(folderPath) if (path != null) { runOnUiThread { loadJpg(path, config_image, config.cropThumbnails) } } - }.start() + } } private fun handleFolderNameDisplay() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt index fc6e60851..2d3256258 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/DirectoryAdapter.kt @@ -17,6 +17,7 @@ import com.simplemobiletools.commons.dialogs.PropertiesDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.dialogs.RenameItemsDialog import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.isOreoPlus import com.simplemobiletools.commons.models.FileDirItem import com.simplemobiletools.commons.views.FastScroller @@ -170,10 +171,10 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: ArrayList Unit)? = null) { deleteFile(fileDirItem, allowDeleteFolder) { if (deleteFromDatabase) { - Thread { + ensureBackgroundThread { deleteDBPath(galleryDB.MediumDao(), fileDirItem.path) runOnUiThread { callback?.invoke(it) } - }.start() + } } else { callback?.invoke(it) } @@ -225,7 +225,7 @@ fun BaseSimpleActivity.tryDeleteFileDirItem(fileDirItem: FileDirItem, allowDelet } fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, mediumDao: MediumDao = galleryDB.MediumDao(), callback: ((wasSuccess: Boolean) -> Unit)?) { - Thread { + ensureBackgroundThread { var pathsCnt = paths.size paths.forEach { val file = File(it) @@ -246,7 +246,7 @@ fun BaseSimpleActivity.movePathsInRecycleBin(paths: ArrayList, mediumDao } } callback?.invoke(pathsCnt == 0) - }.start() + } } fun BaseSimpleActivity.restoreRecycleBinPath(path: String, callback: () -> Unit) { @@ -254,7 +254,7 @@ fun BaseSimpleActivity.restoreRecycleBinPath(path: String, callback: () -> Unit) } fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDao: MediumDao = galleryDB.MediumDao(), callback: () -> Unit) { - Thread { + ensureBackgroundThread { val newPaths = ArrayList() paths.forEach { val source = it @@ -288,11 +288,11 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDa } fixDateTaken(newPaths, false) - }.start() + } } fun BaseSimpleActivity.emptyTheRecycleBin(callback: (() -> Unit)? = null) { - Thread { + ensureBackgroundThread { try { recycleBin.deleteRecursively() galleryDB.MediumDao().clearRecycleBin() @@ -302,16 +302,16 @@ fun BaseSimpleActivity.emptyTheRecycleBin(callback: (() -> Unit)? = null) { } catch (e: Exception) { toast(R.string.unknown_error_occurred) } - }.start() + } } fun BaseSimpleActivity.emptyAndDisableTheRecycleBin(callback: () -> Unit) { - Thread { + ensureBackgroundThread { emptyTheRecycleBin { config.useRecycleBin = false callback() } - }.start() + } } fun BaseSimpleActivity.showRecycleBinEmptyingDialog(callback: () -> Unit) { @@ -321,12 +321,12 @@ fun BaseSimpleActivity.showRecycleBinEmptyingDialog(callback: () -> Unit) { } fun BaseSimpleActivity.updateFavoritePaths(fileDirItems: ArrayList, destination: String) { - Thread { + ensureBackgroundThread { fileDirItems.forEach { val newPath = "$destination/${it.name}" updateDBMediaPath(it.path, newPath) } - }.start() + } } fun Activity.hasNavBar(): Boolean { @@ -515,7 +515,7 @@ fun saveFile(path: String, bitmap: Bitmap, out: FileOutputStream, degrees: Int) } fun Activity.getShortcutImage(tmb: String, drawable: Drawable, callback: () -> Unit) { - Thread { + ensureBackgroundThread { val options = RequestOptions() .format(DecodeFormat.PREFER_ARGB_8888) .skipMemoryCache(true) @@ -538,5 +538,5 @@ fun Activity.getShortcutImage(tmb: String, drawable: Drawable, callback: () -> U runOnUiThread { callback() } - }.start() + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt index 2298bfaaf..ea4b13734 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Context.kt @@ -358,7 +358,7 @@ fun Context.updateSubfolderCounts(children: ArrayList, parentDirs: Ar } fun Context.getNoMediaFolders(callback: (folders: ArrayList) -> Unit) { - Thread { + ensureBackgroundThread { val folders = ArrayList() val uri = MediaStore.Files.getContentUri("external") @@ -384,20 +384,20 @@ fun Context.getNoMediaFolders(callback: (folders: ArrayList) -> Unit) { } callback(folders) - }.start() + } } fun Context.rescanFolderMedia(path: String) { - Thread { + ensureBackgroundThread { rescanFolderMediaSync(path) - }.start() + } } fun Context.rescanFolderMediaSync(path: String) { getCachedMedia(path) { val cached = it GetMediaAsynctask(applicationContext, path, false, false, false) { - Thread { + ensureBackgroundThread { val newMedia = it val mediumDao = galleryDB.MediumDao() val media = newMedia.filter { it is Medium } as ArrayList @@ -411,15 +411,15 @@ fun Context.rescanFolderMediaSync(path: String) { } } } - }.start() + } }.execute() } } fun Context.storeDirectoryItems(items: ArrayList, directoryDao: DirectoryDao) { - Thread { + ensureBackgroundThread { directoryDao.insertAll(items) - }.start() + } } fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet): String { @@ -539,7 +539,7 @@ fun Context.loadSVG(path: String, target: MySquareImageView, cropThumbnails: Boo } fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, directoryDao: DirectoryDao = galleryDB.DirectoryDao(), forceShowHidden: Boolean = false, callback: (ArrayList) -> Unit) { - Thread { + ensureBackgroundThread { val directories = try { directoryDao.getAll() as ArrayList } catch (e: Exception) { @@ -581,12 +581,12 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: callback(clone.distinctBy { it.path.getDistinctPath() } as ArrayList) removeInvalidDBDirectories(filteredDirectories, directoryDao) - }.start() + } } fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, mediumDao: MediumDao = galleryDB.MediumDao(), callback: (ArrayList) -> Unit) { - Thread { + ensureBackgroundThread { val mediaFetcher = MediaFetcher(this) val foldersToScan = if (path.isEmpty()) mediaFetcher.getFoldersToScan() else arrayListOf(path) var media = ArrayList() @@ -644,7 +644,7 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag } } catch (ignored: Exception) { } - }.start() + } } fun Context.removeInvalidDBDirectories(dirs: ArrayList? = null, directoryDao: DirectoryDao = galleryDB.DirectoryDao()) { @@ -768,9 +768,9 @@ fun Context.parseFileChannel(path: String, fc: FileChannel, level: Int, start: L } fun Context.addPathToDB(path: String) { - Thread { + ensureBackgroundThread { if (!File(path).exists()) { - return@Thread + return@ensureBackgroundThread } val type = when { @@ -791,7 +791,7 @@ fun Context.addPathToDB(path: String) { mediumDao.insert(medium) } catch (ignored: Exception) { } - }.start() + } } fun Context.createDirectoryFromMedia(path: String, curMedia: ArrayList, albumCovers: ArrayList, hiddenString: String, diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt index 71c78ff0c..fc80387e9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/PhotoFragment.kt @@ -36,6 +36,7 @@ import com.davemorrissey.labs.subscaleview.ImageRegionDecoder import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.PanoramaPhotoActivity import com.simplemobiletools.gallery.pro.activities.PhotoActivity @@ -241,10 +242,10 @@ class PhotoFragment : ViewPagerFragment() { mLoadZoomableViewHandler.removeCallbacksAndMessages(null) if (mCurrentRotationDegrees != 0) { - Thread { + ensureBackgroundThread { val path = mMedium.path (activity as? BaseSimpleActivity)?.saveRotatedImageToFile(path, path, mCurrentRotationDegrees, false) {} - }.start() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt index a7829beea..1f14eef95 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/fragments/VideoFragment.kt @@ -24,6 +24,7 @@ import com.google.android.exoplayer2.upstream.DataSource import com.google.android.exoplayer2.upstream.DataSpec import com.google.android.exoplayer2.upstream.FileDataSource import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.PanoramaVideoActivity import com.simplemobiletools.gallery.pro.activities.VideoActivity @@ -139,12 +140,12 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S initTimeHolder() checkIfPanorama() - Thread { + ensureBackgroundThread { activity?.getVideoResolution(mMedium.path)?.apply { mVideoSize.x = x mVideoSize.y = y } - }.start() + } if (mIsPanorama) { mView.apply { @@ -657,10 +658,10 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S private fun releaseExoPlayer() { mExoPlayer?.stop() - Thread { + ensureBackgroundThread { mExoPlayer?.release() mExoPlayer = null - }.start() + } } override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {} @@ -670,9 +671,9 @@ class VideoFragment : ViewPagerFragment(), TextureView.SurfaceTextureListener, S override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?) = false override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) { - Thread { + ensureBackgroundThread { mExoPlayer?.setVideoSurface(Surface(mTextureView.surfaceTexture)) - }.start() + } } private fun setVideoSize() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt index 3b4ec5090..5ba0e3811 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MyWidgetProvider.kt @@ -14,6 +14,7 @@ import com.simplemobiletools.commons.extensions.getFileSignature import com.simplemobiletools.commons.extensions.setBackgroundColor import com.simplemobiletools.commons.extensions.setText import com.simplemobiletools.commons.extensions.setVisibleIf +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.activities.MediaActivity import com.simplemobiletools.gallery.pro.extensions.config @@ -34,7 +35,7 @@ class MyWidgetProvider : AppWidgetProvider() { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { super.onUpdate(context, appWidgetManager, appWidgetIds) - Thread { + ensureBackgroundThread { val config = context.config context.widgetsDB.getWidgets().filter { appWidgetIds.contains(it.widgetId) }.forEach { val views = RemoteViews(context.packageName, R.layout.widget).apply { @@ -75,7 +76,7 @@ class MyWidgetProvider : AppWidgetProvider() { setupAppOpenIntent(context, views, R.id.widget_holder, it) appWidgetManager.updateAppWidget(it.widgetId, views) } - }.start() + } } override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, newOptions: Bundle) { @@ -85,10 +86,10 @@ class MyWidgetProvider : AppWidgetProvider() { override fun onDeleted(context: Context, appWidgetIds: IntArray) { super.onDeleted(context, appWidgetIds) - Thread { + ensureBackgroundThread { appWidgetIds.forEach { context.widgetsDB.deleteWidgetId(it) } - }.start() + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/jobs/NewPhotoFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/jobs/NewPhotoFetcher.kt index 07d24df9d..e00e2eb4f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/jobs/NewPhotoFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/jobs/NewPhotoFetcher.kt @@ -14,6 +14,7 @@ import android.os.Handler import android.provider.MediaStore import com.simplemobiletools.commons.extensions.getParentPath import com.simplemobiletools.commons.extensions.getStringValue +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.gallery.pro.extensions.addPathToDB import com.simplemobiletools.gallery.pro.extensions.updateDirectoryPath @@ -94,11 +95,11 @@ class NewPhotoFetcher : JobService() { } } - Thread { + ensureBackgroundThread { affectedFolderPaths.forEach { updateDirectoryPath(it) } - }.start() + } mHandler.post(mWorker) return true