From c08d87777a3cb1a85b9f0a6b445d5d7f88dda2a2 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Mon, 15 May 2023 20:09:56 -0700 Subject: [PATCH] Add delta sync --- .../java/gallery/memories/MainActivity.kt | 8 ++-- app/src/main/java/gallery/memories/NativeX.kt | 3 ++ .../gallery/memories/service/TimelineQuery.kt | 45 +++++++++++++++---- app/src/main/res/values/strings.xml | 3 ++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/gallery/memories/MainActivity.kt b/app/src/main/java/gallery/memories/MainActivity.kt index 5f6fb0fa..b6e9846f 100644 --- a/app/src/main/java/gallery/memories/MainActivity.kt +++ b/app/src/main/java/gallery/memories/MainActivity.kt @@ -217,15 +217,15 @@ import gallery.memories.databinding.ActivityMainBinding fun storeTheme(color: String?, isDark: Boolean) { if (color == null) return getSharedPreferences(getString(R.string.preferences_key), 0).edit() - .putString("themeColor", color) - .putBoolean("themeDark", isDark) + .putString(getString(R.string.preferences_theme_color), color) + .putBoolean(getString(R.string.preferences_theme_dark), isDark) .apply() } fun restoreTheme() { val preferences = getSharedPreferences(getString(R.string.preferences_key), 0) - val color = preferences.getString("themeColor", null) - val isDark = preferences.getBoolean("themeDark", false) + val color = preferences.getString(getString(R.string.preferences_theme_color), null) + val isDark = preferences.getBoolean(getString(R.string.preferences_theme_dark), false) applyTheme(color, isDark) } diff --git a/app/src/main/java/gallery/memories/NativeX.kt b/app/src/main/java/gallery/memories/NativeX.kt index df9b0ac4..ff1bec27 100644 --- a/app/src/main/java/gallery/memories/NativeX.kt +++ b/app/src/main/java/gallery/memories/NativeX.kt @@ -37,6 +37,9 @@ import java.net.URLDecoder init { mDlService = DownloadService(mActivity) + + // Synchronize the database + mQuery.syncDeltaDb() } companion object { diff --git a/app/src/main/java/gallery/memories/service/TimelineQuery.kt b/app/src/main/java/gallery/memories/service/TimelineQuery.kt index 641806b2..2164da34 100644 --- a/app/src/main/java/gallery/memories/service/TimelineQuery.kt +++ b/app/src/main/java/gallery/memories/service/TimelineQuery.kt @@ -16,12 +16,14 @@ import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.collection.ArraySet import androidx.exifinterface.media.ExifInterface +import gallery.memories.R import gallery.memories.mapper.Fields import gallery.memories.mapper.SystemImage import org.json.JSONArray import org.json.JSONException import org.json.JSONObject import java.io.IOException +import java.time.Instant import java.util.concurrent.CountDownLatch class TimelineQuery(private val mCtx: AppCompatActivity) { @@ -40,9 +42,6 @@ class TimelineQuery(private val mCtx: AppCompatActivity) { deleteCallback?.let { it(result) } } } - - // TODO: remove this in favor of a selective sync - fullSyncDb() } @Throws(JSONException::class) @@ -216,16 +215,46 @@ class TimelineQuery(private val mCtx: AppCompatActivity) { } } - private fun fullSyncDb() { - // Flag all images for removal - mDb.execSQL("UPDATE images SET flag = 1") + private fun syncDb(startTime: Long) { + // Date modified is in seconds, not millis + val syncTime = Instant.now().toEpochMilli() / 1000; + + // SystemImage query + var selection: String? = null + var selectionArgs: Array? = null + + // Query everything modified after startTime + if (startTime != 0L) { + selection = MediaStore.Images.Media.DATE_MODIFIED + " > ?" + selectionArgs = arrayOf(startTime.toString()) + } // Iterate all images and videos from system store val files = - SystemImage.query(mCtx, SystemImage.IMAGE_URI, null, null, null) + - SystemImage.query(mCtx, SystemImage.VIDEO_URI, null, null, null) + SystemImage.query(mCtx, SystemImage.IMAGE_URI, selection, selectionArgs, null) + + SystemImage.query(mCtx, SystemImage.VIDEO_URI, selection, selectionArgs, null) files.forEach { insertItemDb(it) } + // Store last sync time + mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0).edit() + .putLong(mCtx.getString(R.string.preferences_last_sync_time), syncTime) + .apply() + } + + fun syncDeltaDb() { + // Get last sync time + val syncTime = mCtx.getSharedPreferences(mCtx.getString(R.string.preferences_key), 0) + .getLong(mCtx.getString(R.string.preferences_last_sync_time), 0L) + syncDb(syncTime) + } + + fun syncFullDb() { + // Flag all images for removal + mDb.execSQL("UPDATE images SET flag = 1") + + // Sync all files, marking them in the process + syncDb(0L) + // Clean up stale files mDb.execSQL("DELETE FROM images WHERE flag = 1") } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2281231e..6b96416f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,4 +3,7 @@ Settings memories + themeColor + themeDark + lastDbSyncTime \ No newline at end of file