From eb02c4c469550a54b98a9cc30b3a9c8549a821ba Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sat, 19 Dec 2020 18:01:08 +0100 Subject: [PATCH] Marker images can now be shared from the list directly via the context menu (like tracks). Fixes #495. --- .../opentracks/MarkerListActivity.java | 16 +++++-- .../fragments/MarkerDetailFragment.java | 2 +- .../opentracks/util/IntentUtils.java | 48 ++++++++++++++----- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/MarkerListActivity.java b/src/main/java/de/dennisguse/opentracks/MarkerListActivity.java index 6a0fe0ee6..b9f2d1f7c 100644 --- a/src/main/java/de/dennisguse/opentracks/MarkerListActivity.java +++ b/src/main/java/de/dennisguse/opentracks/MarkerListActivity.java @@ -93,14 +93,11 @@ public class MarkerListActivity extends AbstractActivity implements DeleteMarker public void onPrepare(Menu menu, int[] positions, long[] ids, boolean showSelectAll) { boolean isSingleSelection = ids.length == 1; - menu.findItem(R.id.list_context_menu_share).setVisible(false); menu.findItem(R.id.list_context_menu_show_on_map).setVisible(isSingleSelection); menu.findItem(R.id.list_context_menu_edit).setVisible(isSingleSelection); menu.findItem(R.id.list_context_menu_delete).setVisible(true); - /* - * Set select all to the same visibility as delete since delete is the - * only action that can be applied to multiple markers. - */ + + // Set select all to the same visibility as delete since delete is the only action that can be applied to multiple markers. menu.findItem(R.id.list_context_menu_select_all).setVisible(showSelectAll); } @@ -229,6 +226,15 @@ public class MarkerListActivity extends AbstractActivity implements DeleteMarker return true; } + if (itemId == R.id.list_context_menu_share) { + Intent intent = IntentUtils.newShareFileIntent(this, markerIds); + if (intent != null) { + intent = Intent.createChooser(intent, null); + startActivity(intent); + } + return true; + } + if (itemId == R.id.list_context_menu_edit) { if (markerIds.length == 1) { Intent intent = IntentUtils.newIntent(this, MarkerEditActivity.class) diff --git a/src/main/java/de/dennisguse/opentracks/fragments/MarkerDetailFragment.java b/src/main/java/de/dennisguse/opentracks/fragments/MarkerDetailFragment.java index 26a8389af..7d592213f 100644 --- a/src/main/java/de/dennisguse/opentracks/fragments/MarkerDetailFragment.java +++ b/src/main/java/de/dennisguse/opentracks/fragments/MarkerDetailFragment.java @@ -207,7 +207,7 @@ public class MarkerDetailFragment extends Fragment { if (item.getItemId() == R.id.marker_detail_share) { if (marker.hasPhoto()) { - Intent intent = IntentUtils.newShareImageIntent(getContext(), marker.getPhotoURI()); + Intent intent = IntentUtils.newShareFileIntent(getContext(), marker.getId()); intent = Intent.createChooser(intent, null); startActivity(intent); } diff --git a/src/main/java/de/dennisguse/opentracks/util/IntentUtils.java b/src/main/java/de/dennisguse/opentracks/util/IntentUtils.java index 883f561f6..2472b5e10 100644 --- a/src/main/java/de/dennisguse/opentracks/util/IntentUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/IntentUtils.java @@ -23,6 +23,7 @@ import android.provider.MediaStore; import android.util.Log; import android.util.Pair; +import androidx.annotation.Nullable; import androidx.core.content.FileProvider; import java.io.File; @@ -80,7 +81,6 @@ public class IntentUtils { trackDescription = track == null ? "" : new DescriptionGenerator(context).generateTrackDescription(track, false); } - String action = trackIds.length == 1 ? Intent.ACTION_SEND : Intent.ACTION_SEND_MULTIPLE; String mime = ""; ArrayList uris = new ArrayList<>(); @@ -95,9 +95,8 @@ public class IntentUtils { uris.add(uriAndMime.first); mime = uriAndMime.second; } - return new Intent(action) + return new Intent(Intent.ACTION_SEND_MULTIPLE) .setType(mime) - .setAction(Intent.ACTION_SEND_MULTIPLE) .putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris) .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) .putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.share_track_subject)) @@ -105,13 +104,41 @@ public class IntentUtils { } /** - * Creates an intent to share an image with an app. + * Creates an intent to share a track file with an app. * - * @param context the context. - * @param uri uri with the image to share. + * @param context the context + * @param markerIds the marker ids + * @return an Intent or null (if nothing can be shared). */ - public static Intent newShareImageIntent(Context context, Uri uri) { - String mime = context.getContentResolver().getType(uri); + @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 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.size() == 0) { + return null; + } /* * Because the #166 bug, when you import KMZ tracks then it creates file:/// from markers with photo. @@ -119,11 +146,10 @@ public class IntentUtils { * 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) + return new Intent(Intent.ACTION_SEND_MULTIPLE) .setType(mime != null ? mime : "image/*") - .setAction(Intent.ACTION_SEND) + .putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris) .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - .putExtra(Intent.EXTRA_STREAM, uri) .putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.share_image_subject)) .putExtra(Intent.EXTRA_TEXT, context.getString(R.string.share_image_body)); }