Package structure: moved share functionality.

Part of #963.
This commit is contained in:
Dennis Guse
2022-01-15 22:28:47 +01:00
parent 88c4e1a109
commit 7caf33fb66
10 changed files with 152 additions and 125 deletions
@@ -24,24 +24,15 @@ import android.provider.MediaStore;
import android.util.Log;
import android.util.Pair;
import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import androidx.documentfile.provider.DocumentFile;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.content.DescriptionGenerator;
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;
/**
* Utilities for creating intents.
@@ -67,99 +58,6 @@ public class IntentUtils {
return new Intent(context, cls).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
}
/**
* 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));
}
public static void showCoordinateOnMap(Context context, Marker marker) {
showCoordinateOnMap(context, marker.getLatitude(), marker.getLongitude(), marker.getName());
}