From ea943548eced6084950fd8d3d1b95c09a437b1a2 Mon Sep 17 00:00:00 2001 From: Jimmy Shih Date: Tue, 1 Oct 2013 13:24:57 -0700 Subject: [PATCH] Use a content provider url to play track in earth --- MyTracks/AndroidManifest.xml | 1 + .../mytracks/content/MyTracksProvider.java | 19 +++++++++++++ .../io/file/exporter/SaveActivity.java | 21 ++++++++------- .../io/file/exporter/SaveAsyncTask.java | 18 ++++++------- .../apps/mytracks/util/GoogleEarthUtils.java | 27 ++++++++++++++++--- 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/MyTracks/AndroidManifest.xml b/MyTracks/AndroidManifest.xml index f68c5a111..1fc353055 100755 --- a/MyTracks/AndroidManifest.xml +++ b/MyTracks/AndroidManifest.xml @@ -283,6 +283,7 @@ limitations under the License. diff --git a/MyTracks/src/com/google/android/apps/mytracks/content/MyTracksProvider.java b/MyTracks/src/com/google/android/apps/mytracks/content/MyTracksProvider.java index 0b4f877f7..84361c946 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/content/MyTracksProvider.java +++ b/MyTracks/src/com/google/android/apps/mytracks/content/MyTracksProvider.java @@ -34,10 +34,13 @@ import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteQueryBuilder; import android.net.Uri; import android.os.Binder; +import android.os.ParcelFileDescriptor; import android.os.Process; import android.text.TextUtils; import android.util.Log; +import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; /** @@ -422,6 +425,22 @@ public class MyTracksProvider extends ContentProvider { return count; } + @Override + public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { + try { + File file = new File(uri.getPath()); + if (file.exists()) { + return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); + } + throw new FileNotFoundException(uri.getPath()); + } finally { + File[] files = getContext().getCacheDir().listFiles(); + for (File file : files) { + file.delete(); + } + } + } + /** * Returns true if the caller can access the content provider. */ diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/exporter/SaveActivity.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/exporter/SaveActivity.java index beec34e8a..b07758a40 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/exporter/SaveActivity.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/exporter/SaveActivity.java @@ -81,26 +81,29 @@ public class SaveActivity extends Activity { return; } - directoryDisplayName = playTrack ? FileUtils.getDirectoryDisplayName( - trackFileFormat.getExtension(), FileUtils.TEMP_DIR) - : FileUtils.getDirectoryDisplayName(trackFileFormat.getExtension()); - - String directoryPath = playTrack ? FileUtils.getDirectoryPath( - trackFileFormat.getExtension(), FileUtils.TEMP_DIR) - : FileUtils.getDirectoryPath(trackFileFormat.getExtension()); - File directory = new File(directoryPath); + File directory = playTrack ? getCacheDir() + : new File(FileUtils.getDirectoryPath(trackFileFormat.getExtension())); if (!FileUtils.ensureDirectoryExists(directory)) { Toast.makeText(this, R.string.external_storage_not_writable, Toast.LENGTH_LONG).show(); finish(); return; } + if (playTrack) { + for (File file : getCacheDir().listFiles()) { + file.delete(); + } + } + + directoryDisplayName = playTrack ? getCacheDir().getName() + : FileUtils.getDirectoryDisplayName(trackFileFormat.getExtension()); + Object retained = getLastNonConfigurationInstance(); if (retained instanceof SaveAsyncTask) { saveAsyncTask = (SaveAsyncTask) retained; saveAsyncTask.setActivity(this); } else { - saveAsyncTask = new SaveAsyncTask(this, trackFileFormat, trackIds, directory, playTrack); + saveAsyncTask = new SaveAsyncTask(this, trackIds, trackFileFormat, playTrack, directory); saveAsyncTask.execute(); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/io/file/exporter/SaveAsyncTask.java b/MyTracks/src/com/google/android/apps/mytracks/io/file/exporter/SaveAsyncTask.java index bd30d1168..c314cd293 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/io/file/exporter/SaveAsyncTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/io/file/exporter/SaveAsyncTask.java @@ -46,10 +46,10 @@ public class SaveAsyncTask extends AsyncTask { private static final String TAG = SaveAsyncTask.class.getSimpleName(); private SaveActivity saveActivity; - private final TrackFileFormat trackFileFormat; private final long[] trackIds; - private final File directory; + private final TrackFileFormat trackFileFormat; private final boolean playTrack; + private final File directory; private final Context context; private final MyTracksProviderUtils myTracksProviderUtils; @@ -72,19 +72,19 @@ public class SaveAsyncTask extends AsyncTask { * Creates an AsyncTask. * * @param saveActivity the activity currently associated with this task - * @param trackFileFormat the track file format * @param trackIds the track ids to save. To save all, set to size 1 with * trackIds[0] == -1L - * @param directory the directory to save to + * @param trackFileFormat the track file format * @param playTrack true to play track + * @param directory the directory to write the file */ - public SaveAsyncTask(SaveActivity saveActivity, TrackFileFormat trackFileFormat, long[] trackIds, - File directory, boolean playTrack) { + public SaveAsyncTask(SaveActivity saveActivity, long[] trackIds, TrackFileFormat trackFileFormat, + boolean playTrack, File directory) { this.saveActivity = saveActivity; - this.trackFileFormat = trackFileFormat; this.trackIds = trackIds; - this.directory = directory; + this.trackFileFormat = trackFileFormat; this.playTrack = playTrack; + this.directory = directory; context = saveActivity.getApplicationContext(); myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context); @@ -186,7 +186,7 @@ public class SaveAsyncTask extends AsyncTask { Track track = tracks[0]; boolean useKmz = !playTrack && trackFileFormat == TrackFileFormat.KML; String extension = useKmz ? KmzTrackExporter.KMZ_EXTENSION : trackFileFormat.getExtension(); - + // Make sure the file doesn't exist yet (possibly by changing the filename) String fileName = FileUtils.buildUniqueFileName(directory, track.getName(), extension); if (fileName == null) { diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/GoogleEarthUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/GoogleEarthUtils.java index d8af4215c..789c6887f 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/GoogleEarthUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/GoogleEarthUtils.java @@ -16,13 +16,18 @@ package com.google.android.apps.mytracks.util; +import com.google.android.apps.mytracks.content.MyTracksProviderUtils; + +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; +import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.net.Uri; +import android.util.Log; -import java.io.File; import java.util.List; /** @@ -32,14 +37,18 @@ import java.util.List; */ public class GoogleEarthUtils { + private static final String TAG = GoogleEarthUtils.class.getSimpleName(); + private static final String GOOGLE_EARTH_CLASS = "com.google.earth.EarthActivity"; private static final String GOOGLE_EARTH_KML_MIME_TYPE = "application/vnd.google-earth.kml+xml"; + private static final int GOOGLE_EARTH_MIN_VERSION_CODE = 13246120; private static final String GOOGLE_EARTH_PACKAGE = "com.google.earth"; private static final String GOOGLE_EARTH_TOUR_FEATURE_ID = "com.google.earth.EXTRA.tour_feature_id"; public static final String GOOGLE_EARTH_MARKET_URL = "market://details?id=" + GOOGLE_EARTH_PACKAGE; + public static final String TOUR_FEATURE_ID_VALUE = "tour"; private GoogleEarthUtils() {} @@ -55,7 +64,14 @@ public class GoogleEarthUtils { for (ResolveInfo info : infos) { if (info.activityInfo != null && info.activityInfo.packageName != null && info.activityInfo.packageName.equals(GOOGLE_EARTH_PACKAGE)) { - return true; + try { + PackageInfo packageInfo = context.getPackageManager() + .getPackageInfo(info.activityInfo.packageName, 0); + return packageInfo.versionCode >= GOOGLE_EARTH_MIN_VERSION_CODE; + } catch (NameNotFoundException e) { + Log.e(TAG, "Unable to get google earth package info", e); + return false; + } } } return false; @@ -67,9 +83,12 @@ public class GoogleEarthUtils { * @param kmlFilePath the kml file path */ public static Intent getPlayInEarthIntent(String kmlFilePath) { - return new Intent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK) + Uri uri = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT) + .authority(MyTracksProviderUtils.AUTHORITY).path(kmlFilePath).build(); + return new Intent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_GRANT_READ_URI_PERMISSION) .putExtra(GOOGLE_EARTH_TOUR_FEATURE_ID, TOUR_FEATURE_ID_VALUE) .setClassName(GOOGLE_EARTH_PACKAGE, GOOGLE_EARTH_CLASS) - .setDataAndType(Uri.fromFile(new File(kmlFilePath)), GOOGLE_EARTH_KML_MIME_TYPE); + .setDataAndType(uri, GOOGLE_EARTH_KML_MIME_TYPE); } }