mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-03 09:53:06 +02:00
Use correct mime type for KML and KMZ.
This commit is contained in:
@@ -10,6 +10,7 @@ import android.os.Bundle;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
@@ -28,15 +29,14 @@ import de.dennisguse.opentracks.io.file.exporter.TrackWriter;
|
||||
/**
|
||||
* A content provider that mimics the behavior of {@link androidx.core.content.FileProvider}, which shares virtual (non-existing) KML-files.
|
||||
* The actual content of the virtual files is generated by using the functionality defined in {@link CustomContentProvider}.
|
||||
*
|
||||
* <p>
|
||||
* Moreover, it manages access to OpenTrack's database via {@link CustomContentProvider}.
|
||||
*
|
||||
* <p>
|
||||
* Explanation:
|
||||
* Although a request is handled by a {@link android.content.ContentProvider} (with temporarily granted permission), Android's security infrastructure prevents forwarding queries to non-exported {@link android.content.ContentProvider}.
|
||||
* Thus, if {@link ShareContentProvider} and {@link CustomContentProvider} would be two different instances, the data would not be accessible to external apps.
|
||||
* While handling a request {@link ShareContentProvider} could `grantPermissions()` to the calling app for {@link CustomContentProvider}'s URI.
|
||||
* However, while handling the request this would allow the calling app to actually contact {@link CustomContentProvider} directly and get access to stored data that should remain private.
|
||||
*
|
||||
*/
|
||||
public class ShareContentProvider extends CustomContentProvider implements IContentResolver {
|
||||
|
||||
@@ -44,11 +44,14 @@ public class ShareContentProvider extends CustomContentProvider implements ICont
|
||||
|
||||
public static final String TAG = ShareContentProvider.class.getCanonicalName();
|
||||
|
||||
public static String MIME = "application/kml+xml";
|
||||
private static final int URI_KML = 1;
|
||||
private final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
||||
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
||||
|
||||
public static Uri createURI(long[] trackIds) {
|
||||
static {
|
||||
uriMatcher.addURI(ContentProviderUtils.AUTHORITY_PACKAGE, TracksColumns.TABLE_NAME + "/kml/*", URI_KML);
|
||||
}
|
||||
|
||||
public static Pair<Uri, String> createURI(long[] trackIds) {
|
||||
if (trackIds.length == 0) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@@ -59,13 +62,9 @@ public class ShareContentProvider extends CustomContentProvider implements ICont
|
||||
}
|
||||
builder.deleteCharAt(builder.lastIndexOf(","));
|
||||
|
||||
return Uri.parse(ContentProviderUtils.CONTENT_BASE_URI + "/" + TracksColumns.TABLE_NAME + "/kml/" + builder + ".kml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreate() {
|
||||
uriMatcher.addURI(ContentProviderUtils.AUTHORITY_PACKAGE, TracksColumns.TABLE_NAME + "/kml/*", URI_KML);
|
||||
return super.onCreate();
|
||||
Uri uri = Uri.parse(ContentProviderUtils.CONTENT_BASE_URI + "/" + TracksColumns.TABLE_NAME + "/kml/" + builder + ".kml");
|
||||
String mime = getTypeMime(uri);
|
||||
return new Pair<>(uri, mime);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,12 +131,24 @@ public class ShareContentProvider extends CustomContentProvider implements ICont
|
||||
return cursor;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getTypeMime(@NonNull Uri uri) {
|
||||
switch (uriMatcher.match(uri)) {
|
||||
case URI_KML:
|
||||
return TrackFileFormat.KML.getMimeType();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getType(@NonNull Uri uri) {
|
||||
if (uriMatcher.match(uri) == URI_KML) {
|
||||
return MIME;
|
||||
String mime = getTypeMime(uri);
|
||||
if (mime != null) {
|
||||
return mime;
|
||||
}
|
||||
|
||||
return super.getType(uri);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user