From a177834e073557111a6a52fb335ca05b4825fc3b Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Wed, 12 Sep 2012 12:33:47 -0700 Subject: [PATCH] When backing/restoring prefereneces, do not include preferences for the current device session. --- MyTracks/res/values/keys.xml | 11 ++- .../io/backup/ExternalFileBackup.java | 4 +- .../io/backup/MyTracksBackupAgent.java | 2 +- .../io/backup/PreferenceBackupHelper.java | 84 +++++++++++++------ .../io/backup/PreferenceBackupHelperTest.java | 7 +- 5 files changed, 71 insertions(+), 37 deletions(-) diff --git a/MyTracks/res/values/keys.xml b/MyTracks/res/values/keys.xml index 0cbdb8b13..1769da0f2 100644 --- a/MyTracks/res/values/keys.xml +++ b/MyTracks/res/values/keys.xml @@ -22,7 +22,6 @@ limitations under the License. antCombinedBikeSensorId antHeartRateMonitorId antSpeedDistanceMonitorId - autoResumeTrackCurrentRetry autoResumeTrackTimeout bluetoothSensor chartByDistance @@ -40,10 +39,7 @@ limitations under the License. minRecordingInterval minRequiredAccuracy pickExistingMap - recordingTrackId - recordingTrackPaused reportSpeed - selectedTrackId sendToDocs sendToFusionTables sendToMaps @@ -59,6 +55,13 @@ limitations under the License. trackColorModeSlow trackColorMode trackName + + + autoResumeTrackCurrentRetry + recordingTrackId + recordingTrackPaused + selectedTrackId + settingsBackup settingsBackupNow diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/backup/ExternalFileBackup.java b/MyTracks/src/com/google/android/apps/mytracks/io/backup/ExternalFileBackup.java index 560cd1408..82fd11e04 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/backup/ExternalFileBackup.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/backup/ExternalFileBackup.java @@ -150,7 +150,7 @@ class ExternalFileBackup { "Writing backup to file " + outputFile.getAbsolutePath()); // Create all the auxiliary classes that will do the writing - PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(); + PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(context); DatabaseDumper trackDumper = new DatabaseDumper( TracksColumns.COLUMNS, TracksColumns.COLUMN_TYPES, @@ -223,7 +223,7 @@ class ExternalFileBackup { Log.d(Constants.TAG, "Restoring from file " + inputFile.getAbsolutePath()); - PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(); + PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(context); ContentResolver resolver = context.getContentResolver(); DatabaseImporter trackImporter = new DatabaseImporter(TracksColumns.CONTENT_URI, resolver, false); diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/backup/MyTracksBackupAgent.java b/MyTracks/src/com/google/android/apps/mytracks/io/backup/MyTracksBackupAgent.java index 7830a482f..c8ea7bc86 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/backup/MyTracksBackupAgent.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/backup/MyTracksBackupAgent.java @@ -59,7 +59,7 @@ public class MyTracksBackupAgent extends BackupAgent { } protected PreferenceBackupHelper createPreferenceBackupHelper() { - return new PreferenceBackupHelper(); + return new PreferenceBackupHelper(this); } @Override diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/backup/PreferenceBackupHelper.java b/MyTracks/src/com/google/android/apps/mytracks/io/backup/PreferenceBackupHelper.java index 1b70dc1f5..9e41e40f5 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/backup/PreferenceBackupHelper.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/backup/PreferenceBackupHelper.java @@ -17,7 +17,9 @@ package com.google.android.apps.mytracks.io.backup; import com.google.android.apps.mytracks.content.ContentTypeIds; import com.google.android.apps.mytracks.util.ApiAdapterFactory; +import com.google.android.maps.mytracks.R; +import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; @@ -26,26 +28,36 @@ import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.util.HashSet; import java.util.Map; /** * Helper for backing up and restoring shared preferences. - * + * * @author Rodrigo Damazio */ -class PreferenceBackupHelper { +public class PreferenceBackupHelper { private static final int BUFFER_SIZE = 2048; + private final HashSet doNotBackup; + + public PreferenceBackupHelper(Context context) { + doNotBackup = new HashSet(); + doNotBackup.add(context.getString(R.string.auto_resume_track_current_retry_key)); + doNotBackup.add(context.getString(R.string.recording_track_id_key)); + doNotBackup.add(context.getString(R.string.recording_track_paused_key)); + doNotBackup.add(context.getString(R.string.selected_track_id_key)); + } + /** * Exports all shared preferences from the given object as a byte array. - * + * * @param preferences the preferences to export * @return the corresponding byte array * @throws IOException if there are any errors while writing to the byte array */ - public byte[] exportPreferences(SharedPreferences preferences) - throws IOException { + public byte[] exportPreferences(SharedPreferences preferences) throws IOException { ByteArrayOutputStream bufStream = new ByteArrayOutputStream(BUFFER_SIZE); DataOutputStream outWriter = new DataOutputStream(bufStream); exportPreferences(preferences, outWriter); @@ -56,16 +68,18 @@ class PreferenceBackupHelper { /** * Exports all shared preferences from the given object into the given output * stream. - * + * * @param preferences the preferences to export * @param outWriter the stream to write them to * @throws IOException if there are any errors while writing the output */ - public void exportPreferences( - SharedPreferences preferences, - DataOutputStream outWriter) throws IOException { + public void exportPreferences(SharedPreferences preferences, DataOutputStream outWriter) + throws IOException { Map values = preferences.getAll(); + for (String name : doNotBackup) { + values.remove(name); + } outWriter.writeInt(values.size()); for (Map.Entry entry : values.entrySet()) { writePreference(entry.getKey(), entry.getValue(), outWriter); @@ -75,13 +89,12 @@ class PreferenceBackupHelper { /** * Imports all preferences from the given byte array. - * + * * @param data the byte array to read preferences from * @param preferences the shared preferences to edit * @throws IOException if there are any errors while reading */ - public void importPreferences(byte[] data, SharedPreferences preferences) - throws IOException { + public void importPreferences(byte[] data, SharedPreferences preferences) throws IOException { ByteArrayInputStream bufStream = new ByteArrayInputStream(data); DataInputStream reader = new DataInputStream(bufStream); @@ -90,13 +103,13 @@ class PreferenceBackupHelper { /** * Imports all preferences from the given stream. - * + * * @param reader the stream to read from * @param preferences the shared preferences to edit * @throws IOException if there are any errors while reading */ - public void importPreferences(DataInputStream reader, - SharedPreferences preferences) throws IOException { + public void importPreferences(DataInputStream reader, SharedPreferences preferences) + throws IOException { Editor editor = preferences.edit(); editor.clear(); @@ -111,37 +124,57 @@ class PreferenceBackupHelper { /** * Reads a single preference and sets it into the given editor. - * + * * @param name the name of the preference to read * @param typeId the type ID of the preference to read * @param reader the reader to read from * @param editor the editor to set the preference in * @throws IOException if there are errors while reading */ - private void readAndSetPreference(String name, byte typeId, - DataInputStream reader, Editor editor) throws IOException { + private void readAndSetPreference(String name, byte typeId, DataInputStream reader, Editor editor) + throws IOException { + + boolean save = true; + if (doNotBackup.contains(name)) { + save = false; + } switch (typeId) { case ContentTypeIds.BOOLEAN_TYPE_ID: - editor.putBoolean(name, reader.readBoolean()); + boolean booleanValue = reader.readBoolean(); + if (save) { + editor.putBoolean(name, booleanValue); + } return; case ContentTypeIds.LONG_TYPE_ID: - editor.putLong(name, reader.readLong()); + long longValue = reader.readLong(); + if (save) { + editor.putLong(name, longValue); + } return; case ContentTypeIds.FLOAT_TYPE_ID: - editor.putFloat(name, reader.readFloat()); + float floatValue = reader.readFloat(); + if (save) { + editor.putFloat(name, floatValue); + } return; case ContentTypeIds.INT_TYPE_ID: - editor.putInt(name, reader.readInt()); + int intValue = reader.readInt(); + if (save) { + editor.putInt(name, intValue); + } return; case ContentTypeIds.STRING_TYPE_ID: - editor.putString(name, reader.readUTF()); + String utfValue = reader.readUTF(); + if (save) { + editor.putString(name, utfValue); + } return; } } /** * Writes a single preference. - * + * * @param name the name of the preference to write * @param value the correctly-typed value of the preference * @param writer the writer to write to @@ -167,8 +200,7 @@ class PreferenceBackupHelper { writer.writeByte(ContentTypeIds.STRING_TYPE_ID); writer.writeUTF((String) value); } else { - throw new IllegalArgumentException( - "Type " + value.getClass().getName() + " not supported"); + throw new IllegalArgumentException("Type " + value.getClass().getName() + " not supported"); } } } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/backup/PreferenceBackupHelperTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/backup/PreferenceBackupHelperTest.java index 66dc55b4d..1035763f8 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/backup/PreferenceBackupHelperTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/backup/PreferenceBackupHelperTest.java @@ -18,19 +18,18 @@ package com.google.android.apps.mytracks.io.backup; import android.annotation.TargetApi; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; +import android.test.AndroidTestCase; import java.util.HashMap; import java.util.Map; import java.util.Set; -import junit.framework.TestCase; - /** * Tests for {@link PreferenceBackupHelper}. * * @author Rodrigo Damazio */ -public class PreferenceBackupHelperTest extends TestCase { +public class PreferenceBackupHelperTest extends AndroidTestCase { private Map preferenceValues; private SharedPreferences preferences; private PreferenceBackupHelper preferenceBackupHelper; @@ -173,7 +172,7 @@ public class PreferenceBackupHelperTest extends TestCase { preferenceValues = new HashMap(); preferences = new MockPreferences(); - preferenceBackupHelper = new PreferenceBackupHelper(); + preferenceBackupHelper = new PreferenceBackupHelper(getContext()); } @TargetApi(9)