mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-10-03 18:03:06 +02:00
@@ -0,0 +1,124 @@
|
||||
package de.dennisguse.opentracks.share;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import de.dennisguse.opentracks.R;
|
||||
import de.dennisguse.opentracks.data.ContentProviderUtils;
|
||||
import de.dennisguse.opentracks.data.ShareContentProvider;
|
||||
import de.dennisguse.opentracks.data.models.Marker;
|
||||
import de.dennisguse.opentracks.data.models.Track;
|
||||
import de.dennisguse.opentracks.io.file.TrackFileFormat;
|
||||
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
||||
import de.dennisguse.opentracks.util.FileUtils;
|
||||
|
||||
public class ShareUtils {
|
||||
|
||||
private static final String TAG = ShareUtils.class.getSimpleName();
|
||||
|
||||
private ShareUtils() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates an intent to share a track file with an app.
|
||||
*
|
||||
* @param context the context
|
||||
* @param trackIds the track ids
|
||||
*/
|
||||
public static Intent newShareFileIntent(Context context, Track.Id... trackIds) {
|
||||
if (trackIds.length == 0) {
|
||||
throw new RuntimeException("Need to share at least one track.");
|
||||
}
|
||||
|
||||
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
|
||||
|
||||
String trackDescription = "";
|
||||
if (trackIds.length == 1) {
|
||||
Track track = contentProviderUtils.getTrack(trackIds[0]);
|
||||
trackDescription = track == null ? "" : new DescriptionGenerator(context).generateTrackDescription(track, false);
|
||||
}
|
||||
|
||||
ArrayList<Uri> uris = new ArrayList<>();
|
||||
for (Track.Id trackId : trackIds) {
|
||||
Track track = contentProviderUtils.getTrack(trackId);
|
||||
if (track == null) {
|
||||
Log.e(TAG, "TrackId " + trackId.getId() + " could not be resolved.");
|
||||
continue;
|
||||
}
|
||||
|
||||
String trackName = FileUtils.sanitizeFileName(track.getName());
|
||||
Pair<Uri, String> uriTrackFile = ShareContentProvider.createURI(trackId, trackName, PreferencesUtils.getExportTrackFileFormat());
|
||||
Pair<Uri, String> uriSharePicture = ShareContentProvider.createURI(trackId, trackName, TrackFileFormat.SHARE_PICTURE_PNG);
|
||||
|
||||
uris.addAll(Arrays.asList(uriSharePicture.first, uriTrackFile.first));
|
||||
}
|
||||
|
||||
return new Intent(Intent.ACTION_SEND_MULTIPLE)
|
||||
.setType("image/*")
|
||||
.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
|
||||
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.share_track_subject))
|
||||
.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.share_track_share_file_body, trackDescription));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an intent to share a track file with an app.
|
||||
*
|
||||
* @param context the context
|
||||
* @param markerIds the marker ids
|
||||
* @return an Intent or null (if nothing can be shared).
|
||||
*/
|
||||
@Nullable
|
||||
public static Intent newShareFileIntent(Context context, Marker.Id... markerIds) {
|
||||
if (markerIds.length == 0) {
|
||||
throw new RuntimeException("Need to share at least one marker.");
|
||||
}
|
||||
|
||||
String mime = null;
|
||||
|
||||
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context);
|
||||
ArrayList<Uri> uris = new ArrayList<>();
|
||||
for (Marker.Id markerId : markerIds) {
|
||||
Marker marker = contentProviderUtils.getMarker(markerId);
|
||||
if (marker == null) {
|
||||
Log.e(TAG, "MarkerId " + markerId.getId() + " could not be resolved.");
|
||||
continue;
|
||||
}
|
||||
if (marker.getPhotoURI() == null) {
|
||||
Log.e(TAG, "MarkerId " + markerId.getId() + " has no picture.");
|
||||
continue;
|
||||
}
|
||||
|
||||
mime = context.getContentResolver().getType(marker.getPhotoURI());
|
||||
|
||||
uris.add(marker.getPhotoURI());
|
||||
}
|
||||
|
||||
if (uris.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Because the #166 bug, when you import KMZ tracks then it creates file:/// from markers with photo.
|
||||
* The photos should be content:/// not file:/// because getType(uri) always returns null for file:///
|
||||
* In .setType, to avoid side effects because the #166 bug described above it checks if mime is null.
|
||||
* If it is then it hardcode "images/*".
|
||||
*/
|
||||
return new Intent(Intent.ACTION_SEND_MULTIPLE)
|
||||
.setType(mime != null ? mime : "image/*")
|
||||
.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
|
||||
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.share_image_subject))
|
||||
.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.share_image_body));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user