Add "show on map" format selection dialog

This commit is contained in:
Peter Storch
2022-05-26 13:58:01 +02:00
parent 6c0033c107
commit 1108684d06
14 changed files with 165 additions and 123 deletions
@@ -5,31 +5,45 @@ import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.util.Pair;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.ContentProviderUtils;
import de.dennisguse.opentracks.data.ShareContentProvider;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.data.tables.MarkerColumns;
import de.dennisguse.opentracks.data.tables.TrackPointsColumns;
import de.dennisguse.opentracks.data.tables.TracksColumns;
import de.dennisguse.opentracks.io.file.TrackFileFormat;
import de.dennisguse.opentracks.settings.PreferencesUtils;
/**
* Create an {@link Intent} to request showing a Dashboard.
* Create an {@link Intent} to request showing tracks on a Map or a Dashboard.
* The receiving {@link android.app.Activity} gets temporary access to the {@link TracksColumns} and the {@link TrackPointsColumns} (incl. update).
*/
public class IntentDashboardUtils {
private static final String TAG = IntentDashboardUtils.class.getSimpleName();
private static final String ACTION_DASHBOARD = "Intent.OpenTracks-Dashboard";
private static final String ACTION_DASHBOARD_PAYLOAD = ACTION_DASHBOARD + ".Payload";
public static final TrackFileFormat[] SHOW_ON_MAP_TRACK_FILE_FORMATS = new TrackFileFormat[] {
TrackFileFormat.KMZ_WITH_TRACKDETAIL_AND_SENSORDATA_AND_PICTURES,
TrackFileFormat.KML_WITH_TRACKDETAIL_AND_SENSORDATA,
TrackFileFormat.GPX};
public static final String PREFERENCE_ID_DASHBOARD = ACTION_DASHBOARD;
public static final String PREFERENCE_ID_ASK = "ASK";
/**
* Assume "v1" if not present.
*/
@@ -49,24 +63,78 @@ public class IntentDashboardUtils {
private static final int TRACK_URI_INDEX = 0;
private static final int TRACKPOINTS_URI_INDEX = 1;
private static final int MARKERS_URI_INDEX = 2;
private static final int NONE_SELECTED = -1;
private IntentDashboardUtils() {
}
/**
* Send intent to show tracks on a map (needs an another app) as resource URIs.
* Shows an AlertDialog with different format option if none is defined as preference.
*
* @param context the context
* @param isRecording are we currently recording?
* @param trackIds the track ids
*/
public static void startDashboard(Context context, boolean isRecording, Track.Id... trackIds) {
startDashboard(context, isRecording, null, null, trackIds);
public static void showTrackOnMap(Context context, boolean isRecording, Track.Id... trackIds) {
Map<String, String> options = TrackFileFormat.toPreferenceIdLabelMap(context.getResources(), IntentDashboardUtils.SHOW_ON_MAP_TRACK_FILE_FORMATS);
options.put(IntentDashboardUtils.PREFERENCE_ID_DASHBOARD, context.getString(R.string.show_on_dashboard));
final String[] optionLabels = options.values().toArray(new String[0]);
final String[] optionValues = options.keySet().toArray(new String[0]);
final AtomicInteger checkedItem = new AtomicInteger(NONE_SELECTED);
String preferenceValue = PreferencesUtils.getShowOnMapFormat();
for (int i = 0; i < optionValues.length; i++) {
if (optionValues[i].equals(preferenceValue)) {
checkedItem.set(i);
}
}
if (checkedItem.get() == NONE_SELECTED) {
checkedItem.set(0); // set first option as default
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.select_show_on_map_behavior);
builder.setSingleChoiceItems(optionLabels, checkedItem.get(), (dialog, which) -> checkedItem.set(which));
builder.setPositiveButton(android.R.string.ok, (dialog, which) -> onFormatSelected(context, isRecording, optionValues[checkedItem.get()], trackIds, false));
builder.setNeutralButton(R.string.always, ((dialog, which) -> onFormatSelected(context, isRecording, optionValues[checkedItem.get()], trackIds, true)));
builder.setNegativeButton(android.R.string.cancel, null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
onFormatSelected(context, isRecording, preferenceValue, trackIds, true);
}
}
/**
* A format was selected, remember if <code>always</code> is true and start the necessary action
*
* @param context the context
* @param isRecording are we currently recording?
* @param selectedValue the chosen format
* @param trackIds the track ids
* @param always set the selectedValue as default preference
*/
private static void onFormatSelected(final Context context, final boolean isRecording, final String selectedValue, final Track.Id[] trackIds, final boolean always) {
if (always) {
PreferencesUtils.setShowOnMapFormat(selectedValue);
}
TrackFileFormat trackFileFormat = TrackFileFormat.valueOfPreferenceId(selectedValue);
if (trackFileFormat != null) {
showTrackOnMapWithFileFormat(context, trackFileFormat, Set.of(trackIds));
} else {
startDashboard(context, isRecording, null, null, trackIds);
}
}
/**
* Send intent to show tracks on a map (needs an another app) as resource URIs.
* By providing a targetPackage and targetClass an explicit intent can be sent,
* thus bypassing the need for the user to select an app.
*
* @param context the context
* @param isRecording are we currently recording?
* @param targetPackage the target package
* @param targetClass the target class
* @param trackIds the track ids
*/
public static void startDashboard(Context context, boolean isRecording, String targetPackage, String targetClass, Track.Id... trackIds) {
if (trackIds.length == 0) {
@@ -108,16 +176,27 @@ public class IntentDashboardUtils {
context.startActivity(intent);
}
public static Set<Track.Id> extractTrackIdsFromIntent(@NonNull Intent intent) {
final ArrayList<Uri> uris = intent.getParcelableArrayListExtra(ACTION_DASHBOARD_PAYLOAD);
final Uri tracksUri = uris.get(TRACK_URI_INDEX);
String[] trackIdsString = ContentProviderUtils.parseTrackIdsFromUri(tracksUri);
Set<Track.Id> trackIds = new HashSet<>(trackIdsString.length);
for (String s : trackIdsString) {
trackIds.add(new Track.Id(Long.parseLong(s)));
/**
* Send intent to show tracks on a map (needs an another app) to support specific trackFileFormat.
*
* @param context the context
* @param trackFileFormat the track file format
* @param trackIds the track ids
*/
private static void showTrackOnMapWithFileFormat(Context context, TrackFileFormat trackFileFormat, Set<Track.Id> trackIds) {
if (trackIds.isEmpty()) {
return;
}
return trackIds;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Pair<Uri, String> uriAndMime = ShareContentProvider.createURI(trackIds, "SharingTrack", trackFileFormat);
intent.setDataAndType(uriAndMime.first, uriAndMime.second);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.open_track_as_trackfileformat, trackFileFormat.getExtension())));
}
}