forked from upstream-mirrors/OpenTracks
Use a content provider url to play track in earth
This commit is contained in:
@@ -283,6 +283,7 @@ limitations under the License.
|
||||
<provider
|
||||
android:authorities="com.google.android.maps.mytracks"
|
||||
android:exported="true"
|
||||
android:grantUriPermissions="true"
|
||||
android:name="com.google.android.apps.mytracks.content.MyTracksProvider"
|
||||
android:readPermission="com.google.android.apps.mytracks.READ_TRACK_DATA"
|
||||
android:writePermission="com.google.android.apps.mytracks.WRITE_TRACK_DATA" />
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,10 +46,10 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
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<Void, Integer, Boolean> {
|
||||
* 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<Void, Integer, Boolean> {
|
||||
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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user