restore proper file paths

This commit is contained in:
tibbi 2019-01-12 15:44:04 +01:00
parent dc7de93cf7
commit 84ebb59482
2 changed files with 49 additions and 49 deletions

View file

@ -307,10 +307,12 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
} }
private fun fixDateTaken() { private fun fixDateTaken() {
activity.fixDateTaken(getSelectedPaths()) { Thread {
listener?.refreshItems() activity.fixDateTaken(getSelectedPaths()) {
finishActMode() listener?.refreshItems()
} finishActMode()
}
}.start()
} }
private fun checkDeleteConfirmation() { private fun checkDeleteConfirmation() {

View file

@ -6,7 +6,6 @@ import android.content.Intent
import android.media.ExifInterface 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
@ -237,6 +236,7 @@ fun BaseSimpleActivity.restoreRecycleBinPath(path: String, callback: () -> Unit)
fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, mediumDao: MediumDao = galleryDB.MediumDao(), callback: () -> Unit) { fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, mediumDao: MediumDao = galleryDB.MediumDao(), callback: () -> Unit) {
Thread { Thread {
val newPaths = ArrayList<String>()
paths.forEach { paths.forEach {
val source = it val source = it
val destination = it.removePrefix(recycleBinPath) val destination = it.removePrefix(recycleBinPath)
@ -250,6 +250,7 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, mediumDa
if (File(source).length() == File(destination).length()) { if (File(source).length() == File(destination).length()) {
mediumDao.updateDeleted(destination.removePrefix(recycleBinPath), 0, "$RECYCLE_BIN$destination") mediumDao.updateDeleted(destination.removePrefix(recycleBinPath), 0, "$RECYCLE_BIN$destination")
} }
newPaths.add(destination)
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e) showErrorToast(e)
} finally { } finally {
@ -262,7 +263,7 @@ fun BaseSimpleActivity.restoreRecycleBinPaths(paths: ArrayList<String>, mediumDa
callback() callback()
} }
fixDateTaken(paths) fixDateTaken(newPaths)
}.start() }.start()
} }
@ -315,54 +316,51 @@ fun Activity.hasNavBar(): Boolean {
fun Activity.fixDateTaken(paths: ArrayList<String>, callback: (() -> Unit)? = null) { fun Activity.fixDateTaken(paths: ArrayList<String>, callback: (() -> Unit)? = null) {
val BATCH_SIZE = 50 val BATCH_SIZE = 50
toast(R.string.fixing) toast(R.string.fixing)
Thread { try {
try { var didUpdateFile = false
var didUpdateFile = false val operations = ArrayList<ContentProviderOperation>()
val operations = ArrayList<ContentProviderOperation>() val mediumDao = galleryDB.MediumDao()
val mediumDao = galleryDB.MediumDao() for (path in paths) {
for (path in paths) { val dateTime = ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL)
val dateTime = ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME_ORIGINAL) ?: ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME) ?: continue
?: ExifInterface(path).getAttribute(ExifInterface.TAG_DATETIME) ?: continue
// some formats contain a "T" in the middle, some don't // some formats contain a "T" in the middle, some don't
// sample dates: 2015-07-26T14:55:23, 2018:09:05 15:09:05 // sample dates: 2015-07-26T14:55:23, 2018:09:05 15:09:05
val t = if (dateTime.substring(10, 11) == "T") "\'T\'" else " " val t = if (dateTime.substring(10, 11) == "T") "\'T\'" else " "
val separator = dateTime.substring(4, 5) val separator = dateTime.substring(4, 5)
val format = "yyyy${separator}MM${separator}dd${t}kk:mm:ss" val format = "yyyy${separator}MM${separator}dd${t}kk:mm:ss"
val formatter = SimpleDateFormat(format, Locale.getDefault()) val formatter = SimpleDateFormat(format, Locale.getDefault())
val timestamp = formatter.parse(dateTime).time val timestamp = formatter.parse(dateTime).time
val uri = getFileUri(path) val uri = getFileUri(path)
ContentProviderOperation.newUpdate(uri).apply { ContentProviderOperation.newUpdate(uri).apply {
val selection = "${MediaStore.Images.Media.DATA} = ?" val selection = "${MediaStore.Images.Media.DATA} = ?"
val selectionArgs = arrayOf(path) val selectionArgs = arrayOf(path)
withSelection(selection, selectionArgs) withSelection(selection, selectionArgs)
withValue(MediaStore.Images.Media.DATE_TAKEN, timestamp) withValue(MediaStore.Images.Media.DATE_TAKEN, timestamp)
operations.add(build()) 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 (operations.size % BATCH_SIZE == 0) {
if (resultSize == 0) { contentResolver.applyBatch(MediaStore.AUTHORITY, operations)
didUpdateFile = false operations.clear()
rescanPaths(paths)
} }
toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred) mediumDao.updateFavoriteDateTaken(path, timestamp)
runOnUiThread { didUpdateFile = true
callback?.invoke()
}
} catch (e: Exception) {
showErrorToast(e)
} }
}.start()
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)
}
} }