add a new setting for keeping last_modified at file copy/move/rename

This commit is contained in:
tibbi 2017-10-28 16:42:34 +02:00
parent deb98581ec
commit 9f7b9619c2
6 changed files with 43 additions and 49 deletions

View file

@ -166,6 +166,9 @@ open class BaseSimpleActivity : AppCompatActivity() {
for (oldFile in files) {
val newFile = File(destinationFolder, oldFile.name)
if (!newFile.exists() && oldFile.renameTo(newFile)) {
if (!baseConfig.keepLastModified) {
newFile.setLastModified(System.currentTimeMillis())
}
updateInMediaStore(oldFile, newFile)
updatedFiles.add(newFile)
}

View file

@ -106,29 +106,10 @@ class CopyMoveTask(val activity: BaseSimpleActivity, val copyOnly: Boolean = fal
if (source.length() == destination.length()) {
mMovedFiles.add(source)
val projection = arrayOf(
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DATE_MODIFIED)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.MediaColumns.DATA} = ?"
var selectionArgs = arrayOf(source.absolutePath)
val cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
val dateModified = cursor.getIntValue(MediaStore.Images.Media.DATE_MODIFIED)
val values = ContentValues().apply {
put(MediaStore.Images.Media.DATE_TAKEN, dateTaken)
put(MediaStore.Images.Media.DATE_MODIFIED, dateModified)
}
selectionArgs = arrayOf(destination.absolutePath)
activity.scanFile(destination) {
activity.contentResolver.update(uri, values, selection, selectionArgs)
}
}
if (activity.baseConfig.keepLastModified) {
copyOldLastModified(source, destination)
} else {
activity.scanFile(destination) {}
}
}
} finally {
@ -147,6 +128,33 @@ class CopyMoveTask(val activity: BaseSimpleActivity, val copyOnly: Boolean = fal
}
}
private fun copyOldLastModified(source: File, destination: File) {
val projection = arrayOf(
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DATE_MODIFIED)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.MediaColumns.DATA} = ?"
var selectionArgs = arrayOf(source.absolutePath)
val cursor = activity.contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
val dateModified = cursor.getIntValue(MediaStore.Images.Media.DATE_MODIFIED)
val values = ContentValues().apply {
put(MediaStore.Images.Media.DATE_TAKEN, dateTaken)
put(MediaStore.Images.Media.DATE_MODIFIED, dateModified)
}
selectionArgs = arrayOf(destination.absolutePath)
activity.scanFile(destination) {
activity.contentResolver.update(uri, values, selection, selectionArgs)
}
}
}
}
interface CopyMoveListener {
fun copySucceeded(copyOnly: Boolean, copiedAll: Boolean)

View file

@ -306,6 +306,9 @@ fun BaseSimpleActivity.renameFile(oldFile: File, newFile: File, callback: (succe
callback(true)
}
} else {
if (!baseConfig.keepLastModified) {
newFile.setLastModified(System.currentTimeMillis())
}
updateInMediaStore(oldFile, newFile)
scanFile(newFile) {
callback(true)

View file

@ -199,31 +199,6 @@ fun Context.updateInMediaStore(oldFile: File, newFile: File): Boolean {
}
}
fun Context.copyDates(oldFile: File, newFile: File) {
val projection = arrayOf(
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DATE_MODIFIED)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.MediaColumns.DATA} = ?"
var selectionArgs = arrayOf(oldFile.absolutePath)
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
val dateModified = cursor.getIntValue(MediaStore.Images.Media.DATE_MODIFIED)
val values = ContentValues().apply {
put(MediaStore.Images.Media.DATE_TAKEN, dateTaken)
put(MediaStore.Images.Media.DATE_MODIFIED, dateModified)
}
selectionArgs = arrayOf(newFile.absolutePath)
contentResolver.update(uri, values, selection, selectionArgs)
}
}
}
// avoid these being set as SD card paths
private val physicalPaths = arrayListOf(
"/storage/sdcard1", // Motorola Xoom

View file

@ -6,7 +6,7 @@ import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.extensions.getSharedPrefs
open class BaseConfig(val context: Context) {
protected val prefs: SharedPreferences = context.getSharedPrefs()
private val prefs: SharedPreferences = context.getSharedPrefs()
companion object {
fun newInstance(context: Context) = BaseConfig(context)
@ -75,4 +75,8 @@ open class BaseConfig(val context: Context) {
var protectionType: Int
get() = prefs.getInt(PROTECTION_TYPE, PROTECTION_PATTERN)
set(protectionType) = prefs.edit().putInt(PROTECTION_TYPE, protectionType).apply()
var keepLastModified: Boolean
get() = prefs.getBoolean(KEEP_LAST_MODIFIED, false)
set(keepLastModified) = prefs.edit().putBoolean(KEEP_LAST_MODIFIED, keepLastModified).apply()
}

View file

@ -22,6 +22,7 @@ val WIDGET_TEXT_COLOR = "widget_text_color"
val IS_PASSWORD_PROTECTION = "password_protection"
val PASSWORD_HASH = "password_hash"
val PROTECTION_TYPE = "protection_type"
val KEEP_LAST_MODIFIED = "keep_last_modified"
// licenses
val LICENSE_KOTLIN = 1