Style cleanup

This commit is contained in:
Rodrigo Damazio
2010-08-24 21:34:28 -03:00
parent 23f1e81cf0
commit 62a54aab58
7 changed files with 95 additions and 59 deletions
@@ -89,7 +89,8 @@ public class MyTracksSettings extends PreferenceActivity {
// Hook up switching of displayed list entries between metric and imperial // Hook up switching of displayed list entries between metric and imperial
// units // units
CheckBoxPreference metricUnitsPreference = CheckBoxPreference metricUnitsPreference =
(CheckBoxPreference) findPreference(getString(R.string.metric_units_key)); (CheckBoxPreference) findPreference(
getString(R.string.metric_units_key));
metricUnitsPreference.setOnPreferenceChangeListener( metricUnitsPreference.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() { new OnPreferenceChangeListener() {
@Override @Override
@@ -118,11 +119,15 @@ public class MyTracksSettings extends PreferenceActivity {
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Preference backupNowPreference = findPreference(getString(R.string.backup_to_sd_key)); Preference backupNowPreference =
Preference restoreNowPreference = findPreference(getString(R.string.restore_from_sd_key)); findPreference(getString(R.string.backup_to_sd_key));
Preference restoreNowPreference =
findPreference(getString(R.string.restore_from_sd_key));
// If recording, disable backup/restore (we don't want to get to inconsistent states) // If recording, disable backup/restore
boolean recording = preferences.getLong(getString(R.string.recording_track_key), -1) != -1; // (we don't want to get to inconsistent states)
boolean recording =
preferences.getLong(getString(R.string.recording_track_key), -1) != -1;
backupNowPreference.setEnabled(!recording); backupNowPreference.setEnabled(!recording);
restoreNowPreference.setEnabled(!recording); restoreNowPreference.setEnabled(!recording);
backupNowPreference.setSummary( backupNowPreference.setSummary(
@@ -133,24 +138,28 @@ public class MyTracksSettings extends PreferenceActivity {
: R.string.settings_restore_from_sd_summary); : R.string.settings_restore_from_sd_summary);
// Add actions to the backup preferences // Add actions to the backup preferences
backupNowPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { backupNowPreference.setOnPreferenceClickListener(
@Override new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) { @Override
Intent intent = new Intent(MyTracksSettings.this, BackupActivity.class); public boolean onPreferenceClick(Preference preference) {
intent.setAction(BackupActivity.BACKUP_ACTION); Intent intent =
startActivity(intent); new Intent(MyTracksSettings.this, BackupActivity.class);
return true; intent.setAction(BackupActivity.BACKUP_ACTION);
} startActivity(intent);
}); return true;
restoreNowPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { }
@Override });
public boolean onPreferenceClick(Preference preference) { restoreNowPreference.setOnPreferenceClickListener(
Intent intent = new Intent(MyTracksSettings.this, BackupActivity.class); new OnPreferenceClickListener() {
intent.setAction(BackupActivity.RESTORE_ACTION); @Override
startActivity(intent); public boolean onPreferenceClick(Preference preference) {
return true; Intent intent =
} new Intent(MyTracksSettings.this, BackupActivity.class);
}); intent.setAction(BackupActivity.RESTORE_ACTION);
startActivity(intent);
return true;
}
});
} }
@Override @Override
@@ -62,7 +62,8 @@ public class BackupActivity extends Activity {
public static final String RESTORE_ACTION = "restore"; public static final String RESTORE_ACTION = "restore";
private final FileUtils fileUtils = new FileUtils(); private final FileUtils fileUtils = new FileUtils();
private final ExternalFileBackup backup = new ExternalFileBackup(this, fileUtils); private final ExternalFileBackup backup =
new ExternalFileBackup(this, fileUtils);
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@@ -155,18 +156,20 @@ public class BackupActivity extends Activity {
Builder confirmationDialogBuilder = new AlertDialog.Builder(this); Builder confirmationDialogBuilder = new AlertDialog.Builder(this);
confirmationDialogBuilder.setMessage(R.string.restore_overwrites_warning); confirmationDialogBuilder.setMessage(R.string.restore_overwrites_warning);
confirmationDialogBuilder.setCancelable(false); confirmationDialogBuilder.setCancelable(false);
confirmationDialogBuilder.setNegativeButton(android.R.string.no, new OnClickListener() { confirmationDialogBuilder.setNegativeButton(android.R.string.no,
@Override new OnClickListener() {
public void onClick(DialogInterface dialog, int which) { @Override
BackupActivity.this.finish(); public void onClick(DialogInterface dialog, int which) {
} BackupActivity.this.finish();
}); }
confirmationDialogBuilder.setPositiveButton(android.R.string.yes, new OnClickListener() { });
@Override confirmationDialogBuilder.setPositiveButton(android.R.string.yes,
public void onClick(DialogInterface dialog, int which) { new OnClickListener() {
pickBackupForRestore(backupDates); @Override
} public void onClick(DialogInterface dialog, int which) {
}); pickBackupForRestore(backupDates);
}
});
confirmationDialogBuilder.create().show(); confirmationDialogBuilder.create().show();
} }
@@ -30,12 +30,14 @@ import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public abstract class BackupPreferencesListener implements OnSharedPreferenceChangeListener { public abstract class BackupPreferencesListener
implements OnSharedPreferenceChangeListener {
/** /**
* Real implementation of the listener, which calls the {@link BackupManager}. * Real implementation of the listener, which calls the {@link BackupManager}.
*/ */
private static class BackupPreferencesListenerImpl extends BackupPreferencesListener { private static class BackupPreferencesListenerImpl
extends BackupPreferencesListener {
private final BackupManager backupManager; private final BackupManager backupManager;
public BackupPreferencesListenerImpl(Context context) { public BackupPreferencesListenerImpl(Context context) {
@@ -43,7 +45,8 @@ public abstract class BackupPreferencesListener implements OnSharedPreferenceCha
} }
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
backupManager.dataChanged(); backupManager.dataChanged();
} }
} }
@@ -51,9 +54,11 @@ public abstract class BackupPreferencesListener implements OnSharedPreferenceCha
/** /**
* Dummy implementation of the listener which does nothing. * Dummy implementation of the listener which does nothing.
*/ */
private static class DummyBackupPreferencesListener extends BackupPreferencesListener { private static class DummyBackupPreferencesListener
extends BackupPreferencesListener {
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
// Do nothing // Do nothing
} }
} }
@@ -60,7 +60,8 @@ class DatabaseDumper {
* This assumes the cursor will have the same column and column indices on * This assumes the cursor will have the same column and column indices on
* every row (and thus may not work with a {@link MergeCursor}). * every row (and thus may not work with a {@link MergeCursor}).
*/ */
public void writeAllRows(Cursor cursor, DataOutputStream writer) throws IOException { public void writeAllRows(Cursor cursor, DataOutputStream writer)
throws IOException {
writeHeaders(cursor, cursor.getCount(), writer); writeHeaders(cursor, cursor.getCount(), writer);
if (!cursor.moveToFirst()) { if (!cursor.moveToFirst()) {
@@ -81,7 +82,8 @@ class DatabaseDumper {
* @param numRows the number of rows that will be later written * @param numRows the number of rows that will be later written
* @throws IOException if there are errors while writing * @throws IOException if there are errors while writing
*/ */
public void writeHeaders(Cursor cursor, int numRows, DataOutputStream writer) throws IOException { public void writeHeaders(Cursor cursor, int numRows, DataOutputStream writer)
throws IOException {
initializeCachedValues(cursor); initializeCachedValues(cursor);
writeQueryMetadata(cursor, numRows, writer); writeQueryMetadata(cursor, numRows, writer);
} }
@@ -93,7 +95,8 @@ class DatabaseDumper {
* @param cursor the cursor to write data from * @param cursor the cursor to write data from
* @throws IOException if there are any errors while writing * @throws IOException if there are any errors while writing
*/ */
public void writeOneRow(Cursor cursor, DataOutputStream writer) throws IOException { public void writeOneRow(Cursor cursor, DataOutputStream writer)
throws IOException {
if (columnIndices == null) { if (columnIndices == null) {
throw new IllegalStateException( throw new IllegalStateException(
"Cannot write rows before writing the header"); "Cannot write rows before writing the header");
@@ -147,7 +150,8 @@ class DatabaseDumper {
* @param numRows the number of rows that will be dumped * @param numRows the number of rows that will be dumped
* @throws IOException if there are any errors while writing * @throws IOException if there are any errors while writing
*/ */
private void writeQueryMetadata(Cursor cursor, int numRows, DataOutputStream writer) throws IOException { private void writeQueryMetadata(
Cursor cursor, int numRows, DataOutputStream writer) throws IOException {
// Write column data // Write column data
writer.writeInt(columnNames.length); writer.writeInt(columnNames.length);
for (int i = 0; i < columnNames.length; i++) { for (int i = 0; i < columnNames.length; i++) {
@@ -169,7 +173,9 @@ class DatabaseDumper {
* @param cursor the cursor to read from * @param cursor the cursor to read from
* @throws IOException if there are any errors while writing * @throws IOException if there are any errors while writing
*/ */
private void writeCell(int columnIdx, byte columnTypeId, Cursor cursor, DataOutputStream writer) throws IOException { private void writeCell(
int columnIdx, byte columnTypeId, Cursor cursor, DataOutputStream writer)
throws IOException {
switch (columnTypeId) { switch (columnTypeId) {
case ContentTypeIds.LONG_TYPE_ID: case ContentTypeIds.LONG_TYPE_ID:
writer.writeLong(cursor.getLong(columnIdx)); writer.writeLong(cursor.getLong(columnIdx));
@@ -190,7 +196,8 @@ class DatabaseDumper {
writer.writeUTF(cursor.getString(columnIdx)); writer.writeUTF(cursor.getString(columnIdx));
return; return;
default: default:
throw new IllegalArgumentException("Type " + columnTypeId + " not supported"); throw new IllegalArgumentException(
"Type " + columnTypeId + " not supported");
} }
} }
@@ -200,7 +207,8 @@ class DatabaseDumper {
* @param columnType the type of the value to write * @param columnType the type of the value to write
* @throws IOException if there are any errors while writing * @throws IOException if there are any errors while writing
*/ */
private void writeDummyCell(byte columnTypeId, DataOutputStream writer) throws IOException { private void writeDummyCell(byte columnTypeId, DataOutputStream writer)
throws IOException {
switch (columnTypeId) { switch (columnTypeId) {
case ContentTypeIds.LONG_TYPE_ID: case ContentTypeIds.LONG_TYPE_ID:
writer.writeLong(0L); writer.writeLong(0L);
@@ -221,7 +229,8 @@ class DatabaseDumper {
writer.writeUTF(""); writer.writeUTF("");
return; return;
default: default:
throw new IllegalArgumentException("Type " + columnTypeId + " not supported"); throw new IllegalArgumentException(
"Type " + columnTypeId + " not supported");
} }
} }
} }
@@ -100,7 +100,8 @@ public class DatabaseImporter {
for (int c = 0; c < numColumns; c++) { for (int c = 0; c < numColumns; c++) {
if ((fields & 1) == 1) { if ((fields & 1) == 1) {
// Field is present, read into values // Field is present, read into values
readOneCell(columnNames[c], columnTypes[c], valueBulk[numValues], reader); readOneCell(columnNames[c], columnTypes[c], valueBulk[numValues],
reader);
} else if (readNullFields) { } else if (readNullFields) {
// Field not present but still written, read and discard // Field not present but still written, read and discard
readOneCell(columnNames[c], columnTypes[c], null, reader); readOneCell(columnNames[c], columnTypes[c], null, reader);
@@ -139,7 +140,8 @@ public class DatabaseImporter {
* in - if null, the value is just discarded * in - if null, the value is just discarded
* @throws IOException if there are any problems while reading * @throws IOException if there are any problems while reading
*/ */
private void readOneCell(String name, byte typeId, ContentValues values, DataInputStream reader) throws IOException { private void readOneCell(String name, byte typeId, ContentValues values,
DataInputStream reader) throws IOException {
switch (typeId) { switch (typeId) {
case ContentTypeIds.BOOLEAN_TYPE_ID: { case ContentTypeIds.BOOLEAN_TYPE_ID: {
boolean value = reader.readBoolean(); boolean value = reader.readBoolean();
@@ -141,7 +141,8 @@ class ExternalFileBackup {
* Synchronously writes a backup to the given file. * Synchronously writes a backup to the given file.
*/ */
private void writeToFile(File outputFile) throws IOException { private void writeToFile(File outputFile) throws IOException {
Log.d(MyTracksConstants.TAG, "Writing backup to file " + outputFile.getAbsolutePath()); Log.d(MyTracksConstants.TAG,
"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();
@@ -212,13 +213,17 @@ class ExternalFileBackup {
* Synchronously restores the backup from the given file. * Synchronously restores the backup from the given file.
*/ */
private void restoreFromFile(File inputFile) throws IOException { private void restoreFromFile(File inputFile) throws IOException {
Log.d(MyTracksConstants.TAG, "Restoring from file " + inputFile.getAbsolutePath()); Log.d(MyTracksConstants.TAG,
"Restoring from file " + inputFile.getAbsolutePath());
PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper(); PreferenceBackupHelper preferencesHelper = new PreferenceBackupHelper();
ContentResolver resolver = context.getContentResolver(); ContentResolver resolver = context.getContentResolver();
DatabaseImporter trackImporter = new DatabaseImporter(TracksColumns.CONTENT_URI, resolver, false); DatabaseImporter trackImporter =
DatabaseImporter waypointImporter = new DatabaseImporter(WaypointsColumns.CONTENT_URI, resolver, false); new DatabaseImporter(TracksColumns.CONTENT_URI, resolver, false);
DatabaseImporter pointImporter = new DatabaseImporter(TrackPointsColumns.CONTENT_URI, resolver, false); DatabaseImporter waypointImporter =
new DatabaseImporter(WaypointsColumns.CONTENT_URI, resolver, false);
DatabaseImporter pointImporter =
new DatabaseImporter(TrackPointsColumns.CONTENT_URI, resolver, false);
FileInputStream inputStream = new FileInputStream(inputFile); FileInputStream inputStream = new FileInputStream(inputFile);
DataInputStream reader = new DataInputStream(inputStream); DataInputStream reader = new DataInputStream(inputStream);
@@ -80,7 +80,8 @@ class PreferenceBackupHelper {
* @return whether the preference change was successful * @return whether the preference change was successful
* @throws IOException if there are any errors while reading * @throws IOException if there are any errors while reading
*/ */
public boolean importPreferences(byte[] data, SharedPreferences preferences) throws IOException { public boolean importPreferences(byte[] data, SharedPreferences preferences)
throws IOException {
ByteArrayInputStream bufStream = new ByteArrayInputStream(data); ByteArrayInputStream bufStream = new ByteArrayInputStream(data);
DataInputStream reader = new DataInputStream(bufStream); DataInputStream reader = new DataInputStream(bufStream);
@@ -148,7 +149,8 @@ class PreferenceBackupHelper {
* @param writer the writer to write to * @param writer the writer to write to
* @throws IOException if there are errors while writing * @throws IOException if there are errors while writing
*/ */
private void writePreference(String name, Object value, DataOutputStream writer) throws IOException { private void writePreference(String name, Object value, DataOutputStream writer)
throws IOException {
writer.writeUTF(name); writer.writeUTF(name);
if (value instanceof Boolean) { if (value instanceof Boolean) {
@@ -167,7 +169,8 @@ 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("Type " + value.getClass().getName() + " not supported"); throw new IllegalArgumentException(
"Type " + value.getClass().getName() + " not supported");
} }
} }
} }