Clean up. Have ImportActivity call ImportAllTracks

This commit is contained in:
Jimmy Shih
2011-11-05 16:27:46 -07:00
parent 827e94b721
commit 6ccb32c837
7 changed files with 86 additions and 239 deletions
@@ -59,7 +59,7 @@ services.
android:id="@+id/send_to_google_result_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/error_generic"
android:text="@string/error_sending_to_google"
android:visibility="gone"
style="?android:attr/textAppearanceLarge"/>
</LinearLayout>
+1 -1
View File
@@ -41,7 +41,7 @@ limitations under the License.
<string name="error_unable_to_stop_recording">No es pot aturar l\'enregistrament. Proveu de nou.</string>
<string name="error_import_unable_to_read_file">No es pot llegir des del fitxer.</string>
<string name="error_import_invalid_gpx_format">El fitxer no conté un format GPX vàlid.</string>
<string name="error_generic">S\'ha produït un error. Proveu de nou.</string>
<string name="error_sending_to_google">S\'ha produït un error. Proveu de nou.</string>
<string name="error_export_track_does_not_exist">No existeix la ruta.</string>
<string name="error_import_out_of_memory">Aquest fitxer és massa gran (ha d\'ésser inferior a 500KB. S\'està cancel·lant.</string>
<string name="error_unable_to_insert_marker">No es pot inserir un marcador. No hi ha cap ubicació fixa disponible.</string>
+1 -1
View File
@@ -41,7 +41,7 @@ limitations under the License.
<string name="error_unable_to_stop_recording">Anhalten unmöglich. Bitte versuche es noch einmal.</string>
<string name="error_import_unable_to_read_file">Konnte Datei nicht lesen.</string>
<string name="error_import_invalid_gpx_format">Datei ist nicht im GPX-Format.</string>
<string name="error_generic">Ein Fehler ist aufgetreten. Bitte versuche es noch einmal.</string>
<string name="error_sending_to_google">Ein Fehler ist aufgetreten. Bitte versuche es noch einmal.</string>
<string name="error_export_track_does_not_exist">Track existiert nicht.</string>
<string name="error_import_out_of_memory">Die Datei ist zu groß (maximal 500 KB). Operation abgebrochen.</string>
<string name="error_unable_to_insert_marker">Der Wegpunkt kann nicht einfügt werden. Keine Positionsbestimmung möglich.</string>
+1 -1
View File
@@ -43,7 +43,7 @@ limitations under the License.
<string name="error_unable_to_stop_recording">No se puede detener la grabación. Inténtelo de nuevo.</string>
<string name="error_import_unable_to_read_file">No pude leer del archivo.</string>
<string name="error_import_invalid_gpx_format">El archivo no tiene un format GPX válido.</string>
<string name="error_generic">A ocurrido un error. Por favor, inténtelo de nuevo.</string>
<string name="error_sending_to_google">A ocurrido un error. Por favor, inténtelo de nuevo.</string>
<string name="error_export_track_does_not_exist">El recorrido no existe.</string>
<string name="error_export_operation_canceled">La operación fue cancelada.</string>
<string name="error_import_out_of_memory">El archivo es demasiado grande (tiene que ser menor a 500 KB). Abortando.</string>
+8 -43
View File
@@ -86,14 +86,12 @@ limitations under the License.
Unable to insert marker. No GPS location. Try again.
</string>
<!-- A generic error message. -->
<string name="error_generic"
translation_description="In an alert dialog, a generic error message indicating that an error
has occurred and asking the user to try again.">
<!-- Error messages when sending to Google. -->
<string name="error_sending_to_google"
translation_description="When sending a track to Google, a message indicating that an error has
occurred and ask the user to try again.">
An error occurred. Please try again.
</string>
<!-- Error messages when sending to Google. -->
<string name="error_sending_to_my_maps"
translation_description="When sending a track to Google My Maps, a message indicating that an
error has occured.">
@@ -108,27 +106,8 @@ limitations under the License.
translation_description="When sending a track to Google Docs, a message indicating that an error
has occurred.">
There was an error while sending your track to Google Docs.
</string>
<!-- Error messages when importing a track. -->
<string name="error_import_unable_to_read_file"
translation_description="When importing a track from the SD card, a messsage indicating that
My Tracks is unable to read the file.">
Unable to read from file.
</string>
<string name="error_import_invalid_gpx_format"
translation_description="When importing a track from the SD card, a message indicating that
the file does not have a valid GPX format.">
File does not have a valid GPX format.
</string>
<!-- TODO(jshih@google.com) confirm if the file size limit is still 500 KB. -->
<string name="error_import_out_of_memory"
translation_description="When importing a track from the SD card, a message indicating that
the file is too big, must be less than 500 KB, and aborting.">
This file is too big (must be less than 500 KB). Aborting.
</string>
</string>
<!-- Error messages when exporting a track. -->
<string name="error_export_generic"
translation_description="When exporting a track to the SD card, a generic error message
@@ -274,12 +253,7 @@ limitations under the License.
Sending a track to Google Docs...
</string>
<!-- Import and Export progress messages. -->
<string name="progress_message_import"
translation_description="When importing a track from the SD card, the message on the progress
dialog indicating that it may take a while.">
Importing a track from the SD card. This may take a while.
</string>
<!-- Export progress messages. -->
<string name="progress_message_export"
translation_description="When exporting a track to the SD card, the message on the progress
dialog indicating that it may take a while.">
@@ -528,7 +502,7 @@ limitations under the License.
<string name="import_multi_done"
translation_description="After importing all the tracks from the SD card, the alert message
indicating success. E.g., 'Imported 5 of 6 GPX files from /mnt/sdcard/MyTracks/gpx/'.">
Imported %1$d of %2$d GPX files from %3$s
Imported to My Tracks %1$d of %2$d GPX files from %3$s
</string>
<string name="import_multi_empty"
translation_description="After importing all the tracks from the SD card, the alert message
@@ -541,15 +515,6 @@ limitations under the License.
one of the tracks. E.g., 'Import error for file /mnt/sdcard/MyTacks/gpx/1.gpx'.">
Import error for file %s
</string>
<string name="import_success"
translation_description="After successfully importing one track, the message on the confirmation
dialog. E.g., 'Successfully imported 5 track(s) from file'.">
Successfully imported %d track(s) from file.
</string>
<string name="import_show_track"
translation_description="After importing one track, the button to see the track.">
Show track
</string>
<!-- Markers page. -->
<string name="waypointslist_pick_one"
@@ -16,6 +16,7 @@
package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.io.file.GpxImporter;
import com.google.android.apps.mytracks.util.FileUtils;
import com.google.android.apps.mytracks.util.SystemUtils;
@@ -24,7 +25,11 @@ import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.widget.Toast;
@@ -49,22 +54,36 @@ import org.xml.sax.SAXException;
public class ImportAllTracks {
private final Activity activity;
private FileUtils fileUtils;
private boolean displayTrack;
private String gpxPath;
private WakeLock wakeLock;
private ProgressDialog progress;
private FileUtils fileUtils;
private String gpxPath;
private int gpxFileCount;
private int importSuccessCount;
private long importedTrackIds[];
public ImportAllTracks(Activity activity) {
this.activity = activity;
Log.i(Constants.TAG, "ImportAllTracks: Starting");
fileUtils = new FileUtils();
gpxPath = fileUtils.buildExternalDirectoryPath("gpx");
new Thread(runner).start();
this(activity, null);
}
/**
* Constructor to import tracks.
*
* @param activity the activity
* @param path path of the gpx file to import and display. If null, then just
* import all the gpx files under MyTracks/gpx and do not display any
* track.
*/
public ImportAllTracks(Activity activity, String path) {
Log.i(Constants.TAG, "ImportAllTracks: Starting");
this.activity = activity;
fileUtils = new FileUtils();
displayTrack = path != null;
gpxPath = path == null ? fileUtils.buildExternalDirectoryPath("gpx") : path;
new Thread(runner).start();
}
private final Runnable runner = new Runnable() {
public void run() {
aquireLocksAndImport();
@@ -81,7 +100,7 @@ public class ImportAllTracks {
if (prefs != null) {
recordingTrackId = prefs.getLong(activity.getString(R.string.recording_track_key), -1);
}
if (recordingTrackId != -1) {
if (recordingTrackId == -1) {
wakeLock = SystemUtils.acquireWakeLock(activity, wakeLock);
}
@@ -107,12 +126,26 @@ public class ImportAllTracks {
Log.i(Constants.TAG, "ImportAllTracks: Done");
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
if (gpxFileCount == 0) {
builder.setMessage(activity.getString(R.string.import_multi_empty, gpxPath + "/"));
builder.setMessage(activity.getString(R.string.import_multi_empty, gpxPath));
} else {
builder.setMessage(activity.getString(R.string.import_multi_done, importSuccessCount, gpxFileCount,
gpxPath + "/"));
gpxPath));
}
builder.setPositiveButton(R.string.ok, null);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (displayTrack) {
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);
activity.startActivity(intent);
activity.finish();
}
}
});
builder.show();
}
@@ -185,7 +218,7 @@ public class ImportAllTracks {
private boolean importFile(File gpxFile, MyTracksProviderUtils providerUtils) {
Log.i(Constants.TAG, "ImportAllTracks: importing: " + gpxFile.getName());
try {
GpxImporter.importGPXFile(new FileInputStream(gpxFile), providerUtils);
importedTrackIds = GpxImporter.importGPXFile(new FileInputStream(gpxFile), providerUtils);
return true;
} catch (FileNotFoundException e) {
Log.w(Constants.TAG, "GPX file wasn't found/went missing: "
@@ -203,15 +236,22 @@ public class ImportAllTracks {
}
/**
* Returns a list of the GPX Files found in the GPX directory.
* Gets a list of the GPX files. If displayTrack is true, returns a list
* containing just the gpxPath file. If displayTrack is false, returns a list
* of GPX files under the gpxPath directory.
*/
private List<File> getGpxFiles() {
List<File> gpxFiles = new LinkedList<File>();
File[] gpxFileCandidates = new File(gpxPath).listFiles();
if (gpxFileCandidates != null) {
for (File file : gpxFileCandidates) {
if (!file.isDirectory() && file.getName().endsWith(".gpx")) {
gpxFiles.add(file);
List<File> gpxFiles = new LinkedList<File>();
File file = new File(gpxPath);
if (displayTrack) {
gpxFiles.add(file);
} else {
File[] gpxFileCandidates = file.listFiles();
if (gpxFileCandidates != null) {
for (File candidate : gpxFileCandidates) {
if (!candidate.isDirectory() && candidate.getName().endsWith(".gpx")) {
gpxFiles.add(candidate);
}
}
}
}
@@ -17,56 +17,21 @@ package com.google.android.apps.mytracks.io.file;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.ImportAllTracks;
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.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
* Helper activity which imports tracks from a file.
* An activity that imports a track from a file and displays the track in My Tracks.
*
* @author Rodrigo Damazio
*/
public class ImportActivity extends Activity {
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);
providerUtils = MyTracksProviderUtils.Factory.get(this);
}
@Override
public void onStart() {
super.onStart();
@@ -74,143 +39,20 @@ public class ImportActivity extends Activity {
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
if (!(Intent.ACTION_VIEW.equals(action) || Intent.ACTION_ATTACH_DATA.equals(action))
|| !UriUtils.isFileUri(data)) {
Log.e(TAG, "Received an intent with unsupported action or data: " + intent);
if (!(Intent.ACTION_VIEW.equals(action) || Intent.ACTION_ATTACH_DATA.equals(action))) {
Log.e(TAG, "Received an intent with unsupported action: " + intent);
finish();
return;
}
Log.i(TAG, "Importing GPX file at " + data);
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.progress_message_import));
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.setNegativeButton(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.setPositiveButton(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();
if (!UriUtils.isFileUri(data)) {
Log.e(TAG, "Received an intent with unsupported data: " + intent);
finish();
return;
}
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) {
showDialog(PROGRESS_DIALOG);
Thread t = new Thread() {
@Override
public void run() {
resultMessage = R.string.success;
importedTrackIds = null;
try {
try {
InputStream is = new FileInputStream(fileName);
importedTrackIds = GpxImporter.importGPXFile(is, providerUtils);
} catch (SAXException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
resultMessage = R.string.error_generic;
} catch (ParserConfigurationException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
resultMessage = R.string.error_generic;
} catch (IOException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
resultMessage = R.string.error_import_unable_to_read_file;
} catch (NullPointerException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
resultMessage = R.string.error_import_invalid_gpx_format;
} catch (OutOfMemoryError e) {
Log.e(TAG, "Caught an unexpected exception.", e);
resultMessage = R.string.error_import_out_of_memory;
}
boolean success = (importedTrackIds != null && importedTrackIds.length > 0);
showImportResult(success);
} finally {
runOnUiThread(new Runnable() {
public void run() {
dismissDialog(PROGRESS_DIALOG);
}
});
}
}
};
t.start();
}
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.
runOnUiThread(new Runnable() {
@Override
public void run() {
showDialog(dialogToShow);
}
});
String path = data.getPath();
Log.i(TAG, "Importing GPX file at " + path);
new ImportAllTracks(this, path);
}
}