Merge heads

This commit is contained in:
youtaol
2012-09-13 11:32:20 +08:00
parent 6f862a09d9
commit 6389b873cf
5 changed files with 71 additions and 37 deletions
+7 -4
View File
@@ -22,7 +22,6 @@ limitations under the License.
<string name="ant_combined_bike_sensor_id_key">antCombinedBikeSensorId</string> <string name="ant_combined_bike_sensor_id_key">antCombinedBikeSensorId</string>
<string name="ant_heart_rate_monitor_id_key">antHeartRateMonitorId</string> <string name="ant_heart_rate_monitor_id_key">antHeartRateMonitorId</string>
<string name="ant_speed_distance_monitor_id_key">antSpeedDistanceMonitorId</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="auto_resume_track_timeout_key">autoResumeTrackTimeout</string>
<string name="bluetooth_sensor_key">bluetoothSensor</string> <string name="bluetooth_sensor_key">bluetoothSensor</string>
<string name="chart_by_distance_key">chartByDistance</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_recording_interval_key">minRecordingInterval</string>
<string name="min_required_accuracy_key">minRequiredAccuracy</string> <string name="min_required_accuracy_key">minRequiredAccuracy</string>
<string name="pick_existing_map_key">pickExistingMap</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="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_docs_key">sendToDocs</string>
<string name="send_to_fusion_tables_key">sendToFusionTables</string> <string name="send_to_fusion_tables_key">sendToFusionTables</string>
<string name="send_to_maps_key">sendToMaps</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_slow_key">trackColorModeSlow</string>
<string name="track_color_mode_key">trackColorMode</string> <string name="track_color_mode_key">trackColorMode</string>
<string name="track_name_key">trackName</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. --> <!-- Keys for non-persisted preferences. Sorted alphabetically. DO NOT TRANSLATE. -->
<string name="settings_backup_key">settingsBackup</string> <string name="settings_backup_key">settingsBackup</string>
<string name="settings_backup_now_key">settingsBackupNow</string> <string name="settings_backup_now_key">settingsBackupNow</string>
@@ -150,7 +150,7 @@ class ExternalFileBackup {
"Writing backup to file " + outputFile.getAbsolutePath()); "Writing backup to file " + outputFile.getAbsolutePath());
// Create all the auxiliary classes that will do the writing // Create all the auxiliary classes that will do the writing
PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(); PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(context);
DatabaseDumper trackDumper = new DatabaseDumper( DatabaseDumper trackDumper = new DatabaseDumper(
TracksColumns.COLUMNS, TracksColumns.COLUMNS,
TracksColumns.COLUMN_TYPES, TracksColumns.COLUMN_TYPES,
@@ -223,7 +223,7 @@ class ExternalFileBackup {
Log.d(Constants.TAG, Log.d(Constants.TAG,
"Restoring from file " + inputFile.getAbsolutePath()); "Restoring from file " + inputFile.getAbsolutePath());
PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(); PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(context);
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
DatabaseImporter trackImporter = DatabaseImporter trackImporter =
new DatabaseImporter(TracksColumns.CONTENT_URI, resolver, false); new DatabaseImporter(TracksColumns.CONTENT_URI, resolver, false);
@@ -59,7 +59,7 @@ public class MyTracksBackupAgent extends BackupAgent {
} }
protected PreferenceBackupHelper createPreferenceBackupHelper() { protected PreferenceBackupHelper createPreferenceBackupHelper() {
return new PreferenceBackupHelper(); return new PreferenceBackupHelper(this);
} }
@Override @Override
@@ -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.content.ContentTypeIds;
import com.google.android.apps.mytracks.util.ApiAdapterFactory; 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;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
@@ -26,26 +28,36 @@ import java.io.ByteArrayOutputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
/** /**
* Helper for backing up and restoring shared preferences. * Helper for backing up and restoring shared preferences.
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
class PreferenceBackupHelper { public class PreferenceBackupHelper {
private static final int BUFFER_SIZE = 2048; 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. * Exports all shared preferences from the given object as a byte array.
* *
* @param preferences the preferences to export * @param preferences the preferences to export
* @return the corresponding byte array * @return the corresponding byte array
* @throws IOException if there are any errors while writing to the byte array * @throws IOException if there are any errors while writing to the byte array
*/ */
public byte[] exportPreferences(SharedPreferences preferences) public byte[] exportPreferences(SharedPreferences preferences) throws IOException {
throws IOException {
ByteArrayOutputStream bufStream = new ByteArrayOutputStream(BUFFER_SIZE); ByteArrayOutputStream bufStream = new ByteArrayOutputStream(BUFFER_SIZE);
DataOutputStream outWriter = new DataOutputStream(bufStream); DataOutputStream outWriter = new DataOutputStream(bufStream);
exportPreferences(preferences, outWriter); exportPreferences(preferences, outWriter);
@@ -56,16 +68,18 @@ class PreferenceBackupHelper {
/** /**
* Exports all shared preferences from the given object into the given output * Exports all shared preferences from the given object into the given output
* stream. * stream.
* *
* @param preferences the preferences to export * @param preferences the preferences to export
* @param outWriter the stream to write them to * @param outWriter the stream to write them to
* @throws IOException if there are any errors while writing the output * @throws IOException if there are any errors while writing the output
*/ */
public void exportPreferences( public void exportPreferences(SharedPreferences preferences, DataOutputStream outWriter)
SharedPreferences preferences, throws IOException {
DataOutputStream outWriter) throws IOException {
Map<String, ?> values = preferences.getAll(); Map<String, ?> values = preferences.getAll();
for (String name : doNotBackup) {
values.remove(name);
}
outWriter.writeInt(values.size()); outWriter.writeInt(values.size());
for (Map.Entry<String, ?> entry : values.entrySet()) { for (Map.Entry<String, ?> entry : values.entrySet()) {
writePreference(entry.getKey(), entry.getValue(), outWriter); writePreference(entry.getKey(), entry.getValue(), outWriter);
@@ -75,13 +89,12 @@ class PreferenceBackupHelper {
/** /**
* Imports all preferences from the given byte array. * Imports all preferences from the given byte array.
* *
* @param data the byte array to read preferences from * @param data the byte array to read preferences from
* @param preferences the shared preferences to edit * @param preferences the shared preferences to edit
* @throws IOException if there are any errors while reading * @throws IOException if there are any errors while reading
*/ */
public void importPreferences(byte[] data, SharedPreferences preferences) public void importPreferences(byte[] data, SharedPreferences preferences) throws IOException {
throws IOException {
ByteArrayInputStream bufStream = new ByteArrayInputStream(data); ByteArrayInputStream bufStream = new ByteArrayInputStream(data);
DataInputStream reader = new DataInputStream(bufStream); DataInputStream reader = new DataInputStream(bufStream);
@@ -90,13 +103,13 @@ class PreferenceBackupHelper {
/** /**
* Imports all preferences from the given stream. * Imports all preferences from the given stream.
* *
* @param reader the stream to read from * @param reader the stream to read from
* @param preferences the shared preferences to edit * @param preferences the shared preferences to edit
* @throws IOException if there are any errors while reading * @throws IOException if there are any errors while reading
*/ */
public void importPreferences(DataInputStream reader, public void importPreferences(DataInputStream reader, SharedPreferences preferences)
SharedPreferences preferences) throws IOException { throws IOException {
Editor editor = preferences.edit(); Editor editor = preferences.edit();
editor.clear(); editor.clear();
@@ -111,37 +124,57 @@ class PreferenceBackupHelper {
/** /**
* Reads a single preference and sets it into the given editor. * Reads a single preference and sets it into the given editor.
* *
* @param name the name of the preference to read * @param name the name of the preference to read
* @param typeId the type ID of the preference to read * @param typeId the type ID of the preference to read
* @param reader the reader to read from * @param reader the reader to read from
* @param editor the editor to set the preference in * @param editor the editor to set the preference in
* @throws IOException if there are errors while reading * @throws IOException if there are errors while reading
*/ */
private void readAndSetPreference(String name, byte typeId, private void readAndSetPreference(String name, byte typeId, DataInputStream reader, Editor editor)
DataInputStream reader, Editor editor) throws IOException { throws IOException {
boolean save = true;
if (doNotBackup.contains(name)) {
save = false;
}
switch (typeId) { switch (typeId) {
case ContentTypeIds.BOOLEAN_TYPE_ID: case ContentTypeIds.BOOLEAN_TYPE_ID:
editor.putBoolean(name, reader.readBoolean()); boolean booleanValue = reader.readBoolean();
if (save) {
editor.putBoolean(name, booleanValue);
}
return; return;
case ContentTypeIds.LONG_TYPE_ID: case ContentTypeIds.LONG_TYPE_ID:
editor.putLong(name, reader.readLong()); long longValue = reader.readLong();
if (save) {
editor.putLong(name, longValue);
}
return; return;
case ContentTypeIds.FLOAT_TYPE_ID: case ContentTypeIds.FLOAT_TYPE_ID:
editor.putFloat(name, reader.readFloat()); float floatValue = reader.readFloat();
if (save) {
editor.putFloat(name, floatValue);
}
return; return;
case ContentTypeIds.INT_TYPE_ID: case ContentTypeIds.INT_TYPE_ID:
editor.putInt(name, reader.readInt()); int intValue = reader.readInt();
if (save) {
editor.putInt(name, intValue);
}
return; return;
case ContentTypeIds.STRING_TYPE_ID: case ContentTypeIds.STRING_TYPE_ID:
editor.putString(name, reader.readUTF()); String utfValue = reader.readUTF();
if (save) {
editor.putString(name, utfValue);
}
return; return;
} }
} }
/** /**
* Writes a single preference. * Writes a single preference.
* *
* @param name the name of the preference to write * @param name the name of the preference to write
* @param value the correctly-typed value of the preference * @param value the correctly-typed value of the preference
* @param writer the writer to write to * @param writer the writer to write to
@@ -167,8 +200,7 @@ class PreferenceBackupHelper {
writer.writeByte(ContentTypeIds.STRING_TYPE_ID); writer.writeByte(ContentTypeIds.STRING_TYPE_ID);
writer.writeUTF((String) value); writer.writeUTF((String) value);
} else { } else {
throw new IllegalArgumentException( throw new IllegalArgumentException("Type " + value.getClass().getName() + " not supported");
"Type " + value.getClass().getName() + " not supported");
} }
} }
} }
@@ -18,19 +18,18 @@ package com.google.android.apps.mytracks.io.backup;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.test.AndroidTestCase;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import junit.framework.TestCase;
/** /**
* Tests for {@link PreferenceBackupHelper}. * Tests for {@link PreferenceBackupHelper}.
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class PreferenceBackupHelperTest extends TestCase { public class PreferenceBackupHelperTest extends AndroidTestCase {
private Map<String, ?> preferenceValues; private Map<String, ?> preferenceValues;
private SharedPreferences preferences; private SharedPreferences preferences;
private PreferenceBackupHelper preferenceBackupHelper; private PreferenceBackupHelper preferenceBackupHelper;
@@ -173,7 +172,7 @@ public class PreferenceBackupHelperTest extends TestCase {
preferenceValues = new HashMap<String, Object>(); preferenceValues = new HashMap<String, Object>();
preferences = new MockPreferences(); preferences = new MockPreferences();
preferenceBackupHelper = new PreferenceBackupHelper(); preferenceBackupHelper = new PreferenceBackupHelper(getContext());
} }
@TargetApi(9) @TargetApi(9)