diff --git a/MyTracks/src/com/google/android/apps/mytracks/ChartActivity.java b/MyTracks/src/com/google/android/apps/mytracks/ChartActivity.java index 1d9ba4e4c..5b7a9346d 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/ChartActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/ChartActivity.java @@ -35,6 +35,7 @@ import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.maps.mytracks.R; import android.app.Activity; +import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.location.Location; @@ -60,6 +61,7 @@ import java.util.EnumSet; */ public class ChartActivity extends Activity implements TrackDataListener { + private static final int CHART_SETTINGS_DIALOG = 1; private final DoubleBuffer elevationBuffer = new DoubleBuffer(Constants.ELEVATION_SMOOTHING_FACTOR); private final DoubleBuffer speedBuffer = @@ -203,34 +205,49 @@ public class ChartActivity extends Activity implements TrackDataListener { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Constants.MENU_CHART_SETTINGS: - showSettingsDialog(); + showDialog(CHART_SETTINGS_DIALOG); return true; } return super.onOptionsItemSelected(item); } - private void showSettingsDialog() { - final ChartSettingsDialog settingsDialog = new ChartSettingsDialog(this); - settingsDialog.setOwnerActivity(this); + @Override + protected Dialog onCreateDialog(int id) { + if (id == CHART_SETTINGS_DIALOG) { + final ChartSettingsDialog settingsDialog = new ChartSettingsDialog(this); + settingsDialog.setOnClickListener(new OnClickListener() { + @Override + public void onClick(DialogInterface arg0, int which) { + if (which != DialogInterface.BUTTON_POSITIVE) return; + for (int i = 0; i < ChartView.NUM_SERIES; i++) { + boolean seriesEnabled = settingsDialog.isSeriesEnabled(i); + chartView.getChartValueSeries(i).setEnabled(seriesEnabled); + } + setMode(settingsDialog.getMode()); + chartView.postInvalidate(); + } + }); + return settingsDialog; + } + + return super.onCreateDialog(id); + } + + @Override + protected void onPrepareDialog(int id, Dialog dialog) { + super.onPrepareDialog(id, dialog); + + if (id == CHART_SETTINGS_DIALOG) { + prepareSettingsDialog((ChartSettingsDialog) dialog); + } + } + + private void prepareSettingsDialog(final ChartSettingsDialog settingsDialog) { settingsDialog.setMode(mode); for (int i = 0; i < ChartView.NUM_SERIES; i++) { settingsDialog.setSeriesEnabled(i, chartView.getChartValueSeries(i).isEnabled()); } - - settingsDialog.setOnClickListener(new OnClickListener() { - @Override - public void onClick(DialogInterface arg0, int which) { - if (which != DialogInterface.BUTTON_POSITIVE) return; - - for (int i = 0; i < ChartView.NUM_SERIES; i++) { - chartView.getChartValueSeries(i).setEnabled(settingsDialog.isSeriesEnabled(i)); - } - setMode(settingsDialog.getMode()); - } - }); - - settingsDialog.show(); } /** diff --git a/MyTracks/src/com/google/android/apps/mytracks/ChartSettingsDialog.java b/MyTracks/src/com/google/android/apps/mytracks/ChartSettingsDialog.java index 228cee046..0e95d4901 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/ChartSettingsDialog.java +++ b/MyTracks/src/com/google/android/apps/mytracks/ChartSettingsDialog.java @@ -1,12 +1,12 @@ /* * Copyright 2009 Google Inc. - * + * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -97,7 +97,7 @@ public class ChartSettingsDialog extends Dialog { public Mode getMode() { if (distance == null) return Mode.BY_DISTANCE; - return distance.isSelected() ? Mode.BY_DISTANCE : Mode.BY_TIME; + return distance.isChecked() ? Mode.BY_DISTANCE : Mode.BY_TIME; } public boolean isSeriesEnabled(int seriesIdx) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/DialogManager.java b/MyTracks/src/com/google/android/apps/mytracks/DialogManager.java index 01eba2c3e..dab5329f3 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/DialogManager.java +++ b/MyTracks/src/com/google/android/apps/mytracks/DialogManager.java @@ -17,12 +17,8 @@ package com.google.android.apps.mytracks; import static com.google.android.apps.mytracks.Constants.TAG; -import com.google.android.maps.mytracks.R; - import android.app.Activity; -import android.app.AlertDialog; import android.app.Dialog; -import android.content.DialogInterface; import android.util.Log; import android.view.WindowManager.BadTokenException; @@ -33,24 +29,6 @@ import android.view.WindowManager.BadTokenException; */ public class DialogManager { - public static void showMessageDialog( - Activity ctx, int message, boolean success, DialogInterface.OnClickListener okListener) { - if (ctx.isFinishing()) { - Log.w(TAG, "Activity finishing - not showing dialog"); - return; - } - - AlertDialog dialog = null; - AlertDialog.Builder builder = new AlertDialog.Builder(ctx); - builder.setMessage(message); - builder.setNeutralButton(R.string.ok, okListener); - builder.setIcon(success ? android.R.drawable.ic_dialog_info : - android.R.drawable.ic_dialog_alert); - builder.setTitle(success ? R.string.success : R.string.error); - dialog = builder.create(); - dialog.show(); - } - /** * The equivalent of {@link #showDialogSafely(int)}, but for a specific * dialog instance. diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/SendToMyMaps.java b/MyTracks/src/com/google/android/apps/mytracks/io/SendToMyMaps.java index f451078d3..d5f0d293e 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/SendToMyMaps.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/SendToMyMaps.java @@ -100,7 +100,6 @@ public class SendToMyMaps implements Runnable { int statusMessageId = R.string.error_sending_to_mymap; boolean success = true; try { - progressIndicator.setProgressValue(1); progressIndicator.setProgressMessage( R.string.progress_message_reading_track); @@ -454,9 +453,10 @@ public class SendToMyMaps implements Runnable { // The percent of the total that represents the completed part of this // segment. int totalPercentage = - (totalLocationsRead + totalLocationsPrepared + totalLocationsUploaded) + (totalLocationsRead + totalLocationsPrepared + totalLocationsUploaded) * 100 / (totalLocations * 3); totalPercentage = Math.min(99, totalPercentage); + Log.d(TAG, "Percentage: read=" + totalLocationsRead + "; prep=" + totalLocationsPrepared + "; up=" + totalLocationsUploaded + "; total=" + totalLocations + "; percentage=" + totalPercentage); progressIndicator.setProgressValue(totalPercentage); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportActivity.java index 7fe66f3be..1ca2b8061 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportActivity.java @@ -17,7 +17,6 @@ package com.google.android.apps.mytracks.io.file; import static com.google.android.apps.mytracks.Constants.TAG; -import com.google.android.apps.mytracks.DialogManager; import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.TracksColumns; import com.google.android.apps.mytracks.util.UriUtils; @@ -26,6 +25,7 @@ import com.google.android.maps.mytracks.R; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; +import android.app.Dialog; import android.app.ProgressDialog; import android.content.ContentUris; import android.content.DialogInterface; @@ -50,9 +50,16 @@ import org.xml.sax.SAXException; */ public class ImportActivity extends Activity { - private ProgressDialog progressDialog; + private static final int PROGRESS_DIALOG = 1; + private static final int SUCCESS_DIALOG = 2; + private static final int FAILURE_DIALOG = 3; + private MyTracksProviderUtils providerUtils; + private ProgressDialog progressDialog; + private int resultMessage; + private long importedTrackIds[]; + @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); @@ -78,49 +85,114 @@ public class ImportActivity extends Activity { startTrackImport(data.getPath()); } + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case PROGRESS_DIALOG: + progressDialog = new ProgressDialog(this); + progressDialog.setIcon(android.R.drawable.ic_dialog_info); + progressDialog.setTitle(R.string.progress_title); + progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); + progressDialog.setMessage(getString(R.string.import_progress_message)); + return progressDialog; + case SUCCESS_DIALOG: + final Builder dialogBuilder = new AlertDialog.Builder(this); + dialogBuilder.setCancelable(true); + dialogBuilder.setPositiveButton(android.R.string.ok, new OnClickListener() { + @Override + public void onClick(DialogInterface arg0, int arg1) { + finish(); + } + }); + dialogBuilder.setNeutralButton(R.string.import_show_track, new OnClickListener() { + @Override + public void onClick(DialogInterface arg0, int arg1) { + showImportedTrack(); + finish(); + } + }); + return dialogBuilder.create(); + case FAILURE_DIALOG: + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setNeutralButton(R.string.ok, new OnClickListener() { + @Override + public void onClick(DialogInterface arg0, int arg1) { + finish(); + } + }); + builder.setIcon(android.R.drawable.ic_dialog_alert); + builder.setTitle(R.string.error); + return builder.create(); + + } + + return null; + } + + @Override + protected void onPrepareDialog(int id, Dialog dialog) { + super.onPrepareDialog(id, dialog); + + switch (id) { + case SUCCESS_DIALOG: + AlertDialog successDialog = (AlertDialog) dialog; + successDialog.setMessage(getString(R.string.import_success, importedTrackIds.length)); + break; + case FAILURE_DIALOG: + AlertDialog failureDialog = (AlertDialog) dialog; + failureDialog.setMessage(getString(resultMessage)); + break; + } + } + + protected void showImportedTrack() { + long lastTrackId = importedTrackIds[importedTrackIds.length - 1]; + Uri trackUri = ContentUris.withAppendedId(TracksColumns.CONTENT_URI, lastTrackId); + + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(trackUri, TracksColumns.CONTENT_ITEMTYPE); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + finish(); + } + private void startTrackImport(final String fileName) { - progressDialog = new ProgressDialog(this); - progressDialog.setOwnerActivity(this); - progressDialog.setIcon(android.R.drawable.ic_dialog_info); - progressDialog.setTitle(R.string.progress_title); - progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); - progressDialog.setMessage(getString(R.string.import_progress_message)); - progressDialog.show(); + showDialog(PROGRESS_DIALOG); Thread t = new Thread() { @Override public void run() { - int message = R.string.success; + resultMessage = R.string.success; - long[] trackIdsImported = null; + importedTrackIds = null; try { try { InputStream is = new FileInputStream(fileName); - trackIdsImported = GpxImporter.importGPXFile(is, providerUtils); + importedTrackIds = GpxImporter.importGPXFile(is, providerUtils); } catch (SAXException e) { Log.e(TAG, "Caught an unexpected exception.", e); - message = R.string.error_generic; + resultMessage = R.string.error_generic; } catch (ParserConfigurationException e) { Log.e(TAG, "Caught an unexpected exception.", e); - message = R.string.error_generic; + resultMessage = R.string.error_generic; } catch (IOException e) { Log.e(TAG, "Caught an unexpected exception.", e); - message = R.string.error_unable_to_read_file; + resultMessage = R.string.error_unable_to_read_file; } catch (NullPointerException e) { Log.e(TAG, "Caught an unexpected exception.", e); - message = R.string.error_invalid_gpx_format; + resultMessage = R.string.error_invalid_gpx_format; } catch (OutOfMemoryError e) { Log.e(TAG, "Caught an unexpected exception.", e); - message = R.string.error_out_of_memory; + resultMessage = R.string.error_out_of_memory; } - boolean success = (trackIdsImported != null && trackIdsImported.length > 0); - onImportDone(success, message, trackIdsImported); + boolean success = (importedTrackIds != null && importedTrackIds.length > 0); + showImportResult(success); } finally { runOnUiThread(new Runnable() { public void run() { - progressDialog.dismiss(); + dismissDialog(PROGRESS_DIALOG); } }); } @@ -129,44 +201,15 @@ public class ImportActivity extends Activity { t.start(); } - private void onImportDone(boolean success, int message, final long[] trackIds) { - OnClickListener finishOnClick = new OnClickListener() { - @Override - public void onClick(DialogInterface arg0, int arg1) { - finish(); - } - }; - - if (!success) { - DialogManager.showMessageDialog(this, message, false /* success */, - finishOnClick); - return; - } + private void showImportResult(boolean success) { + final int dialogToShow = success ? SUCCESS_DIALOG : FAILURE_DIALOG; // Show a dialog telling the user about the import, and asking if he wishes // to open the track right away. - final Builder dialogBuilder = new AlertDialog.Builder(this); - dialogBuilder.setCancelable(true); - dialogBuilder.setMessage(getString(R.string.import_success, trackIds.length)); - dialogBuilder.setPositiveButton(android.R.string.ok, finishOnClick); - dialogBuilder.setNeutralButton(R.string.import_show_track, new OnClickListener() { - @Override - public void onClick(DialogInterface arg0, int arg1) { - long lastTrackId = trackIds[trackIds.length - 1]; - Uri trackUri = ContentUris.withAppendedId(TracksColumns.CONTENT_URI, lastTrackId); - - Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(trackUri, TracksColumns.CONTENT_ITEMTYPE); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - startActivity(intent); - finish(); - } - }); - runOnUiThread(new Runnable() { @Override public void run() { - dialogBuilder.show(); + showDialog(dialogToShow); } }); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java index d825c482c..429d2d772 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java @@ -18,7 +18,6 @@ package com.google.android.apps.mytracks.io.file; import static com.google.android.apps.mytracks.Constants.TAG; import com.google.android.apps.mytracks.Constants; -import com.google.android.apps.mytracks.DialogManager; import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.TracksColumns; import com.google.android.apps.mytracks.io.file.TrackWriterFactory.TrackFileFormat; @@ -27,9 +26,12 @@ import com.google.android.apps.mytracks.util.UriUtils; import com.google.android.maps.mytracks.R; import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; import android.content.ContentUris; import android.content.Context; import android.content.DialogInterface; +import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.net.Uri; @@ -46,12 +48,15 @@ import java.io.File; public class SaveActivity extends Activity { public static final String EXTRA_SHARE_FILE = "share_file"; public static final String EXTRA_FILE_FORMAT = "file_format"; + private static final int RESULT_DIALOG = 1; + /* VisibleForTesting */ static final int PROGRESS_DIALOG = 2; private MyTracksProviderUtils providerUtils; private long trackId; private TrackWriter writer; private boolean shareFile; private TrackFileFormat format; + private WriteProgressController controller; @Override protected void onCreate(Bundle savedInstanceState) { @@ -99,7 +104,7 @@ public class SaveActivity extends Activity { writer.setDirectory(dir); } - WriteProgressController controller = new WriteProgressController(this, writer); + controller = new WriteProgressController(this, writer, PROGRESS_DIALOG); controller.setOnCompletionListener(new WriteProgressController.OnCompletionListener() { @Override public void onComplete() { @@ -138,14 +143,47 @@ public class SaveActivity extends Activity { } private void showResultDialog() { - DialogManager.showMessageDialog(this, writer.getErrorMessage(), writer.wasSuccess(), - new OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int arg1) { - dialog.dismiss(); - finish(); - } - }); + removeDialog(RESULT_DIALOG); + showDialog(RESULT_DIALOG); + } + + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case RESULT_DIALOG: + return createResultDialog(); + case PROGRESS_DIALOG: + if (controller != null) { + return controller.createProgressDialog(); + } + default: + return super.onCreateDialog(id); + } + } + + private Dialog createResultDialog() { + boolean success = writer.wasSuccess(); + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage(writer.getErrorMessage()); + builder.setNeutralButton(R.string.ok, new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int arg1) { + dialog.dismiss(); + finish(); + } + }); + builder.setOnCancelListener(new OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + dialog.dismiss(); + finish(); + } + }); + builder.setIcon(success ? android.R.drawable.ic_dialog_info : + android.R.drawable.ic_dialog_alert); + builder.setTitle(success ? R.string.success : R.string.error); + return builder.create(); } public static void handleExportTrackAction(Context ctx, long trackId, int actionCode) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/WriteProgressController.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/WriteProgressController.java index 861df8ddd..eb7ca9c06 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/WriteProgressController.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/WriteProgressController.java @@ -15,7 +15,6 @@ */ package com.google.android.apps.mytracks.io.file; -import com.google.android.apps.mytracks.DialogManager; import com.google.android.maps.mytracks.R; import android.app.Activity; @@ -46,9 +45,10 @@ class WriteProgressController { private final Activity activity; private final TrackWriter writer; - private final ProgressDialog dialog; + private ProgressDialog dialog; private OnCompletionListener onCompletionListener; + private final int progressDialogId; /** * @param activity the activity associated with this write @@ -57,17 +57,10 @@ class WriteProgressController { * interested in notification upon completion of the write, they should * use {@link #setOnCompletionListener}. */ - public WriteProgressController(Activity activity, TrackWriter writer) { + public WriteProgressController(Activity activity, TrackWriter writer, int progressDialogId) { this.activity = activity; this.writer = writer; - - dialog = new ProgressDialog(activity); - dialog.setIcon(android.R.drawable.ic_dialog_info); - dialog.setTitle(activity.getString(R.string.progress_title)); - dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - dialog.setMessage(activity.getString(R.string.write_progress_message)); - dialog.setIndeterminate(true); - dialog.setOnCancelListener(dialogCancelListener); + this.progressDialogId = progressDialogId; writer.setOnCompletionListener(writerCompleteListener); writer.setOnWriteListener(writerWriteListener); @@ -78,9 +71,20 @@ class WriteProgressController { this.onCompletionListener = onCompletionListener; } + public ProgressDialog createProgressDialog() { + dialog = new ProgressDialog(activity); + dialog.setIcon(android.R.drawable.ic_dialog_info); + dialog.setTitle(activity.getString(R.string.progress_title)); + dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + dialog.setMessage(activity.getString(R.string.write_progress_message)); + dialog.setIndeterminate(true); + dialog.setOnCancelListener(dialogCancelListener); + return dialog; + } + /** Initiate an asynchronous write. */ public void startWrite() { - DialogManager.showDialogSafely(activity, dialog); + activity.showDialog(progressDialogId); writer.writeTrackAsync(); } @@ -101,7 +105,7 @@ class WriteProgressController { new TrackWriter.OnCompletionListener() { @Override public void onComplete() { - DialogManager.dismissDialogSafely(activity, dialog); + activity.dismissDialog(progressDialogId); if (onCompletionListener != null) { onCompletionListener.onComplete(); diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/ResultDialogFactory.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/ResultDialogFactory.java index f4d77892e..544d3aee0 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/ResultDialogFactory.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/ResultDialogFactory.java @@ -58,7 +58,8 @@ public class ResultDialogFactory { */ public static AlertDialog makeDialog(Activity activity, List results, DialogInterface.OnClickListener onOkClickListener, - DialogInterface.OnClickListener onShareClickListener) { + DialogInterface.OnClickListener onShareClickListener, + DialogInterface.OnCancelListener onCancelListener) { boolean success = true; for (SendResult result : results) { if (!result.isSuccess()) { @@ -84,6 +85,8 @@ public class ResultDialogFactory { onShareClickListener); } + builder.setOnCancelListener(onCancelListener); + return builder.create(); } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendActivity.java index 06876b5c0..05ae0105d 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/sendtogoogle/SendActivity.java @@ -21,7 +21,6 @@ import com.google.android.accounts.Account; import com.google.android.apps.analytics.GoogleAnalyticsTracker; import com.google.android.apps.mytracks.AccountChooser; import com.google.android.apps.mytracks.Constants; -import com.google.android.apps.mytracks.DialogManager; import com.google.android.apps.mytracks.MyMapsList; import com.google.android.apps.mytracks.ProgressIndicator; import com.google.android.apps.mytracks.content.MyTracksProviderUtils; @@ -40,11 +39,12 @@ import com.google.android.apps.mytracks.util.UriUtils; import com.google.android.maps.mytracks.R; import android.app.Activity; -import android.app.AlertDialog; +import android.app.Dialog; import android.app.ProgressDialog; import android.content.ContentUris; import android.content.Context; import android.content.DialogInterface; +import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.SharedPreferences; @@ -66,6 +66,10 @@ import java.util.List; public class SendActivity extends Activity implements ProgressIndicator { public static final String EXTRA_SHARE_LINK = "share_link"; + private static final int SEND_DIALOG = 1; + private static final int PROGRESS_DIALOG = 2; + /* @VisibleForTesting */ static final int DONE_DIALOG = 3; + // Services private MyTracksProviderUtils providerUtils; private SharedPreferences sharedPreferences; @@ -101,6 +105,13 @@ public class SendActivity extends Activity implements ProgressIndicator { private SendDialog sendDialog; private ProgressDialog progressDialog; + private final OnCancelListener finishOnCancelListener = new OnCancelListener() { + @Override + public void onCancel(DialogInterface arg0) { + onAllDone(); + } + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -134,20 +145,39 @@ public class SendActivity extends Activity implements ProgressIndicator { sendTrackId = ContentUris.parseId(data); + showDialog(SEND_DIALOG); + } + + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case SEND_DIALOG: + return createSendDialog(); + case PROGRESS_DIALOG: + return createProgressDialog(); + case DONE_DIALOG: + return createDoneDialog(); + } + + return null; + } + + private Dialog createSendDialog() { sendDialog = new SendDialog(this); - sendDialog.setOwnerActivity(this); sendDialog.setOnClickListener(new OnClickListener() { @Override - public void onClick(DialogInterface arg0, int which) { + public void onClick(DialogInterface dialog, int which) { if (which != DialogInterface.BUTTON_POSITIVE) { finish(); return; } + dialog.dismiss(); doSend(); } }); - sendDialog.show(); + sendDialog.setOnCancelListener(finishOnCancelListener); + return sendDialog; } @Override @@ -163,18 +193,9 @@ public class SendActivity extends Activity implements ProgressIndicator { * Initiates the process to send tracks to google. * This is called once the user has selected sending options via the * SendToGoogleDialog. - * - * TODO: Change this whole flow to an actual state machine. */ private void doSend() { - progressDialog = new ProgressDialog(this); - progressDialog.setIcon(android.R.drawable.ic_dialog_info); - progressDialog.setTitle(R.string.progress_title); - progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - progressDialog.setMessage(""); - progressDialog.setMax(100); - progressDialog.setProgress(0); - progressDialog.show(); + showDialog(PROGRESS_DIALOG); if (sendDialog.getSendToMyMaps()) { sendToGoogleMapsOrPickMap(); @@ -188,6 +209,18 @@ public class SendActivity extends Activity implements ProgressIndicator { } } + private Dialog createProgressDialog() { + progressDialog = new ProgressDialog(this); + progressDialog.setCancelable(false); + progressDialog.setIcon(android.R.drawable.ic_dialog_info); + progressDialog.setTitle(R.string.progress_title); + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progressDialog.setMax(100); + progressDialog.setProgress(0); + + return progressDialog; + } + private void sendToGoogleMapsOrPickMap() { if (!sendDialog.getCreateNewMap()) { // Ask the user to choose a map to upload into @@ -370,19 +403,24 @@ public class SendActivity extends Activity implements ProgressIndicator { private void onSendToGoogleDone() { tracker.dispatch(); - final boolean sentToMyMaps = sendDialog.getSendToMyMaps(); - final boolean sentToFusionTables = sendDialog.getSendToFusionTables(); - List results = makeSendToGoogleResults(); - runOnUiThread(new Runnable() { @Override public void run() { - progressDialog.dismiss(); - progressDialog = null; - sendDialog = null; + Log.d(TAG, "Sending to Google done."); + dismissDialog(PROGRESS_DIALOG); + dismissDialog(SEND_DIALOG); + + // Ensure a new done dialog is created each time. + // This is required because the send results must be available at the + // time the dialog is created. + removeDialog(DONE_DIALOG); + showDialog(DONE_DIALOG); } }); + } + private Dialog createDoneDialog() { + Log.d(TAG, "Creating done dialog"); // We've finished sending the track to the user-selected services. Now // we tell them the results of the upload, and optionally share the track. // There are a few different paths through this code: @@ -401,14 +439,16 @@ public class SendActivity extends Activity implements ProgressIndicator { // which we succeeded in uploading the track are incompatible with // sharing. We won't display a share button. - final boolean canShare = sendToFusionTablesTableId != null - || sendToMyMapsMapId != null; + final boolean sentToMyMaps = sendDialog.getSendToMyMaps(); + final boolean sentToFusionTables = sendDialog.getSendToFusionTables(); + List results = makeSendToGoogleResults(); + final boolean canShare = sendToFusionTablesTableId != null || sendToMyMapsMapId != null; final OnClickListener finishListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); - finish(); + onAllDone(); } }; @@ -417,11 +457,13 @@ public class SendActivity extends Activity implements ProgressIndicator { doShareListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + if (!shareLinkToMap(sentToMyMaps, sentToFusionTables)) { Log.w(TAG, "Failed to share link"); } - finishListener.onClick(dialog, which); + onAllDone(); } }; } @@ -431,9 +473,17 @@ public class SendActivity extends Activity implements ProgressIndicator { DialogInterface.OnClickListener onShareListener = (canShare && !shareRequested) ? doShareListener : null; - AlertDialog sendToGoogleResultDialog = ResultDialogFactory.makeDialog(this, - results, onOkListener, onShareListener); - DialogManager.showDialogSafely(this, sendToGoogleResultDialog); + return ResultDialogFactory.makeDialog(this, results, onOkListener, onShareListener, finishOnCancelListener); + } + + private void onAllDone() { + Log.d(TAG, "All sending done."); + removeDialog(PROGRESS_DIALOG); + removeDialog(SEND_DIALOG); + removeDialog(DONE_DIALOG); + progressDialog = null; + sendDialog = null; + finish(); } boolean shareLinkToMap(boolean sentToMyMaps, boolean sentToFusionTables) { @@ -535,9 +585,9 @@ public class SendActivity extends Activity implements ProgressIndicator { @Override public void onAccountSelected(Account account) { if (account == null) { - progressDialog.dismiss(); - progressDialog = null; + dismissDialog(PROGRESS_DIALOG); finish(); + return; } doLogin(results, requestCode, service, account); @@ -565,7 +615,8 @@ public class SendActivity extends Activity implements ProgressIndicator { // make this return path explicit. if (resultCode != RESULT_OK || lastAuth == null || !lastAuth.authResult(resultCode, results)) { - progressDialog.dismiss(); + dismissDialog(PROGRESS_DIALOG); + finish(); } break; } diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/WriteProgressControllerTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/WriteProgressControllerTest.java index 19d251281..7810af87d 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/file/WriteProgressControllerTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/file/WriteProgressControllerTest.java @@ -15,10 +15,6 @@ */ package com.google.android.apps.mytracks.io.file; -import com.google.android.apps.mytracks.MyTracks; -import com.google.android.apps.mytracks.io.file.TrackWriter; -import com.google.android.apps.mytracks.io.file.WriteProgressController; - import android.app.ProgressDialog; import android.test.ActivityInstrumentationTestCase2; @@ -30,9 +26,9 @@ import java.util.concurrent.atomic.AtomicReference; * * @author Matthew Simmons */ -public class WriteProgressControllerTest extends ActivityInstrumentationTestCase2 { +public class WriteProgressControllerTest extends ActivityInstrumentationTestCase2 { public WriteProgressControllerTest() { - super(MyTracks.class); + super(SaveActivity.class); } private static void assertProgress(ProgressDialog dialog, int expectedProgress, @@ -58,7 +54,7 @@ public class WriteProgressControllerTest extends ActivityInstrumentationTestCase }; WriteProgressController controller = new WriteProgressController( - getActivity(), mockWriter); + getActivity(), mockWriter, SaveActivity.PROGRESS_DIALOG); controller.setOnCompletionListener(new WriteProgressController.OnCompletionListener() { @Override public void onComplete() { @@ -66,7 +62,7 @@ public class WriteProgressControllerTest extends ActivityInstrumentationTestCase } }); - dialogRef.set(controller.getDialog()); + dialogRef.set(controller.createProgressDialog()); controller.startWrite(); diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/ResultDialogFactoryTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/ResultDialogFactoryTest.java index c88035e71..b13e3d1a9 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/ResultDialogFactoryTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/io/sendtogoogle/ResultDialogFactoryTest.java @@ -15,7 +15,6 @@ */ package com.google.android.apps.mytracks.io.sendtogoogle; -import com.google.android.apps.mytracks.MyTracks; import com.google.android.maps.mytracks.R; import android.app.AlertDialog; @@ -33,9 +32,9 @@ import java.util.List; * * @author Matthew Simmons */ -public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2 { +public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2 { public ResultDialogFactoryTest() { - super(MyTracks.class); + super(SendActivity.class); } private List makeResults(SendResult... results) { @@ -55,7 +54,7 @@ public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2 results = makeResults(new SendResult(SendType.MYMAPS, true), new SendResult(SendType.DOCS, true)); - AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null); + AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null, null); dialog.show(); ListView listView = (ListView) dialog.findViewById(R.id.send_to_google_result_list); @@ -87,7 +86,7 @@ public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2 results = makeResults(new SendResult(SendType.MYMAPS, true), new SendResult(SendType.DOCS, false)); - AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null); + AlertDialog dialog = ResultDialogFactory.makeDialog(getActivity(), results, clickListener, null, null); dialog.show(); assertEquals(View.GONE, @@ -112,7 +111,7 @@ public class ResultDialogFactoryTest extends ActivityInstrumentationTestCase2