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
|
<provider
|
||||||
android:authorities="com.google.android.maps.mytracks"
|
android:authorities="com.google.android.maps.mytracks"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
|
android:grantUriPermissions="true"
|
||||||
android:name="com.google.android.apps.mytracks.content.MyTracksProvider"
|
android:name="com.google.android.apps.mytracks.content.MyTracksProvider"
|
||||||
android:readPermission="com.google.android.apps.mytracks.READ_TRACK_DATA"
|
android:readPermission="com.google.android.apps.mytracks.READ_TRACK_DATA"
|
||||||
android:writePermission="com.google.android.apps.mytracks.WRITE_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.database.sqlite.SQLiteQueryBuilder;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
|
import android.os.ParcelFileDescriptor;
|
||||||
import android.os.Process;
|
import android.os.Process;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -422,6 +425,22 @@ public class MyTracksProvider extends ContentProvider {
|
|||||||
return count;
|
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.
|
* Returns true if the caller can access the content provider.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -81,26 +81,29 @@ public class SaveActivity extends Activity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
directoryDisplayName = playTrack ? FileUtils.getDirectoryDisplayName(
|
File directory = playTrack ? getCacheDir()
|
||||||
trackFileFormat.getExtension(), FileUtils.TEMP_DIR)
|
: new File(FileUtils.getDirectoryPath(trackFileFormat.getExtension()));
|
||||||
: FileUtils.getDirectoryDisplayName(trackFileFormat.getExtension());
|
|
||||||
|
|
||||||
String directoryPath = playTrack ? FileUtils.getDirectoryPath(
|
|
||||||
trackFileFormat.getExtension(), FileUtils.TEMP_DIR)
|
|
||||||
: FileUtils.getDirectoryPath(trackFileFormat.getExtension());
|
|
||||||
File directory = new File(directoryPath);
|
|
||||||
if (!FileUtils.ensureDirectoryExists(directory)) {
|
if (!FileUtils.ensureDirectoryExists(directory)) {
|
||||||
Toast.makeText(this, R.string.external_storage_not_writable, Toast.LENGTH_LONG).show();
|
Toast.makeText(this, R.string.external_storage_not_writable, Toast.LENGTH_LONG).show();
|
||||||
finish();
|
finish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (playTrack) {
|
||||||
|
for (File file : getCacheDir().listFiles()) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
directoryDisplayName = playTrack ? getCacheDir().getName()
|
||||||
|
: FileUtils.getDirectoryDisplayName(trackFileFormat.getExtension());
|
||||||
|
|
||||||
Object retained = getLastNonConfigurationInstance();
|
Object retained = getLastNonConfigurationInstance();
|
||||||
if (retained instanceof SaveAsyncTask) {
|
if (retained instanceof SaveAsyncTask) {
|
||||||
saveAsyncTask = (SaveAsyncTask) retained;
|
saveAsyncTask = (SaveAsyncTask) retained;
|
||||||
saveAsyncTask.setActivity(this);
|
saveAsyncTask.setActivity(this);
|
||||||
} else {
|
} else {
|
||||||
saveAsyncTask = new SaveAsyncTask(this, trackFileFormat, trackIds, directory, playTrack);
|
saveAsyncTask = new SaveAsyncTask(this, trackIds, trackFileFormat, playTrack, directory);
|
||||||
saveAsyncTask.execute();
|
saveAsyncTask.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,10 +46,10 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
private static final String TAG = SaveAsyncTask.class.getSimpleName();
|
private static final String TAG = SaveAsyncTask.class.getSimpleName();
|
||||||
|
|
||||||
private SaveActivity saveActivity;
|
private SaveActivity saveActivity;
|
||||||
private final TrackFileFormat trackFileFormat;
|
|
||||||
private final long[] trackIds;
|
private final long[] trackIds;
|
||||||
private final File directory;
|
private final TrackFileFormat trackFileFormat;
|
||||||
private final boolean playTrack;
|
private final boolean playTrack;
|
||||||
|
private final File directory;
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final MyTracksProviderUtils myTracksProviderUtils;
|
private final MyTracksProviderUtils myTracksProviderUtils;
|
||||||
|
|
||||||
@@ -72,19 +72,19 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
|||||||
* Creates an AsyncTask.
|
* Creates an AsyncTask.
|
||||||
*
|
*
|
||||||
* @param saveActivity the activity currently associated with this task
|
* @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
|
* @param trackIds the track ids to save. To save all, set to size 1 with
|
||||||
* trackIds[0] == -1L
|
* trackIds[0] == -1L
|
||||||
* @param directory the directory to save to
|
* @param trackFileFormat the track file format
|
||||||
* @param playTrack true to play track
|
* @param playTrack true to play track
|
||||||
|
* @param directory the directory to write the file
|
||||||
*/
|
*/
|
||||||
public SaveAsyncTask(SaveActivity saveActivity, TrackFileFormat trackFileFormat, long[] trackIds,
|
public SaveAsyncTask(SaveActivity saveActivity, long[] trackIds, TrackFileFormat trackFileFormat,
|
||||||
File directory, boolean playTrack) {
|
boolean playTrack, File directory) {
|
||||||
this.saveActivity = saveActivity;
|
this.saveActivity = saveActivity;
|
||||||
this.trackFileFormat = trackFileFormat;
|
|
||||||
this.trackIds = trackIds;
|
this.trackIds = trackIds;
|
||||||
this.directory = directory;
|
this.trackFileFormat = trackFileFormat;
|
||||||
this.playTrack = playTrack;
|
this.playTrack = playTrack;
|
||||||
|
this.directory = directory;
|
||||||
context = saveActivity.getApplicationContext();
|
context = saveActivity.getApplicationContext();
|
||||||
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(context);
|
||||||
|
|
||||||
|
|||||||
@@ -16,13 +16,18 @@
|
|||||||
|
|
||||||
package com.google.android.apps.mytracks.util;
|
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.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,14 +37,18 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class GoogleEarthUtils {
|
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_CLASS = "com.google.earth.EarthActivity";
|
||||||
private static final String GOOGLE_EARTH_KML_MIME_TYPE = "application/vnd.google-earth.kml+xml";
|
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_PACKAGE = "com.google.earth";
|
||||||
private static final String
|
private static final String
|
||||||
GOOGLE_EARTH_TOUR_FEATURE_ID = "com.google.earth.EXTRA.tour_feature_id";
|
GOOGLE_EARTH_TOUR_FEATURE_ID = "com.google.earth.EXTRA.tour_feature_id";
|
||||||
|
|
||||||
public static final String GOOGLE_EARTH_MARKET_URL = "market://details?id="
|
public static final String GOOGLE_EARTH_MARKET_URL = "market://details?id="
|
||||||
+ GOOGLE_EARTH_PACKAGE;
|
+ GOOGLE_EARTH_PACKAGE;
|
||||||
|
|
||||||
public static final String TOUR_FEATURE_ID_VALUE = "tour";
|
public static final String TOUR_FEATURE_ID_VALUE = "tour";
|
||||||
|
|
||||||
private GoogleEarthUtils() {}
|
private GoogleEarthUtils() {}
|
||||||
@@ -55,7 +64,14 @@ public class GoogleEarthUtils {
|
|||||||
for (ResolveInfo info : infos) {
|
for (ResolveInfo info : infos) {
|
||||||
if (info.activityInfo != null && info.activityInfo.packageName != null
|
if (info.activityInfo != null && info.activityInfo.packageName != null
|
||||||
&& info.activityInfo.packageName.equals(GOOGLE_EARTH_PACKAGE)) {
|
&& 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;
|
return false;
|
||||||
@@ -67,9 +83,12 @@ public class GoogleEarthUtils {
|
|||||||
* @param kmlFilePath the kml file path
|
* @param kmlFilePath the kml file path
|
||||||
*/
|
*/
|
||||||
public static Intent getPlayInEarthIntent(String kmlFilePath) {
|
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)
|
.putExtra(GOOGLE_EARTH_TOUR_FEATURE_ID, TOUR_FEATURE_ID_VALUE)
|
||||||
.setClassName(GOOGLE_EARTH_PACKAGE, GOOGLE_EARTH_CLASS)
|
.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