diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt index 0206d451e..606cfd2a4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/adapters/MediaAdapter.kt @@ -1,11 +1,8 @@ package com.simplemobiletools.gallery.pro.adapters -import android.content.ContentProviderOperation -import android.media.ExifInterface import android.media.MediaMetadataRetriever import android.os.Handler import android.os.Looper -import android.provider.MediaStore import android.view.Menu import android.view.View import android.view.ViewGroup @@ -32,7 +29,6 @@ import com.simplemobiletools.gallery.pro.models.ThumbnailItem import com.simplemobiletools.gallery.pro.models.ThumbnailSection import kotlinx.android.synthetic.main.photo_video_item_grid.view.* import kotlinx.android.synthetic.main.thumbnail_section.view.* -import java.text.SimpleDateFormat import java.util.* class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, val listener: MediaOperationsListener?, val isAGetIntent: Boolean, @@ -41,7 +37,6 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList() - val mediumDao = activity.galleryDB.MediumDao() - val paths = getSelectedPaths() - for (path in paths) { - val dateTime = ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL) - ?: ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME) ?: continue - - // some formats contain a "T" in the middle, some don't - // sample dates: 2015-07-26T14:55:23, 2018:09:05 15:09:05 - val t = if (dateTime.substring(10, 11) == "T") "\'T\'" else " " - val separator = dateTime.substring(4, 5) - val format = "yyyy${separator}MM${separator}dd${t}kk:mm:ss" - val formatter = SimpleDateFormat(format, Locale.getDefault()) - val timestamp = formatter.parse(dateTime).time - - val uri = activity.getFileUri(path) - ContentProviderOperation.newUpdate(uri).apply { - val selection = "${MediaStore.Images.Media.DATA} = ?" - val selectionArgs = arrayOf(path) - withSelection(selection, selectionArgs) - withValue(MediaStore.Images.Media.DATE_TAKEN, timestamp) - operations.add(build()) - } - - if (operations.size % BATCH_SIZE == 0) { - activity.contentResolver.applyBatch(MediaStore.AUTHORITY, operations) - operations.clear() - } - - mediumDao.updateFavoriteDateTaken(path, timestamp) - didUpdateFile = true - } - - val resultSize = activity.contentResolver.applyBatch(MediaStore.AUTHORITY, operations).size - if (resultSize == 0) { - didUpdateFile = false - activity.rescanPaths(paths) - } - - activity.toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred) - activity.runOnUiThread { - listener?.refreshItems() - finishActMode() - } - } catch (e: Exception) { - activity.showErrorToast(e) - } - }.start() + activity.fixDateTaken(getSelectedPaths()) { + listener?.refreshItems() + finishActMode() + } } private fun checkDeleteConfirmation() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt index a8103b206..bf9a17437 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt @@ -1,9 +1,12 @@ package com.simplemobiletools.gallery.pro.extensions import android.app.Activity +import android.content.ContentProviderOperation import android.content.Intent +import android.media.ExifInterface import android.provider.MediaStore import android.util.DisplayMetrics +import android.util.Log import android.view.View import androidx.appcompat.app.AppCompatActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity @@ -22,6 +25,7 @@ import com.simplemobiletools.gallery.pro.interfaces.MediumDao import java.io.File import java.io.InputStream import java.io.OutputStream +import java.text.SimpleDateFormat import java.util.* fun Activity.sharePath(path: String) { @@ -257,6 +261,8 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList, mediumDa runOnUiThread { callback() } + + fixDateTaken(paths) }.start() } @@ -305,3 +311,58 @@ fun Activity.hasNavBar(): Boolean { return (realDisplayMetrics.widthPixels - displayMetrics.widthPixels > 0) || (realDisplayMetrics.heightPixels - displayMetrics.heightPixels > 0) } + +fun Activity.fixDateTaken(paths: ArrayList, callback: (() -> Unit)? = null) { + val BATCH_SIZE = 50 + toast(R.string.fixing) + Thread { + try { + var didUpdateFile = false + val operations = ArrayList() + val mediumDao = galleryDB.MediumDao() + for (path in paths) { + val dateTime = ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL) + ?: ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME) ?: continue + + // some formats contain a "T" in the middle, some don't + // sample dates: 2015-07-26T14:55:23, 2018:09:05 15:09:05 + val t = if (dateTime.substring(10, 11) == "T") "\'T\'" else " " + val separator = dateTime.substring(4, 5) + val format = "yyyy${separator}MM${separator}dd${t}kk:mm:ss" + val formatter = SimpleDateFormat(format, Locale.getDefault()) + val timestamp = formatter.parse(dateTime).time + + val uri = getFileUri(path) + ContentProviderOperation.newUpdate(uri).apply { + val selection = "${MediaStore.Images.Media.DATA} = ?" + val selectionArgs = arrayOf(path) + withSelection(selection, selectionArgs) + withValue(MediaStore.Images.Media.DATE_TAKEN, timestamp) + operations.add(build()) + } + + if (operations.size % BATCH_SIZE == 0) { + contentResolver.applyBatch(MediaStore.AUTHORITY, operations) + operations.clear() + } + + Log.e("DEBUG", "restoring $path") + mediumDao.updateFavoriteDateTaken(path, timestamp) + didUpdateFile = true + } + + val resultSize = contentResolver.applyBatch(MediaStore.AUTHORITY, operations).size + if (resultSize == 0) { + didUpdateFile = false + rescanPaths(paths) + } + + toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred) + runOnUiThread { + callback?.invoke() + } + } catch (e: Exception) { + showErrorToast(e) + } + }.start() +}