Moving track importing out of MyTracks class.

This commit is contained in:
Rodrigo Damazio
2011-05-25 19:19:21 -03:00
parent e8a872d351
commit 90fe6921e2
9 changed files with 213 additions and 207 deletions
+16 -11
View File
@@ -46,17 +46,6 @@ limitations under the License.
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.track" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.ATTACH_DATA" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:mimeType="application/gpx+xml" />
<data android:mimeType="application/gpx" />
<data android:mimeType="application/vnd.google-earth.gpx" />
<data android:mimeType="application/vnd.google-earth.gpx+xml" />
</intent-filter>
</activity>
<activity android:name="com.google.android.apps.mytracks.MapActivity"
@@ -138,6 +127,22 @@ limitations under the License.
android:label="@string/tracklist_write_to_sd"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:noHistory="true" />
<activity android:name="com.google.android.apps.mytracks.io.file.ImportActivity"
android:label="@string/import_title"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.ATTACH_DATA" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:mimeType="application/gpx+xml" />
<data android:mimeType="application/gpx" />
<data android:mimeType="application/vnd.google-earth.gpx" />
<data android:mimeType="application/vnd.google-earth.gpx+xml" />
</intent-filter>
</activity>
<activity android:name="com.google.android.apps.mytracks.WelcomeActivity"
android:label="@string/welcome"
+2 -2
View File
@@ -159,8 +159,8 @@ limitations under the License.
$2 : Attempted imports
$3 : MyTracks gpx path
-->
<string name="import_done">%3$s からGPXファイル %2$d件のうち %1$d件をインポートしました</string>
<string name="import_empty">%s ではGPXファイルがありません.SDカードを読み込めないかディレクトリにGPXファイルが無いかも知れません.</string>
<string name="import_multi_done">%3$s からGPXファイル %2$d件のうち %1$d件をインポートしました</string>
<string name="import_multi_empty">%s ではGPXファイルがありません.SDカードを読み込めないかディレクトリにGPXファイルが無いかも知れません.</string>
<string name="import_error">ファイル %s のインポートエラー</string>
<!-- Waypoints list -->
+2 -2
View File
@@ -162,8 +162,8 @@ limitations under the License.
$2 : Attempted imports
$3 : MyTracks gpx path
-->
<string name="import_done">Импортировано %1$d из %2$d GPX файлов с %3$s</string>
<string name="import_empty">GPX файлы не найденны в %s. Возможно не читается SD-карта или каталог не содержит GPX файлы.</string>
<string name="import_multi_done">Импортировано %1$d из %2$d GPX файлов с %3$s</string>
<string name="import_multi_empty">GPX файлы не найденны в %s. Возможно не читается SD-карта или каталог не содержит GPX файлы.</string>
<string name="import_error">Ошибка импорта файла %s</string>
<!-- Waypoints list -->
+5 -2
View File
@@ -163,9 +163,12 @@ limitations under the License.
$2 : Attempted imports
$3 : MyTracks gpx path
-->
<string name="import_done">Imported %1$d of %2$d GPX files from %3$s</string>
<string name="import_empty">No GPX files found in %s. SD card may be unreadable or directory may not contain GPX files.</string>
<string name="import_multi_done">Imported %1$d of %2$d GPX files from %3$s</string>
<string name="import_multi_empty">No GPX files found in %s. SD card may be unreadable or directory may not contain GPX files.</string>
<string name="import_error">Import error for file %s</string>
<string name="import_title">Import into My Tracks</string>
<string name="import_success">Successfully imported %1$d track(s) from file.</string>
<string name="import_show_track">Show track</string>
<!-- Waypoints list -->
<string name="waypointslist_btn_delete_all">Delete all markers</string>
@@ -17,16 +17,13 @@ package com.google.android.apps.mytracks;
import static com.google.android.apps.mytracks.Constants.TAG;
import com.google.android.apps.mytracks.io.sendtogoogle.SendDialog;
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.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager.BadTokenException;
@@ -37,74 +34,12 @@ import android.view.WindowManager.BadTokenException;
*/
public class DialogManager {
public static final int DIALOG_IMPORT_PROGRESS = 2;
public static final int DIALOG_PROGRESS = 3;
private ProgressDialog progressDialog;
private ProgressDialog importProgressDialog;
private SendDialog sendToGoogleDialog;
private MyTracks activity;
public DialogManager(MyTracks activity) {
this.activity = activity;
}
protected Dialog onCreateDialog(int id, Bundle args) {
switch (id) {
case DIALOG_IMPORT_PROGRESS:
importProgressDialog = new ProgressDialog(activity);
importProgressDialog.setIcon(android.R.drawable.ic_dialog_info);
importProgressDialog.setTitle(
activity.getString(R.string.progress_title));
importProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
importProgressDialog.setMessage(
activity.getString(R.string.import_progress_message));
return importProgressDialog;
case DIALOG_PROGRESS:
progressDialog = new ProgressDialog(activity);
progressDialog.setIcon(android.R.drawable.ic_dialog_info);
progressDialog.setTitle(activity.getString(R.string.progress_title));
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("");
progressDialog.setMax(100);
progressDialog.setProgress(10);
return progressDialog;
}
return null;
}
public void setProgressMessage(final String message) {
activity.runOnUiThread(new Runnable() {
public void run() {
synchronized (this) {
if (progressDialog != null) {
progressDialog.setMessage(message);
}
}
}
});
}
public void setProgressValue(final int percent) {
activity.runOnUiThread(new Runnable() {
public void run() {
synchronized (this) {
if (progressDialog != null) {
progressDialog.setProgress(percent);
}
}
}
});
}
/**
* @return the sendToGoogleDialog
*/
public SendDialog getSendToGoogleDialog() {
return sendToGoogleDialog;
}
/**
* Shows a dialog with the given message.
* Does it on the UI thread.
@@ -104,9 +104,9 @@ 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_empty, gpxPath + "/"));
builder.setMessage(activity.getString(R.string.import_multi_empty, gpxPath + "/"));
} else {
builder.setMessage(activity.getString(R.string.import_done, importSuccessCount, gpxFileCount,
builder.setMessage(activity.getString(R.string.import_multi_done, importSuccessCount, gpxFileCount,
gpxPath + "/"));
}
builder.setPositiveButton(R.string.ok, null);
@@ -16,12 +16,11 @@
package com.google.android.apps.mytracks;
import static com.google.android.apps.mytracks.Constants.TAG;
import static com.google.android.apps.mytracks.DialogManager.DIALOG_IMPORT_PROGRESS;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.TrackDataHub;
import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.io.file.GpxImporter;
import com.google.android.apps.mytracks.io.file.TempFileCleaner;
import com.google.android.apps.mytracks.services.ITrackRecordingService;
import com.google.android.apps.mytracks.services.TrackRecordingService;
@@ -32,9 +31,9 @@ import com.google.android.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
@@ -55,21 +54,12 @@ import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
* The super activity that embeds our sub activities.
*
* @author Leif Hendrik Wilden
*/
public class MyTracks extends TabActivity implements OnTouchListener,
ProgressIndicator {
public class MyTracks extends TabActivity implements OnTouchListener {
private TrackDataHub dataHub;
/*
@@ -230,73 +220,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
// This will show the eula until the user accepts or quits the app.
Eula.showEulaRequireAcceptance(this);
// Check if we got invoked via the VIEW intent:
Intent intent = getIntent();
String action;
if (intent != null && (action = intent.getAction()) != null) {
if (action.equals(Intent.ACTION_MAIN)) {
// Do nothing.
} else if (action.equals(Intent.ACTION_VIEW)) {
if (intent.getScheme() != null && intent.getScheme().equals("file")) {
Log.w(TAG, "Received a VIEW intent with file scheme. Importing.");
importGpxFile(intent.getData().getPath());
} else {
Log.w(TAG, "Received a VIEW intent with unsupported scheme " + intent.getScheme());
}
} else {
Log.w(TAG, "Received an intent with unsupported action " + action);
}
} else {
Log.d(TAG, "Received an intent with no action.");
}
}
private void importGpxFile(final String fileName) {
dialogManager.showDialogSafely(DIALOG_IMPORT_PROGRESS);
Thread t = new Thread() {
@Override
public void run() {
int message = R.string.success;
long[] trackIdsImported = null;
try {
try {
InputStream is = new FileInputStream(fileName);
trackIdsImported = GpxImporter.importGPXFile(is, providerUtils);
} catch (SAXException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
message = R.string.error_generic;
} catch (ParserConfigurationException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
message = R.string.error_generic;
} catch (IOException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
message = 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;
} catch (OutOfMemoryError e) {
Log.e(TAG, "Caught an unexpected exception.", e);
message = R.string.error_out_of_memory;
}
if (trackIdsImported != null && trackIdsImported.length > 0) {
// select last track from import file
dataHub.loadTrack(trackIdsImported[trackIdsImported.length - 1]);
} else {
dialogManager.showMessageDialog(message, false/* success */);
}
} finally {
runOnUiThread(new Runnable() {
public void run() {
dismissDialog(DIALOG_IMPORT_PROGRESS);
}
});
}
}
};
t.start();
}
@Override
@@ -349,6 +272,14 @@ public class MyTracks extends TabActivity implements OnTouchListener,
Intent startIntent = new Intent(this, TrackRecordingService.class);
startService(startIntent);
}
Intent intent = getIntent();
String action = intent.getAction();
if ((Intent.ACTION_VIEW.equals(action) || Intent.ACTION_EDIT.equals(action))
&& TracksColumns.CONTENT_ITEMTYPE.equals(intent.getType())) {
long trackId = ContentUris.parseId(intent.getData());
dataHub.loadTrack(trackId);
}
}
/*
@@ -376,21 +307,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
: super.onOptionsItemSelected(item);
}
/*
* Dialog events:
* ==============
*/
@Override
protected Dialog onCreateDialog(int id, Bundle args) {
return dialogManager.onCreateDialog(id, args);
}
@Override
protected Dialog onCreateDialog(int id) {
return dialogManager.onCreateDialog(id, null);
}
/*
* Key events:
* ===========
@@ -487,22 +403,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
}
return false;
}
// ProgressIndicator implementation
@Override
public void setProgressMessage(int resId) {
dialogManager.setProgressMessage(getString(resId));
}
@Override
public void clearProgressMessage() {
dialogManager.setProgressMessage("");
}
@Override
public void setProgressValue(final int percent) {
dialogManager.setProgressValue(percent);
}
/**
* Deletes the track with the given id.
@@ -0,0 +1,175 @@
/*
* Copyright 2011 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
* License for the specific language governing permissions and limitations under
* the License.
*/
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.maps.mytracks.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
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.
*
* @author Rodrigo Damazio
*/
public class ImportActivity extends Activity {
private ProgressDialog progressDialog;
private MyTracksProviderUtils providerUtils;
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
providerUtils = MyTracksProviderUtils.Factory.get(this);
}
@Override
public void onStart() {
super.onStart();
Intent intent = getIntent();
String action = intent.getAction();
if (!Intent.ACTION_VIEW.equals(action) &&
!Intent.ACTION_ATTACH_DATA.equals(action)) {
Log.e(TAG, "Received an intent with unsupported action: " + intent);
finish();
return;
}
if (!"file".equals(intent.getScheme())) {
Log.e(TAG, "Received a VIEW intent with unsupported scheme " + intent.getScheme());
finish();
return;
}
Log.i(TAG, "Received a VIEW intent with file scheme. Importing.");
startTrackImport(intent.getData().getPath());
}
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();
Thread t = new Thread() {
@Override
public void run() {
int message = R.string.success;
long[] trackIdsImported = null;
try {
try {
InputStream is = new FileInputStream(fileName);
trackIdsImported = GpxImporter.importGPXFile(is, providerUtils);
} catch (SAXException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
message = R.string.error_generic;
} catch (ParserConfigurationException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
message = R.string.error_generic;
} catch (IOException e) {
Log.e(TAG, "Caught an unexpected exception.", e);
message = 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;
} catch (OutOfMemoryError e) {
Log.e(TAG, "Caught an unexpected exception.", e);
message = R.string.error_out_of_memory;
}
boolean success = (trackIdsImported != null && trackIdsImported.length > 0);
onImportDone(success, message, trackIdsImported);
} finally {
runOnUiThread(new Runnable() {
public void run() {
progressDialog.dismiss();
}
});
}
}
};
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;
}
// 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);
startActivity(intent);
finish();
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
dialogBuilder.show();
}
});
}
}
@@ -15,8 +15,6 @@
*/
package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.io.sendtogoogle.SendDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.test.ActivityInstrumentationTestCase2;
@@ -45,14 +43,4 @@ public class DialogManagerTest
assertNotNull(d);
assertTrue(d instanceof ProgressDialog);
}
public void test_onCreateProgress() {
Dialog d =
getActivity().onCreateDialog(DialogManager.DIALOG_PROGRESS, null);
assertNotNull(d);
assertTrue(d instanceof ProgressDialog);
ProgressDialog pd = (ProgressDialog) d;
assertEquals(100, pd.getMax());
assertEquals(10, pd.getProgress());
}
}