diff --git a/MyTracks/AndroidManifest.xml b/MyTracks/AndroidManifest.xml index de2d788da..2e1727b66 100755 --- a/MyTracks/AndroidManifest.xml +++ b/MyTracks/AndroidManifest.xml @@ -160,6 +160,26 @@ limitations under the License. android:value="com.google.android.apps.mytracks.SearchActivity" /> + + + + + + + + + + + + + + + + @@ -264,25 +284,6 @@ limitations under the License. android:label="@string/welcome_title" android:name="com.google.android.apps.mytracks.WelcomeActivity" /> - - - - - - - - - - - - - - - gpxFiles = getGpxFiles(); - gpxFileCount = gpxFiles.size(); - if (gpxFileCount == 0) { - return; - } - - Log.i(Constants.TAG, "ImportAllTracks: Importing: " + gpxFileCount + " tracks."); - activity.runOnUiThread(new Runnable() { - public void run() { - makeProgressDialog(gpxFileCount); - } - }); - - Iterator gpxFilesIterator = gpxFiles.iterator(); - for (int currentFileNumber = 0; gpxFilesIterator.hasNext(); currentFileNumber++) { - File currentFile = gpxFilesIterator.next(); - final int status = currentFileNumber; - activity.runOnUiThread(new Runnable() { - public void run() { - synchronized (this) { - if (progress == null) { - return; - } - progress.setProgress(status); - } - } - }); - if (importFile(currentFile, providerUtils)) { - importSuccessCount++; - } - } - - if (progress != null) { - synchronized (this) { - progress.dismiss(); - progress = null; - } - } - } - - /** - * Attempts to import a GPX file. Returns true on success, issues error - * notifications and returns false on failure. - */ - private boolean importFile(final File gpxFile, MyTracksProviderUtils providerUtils) { - Log.i(Constants.TAG, "ImportAllTracks: importing: " + gpxFile.getName()); - try { - importedTrackIds = GpxImporter.importGPXFile(new FileInputStream(gpxFile), providerUtils); - return true; - } catch (FileNotFoundException e) { - Log.w(Constants.TAG, "GPX file wasn't found/went missing: " - + gpxFile.getAbsolutePath(), e); - } catch (ParserConfigurationException e) { - Log.w(Constants.TAG, "Error parsing file: " + gpxFile.getAbsolutePath(), e); - } catch (SAXException e) { - Log.w(Constants.TAG, "Error parsing file: " + gpxFile.getAbsolutePath(), e); - } catch (IOException e) { - Log.w(Constants.TAG, "Error reading file: " + gpxFile.getAbsolutePath(), e); - } - activity.runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(activity, activity.getString(R.string.import_error, gpxFile.getName()), - Toast.LENGTH_LONG).show(); - } - }); - return false; - } - - /** - * Gets a list of the GPX files. If singleTrackSelected is true, returns a - * list containing just the gpxPath file. If singleTrackSelected is false, - * returns a list of GPX files under the gpxPath directory. - */ - private List getGpxFiles() { - List gpxFiles = new LinkedList(); - File file = new File(gpxPath); - if (singleTrackSelected) { - 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); - } - } - } - } - return gpxFiles; - } -} \ No newline at end of file diff --git a/MyTracks/src/com/google/android/apps/mytracks/ImportAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/ImportAsyncTask.java new file mode 100644 index 000000000..4e8898936 --- /dev/null +++ b/MyTracks/src/com/google/android/apps/mytracks/ImportAsyncTask.java @@ -0,0 +1,221 @@ +/* + * Copyright 2012 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; + +import com.google.android.apps.mytracks.content.MyTracksProviderUtils; +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; +import com.google.android.maps.mytracks.R; + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.AsyncTask; +import android.os.PowerManager.WakeLock; +import android.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.ParserConfigurationException; + +import org.xml.sax.SAXException; + +/** + * AsyncTask to import GPX files from the SD card. + * + * @author Jimmy Shih + */ +public class ImportAsyncTask extends AsyncTask { + + private static final String TAG = ImportAsyncTask.class.getSimpleName(); + + private ImportActivity importActivity; + private final boolean importAll; + private final String path; + private final MyTracksProviderUtils myTracksProviderUtils; + private WakeLock wakeLock; + + // true if the AsyncTask result is success + private boolean success; + + // true if the AsyncTask has completed + private boolean completed; + + // number of files successfully imported + private int successCount; + + // number of files to import + private int totalCount; + + // the last successfully imported track id + private long trackId; + + /** + * Creates an AsyncTask. + * + * @param importActivity the activity currently associated with this AsyncTask + * @param importAll true to import all GPX files + * @param path path to import GPX files + */ + public ImportAsyncTask(ImportActivity importActivity, boolean importAll, String path) { + this.importActivity = importActivity; + this.importAll = importAll; + this.path = path; + + myTracksProviderUtils = MyTracksProviderUtils.Factory.get(importActivity); + SharedPreferences sharedPreferences = importActivity.getSharedPreferences( + Constants.SETTINGS_NAME, Context.MODE_PRIVATE); + long recordingTrackId = sharedPreferences.getLong( + importActivity.getString(R.string.recording_track_key), -1L); + + // Get the wake lock if not recording + if (recordingTrackId == -1L) { + wakeLock = SystemUtils.acquireWakeLock(importActivity, wakeLock); + } + + success = false; + completed = false; + successCount = 0; + totalCount = 0; + trackId = -1L; + } + + /** + * Sets the current {@link ImportActivity} associated with this AyncTask. + * + * @param importActivity the current {@link ImportActivity}, can be null + */ + public void setActivity(ImportActivity importActivity) { + this.importActivity = importActivity; + if (completed && importActivity != null) { + importActivity.onAsyncTaskCompleted(success, successCount, totalCount, trackId); + } + } + + @Override + protected void onPreExecute() { + if (importActivity != null) { + importActivity.showProgressDialog(); + } + } + + @Override + protected Boolean doInBackground(Void... params) { + try { + FileUtils fileUtils = new FileUtils(); + if (!fileUtils.isSdCardAvailable()) { + return false; + } + + List files = getFiles(); + totalCount = files.size(); + if (totalCount == 0) { + return true; + } + + for (int i = 0; i < totalCount; i++) { + if (isCancelled()) { + // If cancelled, return true to show the number of files imported + return true; + } + File file = files.get(i); + if (importFile(file)) { + successCount++; + } + publishProgress(i + 1, totalCount); + } + return true; + } finally { + // Release the wake lock if obtained + if (wakeLock != null && wakeLock.isHeld()) { + wakeLock.release(); + } + } + } + + @Override + protected void onProgressUpdate(Integer... values) { + if (importActivity != null) { + importActivity.setProgressDialogValue(values[0], values[1]); + } + } + + @Override + protected void onPostExecute(Boolean result) { + success = result; + completed = true; + if (importActivity != null) { + importActivity.onAsyncTaskCompleted(success, successCount, totalCount, trackId); + } + } + + /** + * Imports a GPX file. + * + * @param file the file + */ + private boolean importFile(final File file) { + try { + long trackIds[] = GpxImporter.importGPXFile(new FileInputStream(file), myTracksProviderUtils); + int length = trackIds.length; + if (length > 0) { + trackId = trackIds[length - 1]; + } + return true; + } catch (FileNotFoundException e) { + Log.d(TAG, "file: " + file.getAbsolutePath(), e); + return false; + } catch (ParserConfigurationException e) { + Log.d(TAG, "file: " + file.getAbsolutePath(), e); + return false; + } catch (SAXException e) { + Log.d(TAG, "file: " + file.getAbsolutePath(), e); + return false; + } catch (IOException e) { + Log.d(TAG, "file: " + file.getAbsolutePath(), e); + return false; + } + } + + /** + * Gets a list of GPX files. If importAll is true, returns a list of GPX files + * under the path directory. If importAll is false, returns a list containing + * just the path file. + */ + private List getFiles() { + List files = new ArrayList(); + File file = new File(path); + if (importAll) { + File[] candidates = file.listFiles(); + if (candidates != null) { + for (File candidate : candidates) { + if (!candidate.isDirectory() && candidate.getName().endsWith(".gpx")) { + files.add(candidate); + } + } + } + } else { + files.add(file); + } + return files; + } +} diff --git a/MyTracks/src/com/google/android/apps/mytracks/TrackList.java b/MyTracks/src/com/google/android/apps/mytracks/TrackList.java index a737be462..c71b03aab 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/TrackList.java +++ b/MyTracks/src/com/google/android/apps/mytracks/TrackList.java @@ -254,18 +254,17 @@ public class TrackList extends ListActivity @Override public void onClick(View v) { switch (v.getId()) { - case R.id.tracklist_btn_delete_all: { + case R.id.tracklist_btn_delete_all: showDialog(DIALOG_DELETE_ALL_ID); break; - } - case R.id.tracklist_btn_export_all: { + case R.id.tracklist_btn_export_all: showDialog(DIALOG_EXPORT_ALL_ID); break; - } - case R.id.tracklist_btn_import_all: { - new ImportAllTracks(this); - break; - } + case R.id.tracklist_btn_import_all: + Intent intent = new Intent(this, ImportActivity.class) + .putExtra(ImportActivity.EXTRA_IMPORT_ALL, true); + startActivity(intent); + break; } } 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 deleted file mode 100644 index 027e35002..000000000 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/ImportActivity.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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.ImportAllTracks; -import com.google.android.apps.mytracks.util.UriUtils; - -import android.app.Activity; -import android.content.Intent; -import android.net.Uri; -import android.util.Log; - -/** - * An activity that imports a track from a file and displays the track in My Tracks. - * - * @author Rodrigo Damazio - */ -public class ImportActivity extends Activity { - - @Override - public void onStart() { - super.onStart(); - - Intent intent = getIntent(); - String action = intent.getAction(); - Uri data = intent.getData(); - 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 (!UriUtils.isFileUri(data)) { - Log.e(TAG, "Received an intent with unsupported data: " + intent); - finish(); - return; - } - - String path = data.getPath(); - Log.i(TAG, "Importing GPX file at " + path); - new ImportAllTracks(this, path); - } -}