BackupActivity doesn't need to be an activity anymore, it has no own content

This commit is contained in:
Rodrigo Damazio
2010-08-24 21:41:40 -03:00
parent 62a54aab58
commit ad6340c1a7
3 changed files with 32 additions and 75 deletions
-3
View File
@@ -65,9 +65,6 @@
<activity android:name="com.google.android.apps.mytracks.WelcomeActivity" <activity android:name="com.google.android.apps.mytracks.WelcomeActivity"
android:label="@string/welcome"/> android:label="@string/welcome"/>
<activity android:name="com.google.android.apps.mytracks.io.backup.BackupActivity"
android:label="@string/settings_backup" />
<service android:name="com.google.android.apps.mytracks.services.TrackRecordingService" <service android:name="com.google.android.apps.mytracks.services.TrackRecordingService"
android:process=":trackrecord" android:process=":trackrecord"
android:label="Track Recording Service" android:label="Track Recording Service"
@@ -15,7 +15,7 @@
*/ */
package com.google.android.apps.mytracks; package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.io.backup.BackupActivity; import com.google.android.apps.mytracks.io.backup.BackupActivityHelper;
import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener; import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener;
import com.google.android.apps.mytracks.services.SafeStatusAnnouncerTask; import com.google.android.apps.mytracks.services.SafeStatusAnnouncerTask;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
@@ -142,10 +142,9 @@ public class MyTracksSettings extends PreferenceActivity {
new OnPreferenceClickListener() { new OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
Intent intent = BackupActivityHelper backupHelper =
new Intent(MyTracksSettings.this, BackupActivity.class); new BackupActivityHelper(MyTracksSettings.this);
intent.setAction(BackupActivity.BACKUP_ACTION); backupHelper.writeBackup();
startActivity(intent);
return true; return true;
} }
}); });
@@ -153,10 +152,9 @@ public class MyTracksSettings extends PreferenceActivity {
new OnPreferenceClickListener() { new OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
Intent intent = BackupActivityHelper backupHelper =
new Intent(MyTracksSettings.this, BackupActivity.class); new BackupActivityHelper(MyTracksSettings.this);
intent.setAction(BackupActivity.RESTORE_ACTION); backupHelper.restoreBackup();
startActivity(intent);
return true; return true;
} }
}); });
@@ -25,10 +25,8 @@ import android.app.AlertDialog.Builder;
import android.app.Dialog; import android.app.Dialog;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast; import android.widget.Toast;
import java.io.IOException; import java.io.IOException;
@@ -38,13 +36,13 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
/** /**
* Activity which shows a UI for writing or restoring a backup, * Helper which shows a UI for writing or restoring a backup,
* and calls the approriate handler for actually executing those * and calls the appropriate handler for actually executing those
* operations. * operations.
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class BackupActivity extends Activity { public class BackupActivityHelper {
// Since the user sees this format, we use the local timezone // Since the user sees this format, we use the local timezone
private static final SimpleDateFormat DISPLAY_BACKUP_FORMAT = private static final SimpleDateFormat DISPLAY_BACKUP_FORMAT =
@@ -58,53 +56,35 @@ public class BackupActivity extends Activity {
} }
}; };
public static final String BACKUP_ACTION = "backup"; private final FileUtils fileUtils;
public static final String RESTORE_ACTION = "restore"; private final ExternalFileBackup backup;
private final Activity activity;
private final FileUtils fileUtils = new FileUtils(); public BackupActivityHelper(Activity activity) {
private final ExternalFileBackup backup = this.activity = activity;
new ExternalFileBackup(this, fileUtils); this.fileUtils = new FileUtils();
this.backup = new ExternalFileBackup(activity, fileUtils);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
if (action != null) {
if (action.equals(BACKUP_ACTION)) {
writeBackup();
} else if (action.equals(RESTORE_ACTION)) {
restoreBackup();
} else {
throw new IllegalArgumentException("Unknown action: " + action);
}
}
}
} }
/** /**
* Writes a full backup to the default file. * Writes a full backup to the default file.
* This shows the results to the user. * This shows the results to the user.
*/ */
private void writeBackup() { public void writeBackup() {
if (!fileUtils.isSdCardAvailable()) { if (!fileUtils.isSdCardAvailable()) {
showToast(R.string.io_no_external_storage_found); showToast(R.string.io_no_external_storage_found);
finish();
return; return;
} }
if (!backup.isBackupsDirectoryAvailable(true)) { if (!backup.isBackupsDirectoryAvailable(true)) {
showToast(R.string.io_create_dir_failed); showToast(R.string.io_create_dir_failed);
finish();
return; return;
} }
final ProgressDialog progressDialog = ProgressDialog.show( final ProgressDialog progressDialog = ProgressDialog.show(
this, activity,
getString(R.string.progress_title), activity.getString(R.string.progress_title),
getString(R.string.backup_write_progress_message), activity.getString(R.string.backup_write_progress_message),
true); true);
// Do the writing in another thread // Do the writing in another thread
@@ -119,7 +99,6 @@ public class BackupActivity extends Activity {
return; return;
} finally { } finally {
dismissDialog(progressDialog); dismissDialog(progressDialog);
finish();
} }
} }
}.start(); }.start();
@@ -130,39 +109,29 @@ public class BackupActivity extends Activity {
* The user will be given a choice of which backup to restore as well as a * The user will be given a choice of which backup to restore as well as a
* confirmation dialog. * confirmation dialog.
*/ */
private void restoreBackup() { public void restoreBackup() {
// Get the list of existing backups // Get the list of existing backups
if (!fileUtils.isSdCardAvailable()) { if (!fileUtils.isSdCardAvailable()) {
showToast(R.string.io_no_external_storage_found); showToast(R.string.io_no_external_storage_found);
finish();
return; return;
} }
if (!backup.isBackupsDirectoryAvailable(false)) { if (!backup.isBackupsDirectoryAvailable(false)) {
showToast(R.string.no_backups); showToast(R.string.no_backups);
finish();
return; return;
} }
final Date[] backupDates = backup.getAvailableBackups(); final Date[] backupDates = backup.getAvailableBackups();
if (backupDates == null || backupDates.length == 0) { if (backupDates == null || backupDates.length == 0) {
showToast(R.string.no_backups); showToast(R.string.no_backups);
finish();
return; return;
} }
Arrays.sort(backupDates, REVERSE_DATE_ORDER); Arrays.sort(backupDates, REVERSE_DATE_ORDER);
// Show a confirmation dialog // Show a confirmation dialog
Builder confirmationDialogBuilder = new AlertDialog.Builder(this); Builder confirmationDialogBuilder = new AlertDialog.Builder(activity);
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() {
@Override
public void onClick(DialogInterface dialog, int which) {
BackupActivity.this.finish();
}
});
confirmationDialogBuilder.setPositiveButton(android.R.string.yes, confirmationDialogBuilder.setPositiveButton(android.R.string.yes,
new OnClickListener() { new OnClickListener() {
@Override @Override
@@ -183,7 +152,6 @@ public class BackupActivity extends Activity {
if (backupDates.length == 1) { if (backupDates.length == 1) {
// Only one choice, don't bother showing the list // Only one choice, don't bother showing the list
restoreFromDateAsync(backupDates[0]); restoreFromDateAsync(backupDates[0]);
finish();
return; return;
} }
@@ -194,7 +162,7 @@ public class BackupActivity extends Activity {
} }
// Show a dialog for the user to pick which backup to restore // Show a dialog for the user to pick which backup to restore
Builder dialogBuilder = new AlertDialog.Builder(this); Builder dialogBuilder = new AlertDialog.Builder(activity);
dialogBuilder.setCancelable(true); dialogBuilder.setCancelable(true);
dialogBuilder.setTitle(R.string.select_backup_to_restore); dialogBuilder.setTitle(R.string.select_backup_to_restore);
dialogBuilder.setItems(backupDateStrs, new OnClickListener() { dialogBuilder.setItems(backupDateStrs, new OnClickListener() {
@@ -204,12 +172,6 @@ public class BackupActivity extends Activity {
restoreFromDateAsync(backupDates[which]); restoreFromDateAsync(backupDates[which]);
} }
}); });
dialogBuilder.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
BackupActivity.this.finish();
}
});
dialogBuilder.create().show(); dialogBuilder.create().show();
} }
@@ -221,9 +183,9 @@ public class BackupActivity extends Activity {
private void restoreFromDateAsync(final Date date) { private void restoreFromDateAsync(final Date date) {
// Show a progress dialog // Show a progress dialog
final ProgressDialog progressDialog = ProgressDialog.show( final ProgressDialog progressDialog = ProgressDialog.show(
this, activity,
getString(R.string.progress_title), activity.getString(R.string.progress_title),
getString(R.string.backup_import_progress_message), activity.getString(R.string.backup_import_progress_message),
true); true);
// Do the actual importing in another thread (don't block the UI) // Do the actual importing in another thread (don't block the UI)
@@ -250,19 +212,19 @@ public class BackupActivity extends Activity {
* This forces any modified data to be re-read. * This forces any modified data to be re-read.
*/ */
private void restartApplication() { private void restartApplication() {
Intent intent = new Intent(this, MyTracks.class); Intent intent = new Intent(activity, MyTracks.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent); activity.startActivity(intent);
} }
/** /**
* Shows a toast with the given contents. * Shows a toast with the given contents.
*/ */
private void showToast(final int resId) { private void showToast(final int resId) {
runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
Toast.makeText(BackupActivity.this, resId, Toast.LENGTH_LONG).show(); Toast.makeText(activity, resId, Toast.LENGTH_LONG).show();
} }
}); });
} }
@@ -271,7 +233,7 @@ public class BackupActivity extends Activity {
* Safely dismisses the given dialog. * Safely dismisses the given dialog.
*/ */
private void dismissDialog(final Dialog dialog) { private void dismissDialog(final Dialog dialog) {
runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
dialog.dismiss(); dialog.dismiss();