Catch ActivityNotFound exception for starting geo-data intents.

This commit is contained in:
Dennis Guse
2019-08-25 22:20:03 +02:00
parent 8685e43dc2
commit 5788f99b20
5 changed files with 21 additions and 16 deletions
@@ -42,7 +42,6 @@ import androidx.loader.content.Loader;
import de.dennisguse.opentracks.content.ContentProviderUtils;
import de.dennisguse.opentracks.content.Track;
import de.dennisguse.opentracks.content.Waypoint;
import de.dennisguse.opentracks.content.Waypoint.WaypointType;
import de.dennisguse.opentracks.content.WaypointsColumns;
import de.dennisguse.opentracks.fragments.DeleteMarkerDialogFragment;
@@ -284,9 +283,7 @@ public class MarkerListActivity extends AbstractActivity implements DeleteMarker
switch (itemId) {
case R.id.list_context_menu_show_on_map:
if (markerIds.length == 1) {
Waypoint marker = contentProviderUtils.getWaypoint(markerIds[0]);
intent = IntentUtils.newShowCoordinateOnMapIntent(marker);
startActivity(intent);
IntentUtils.showCoordinateOnMap(this, contentProviderUtils.getWaypoint(markerIds[0]));
}
return true;
case R.id.list_context_menu_edit:
@@ -316,8 +316,7 @@ public class SearchListActivity extends AbstractListActivity implements DeleteMa
Log.e(TAG, "Not implemented");
return true;
case R.id.list_context_menu_show_on_map:
intent = IntentUtils.newShowCoordinateOnMapIntent((double) item.get(MARKER_LATITUDE_FIELD), (double) item.get(MARKER_LONGITUDE_FIELD), item.get(NAME_FIELD) + "");
startActivity(intent);
IntentUtils.showCoordinateOnMap(this, (double) item.get(MARKER_LATITUDE_FIELD), (double) item.get(MARKER_LONGITUDE_FIELD), item.get(NAME_FIELD) + "");
return true;
case R.id.list_context_menu_edit:
if (markerId != null) {
@@ -190,8 +190,7 @@ public class MarkerDetailFragment extends Fragment {
Intent intent;
switch (item.getItemId()) {
case R.id.marker_detail_show_on_map:
intent = IntentUtils.newShowCoordinateOnMapIntent(waypoint);
startActivity(intent);
IntentUtils.showCoordinateOnMap(getContext(), waypoint);
return true;
case R.id.marker_detail_edit:
intent = IntentUtils.newIntent(fragmentActivity, MarkerEditActivity.class)
@@ -64,10 +64,6 @@ public class IntentUtils {
private static final String JPEG_EXTENSION = "jpeg";
public static Intent newShowCoordinateOnMapIntent(Waypoint waypoint) {
return newShowCoordinateOnMapIntent(waypoint.getLocation().getLatitude(), waypoint.getLocation().getLongitude(), waypoint.getName());
}
/**
* Creates an intent to share a track file with an app.
*
@@ -90,7 +86,11 @@ public class IntentUtils {
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
public static Intent newShowCoordinateOnMapIntent(double latitude, double longitude, String label) {
public static void showCoordinateOnMap(Context context, Waypoint waypoint) {
showCoordinateOnMap(context, waypoint.getLocation().getLatitude(), waypoint.getLocation().getLongitude(), waypoint.getName());
}
public static void showCoordinateOnMap(Context context, double latitude, double longitude, String label) {
//SEE https://developer.android.com/guide/components/intents-common.html#Maps
String uri = "geo:0,0?q=" + latitude + "," + longitude;
if (label != null && label.length() > 0) {
@@ -98,7 +98,12 @@ public class IntentUtils {
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(uri));
return intent;
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, context.getString(R.string.app_not_installed_show_on_map), Toast.LENGTH_SHORT).show();
}
}
/**
@@ -120,7 +125,7 @@ public class IntentUtils {
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No app installed that can show the tracks on a map.", Toast.LENGTH_SHORT).show();
Toast.makeText(context, context.getString(R.string.app_not_installed_show_on_map), Toast.LENGTH_SHORT).show();
}
}
+6 -1
View File
@@ -304,7 +304,11 @@ limitations under the License.
wind surfing
</string>
<!-- Description -->
<string name="app_not_installed_show_on_map" translation_description="When 'show on map' is used and no app with this functionality is installed.">
Please install an app that supports presenting map data.
</string>
<!-- Description -->
<string name="description_activity_type" translation_description="Activity type description.
%1$s activity type. E.g., 'hiking'.">
@@ -985,6 +989,7 @@ limitations under the License.
what will be searched for.">
Speak the name of a track or marker
</string>
<!-- Sensor State -->
<string name="sensor_state_battery" translation_description="In the 'Sensor state' page, the label for the battery level on the bike
sensor. E.g., 'Battery level: 50 %'.">