Some more preferences included in the backup.

This commit is contained in:
Anton Chekulaev 2020-11-05 12:52:21 +11:00
parent 3311cd0958
commit 7b46849d07
2 changed files with 79 additions and 17 deletions

View File

@ -65,6 +65,7 @@ public class FullBackupImporter extends FullBackupBase {
* we use these 3-char prefixes to explicitly cast the values before inserting to a preference file. * we use these 3-char prefixes to explicitly cast the values before inserting to a preference file.
*/ */
public static final String PREF_PREFIX_TYPE_INT = "i__"; public static final String PREF_PREFIX_TYPE_INT = "i__";
public static final String PREF_PREFIX_TYPE_BOOLEAN = "b__";
private static final String TAG = FullBackupImporter.class.getSimpleName(); private static final String TAG = FullBackupImporter.class.getSimpleName();
@ -197,6 +198,11 @@ public class FullBackupImporter extends FullBackupBase {
key.substring(3), key.substring(3),
Integer.parseInt(value) Integer.parseInt(value)
).commit(); ).commit();
} else if (key.startsWith(PREF_PREFIX_TYPE_BOOLEAN)) {
preferences.edit().putBoolean(
key.substring(3),
Boolean.parseBoolean(value)
).commit();
} else { } else {
preferences.edit().putString(key, value).commit(); preferences.edit().putString(key, value).commit();
} }

View File

@ -32,6 +32,7 @@ import java.util.Set;
import network.loki.messenger.R; import network.loki.messenger.R;
import static org.thoughtcrime.securesms.backup.FullBackupImporter.PREF_PREFIX_TYPE_BOOLEAN;
import static org.thoughtcrime.securesms.backup.FullBackupImporter.PREF_PREFIX_TYPE_INT; import static org.thoughtcrime.securesms.backup.FullBackupImporter.PREF_PREFIX_TYPE_INT;
public class TextSecurePreferences { public class TextSecurePreferences {
@ -1353,25 +1354,80 @@ public class TextSecurePreferences {
} }
final LinkedList<BackupProtos.SharedPreference> prefList = new LinkedList<>(); final LinkedList<BackupProtos.SharedPreference> prefList = new LinkedList<>();
addBackupEntryInt (prefList, preferences, prefsFileName, LOCAL_REGISTRATION_ID_PREF);
prefList.add(BackupProtos.SharedPreference.newBuilder() addBackupEntryString(prefList, preferences, prefsFileName, LOCAL_NUMBER_PREF);
.setFile(prefsFileName) addBackupEntryString(prefList, preferences, prefsFileName, PROFILE_NAME_PREF);
.setKey(PREF_PREFIX_TYPE_INT + LOCAL_REGISTRATION_ID_PREF) addBackupEntryString(prefList, preferences, prefsFileName, ATTACHMENT_ENCRYPTED_SECRET);
.setValue(String.valueOf(preferences.getInt(LOCAL_REGISTRATION_ID_PREF, 0))) addBackupEntryString(prefList, preferences, prefsFileName, ATTACHMENT_UNENCRYPTED_SECRET);
.build()); addBackupEntryString(prefList, preferences, prefsFileName, PROFILE_AVATAR_URL_PREF);
prefList.add(BackupProtos.SharedPreference.newBuilder() addBackupEntryInt (prefList, preferences, prefsFileName, PROFILE_AVATAR_ID_PREF);
.setFile(prefsFileName) addBackupEntryString(prefList, preferences, prefsFileName, PROFILE_KEY_PREF);
.setKey(LOCAL_NUMBER_PREF)
.setValue(preferences.getString(LOCAL_NUMBER_PREF, null))
.build());
prefList.add(BackupProtos.SharedPreference.newBuilder()
.setFile(prefsFileName)
.setKey(PROFILE_NAME_PREF)
.setValue(preferences.getString(PROFILE_NAME_PREF, null))
.build());
return prefList; return prefList;
} }
private static void addBackupEntryString(
List<BackupProtos.SharedPreference> outPrefList,
SharedPreferences prefs,
String prefFileName,
String prefKey) {
String value = prefs.getString(prefKey, null);
if (value == null) {
backupEntryLog(prefKey, false);
return;
}
outPrefList.add(BackupProtos.SharedPreference.newBuilder()
.setFile(prefFileName)
.setKey(prefKey)
.setValue(value)
.build());
backupEntryLog(prefKey, true);
}
private static void addBackupEntryInt(
List<BackupProtos.SharedPreference> outPrefList,
SharedPreferences prefs,
String prefFileName,
String prefKey) {
int value = prefs.getInt(prefKey, -1);
if (value == -1) {
backupEntryLog(prefKey, false);
return;
}
outPrefList.add(BackupProtos.SharedPreference.newBuilder()
.setFile(prefFileName)
.setKey(PREF_PREFIX_TYPE_INT + prefKey) // The prefix denotes the type of the preference.
.setValue(String.valueOf(value))
.build());
backupEntryLog(prefKey, true);
}
private static void addBackupEntryBoolean(
List<BackupProtos.SharedPreference> outPrefList,
SharedPreferences prefs,
String prefFileName,
String prefKey) {
if (!prefs.contains(prefKey)) {
backupEntryLog(prefKey, false);
return;
}
outPrefList.add(BackupProtos.SharedPreference.newBuilder()
.setFile(prefFileName)
.setKey(PREF_PREFIX_TYPE_BOOLEAN + prefKey) // The prefix denotes the type of the preference.
.setValue(String.valueOf(prefs.getBoolean(prefKey, false)))
.build());
backupEntryLog(prefKey, true);
}
private static void backupEntryLog(String prefName, boolean wasIncluded) {
StringBuilder sb = new StringBuilder();
sb.append("Backup preference ");
sb.append(wasIncluded ? "+ " : "- ");
sb.append('\"').append(prefName).append("\" ");
if (!wasIncluded) {
sb.append("(is empty and not included)");
}
Log.d(TAG, sb.toString());
}
// endregion // endregion
} }