restore date taken values automatically at restoring files from the recycle bin

This commit is contained in:
tibbi 2019-01-12 15:25:21 +01:00
parent 31db3e69ba
commit dc7de93cf7
2 changed files with 65 additions and 57 deletions

View file

@ -1,11 +1,8 @@
package com.simplemobiletools.gallery.pro.adapters package com.simplemobiletools.gallery.pro.adapters
import android.content.ContentProviderOperation
import android.media.ExifInterface
import android.media.MediaMetadataRetriever import android.media.MediaMetadataRetriever
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.provider.MediaStore
import android.view.Menu import android.view.Menu
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -32,7 +29,6 @@ import com.simplemobiletools.gallery.pro.models.ThumbnailItem
import com.simplemobiletools.gallery.pro.models.ThumbnailSection import com.simplemobiletools.gallery.pro.models.ThumbnailSection
import kotlinx.android.synthetic.main.photo_video_item_grid.view.* import kotlinx.android.synthetic.main.photo_video_item_grid.view.*
import kotlinx.android.synthetic.main.thumbnail_section.view.* import kotlinx.android.synthetic.main.thumbnail_section.view.*
import java.text.SimpleDateFormat
import java.util.* import java.util.*
class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<ThumbnailItem>, val listener: MediaOperationsListener?, val isAGetIntent: Boolean, class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<ThumbnailItem>, val listener: MediaOperationsListener?, val isAGetIntent: Boolean,
@ -41,7 +37,6 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
private val INSTANT_LOAD_DURATION = 2000L private val INSTANT_LOAD_DURATION = 2000L
private val IMAGE_LOAD_DELAY = 100L private val IMAGE_LOAD_DELAY = 100L
private val BATCH_SIZE = 100
private val ITEM_SECTION = 0 private val ITEM_SECTION = 0
private val ITEM_MEDIUM = 1 private val ITEM_MEDIUM = 1
@ -312,58 +307,10 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
} }
private fun fixDateTaken() { private fun fixDateTaken() {
activity.toast(R.string.fixing) activity.fixDateTaken(getSelectedPaths()) {
Thread { listener?.refreshItems()
try { finishActMode()
var didUpdateFile = false }
val operations = ArrayList<ContentProviderOperation>()
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()
} }
private fun checkDeleteConfirmation() { private fun checkDeleteConfirmation() {

View file

@ -1,9 +1,12 @@
package com.simplemobiletools.gallery.pro.extensions package com.simplemobiletools.gallery.pro.extensions
import android.app.Activity import android.app.Activity
import android.content.ContentProviderOperation
import android.content.Intent import android.content.Intent
import android.media.ExifInterface
import android.provider.MediaStore import android.provider.MediaStore
import android.util.DisplayMetrics import android.util.DisplayMetrics
import android.util.Log
import android.view.View import android.view.View
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
@ -22,6 +25,7 @@ import com.simplemobiletools.gallery.pro.interfaces.MediumDao
import java.io.File import java.io.File
import java.io.InputStream import java.io.InputStream
import java.io.OutputStream import java.io.OutputStream
import java.text.SimpleDateFormat
import java.util.* import java.util.*
fun Activity.sharePath(path: String) { fun Activity.sharePath(path: String) {
@ -257,6 +261,8 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, mediumDa
runOnUiThread { runOnUiThread {
callback() callback()
} }
fixDateTaken(paths)
}.start() }.start()
} }
@ -305,3 +311,58 @@ fun Activity.hasNavBar(): Boolean {
return (realDisplayMetrics.widthPixels - displayMetrics.widthPixels > 0) || (realDisplayMetrics.heightPixels - displayMetrics.heightPixels > 0) return (realDisplayMetrics.widthPixels - displayMetrics.widthPixels > 0) || (realDisplayMetrics.heightPixels - displayMetrics.heightPixels > 0)
} }
fun Activity.fixDateTaken(paths: ArrayList<String>, callback: (() -> Unit)? = null) {
val BATCH_SIZE = 50
toast(R.string.fixing)
Thread {
try {
var didUpdateFile = false
val operations = ArrayList<ContentProviderOperation>()
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()
}