From 95d8abfb4608d262cdb13eed19aa764269d0486d Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Wed, 9 Oct 2024 09:58:00 -0400 Subject: [PATCH] Clean up dangling wallpapers. --- .../securesms/database/RecipientTable.kt | 30 +++++++++++++ .../securesms/database/SignalDatabase.kt | 1 + .../securesms/jobs/JobManagerFactories.java | 2 + .../migrations/ApplicationMigrations.java | 7 ++- .../WallpaperCleanupMigrationJob.kt | 43 +++++++++++++++++++ 5 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/org/thoughtcrime/securesms/migrations/WallpaperCleanupMigrationJob.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt index 593a4b219e..3b7aed292f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientTable.kt @@ -2073,6 +2073,36 @@ open class RecipientTable(context: Context, databaseHelper: SignalDatabase) : Da .run() } + /** + * A follow up to [migrateWallpaperUri]. This clears out any remaining wallpapers using the old URI scheme, which implies + * that they were not migrated, which would only happen if they could not be read or were no longer found (the latter being + * more common, as this would happen after restoring a backup). + */ + fun clearMissingFileWallpapersPostMigration(): Int { + return writableDatabase + .update(TABLE_NAME) + .values( + WALLPAPER to null, + WALLPAPER_URI to null + ) + .where("$WALLPAPER_URI LIKE ?", "%wallpaper%") + .run() + } + + /** + * Our current backup system does not backup file wallpapers. So we should clear them post-restore to avoid any weird UI issues. + */ + fun clearFileWallpapersPostBackupRestore() { + writableDatabase + .update(TABLE_NAME) + .values( + WALLPAPER to null, + WALLPAPER_URI to null + ) + .where("$WALLPAPER_URI NOT NULL") + .run() + } + fun getPhoneNumberDiscoverability(id: RecipientId): PhoneNumberDiscoverableState? { return readableDatabase .select(PHONE_NUMBER_DISCOVERABLE) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/SignalDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/database/SignalDatabase.kt index d8b6e717cd..1bb02120f1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/SignalDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/SignalDatabase.kt @@ -300,6 +300,7 @@ open class SignalDatabase(private val context: Application, databaseSecret: Data instance!!.messageTable.trimEntriesForExpiredMessages() instance!!.reactionTable.deleteAbandonedReactions() instance!!.searchTable.fullyResetTables(useTransaction = false) + instance!!.recipientTable.clearFileWallpapersPostBackupRestore() instance!!.rawWritableDatabase.execSQL("DROP TABLE IF EXISTS key_value") instance!!.rawWritableDatabase.execSQL("DROP TABLE IF EXISTS megaphone") instance!!.rawWritableDatabase.execSQL("DROP TABLE IF EXISTS job_spec") diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/JobManagerFactories.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/JobManagerFactories.java index 7d24b8357a..82831e390c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/JobManagerFactories.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/JobManagerFactories.java @@ -96,6 +96,7 @@ import org.thoughtcrime.securesms.migrations.TrimByLengthSettingsMigrationJob; import org.thoughtcrime.securesms.migrations.UpdateSmsJobsMigrationJob; import org.thoughtcrime.securesms.migrations.UserNotificationMigrationJob; import org.thoughtcrime.securesms.migrations.UuidMigrationJob; +import org.thoughtcrime.securesms.migrations.WallpaperCleanupMigrationJob; import org.thoughtcrime.securesms.migrations.WallpaperStorageMigrationJob; import java.util.Arrays; @@ -318,6 +319,7 @@ public final class JobManagerFactories { put(UpdateSmsJobsMigrationJob.KEY, new UpdateSmsJobsMigrationJob.Factory()); put(UserNotificationMigrationJob.KEY, new UserNotificationMigrationJob.Factory()); put(UuidMigrationJob.KEY, new UuidMigrationJob.Factory()); + put(WallpaperCleanupMigrationJob.KEY, new WallpaperCleanupMigrationJob.Factory()); put(WallpaperStorageMigrationJob.KEY, new WallpaperStorageMigrationJob.Factory()); // Dead jobs diff --git a/app/src/main/java/org/thoughtcrime/securesms/migrations/ApplicationMigrations.java b/app/src/main/java/org/thoughtcrime/securesms/migrations/ApplicationMigrations.java index a071bb3e5f..c74ad49ca5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/migrations/ApplicationMigrations.java +++ b/app/src/main/java/org/thoughtcrime/securesms/migrations/ApplicationMigrations.java @@ -159,9 +159,10 @@ public class ApplicationMigrations { static final int WALLPAPER_MIGRATION = 115; static final int BACKFILL_DIGESTS_V3 = 116; static final int SVR2_ENCLAVE_UPDATE_2 = 117; + static final int WALLPAPER_MIGRATION_CLEANUP = 118; } - public static final int CURRENT_VERSION = 117; + public static final int CURRENT_VERSION = 118; /** * This *must* be called after the {@link JobManager} has been instantiated, but *before* the call @@ -728,6 +729,10 @@ public class ApplicationMigrations { jobs.put(Version.SVR2_ENCLAVE_UPDATE_2, new Svr2MirrorMigrationJob()); } + if (lastSeenVersion < Version.WALLPAPER_MIGRATION_CLEANUP) { + jobs.put(Version.WALLPAPER_MIGRATION_CLEANUP, new WallpaperCleanupMigrationJob()); + } + return jobs; } diff --git a/app/src/main/java/org/thoughtcrime/securesms/migrations/WallpaperCleanupMigrationJob.kt b/app/src/main/java/org/thoughtcrime/securesms/migrations/WallpaperCleanupMigrationJob.kt new file mode 100644 index 0000000000..76d595bde6 --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/migrations/WallpaperCleanupMigrationJob.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2024 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.thoughtcrime.securesms.migrations + +import org.signal.core.util.logging.Log +import org.thoughtcrime.securesms.database.SignalDatabase +import org.thoughtcrime.securesms.jobmanager.Job + +/** + * [WallpaperStorageMigrationJob] left some stragglers in the DB for wallpapers that couldn't be found on disk. This cleans those up. + * It'd be great if we could do this in a database migration, but unfortunately we need to ensure that the aforementioned + * [WallpaperStorageMigrationJob] finished. + */ +internal class WallpaperCleanupMigrationJob(parameters: Parameters = Parameters.Builder().build()) : MigrationJob(parameters) { + companion object { + private val TAG = Log.tag(WallpaperCleanupMigrationJob::class.java) + const val KEY = "WallpaperCleanupMigrationJob" + } + + override fun getFactoryKey(): String = KEY + + override fun isUiBlocking(): Boolean = false + + override fun performMigration() { + val count = SignalDatabase.recipients.clearMissingFileWallpapersPostMigration() + if (count > 0) { + Log.w(TAG, "There were $count legacy wallpapers that needed to be cleared.") + } else { + Log.i(TAG, "No legacy wallpapers needed to be cleared.") + } + } + + override fun shouldRetry(e: Exception): Boolean = false + + class Factory : Job.Factory { + override fun create(parameters: Parameters, serializedData: ByteArray?): WallpaperCleanupMigrationJob { + return WallpaperCleanupMigrationJob(parameters) + } + } +}