From bb7f67030d8f87f13f68dfbdf53e34c8fb27b8a0 Mon Sep 17 00:00:00 2001 From: vivabelarus <74508922+vivabelarus@users.noreply.github.com> Date: Tue, 4 Feb 2025 16:02:01 +0300 Subject: [PATCH] clear database after file protection disabling --- .../org/telegram/messenger/SharedConfig.java | 13 +++++- .../org/telegram/messenger/UserConfig.java | 5 ++- .../FileProtectionPostRestartCleaner.java | 44 +++++++++++++++++++ .../FileProtectionSwitcher.java | 10 +++++ .../java/org/telegram/ui/LaunchActivity.java | 4 +- 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 TMessagesProj/src/main/java/org/telegram/messenger/partisan/fileprotection/FileProtectionPostRestartCleaner.java diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java b/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java index 2773113b3..5d971c691 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/SharedConfig.java @@ -444,6 +444,7 @@ public class SharedConfig { public static boolean showEncryptedChatsFromEncryptedGroups = false; public static boolean encryptedGroupsEnabled = false; public static boolean fileProtectionForAllAccountsEnabled = true; + public static boolean disableFileProtectionAfterRestart = false; public static boolean fileProtectionWorksWhenFakePasscodeActivated = true; private static final int[] LOW_SOC = { @@ -738,7 +739,8 @@ public class SharedConfig { BrowserHistory.clearHistory(); }, 1000); sharedConfigMigrationVersion++; - } if (sharedConfigMigrationVersion == 1) { + } + if (sharedConfigMigrationVersion == 1) { boolean updatedFromOldPtg = prevMigrationVersion == 1 || !fakePasscodes.isEmpty() || !passcodeHash.isEmpty(); if (updatedFromOldPtg) { // check if ptg has just been updated SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("userconfing", Context.MODE_PRIVATE); @@ -963,6 +965,7 @@ public class SharedConfig { showEncryptedChatsFromEncryptedGroups = preferences.getBoolean("showEncryptedChatsFromEncryptedGroups", false); encryptedGroupsEnabled = preferences.getBoolean("encryptedGroupsEnabled", encryptedGroupsEnabled); fileProtectionForAllAccountsEnabled = preferences.getBoolean("fileProtectionForAllAccountsEnabled", fileProtectionForAllAccountsEnabled); + disableFileProtectionAfterRestart = preferences.getBoolean("disableFileProtectionAfterRestart", disableFileProtectionAfterRestart); fileProtectionWorksWhenFakePasscodeActivated = preferences.getBoolean("fileProtectionWorksWhenFakePasscodeActivated", fileProtectionWorksWhenFakePasscodeActivated); dayNightWallpaperSwitchHint = preferences.getInt("dayNightWallpaperSwitchHint", 0); bigCameraForRound = preferences.getBoolean("bigCameraForRound", false); @@ -1084,6 +1087,14 @@ public class SharedConfig { editor.commit(); } + public static void setDisableFileProtectionAfterRestart(boolean value) { + disableFileProtectionAfterRestart = value; + SharedPreferences preferences = MessagesController.getGlobalMainSettings(); + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean("disableFileProtectionAfterRestart", value); + editor.commit(); + } + public static void toggleFileProtectionWorksWhenFakePasscodeActivated() { fileProtectionWorksWhenFakePasscodeActivated = !fileProtectionWorksWhenFakePasscodeActivated; SharedPreferences preferences = MessagesController.getGlobalMainSettings(); diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/UserConfig.java b/TMessagesProj/src/main/java/org/telegram/messenger/UserConfig.java index 5741b7f80..d2d72dabe 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/UserConfig.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/UserConfig.java @@ -21,7 +21,6 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.google.android.exoplayer2.util.Log; -import org.telegram.messenger.fakepasscode.FakePasscode; import org.telegram.messenger.fakepasscode.FakePasscodeUtils; import org.telegram.messenger.partisan.SecurityIssue; import org.telegram.tgnet.SerializedData; @@ -120,6 +119,7 @@ public class UserConfig extends BaseController { public boolean showSecuritySuggestions = false; public long lastSecuritySuggestionsShow = 0; public boolean fileProtectionEnabled = false; + public boolean disableFileProtectionAfterRestart = false; private final Map temporarilyLoadedPinnedDialogs = new ConcurrentHashMap<>(); private static ObjectMapper jsonMapper = null; @@ -299,6 +299,7 @@ public class UserConfig extends BaseController { editor.putBoolean("showSecuritySuggestions", showSecuritySuggestions); editor.putLong("lastSecuritySuggestionsShow", lastSecuritySuggestionsShow); editor.putBoolean("fileProtectionEnabled", fileProtectionEnabled); + editor.putBoolean("disableFileProtectionAfterRestart", disableFileProtectionAfterRestart); String savedChannelsStr = savedChannels.stream().reduce("", (acc, s) -> acc.isEmpty() ? s : acc + "," + s); editor.putString("savedChannels", savedChannelsStr); String pinnedSavedChannelsStr = pinnedSavedChannels.stream().reduce("", (acc, s) -> acc.isEmpty() ? s : acc + "," + s); @@ -471,6 +472,7 @@ public class UserConfig extends BaseController { if (SharedConfig.fileProtectionForAllAccountsEnabled) { // Don't enable file protection for accounts if global file protection enabled. fileProtectionEnabled = false; } + disableFileProtectionAfterRestart = preferences.getBoolean("disableFileProtectionAfterRestart", disableFileProtectionAfterRestart); String savedChannelsStr = preferences.getString("savedChannels", defaultChannels); savedChannels = new HashSet<>(Arrays.asList(savedChannelsStr.split(","))); savedChannels.remove(""); @@ -651,6 +653,7 @@ public class UserConfig extends BaseController { showSecuritySuggestions = false; lastSecuritySuggestionsShow = 0; fileProtectionEnabled = false; + disableFileProtectionAfterRestart = false; registeredForPush = false; contactsSavedCount = 0; lastSendMessageId = -210000; diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/partisan/fileprotection/FileProtectionPostRestartCleaner.java b/TMessagesProj/src/main/java/org/telegram/messenger/partisan/fileprotection/FileProtectionPostRestartCleaner.java new file mode 100644 index 000000000..2172a0528 --- /dev/null +++ b/TMessagesProj/src/main/java/org/telegram/messenger/partisan/fileprotection/FileProtectionPostRestartCleaner.java @@ -0,0 +1,44 @@ +package org.telegram.messenger.partisan.fileprotection; + +import org.telegram.messenger.AndroidUtilities; +import org.telegram.messenger.NotificationCenter; +import org.telegram.messenger.SharedConfig; +import org.telegram.messenger.UserConfig; +import org.telegram.messenger.partisan.PartisanLog; +import org.telegram.messenger.partisan.Utils; + +import java.util.HashSet; +import java.util.Set; + +public class FileProtectionPostRestartCleaner implements NotificationCenter.NotificationCenterDelegate { + private final Set accountsToClear = new HashSet<>(); + + public void checkAndClean() { + Utils.foreachActivatedAccountInstance(accountInstance -> { + UserConfig userConfig = accountInstance.getUserConfig(); + if (userConfig.disableFileProtectionAfterRestart || SharedConfig.disableFileProtectionAfterRestart) { + PartisanLog.d("Clean the database after disabling file protection for account " + accountInstance.getCurrentAccount()); + accountsToClear.add(accountInstance.getCurrentAccount()); + AndroidUtilities.runOnUIThread(() -> { + accountInstance.getNotificationCenter().addObserver(this, NotificationCenter.onDatabaseReset); + accountInstance.getMessagesStorage().clearLocalDatabase(); + }); + } + }); + } + + @Override + public void didReceivedNotification(int id, int account, Object... args) { + if (id == NotificationCenter.onDatabaseReset) { + PartisanLog.d("Cleaning the database after disabling file protection for account " + account + " finished"); + NotificationCenter.getInstance(account).removeObserver(this, NotificationCenter.onDatabaseReset); + UserConfig userConfig = UserConfig.getInstance(account); + userConfig.disableFileProtectionAfterRestart = false; + userConfig.saveConfig(false); + accountsToClear.remove(account); + if (accountsToClear.isEmpty()) { + SharedConfig.setDisableFileProtectionAfterRestart(false); + } + } + } +} diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/partisan/fileprotection/FileProtectionSwitcher.java b/TMessagesProj/src/main/java/org/telegram/messenger/partisan/fileprotection/FileProtectionSwitcher.java index f2eb6fb31..92844f65d 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/partisan/fileprotection/FileProtectionSwitcher.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/partisan/fileprotection/FileProtectionSwitcher.java @@ -52,11 +52,15 @@ public class FileProtectionSwitcher implements NotificationCenter.NotificationCe fragment.showDialog(enablingFileProtectionDialog); if (!enableForAllAccounts && valuesPerAccounts.isEmpty()) { + if (SharedConfig.fileProtectionForAllAccountsEnabled) { + SharedConfig.setDisableFileProtectionAfterRestart(true); + } SharedConfig.setFileProtectionForAllAccounts(enableForAllAccounts); Utils.foreachActivatedAccountInstance(accountInstance -> { UserConfig userConfig = accountInstance.getUserConfig(); if (userConfig.fileProtectionEnabled) { userConfig.fileProtectionEnabled = false; + userConfig.disableFileProtectionAfterRestart = true; userConfig.clearPinnedDialogsLoaded(); accountInstance.getUserConfig().saveConfig(false); } @@ -89,6 +93,9 @@ public class FileProtectionSwitcher implements NotificationCenter.NotificationCe } private void updateConfigs() { + if (SharedConfig.fileProtectionForAllAccountsEnabled) { + SharedConfig.setDisableFileProtectionAfterRestart(true); + } SharedConfig.setFileProtectionForAllAccounts(enableForAllAccounts); Utils.foreachActivatedAccountInstance(accountInstance -> { boolean enabledInConfig = valuesPerAccounts.getOrDefault(accountInstance.getCurrentAccount(), false); @@ -98,6 +105,9 @@ public class FileProtectionSwitcher implements NotificationCenter.NotificationCe if (userConfig.fileProtectionEnabled != enabledForAccountOrGlobally) { userConfig.clearPinnedDialogsLoaded(); } + if (userConfig.fileProtectionEnabled && !enabledInConfig) { + userConfig.disableFileProtectionAfterRestart = true; + } userConfig.fileProtectionEnabled = enabledInConfig; userConfig.saveConfig(false); }); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java index eb189d869..610252766 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LaunchActivity.java @@ -132,8 +132,7 @@ import org.telegram.messenger.partisan.appmigration.MigrationIntentHandler; import org.telegram.messenger.partisan.appmigration.AppMigrator; import org.telegram.messenger.partisan.appmigration.AppMigratorPreferences; import org.telegram.messenger.partisan.appmigration.MaskedMigratorHelper; -import org.telegram.messenger.partisan.secretgroups.EncryptedGroup; -import org.telegram.messenger.partisan.secretgroups.EncryptedGroupState; +import org.telegram.messenger.partisan.fileprotection.FileProtectionPostRestartCleaner; import org.telegram.messenger.partisan.secretgroups.EncryptedGroupUtils; import org.telegram.messenger.partisan.update.UpdateChecker; import org.telegram.messenger.partisan.update.UpdateData; @@ -1606,6 +1605,7 @@ public class LaunchActivity extends BasePermissionsActivity implements INavigati NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.fileLoadProgressChanged); NotificationCenter.getInstance(currentAccount).addObserver(this, NotificationCenter.fileLoadFailed); SecurityChecker.checkSecurityIssuesAndSave(this, currentAccount, false); + Utilities.globalQueue.postRunnable(() -> new FileProtectionPostRestartCleaner().checkAndClean(), 1000); if (updateLayout != null && updateLayout.isCancelIcon() && SharedConfig.pendingPtgAppUpdate != null && getUpdateAccountNum() != currentAccount) { NotificationCenter.getInstance(getUpdateAccountNum()).addObserver(this, NotificationCenter.fileLoaded); NotificationCenter.getInstance(getUpdateAccountNum()).addObserver(this, NotificationCenter.fileLoadProgressChanged);