diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt index 172fc25e3..145013260 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -5,12 +5,6 @@ import android.content.Intent import android.provider.MediaStore import android.support.v7.app.AppCompatActivity import android.view.View -import android.widget.ImageView -import com.bumptech.glide.Glide -import com.bumptech.glide.load.DecodeFormat -import com.bumptech.glide.load.engine.DiskCacheStrategy -import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions -import com.bumptech.glide.request.RequestOptions import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.* @@ -21,13 +15,7 @@ import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.helpers.NOMEDIA -import com.simplemobiletools.gallery.helpers.TYPE_GIF -import com.simplemobiletools.gallery.helpers.TYPE_IMAGE -import com.simplemobiletools.gallery.helpers.TYPE_VIDEO -import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium -import com.simplemobiletools.gallery.views.MySquareImageView -import pl.droidsonroids.gif.GifDrawable import java.io.File import java.util.* @@ -166,33 +154,6 @@ fun BaseSimpleActivity.toggleFileVisibility(oldPath: String, hide: Boolean, call } } -fun Activity.loadImage(type: Int, path: String, target: MySquareImageView, horizontalScroll: Boolean, animateGifs: Boolean, cropThumbnails: Boolean) { - target.isHorizontalScrolling = horizontalScroll - if (type == TYPE_IMAGE || type == TYPE_VIDEO) { - if (type == TYPE_IMAGE && path.isPng()) { - loadPng(path, target, cropThumbnails) - } else { - loadJpg(path, target, cropThumbnails) - } - } else if (type == TYPE_GIF) { - try { - val gifDrawable = GifDrawable(path) - target.setImageDrawable(gifDrawable) - if (animateGifs) { - gifDrawable.start() - } else { - gifDrawable.stop() - } - - target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER - } catch (e: Exception) { - loadJpg(path, target, cropThumbnails) - } catch (e: OutOfMemoryError) { - loadJpg(path, target, cropThumbnails) - } - } -} - fun BaseSimpleActivity.tryCopyMoveFilesTo(fileDirItems: ArrayList, isCopyOperation: Boolean, callback: (destinationPath: String) -> Unit) { if (fileDirItems.isEmpty()) { toast(R.string.unknown_error_occurred) @@ -204,60 +165,3 @@ fun BaseSimpleActivity.tryCopyMoveFilesTo(fileDirItems: ArrayList, copyMoveFilesTo(fileDirItems, source.trimEnd('/'), it, isCopyOperation, true, config.shouldShowHidden, callback) } } - -fun BaseSimpleActivity.addTempFolderIfNeeded(dirs: ArrayList): ArrayList { - val directories = ArrayList() - val tempFolderPath = config.tempFolderPath - if (tempFolderPath.isNotEmpty()) { - val newFolder = Directory(null, tempFolderPath, "", tempFolderPath.getFilenameFromPath(), 0, 0, 0, 0L, isPathOnSD(tempFolderPath)) - directories.add(newFolder) - } - directories.addAll(dirs) - return directories -} - -fun Activity.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boolean) { - val options = RequestOptions() - .signature(path.getFileSignature()) - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) - .format(DecodeFormat.PREFER_ARGB_8888) - - val builder = Glide.with(applicationContext) - .asBitmap() - .load(path) - - if (cropThumbnails) options.centerCrop() else options.fitCenter() - builder.apply(options).into(target) -} - -fun Activity.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boolean) { - val options = RequestOptions() - .signature(path.getFileSignature()) - .diskCacheStrategy(DiskCacheStrategy.RESOURCE) - - val builder = Glide.with(applicationContext) - .load(path) - - if (cropThumbnails) options.centerCrop() else options.fitCenter() - builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) -} - -fun Activity.getCachedDirectories(callback: (ArrayList) -> Unit) { - Thread { - val directoryDao = galleryDB.DirectoryDao() - val directories = directoryDao.getAll() as ArrayList - callback(directories) - - directories.filter { !File(it.path).exists() }.forEach { - directoryDao.deleteDir(it) - } - }.start() -} - -fun Activity.getCachedMedia(path: String, callback: (ArrayList) -> Unit) { - Thread { - val mediumDao = galleryDB.MediumDao() - val media = mediumDao.getMediaFromPath(path) as ArrayList - callback(media) - }.start() -} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index 75b6536bb..07cbaa91b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -9,6 +9,12 @@ import android.media.AudioManager import android.os.Build import android.provider.MediaStore import android.view.WindowManager +import android.widget.ImageView +import com.bumptech.glide.Glide +import com.bumptech.glide.load.DecodeFormat +import com.bumptech.glide.load.engine.DiskCacheStrategy +import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions +import com.bumptech.glide.request.RequestOptions import com.google.gson.Gson import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.OTG_PATH @@ -17,11 +23,11 @@ import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask import com.simplemobiletools.gallery.databases.GalleryDataBase -import com.simplemobiletools.gallery.helpers.Config -import com.simplemobiletools.gallery.helpers.NOMEDIA -import com.simplemobiletools.gallery.helpers.SAVE_MEDIA_CNT +import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium +import com.simplemobiletools.gallery.views.MySquareImageView +import pl.droidsonroids.gif.GifDrawable import java.io.File val Context.portrait get() = resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT @@ -187,3 +193,87 @@ fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: dirName } } + +fun Context.loadImage(type: Int, path: String, target: MySquareImageView, horizontalScroll: Boolean, animateGifs: Boolean, cropThumbnails: Boolean) { + target.isHorizontalScrolling = horizontalScroll + if (type == TYPE_IMAGE || type == TYPE_VIDEO) { + if (type == TYPE_IMAGE && path.isPng()) { + loadPng(path, target, cropThumbnails) + } else { + loadJpg(path, target, cropThumbnails) + } + } else if (type == TYPE_GIF) { + try { + val gifDrawable = GifDrawable(path) + target.setImageDrawable(gifDrawable) + if (animateGifs) { + gifDrawable.start() + } else { + gifDrawable.stop() + } + + target.scaleType = if (cropThumbnails) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER + } catch (e: Exception) { + loadJpg(path, target, cropThumbnails) + } catch (e: OutOfMemoryError) { + loadJpg(path, target, cropThumbnails) + } + } +} + +fun Context.addTempFolderIfNeeded(dirs: ArrayList): ArrayList { + val directories = ArrayList() + val tempFolderPath = config.tempFolderPath + if (tempFolderPath.isNotEmpty()) { + val newFolder = Directory(null, tempFolderPath, "", tempFolderPath.getFilenameFromPath(), 0, 0, 0, 0L, isPathOnSD(tempFolderPath)) + directories.add(newFolder) + } + directories.addAll(dirs) + return directories +} + +fun Context.loadPng(path: String, target: MySquareImageView, cropThumbnails: Boolean) { + val options = RequestOptions() + .signature(path.getFileSignature()) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .format(DecodeFormat.PREFER_ARGB_8888) + + val builder = Glide.with(applicationContext) + .asBitmap() + .load(path) + + if (cropThumbnails) options.centerCrop() else options.fitCenter() + builder.apply(options).into(target) +} + +fun Context.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Boolean) { + val options = RequestOptions() + .signature(path.getFileSignature()) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + + val builder = Glide.with(applicationContext) + .load(path) + + if (cropThumbnails) options.centerCrop() else options.fitCenter() + builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) +} + +fun Context.getCachedDirectories(callback: (ArrayList) -> Unit) { + Thread { + val directoryDao = galleryDB.DirectoryDao() + val directories = directoryDao.getAll() as ArrayList + callback(directories) + + directories.filter { !File(it.path).exists() }.forEach { + directoryDao.deleteDir(it) + } + }.start() +} + +fun Context.getCachedMedia(path: String, callback: (ArrayList) -> Unit) { + Thread { + val mediumDao = galleryDB.MediumDao() + val media = mediumDao.getMediaFromPath(path) as ArrayList + callback(media) + }.start() +}