forked from upstream-mirrors/OpenTracks
When backing/restoring prefereneces, do not include preferences for
the current device session.
This commit is contained in:
@@ -22,7 +22,6 @@ limitations under the License.
|
||||
<string name="ant_combined_bike_sensor_id_key">antCombinedBikeSensorId</string>
|
||||
<string name="ant_heart_rate_monitor_id_key">antHeartRateMonitorId</string>
|
||||
<string name="ant_speed_distance_monitor_id_key">antSpeedDistanceMonitorId</string>
|
||||
<string name="auto_resume_track_current_retry_key">autoResumeTrackCurrentRetry</string>
|
||||
<string name="auto_resume_track_timeout_key">autoResumeTrackTimeout</string>
|
||||
<string name="bluetooth_sensor_key">bluetoothSensor</string>
|
||||
<string name="chart_by_distance_key">chartByDistance</string>
|
||||
@@ -40,10 +39,7 @@ limitations under the License.
|
||||
<string name="min_recording_interval_key">minRecordingInterval</string>
|
||||
<string name="min_required_accuracy_key">minRequiredAccuracy</string>
|
||||
<string name="pick_existing_map_key">pickExistingMap</string>
|
||||
<string name="recording_track_id_key">recordingTrackId</string>
|
||||
<string name="recording_track_paused_key">recordingTrackPaused</string>
|
||||
<string name="report_speed_key">reportSpeed</string>
|
||||
<string name="selected_track_id_key">selectedTrackId</string>
|
||||
<string name="send_to_docs_key">sendToDocs</string>
|
||||
<string name="send_to_fusion_tables_key">sendToFusionTables</string>
|
||||
<string name="send_to_maps_key">sendToMaps</string>
|
||||
@@ -59,6 +55,13 @@ limitations under the License.
|
||||
<string name="track_color_mode_slow_key">trackColorModeSlow</string>
|
||||
<string name="track_color_mode_key">trackColorMode</string>
|
||||
<string name="track_name_key">trackName</string>
|
||||
|
||||
<!-- Keys for persistend preferences. But they should not get backed up or restored. -->
|
||||
<string name="auto_resume_track_current_retry_key">autoResumeTrackCurrentRetry</string>
|
||||
<string name="recording_track_id_key">recordingTrackId</string>
|
||||
<string name="recording_track_paused_key">recordingTrackPaused</string>
|
||||
<string name="selected_track_id_key">selectedTrackId</string>
|
||||
|
||||
<!-- Keys for non-persisted preferences. Sorted alphabetically. DO NOT TRANSLATE. -->
|
||||
<string name="settings_backup_key">settingsBackup</string>
|
||||
<string name="settings_backup_now_key">settingsBackupNow</string>
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -59,7 +59,7 @@ public class MyTracksBackupAgent extends BackupAgent {
|
||||
}
|
||||
|
||||
protected PreferenceBackupHelper createPreferenceBackupHelper() {
|
||||
return new PreferenceBackupHelper();
|
||||
return new PreferenceBackupHelper(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+58
-26
@@ -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<String> doNotBackup;
|
||||
|
||||
public PreferenceBackupHelper(Context context) {
|
||||
doNotBackup = new HashSet<String>();
|
||||
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<String, ?> values = preferences.getAll();
|
||||
|
||||
for (String name : doNotBackup) {
|
||||
values.remove(name);
|
||||
}
|
||||
outWriter.writeInt(values.size());
|
||||
for (Map.Entry<String, ?> 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-4
@@ -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<String, ?> preferenceValues;
|
||||
private SharedPreferences preferences;
|
||||
private PreferenceBackupHelper preferenceBackupHelper;
|
||||
@@ -173,7 +172,7 @@ public class PreferenceBackupHelperTest extends TestCase {
|
||||
|
||||
preferenceValues = new HashMap<String, Object>();
|
||||
preferences = new MockPreferences();
|
||||
preferenceBackupHelper = new PreferenceBackupHelper();
|
||||
preferenceBackupHelper = new PreferenceBackupHelper(getContext());
|
||||
}
|
||||
|
||||
@TargetApi(9)
|
||||
|
||||
Reference in New Issue
Block a user