Search in sources :

Example 1 with FavouritesHelper

use of net.osmand.plus.myplaces.FavouritesHelper in project Osmand by osmandapp.

the class ExternalApiHelper method processApiRequest.

public Intent processApiRequest(Intent intent) {
    Intent result = new Intent();
    OsmandApplication app = (OsmandApplication) mapActivity.getApplication();
    try {
        Uri uri = intent.getData();
        String cmd = uri.getHost().toLowerCase();
        if (API_CMD_SHOW_GPX.equals(cmd) || API_CMD_NAVIGATE_GPX.equals(cmd)) {
            boolean navigate = API_CMD_NAVIGATE_GPX.equals(cmd);
            String path = uri.getQueryParameter(PARAM_PATH);
            GPXFile gpx = null;
            if (path != null) {
                File f = new File(path);
                if (f.exists()) {
                    gpx = GPXUtilities.loadGPXFile(f);
                }
            } else if (intent.getStringExtra(PARAM_DATA) != null) {
                String gpxStr = intent.getStringExtra(PARAM_DATA);
                if (!Algorithms.isEmpty(gpxStr)) {
                    gpx = GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxStr.getBytes()));
                }
            } else if (uri.getBooleanQueryParameter(PARAM_URI, false)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    LOG.debug("uriString=" + intent.getClipData().getItemAt(0).getUri());
                    Uri gpxUri = intent.getClipData().getItemAt(0).getUri();
                    ParcelFileDescriptor gpxParcelDescriptor = mapActivity.getContentResolver().openFileDescriptor(gpxUri, "r");
                    if (gpxParcelDescriptor != null) {
                        FileDescriptor fileDescriptor = gpxParcelDescriptor.getFileDescriptor();
                        gpx = GPXUtilities.loadGPXFile(new FileInputStream(fileDescriptor));
                    } else {
                        finish = true;
                        resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND;
                    }
                } else {
                    finish = true;
                    resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND;
                }
            } else {
                finish = true;
                resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND;
            }
            if (gpx != null) {
                if (navigate) {
                    boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false);
                    boolean locationPermission = uri.getBooleanQueryParameter(PARAM_LOCATION_PERMISSION, false);
                    saveAndNavigateGpx(mapActivity, gpx, force, locationPermission);
                } else {
                    app.getSelectedGpxHelper().setGpxFileToDisplay(gpx);
                }
                resultCode = Activity.RESULT_OK;
            } else {
                finish = true;
                resultCode = RESULT_CODE_ERROR_GPX_NOT_FOUND;
            }
        } else if (API_CMD_NAVIGATE.equals(cmd)) {
            String profileStr = uri.getQueryParameter(PARAM_PROFILE);
            final ApplicationMode profile = findNavigationProfile(app, profileStr);
            if (profile == null) {
                resultCode = RESULT_CODE_ERROR_INVALID_PROFILE;
            } else {
                String startName = uri.getQueryParameter(PARAM_START_NAME);
                if (Algorithms.isEmpty(startName)) {
                    startName = "";
                }
                String destName = uri.getQueryParameter(PARAM_DEST_NAME);
                if (Algorithms.isEmpty(destName)) {
                    destName = "";
                }
                final LatLon start;
                final PointDescription startDesc;
                String startLatStr = uri.getQueryParameter(PARAM_START_LAT);
                String startLonStr = uri.getQueryParameter(PARAM_START_LON);
                if (!Algorithms.isEmpty(startLatStr) && !Algorithms.isEmpty(startLonStr)) {
                    double lat = Double.parseDouble(startLatStr);
                    double lon = Double.parseDouble(startLonStr);
                    start = new LatLon(lat, lon);
                    startDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, startName);
                } else {
                    start = null;
                    startDesc = null;
                }
                String destLatStr = uri.getQueryParameter(PARAM_DEST_LAT);
                String destLonStr = uri.getQueryParameter(PARAM_DEST_LON);
                final LatLon dest;
                if (!Algorithms.isEmpty(destLatStr) && !Algorithms.isEmpty(destLonStr)) {
                    double destLat = Double.parseDouble(destLatStr);
                    double destLon = Double.parseDouble(destLonStr);
                    dest = new LatLon(destLat, destLon);
                } else {
                    dest = null;
                }
                final PointDescription destDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, destName);
                boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false);
                final boolean locationPermission = uri.getBooleanQueryParameter(PARAM_LOCATION_PERMISSION, false);
                final RoutingHelper routingHelper = app.getRoutingHelper();
                if (routingHelper.isFollowingMode() && !force) {
                    mapActivity.getMapActions().stopNavigationActionConfirm(new DialogInterface.OnDismissListener() {

                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            if (!routingHelper.isFollowingMode()) {
                                startNavigation(mapActivity, start, startDesc, dest, destDesc, profile, locationPermission);
                            }
                        }
                    });
                } else {
                    startNavigation(mapActivity, start, startDesc, dest, destDesc, profile, locationPermission);
                }
            }
        } else if (API_CMD_NAVIGATE_SEARCH.equals(cmd)) {
            String profileStr = uri.getQueryParameter(PARAM_PROFILE);
            final ApplicationMode profile = findNavigationProfile(app, profileStr);
            final boolean showSearchResults = uri.getBooleanQueryParameter(PARAM_SHOW_SEARCH_RESULTS, false);
            final String searchQuery = uri.getQueryParameter(PARAM_DEST_SEARCH_QUERY);
            if (Algorithms.isEmpty(searchQuery)) {
                resultCode = RESULT_CODE_ERROR_EMPTY_SEARCH_QUERY;
            } else if (profile == null) {
                resultCode = RESULT_CODE_ERROR_INVALID_PROFILE;
            } else {
                String startName = uri.getQueryParameter(PARAM_START_NAME);
                if (Algorithms.isEmpty(startName)) {
                    startName = "";
                }
                final LatLon start;
                final PointDescription startDesc;
                String startLatStr = uri.getQueryParameter(PARAM_START_LAT);
                String startLonStr = uri.getQueryParameter(PARAM_START_LON);
                if (!Algorithms.isEmpty(startLatStr) && !Algorithms.isEmpty(startLonStr)) {
                    double lat = Double.parseDouble(startLatStr);
                    double lon = Double.parseDouble(startLonStr);
                    start = new LatLon(lat, lon);
                    startDesc = new PointDescription(PointDescription.POINT_TYPE_LOCATION, startName);
                } else {
                    start = null;
                    startDesc = null;
                }
                final LatLon searchLocation;
                String searchLatStr = uri.getQueryParameter(PARAM_SEARCH_LAT);
                String searchLonStr = uri.getQueryParameter(PARAM_SEARCH_LON);
                if (!Algorithms.isEmpty(searchLatStr) && !Algorithms.isEmpty(searchLonStr)) {
                    double lat = Double.parseDouble(searchLatStr);
                    double lon = Double.parseDouble(searchLonStr);
                    searchLocation = new LatLon(lat, lon);
                } else {
                    searchLocation = null;
                }
                if (searchLocation == null) {
                    resultCode = RESULT_CODE_ERROR_SEARCH_LOCATION_UNDEFINED;
                } else {
                    boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false);
                    final boolean locationPermission = uri.getBooleanQueryParameter(PARAM_LOCATION_PERMISSION, false);
                    final RoutingHelper routingHelper = app.getRoutingHelper();
                    if (routingHelper.isFollowingMode() && !force) {
                        mapActivity.getMapActions().stopNavigationActionConfirm(new DialogInterface.OnDismissListener() {

                            @Override
                            public void onDismiss(DialogInterface dialog) {
                                if (!routingHelper.isFollowingMode()) {
                                    searchAndNavigate(mapActivity, searchLocation, start, startDesc, profile, searchQuery, showSearchResults, locationPermission);
                                }
                            }
                        });
                    } else {
                        searchAndNavigate(mapActivity, searchLocation, start, startDesc, profile, searchQuery, showSearchResults, locationPermission);
                    }
                    resultCode = Activity.RESULT_OK;
                }
            }
        } else if (API_CMD_PAUSE_NAVIGATION.equals(cmd)) {
            RoutingHelper routingHelper = mapActivity.getRoutingHelper();
            if (routingHelper.isRouteCalculated() && !routingHelper.isRoutePlanningMode()) {
                routingHelper.setRoutePlanningMode(true);
                routingHelper.setFollowingMode(false);
                routingHelper.setPauseNavigation(true);
                resultCode = Activity.RESULT_OK;
            }
        } else if (API_CMD_RESUME_NAVIGATION.equals(cmd)) {
            RoutingHelper routingHelper = mapActivity.getRoutingHelper();
            if (routingHelper.isRouteCalculated() && routingHelper.isRoutePlanningMode()) {
                routingHelper.setRoutePlanningMode(false);
                routingHelper.setFollowingMode(true);
                resultCode = Activity.RESULT_OK;
            }
        } else if (API_CMD_STOP_NAVIGATION.equals(cmd)) {
            RoutingHelper routingHelper = mapActivity.getRoutingHelper();
            if (routingHelper.isPauseNavigation() || routingHelper.isFollowingMode()) {
                mapActivity.getMapLayers().getMapControlsLayer().stopNavigationWithoutConfirm();
                resultCode = Activity.RESULT_OK;
            }
        } else if (API_CMD_MUTE_NAVIGATION.equals(cmd)) {
            mapActivity.getRoutingHelper().getVoiceRouter().setMute(true);
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_UNMUTE_NAVIGATION.equals(cmd)) {
            mapActivity.getRoutingHelper().getVoiceRouter().setMute(false);
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_RECORD_AUDIO.equals(cmd) || API_CMD_RECORD_VIDEO.equals(cmd) || API_CMD_RECORD_PHOTO.equals(cmd) || API_CMD_STOP_AV_REC.equals(cmd)) {
            AudioVideoNotesPlugin plugin = OsmandPlugin.getActivePlugin(AudioVideoNotesPlugin.class);
            if (plugin == null) {
                resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE;
                finish = true;
            } else {
                if (API_CMD_STOP_AV_REC.equals(cmd)) {
                    plugin.stopRecording(mapActivity, false);
                } else {
                    double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT));
                    double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON));
                    if (API_CMD_RECORD_AUDIO.equals(cmd)) {
                        plugin.recordAudio(lat, lon, mapActivity);
                    } else if (API_CMD_RECORD_VIDEO.equals(cmd)) {
                        plugin.recordVideo(lat, lon, mapActivity, false);
                    } else if (API_CMD_RECORD_PHOTO.equals(cmd)) {
                        plugin.takePhoto(lat, lon, mapActivity, true, false);
                    }
                }
                resultCode = Activity.RESULT_OK;
            }
        } else if (API_CMD_GET_INFO.equals(cmd)) {
            Location location = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation();
            if (location != null) {
                result.putExtra(PARAM_LAT, location.getLatitude());
                result.putExtra(PARAM_LON, location.getLongitude());
            }
            LatLon mapLocation = mapActivity.getMapLocation();
            if (location != null) {
                result.putExtra(PARAM_MAP_LAT, mapLocation.getLatitude());
                result.putExtra(PARAM_MAP_LON, mapLocation.getLongitude());
            }
            RoutingHelper routingHelper = app.getRoutingHelper();
            if (routingHelper.isRouteCalculated()) {
                LatLon finalLocation = routingHelper.getFinalLocation();
                result.putExtra(PARAM_DESTINATION_LAT, finalLocation.getLatitude());
                result.putExtra(PARAM_DESTINATION_LON, finalLocation.getLongitude());
                int time = routingHelper.getLeftTime();
                long eta = time + System.currentTimeMillis() / 1000;
                result.putExtra(PARAM_ETA, eta);
                result.putExtra(PARAM_TIME_LEFT, time);
                result.putExtra(PARAM_DISTANCE_LEFT, routingHelper.getLeftDistance());
                result.putExtras(getRouteDirectionsInfo(app));
            }
            result.putExtra(PARAM_VERSION, VERSION_CODE);
            finish = true;
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_ADD_FAVORITE.equals(cmd)) {
            String name = uri.getQueryParameter(PARAM_NAME);
            String desc = uri.getQueryParameter(PARAM_DESC);
            String category = uri.getQueryParameter(PARAM_CATEGORY);
            double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT));
            double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON));
            String colorTag = uri.getQueryParameter(PARAM_COLOR);
            boolean visible = uri.getBooleanQueryParameter(PARAM_VISIBLE, true);
            if (name == null) {
                name = "";
            }
            if (desc == null) {
                desc = "";
            }
            if (category == null) {
                category = "";
            }
            int color = 0;
            if (!Algorithms.isEmpty(colorTag)) {
                color = ColorDialogs.getColorByTag(colorTag);
                if (color == 0) {
                    LOG.error("Wrong color tag: " + colorTag);
                }
            }
            FavouritePoint fav = new FavouritePoint(lat, lon, name, category);
            fav.setDescription(desc);
            fav.setColor(color);
            fav.setVisible(visible);
            FavouritesHelper helper = app.getFavoritesHelper();
            helper.addFavourite(fav);
            showOnMap(lat, lon, fav, mapActivity.getMapLayers().getFavouritesLayer().getObjectName(fav));
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_ADD_MAP_MARKER.equals(cmd)) {
            double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT));
            double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON));
            String name = uri.getQueryParameter(PARAM_NAME);
            PointDescription pd = new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, name != null ? name : "");
            MapMarkersHelper markersHelper = app.getMapMarkersHelper();
            markersHelper.addMapMarker(new LatLon(lat, lon), pd, null);
            MapMarker marker = markersHelper.getFirstMapMarker();
            if (marker != null) {
                showOnMap(lat, lon, marker, mapActivity.getMapLayers().getMapMarkersLayer().getObjectName(marker));
            }
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_SHOW_LOCATION.equals(cmd)) {
            double lat = Double.parseDouble(uri.getQueryParameter(PARAM_LAT));
            double lon = Double.parseDouble(uri.getQueryParameter(PARAM_LON));
            showOnMap(lat, lon, null, null);
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_START_GPX_REC.equals(cmd)) {
            OsmandMonitoringPlugin plugin = OsmandPlugin.getActivePlugin(OsmandMonitoringPlugin.class);
            if (plugin == null) {
                resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE;
                finish = true;
            } else {
                plugin.startGPXMonitoring(null);
            }
            if (uri.getBooleanQueryParameter(PARAM_CLOSE_AFTER_COMMAND, true)) {
                finish = true;
            }
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_STOP_GPX_REC.equals(cmd)) {
            OsmandMonitoringPlugin plugin = OsmandPlugin.getActivePlugin(OsmandMonitoringPlugin.class);
            if (plugin == null) {
                resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE;
                finish = true;
            } else {
                plugin.stopRecording();
            }
            if (uri.getBooleanQueryParameter(PARAM_CLOSE_AFTER_COMMAND, true)) {
                finish = true;
            }
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_SAVE_GPX.equals(cmd)) {
            OsmandMonitoringPlugin plugin = OsmandPlugin.getActivePlugin(OsmandMonitoringPlugin.class);
            if (plugin == null) {
                resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE;
                finish = true;
            } else {
                plugin.saveCurrentTrack();
            }
            if (uri.getBooleanQueryParameter(PARAM_CLOSE_AFTER_COMMAND, true)) {
                finish = true;
            }
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_CLEAR_GPX.equals(cmd)) {
            OsmandMonitoringPlugin plugin = OsmandPlugin.getActivePlugin(OsmandMonitoringPlugin.class);
            if (plugin == null) {
                resultCode = RESULT_CODE_ERROR_PLUGIN_INACTIVE;
                finish = true;
            } else {
                app.getSavingTrackHelper().clearRecordedData(true);
            }
            if (uri.getBooleanQueryParameter(PARAM_CLOSE_AFTER_COMMAND, true)) {
                finish = true;
            }
            resultCode = Activity.RESULT_OK;
        } else if (API_CMD_EXECUTE_QUICK_ACTION.equals(cmd)) {
            int actionNumber = Integer.parseInt(uri.getQueryParameter(PARAM_QUICK_ACTION_NUMBER));
            List<QuickAction> actionsList = app.getQuickActionRegistry().getFilteredQuickActions();
            if (actionNumber >= 0 && actionNumber < actionsList.size()) {
                QuickActionRegistry.produceAction(actionsList.get(actionNumber)).execute(mapActivity);
                resultCode = Activity.RESULT_OK;
            } else {
                resultCode = RESULT_CODE_ERROR_QUICK_ACTION_NOT_FOUND;
            }
            if (uri.getBooleanQueryParameter(PARAM_CLOSE_AFTER_COMMAND, true)) {
                finish = true;
            }
        } else if (API_CMD_GET_QUICK_ACTION_INFO.equals(cmd)) {
            int actionNumber = Integer.parseInt(uri.getQueryParameter(PARAM_QUICK_ACTION_NUMBER));
            List<QuickAction> actionsList = app.getQuickActionRegistry().getFilteredQuickActions();
            if (actionNumber >= 0 && actionNumber < actionsList.size()) {
                QuickAction action = actionsList.get(actionNumber);
                Gson gson = new Gson();
                Type type = new TypeToken<HashMap<String, String>>() {
                }.getType();
                result.putExtra(PARAM_QUICK_ACTION_NAME, action.getName(app));
                result.putExtra(PARAM_QUICK_ACTION_TYPE, action.getActionType().getStringId());
                result.putExtra(PARAM_QUICK_ACTION_PARAMS, gson.toJson(action.getParams(), type));
                result.putExtra(PARAM_VERSION, VERSION_CODE);
                resultCode = Activity.RESULT_OK;
            } else {
                resultCode = RESULT_CODE_ERROR_QUICK_ACTION_NOT_FOUND;
            }
            if (uri.getBooleanQueryParameter(PARAM_CLOSE_AFTER_COMMAND, true)) {
                finish = true;
            }
        } else if (API_CMD_SUBSCRIBE_VOICE_NOTIFICATIONS.equals(cmd)) {
            // not implemented yet
            resultCode = RESULT_CODE_ERROR_NOT_IMPLEMENTED;
        }
    } catch (Exception e) {
        LOG.error("Error processApiRequest:", e);
        resultCode = RESULT_CODE_ERROR_UNKNOWN;
    }
    return result;
}
Also used : QuickAction(net.osmand.plus.quickaction.QuickAction) OsmandApplication(net.osmand.plus.OsmandApplication) AudioVideoNotesPlugin(net.osmand.plus.plugins.audionotes.AudioVideoNotesPlugin) FavouritePoint(net.osmand.data.FavouritePoint) FavouritesHelper(net.osmand.plus.myplaces.FavouritesHelper) MapMarker(net.osmand.plus.mapmarkers.MapMarker) DialogInterface(android.content.DialogInterface) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) Uri(android.net.Uri) OsmandMonitoringPlugin(net.osmand.plus.plugins.monitoring.OsmandMonitoringPlugin) List(java.util.List) ArrayList(java.util.ArrayList) Intent(android.content.Intent) RoutingHelper(net.osmand.plus.routing.RoutingHelper) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) FileInputStream(java.io.FileInputStream) FavouritePoint(net.osmand.data.FavouritePoint) LatLon(net.osmand.data.LatLon) Type(java.lang.reflect.Type) ObjectType(net.osmand.search.core.ObjectType) TurnType(net.osmand.router.TurnType) MapMarkersHelper(net.osmand.plus.mapmarkers.MapMarkersHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) PointDescription(net.osmand.data.PointDescription) ParcelFileDescriptor(android.os.ParcelFileDescriptor) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXFile(net.osmand.GPXUtilities.GPXFile) SelectedGpxFile(net.osmand.plus.track.helpers.GpxSelectionHelper.SelectedGpxFile) File(java.io.File) Location(net.osmand.Location)

Example 2 with FavouritesHelper

use of net.osmand.plus.myplaces.FavouritesHelper in project Osmand by osmandapp.

the class FavoritesImportTask method mergeFavorites.

public static void mergeFavorites(@NonNull OsmandApplication app, @NonNull GPXFile gpxFile, @NonNull String fileName, boolean forceImportFavourites) {
    List<FavouritePoint> favourites = asFavourites(app, gpxFile.getPoints(), fileName, forceImportFavourites);
    FavouritesHelper favoritesHelper = app.getFavoritesHelper();
    checkDuplicateNames(favourites);
    for (FavouritePoint favourite : favourites) {
        favoritesHelper.deleteFavourite(favourite, false);
        favoritesHelper.addFavourite(favourite, false);
    }
    favoritesHelper.sortAll();
    favoritesHelper.saveCurrentPointsIntoFile();
}
Also used : FavouritePoint(net.osmand.data.FavouritePoint) FavouritesHelper(net.osmand.plus.myplaces.FavouritesHelper)

Example 3 with FavouritesHelper

use of net.osmand.plus.myplaces.FavouritesHelper in project Osmand by osmandapp.

the class AddPointBottomSheetDialog method createFavoritesScrollItem.

private void createFavoritesScrollItem() {
    final OsmandApplication app = getMyApplication();
    if (app != null) {
        List<Object> items = new ArrayList<>();
        final FavoritesItemsAdapter adapter = new FavoritesItemsAdapter(app, items);
        adapter.setItemClickListener(getAdapterOnClickListener(items));
        final FavouritesHelper helper = app.getFavoritesHelper();
        if (helper.isFavoritesLoaded()) {
            loadFavoritesItems(items, helper);
        } else {
            addMainScrollItems(items);
            helper.addListener(new FavouritesHelper.FavoritesListener() {

                private void reloadFavoritesItems() {
                    MapActivity mapActivity = (MapActivity) getActivity();
                    if (mapActivity != null) {
                        loadFavoritesItems(adapter.getItems(), helper);
                        adapter.notifyDataSetChanged();
                    }
                }

                @Override
                public void onFavoritesLoaded() {
                    reloadFavoritesItems();
                }

                @Override
                public void onFavoriteDataUpdated(@NonNull FavouritePoint favouritePoint) {
                    reloadFavoritesItems();
                }
            });
        }
        BaseBottomSheetItem scrollItem = new HorizontalRecyclerBottomSheetItem.Builder().setAdapter(adapter).setLayoutId(R.layout.bottom_sheet_item_recyclerview).create();
        this.items.add(scrollItem);
    }
}
Also used : BaseBottomSheetItem(net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem) OsmandApplication(net.osmand.plus.OsmandApplication) FavouritesHelper(net.osmand.plus.myplaces.FavouritesHelper) FavouritePoint(net.osmand.data.FavouritePoint) ArrayList(java.util.ArrayList) HorizontalRecyclerBottomSheetItem(net.osmand.plus.base.bottomsheetmenu.HorizontalRecyclerBottomSheetItem) MapActivity(net.osmand.plus.activities.MapActivity)

Example 4 with FavouritesHelper

use of net.osmand.plus.myplaces.FavouritesHelper in project Osmand by osmandapp.

the class AddPointBottomSheetDialog method getAdapterOnClickListener.

private OnClickListener getAdapterOnClickListener(final List<Object> items) {
    return new OnClickListener() {

        @Override
        public void onClick(View v) {
            MapActivity mapActivity = (MapActivity) getActivity();
            RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) v.getTag();
            int position = viewHolder != null ? viewHolder.getAdapterPosition() : RecyclerView.NO_POSITION;
            if (mapActivity == null || position == RecyclerView.NO_POSITION) {
                return;
            }
            Object item = items.get(position);
            if (item.equals(FAVORITES)) {
                openFavoritesDialog();
            } else if (item.equals(MARKERS)) {
                MapRouteInfoMenu menu = mapActivity.getMapRouteInfoMenu();
                menu.selectMapMarker(-1, pointType);
                dismiss();
            } else if (item instanceof MapMarker) {
                MapRouteInfoMenu menu = mapActivity.getMapRouteInfoMenu();
                menu.selectMapMarker((MapMarker) item, pointType);
                dismiss();
            } else {
                TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper();
                Pair<LatLon, PointDescription> pair = getLocationAndDescrFromItem(item);
                LatLon ll = pair.first;
                PointDescription name = pair.second;
                if (ll == null) {
                    if (item instanceof PointType) {
                        AddPointBottomSheetDialog.showInstance(mapActivity, (PointType) item);
                    } else {
                        dismiss();
                    }
                } else {
                    FavouritesHelper favorites = requiredMyApplication().getFavoritesHelper();
                    switch(pointType) {
                        case START:
                            targetPointsHelper.setStartPoint(ll, true, name);
                            break;
                        case TARGET:
                            targetPointsHelper.navigateToPoint(ll, true, -1, name);
                            break;
                        case INTERMEDIATE:
                            targetPointsHelper.navigateToPoint(ll, true, targetPointsHelper.getIntermediatePoints().size(), name);
                            break;
                        case HOME:
                            favorites.setSpecialPoint(ll, FavouritePoint.SpecialPointType.HOME, null);
                            break;
                        case WORK:
                            favorites.setSpecialPoint(ll, FavouritePoint.SpecialPointType.WORK, null);
                            break;
                        case PARKING:
                            favorites.setSpecialPoint(ll, FavouritePoint.SpecialPointType.PARKING, null);
                            break;
                    }
                    dismiss();
                }
            }
        }
    };
}
Also used : MapMarker(net.osmand.plus.mapmarkers.MapMarker) FavouritesHelper(net.osmand.plus.myplaces.FavouritesHelper) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TextView(android.widget.TextView) FavouritePoint(net.osmand.data.FavouritePoint) TargetPoint(net.osmand.plus.helpers.TargetPointsHelper.TargetPoint) LatLon(net.osmand.data.LatLon) PointDescription(net.osmand.data.PointDescription) OnClickListener(android.view.View.OnClickListener) PointType(net.osmand.plus.routepreparationmenu.MapRouteInfoMenu.PointType) RecyclerView(androidx.recyclerview.widget.RecyclerView) TargetPointsHelper(net.osmand.plus.helpers.TargetPointsHelper) MapActivity(net.osmand.plus.activities.MapActivity)

Example 5 with FavouritesHelper

use of net.osmand.plus.myplaces.FavouritesHelper in project Osmand by osmandapp.

the class AddPointBottomSheetDialog method getLocationAndDescrFromItem.

private Pair<LatLon, PointDescription> getLocationAndDescrFromItem(Object item) {
    PointDescription name = null;
    LatLon ll = null;
    if (item instanceof FavouritePoint) {
        FavouritePoint point = (FavouritePoint) item;
        ll = new LatLon(point.getLatitude(), point.getLongitude());
        name = point.getPointDescription(requireActivity());
    } else if (item instanceof PointType) {
        MapActivity mapActivity = (MapActivity) getActivity();
        if (mapActivity != null) {
            FavouritesHelper favorites = mapActivity.getMyApplication().getFavoritesHelper();
            FavouritePoint point = null;
            if (item == PointType.HOME) {
                point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.HOME);
            } else if (item == PointType.WORK) {
                point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.WORK);
            } else if (item == PointType.PARKING) {
                point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.PARKING);
            }
            if (point != null) {
                ll = new LatLon(point.getLatitude(), point.getLongitude());
                name = point.getPointDescription(mapActivity);
            }
        }
    }
    return new Pair<>(ll, name);
}
Also used : LatLon(net.osmand.data.LatLon) FavouritePoint(net.osmand.data.FavouritePoint) FavouritesHelper(net.osmand.plus.myplaces.FavouritesHelper) PointDescription(net.osmand.data.PointDescription) PointType(net.osmand.plus.routepreparationmenu.MapRouteInfoMenu.PointType) MapActivity(net.osmand.plus.activities.MapActivity) Pair(android.util.Pair)

Aggregations

FavouritesHelper (net.osmand.plus.myplaces.FavouritesHelper)31 FavouritePoint (net.osmand.data.FavouritePoint)20 OsmandApplication (net.osmand.plus.OsmandApplication)12 MapActivity (net.osmand.plus.activities.MapActivity)10 FavoriteGroup (net.osmand.plus.myplaces.FavoriteGroup)10 LatLon (net.osmand.data.LatLon)9 Context (android.content.Context)6 DialogInterface (android.content.DialogInterface)6 View (android.view.View)6 TextView (android.widget.TextView)5 NonNull (androidx.annotation.NonNull)5 AlertDialog (androidx.appcompat.app.AlertDialog)5 PointDescription (net.osmand.data.PointDescription)5 Bundle (android.os.Bundle)4 EditText (android.widget.EditText)4 ArrayList (java.util.ArrayList)4 SuppressLint (android.annotation.SuppressLint)3 Activity (android.app.Activity)3 Dialog (android.app.Dialog)3 ImageView (android.widget.ImageView)3