Search in sources :

Example 1 with QuickAction

use of net.osmand.plus.quickaction.QuickAction 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 QuickAction

use of net.osmand.plus.quickaction.QuickAction in project Osmand by osmandapp.

the class QuickActionsSettingsItem method apply.

@Override
public void apply() {
    List<QuickAction> newItems = getNewItems();
    if (!newItems.isEmpty() || !duplicateItems.isEmpty()) {
        appliedItems = new ArrayList<>(newItems);
        List<QuickAction> newActions = new ArrayList<>(existingItems);
        if (!duplicateItems.isEmpty()) {
            if (shouldReplace) {
                for (QuickAction duplicateItem : duplicateItems) {
                    for (QuickAction savedAction : existingItems) {
                        if (duplicateItem.getName(app).equals(savedAction.getName(app))) {
                            newActions.remove(savedAction);
                        }
                    }
                }
            } else {
                for (QuickAction duplicateItem : duplicateItems) {
                    renameItem(duplicateItem);
                }
            }
            appliedItems.addAll(duplicateItems);
        }
        newActions.addAll(appliedItems);
        actionRegistry.updateQuickActions(newActions);
    }
}
Also used : QuickAction(net.osmand.plus.quickaction.QuickAction) ArrayList(java.util.ArrayList)

Example 3 with QuickAction

use of net.osmand.plus.quickaction.QuickAction in project Osmand by osmandapp.

the class QuickActionsSettingsItem method readItemsFromJson.

@Override
void readItemsFromJson(@NonNull JSONObject json) throws IllegalArgumentException {
    try {
        if (!json.has("items")) {
            return;
        }
        Gson gson = new Gson();
        Type type = new TypeToken<HashMap<String, String>>() {
        }.getType();
        QuickActionRegistry quickActionRegistry = app.getQuickActionRegistry();
        JSONArray itemsJson = json.getJSONArray("items");
        for (int i = 0; i < itemsJson.length(); i++) {
            JSONObject object = itemsJson.getJSONObject(i);
            String name = object.getString("name");
            QuickAction quickAction = null;
            if (object.has("actionType")) {
                quickAction = quickActionRegistry.newActionByStringType(object.getString("actionType"));
            } else if (object.has("type")) {
                quickAction = quickActionRegistry.newActionByType(object.getInt("type"));
            }
            if (quickAction != null) {
                String paramsString = object.getString("params");
                HashMap<String, String> params = gson.fromJson(paramsString, type);
                if (!name.isEmpty()) {
                    quickAction.setName(name);
                }
                quickAction.setParams(params);
                items.add(quickAction);
            } else {
                warnings.add(app.getString(R.string.settings_item_read_error, name));
            }
        }
    } catch (JSONException e) {
        warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType())));
        throw new IllegalArgumentException("Json parse error", e);
    }
}
Also used : QuickAction(net.osmand.plus.quickaction.QuickAction) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) JSONException(org.json.JSONException) Type(java.lang.reflect.Type) SettingsItemType(net.osmand.plus.settings.backend.backup.SettingsItemType) JSONObject(org.json.JSONObject) QuickActionRegistry(net.osmand.plus.quickaction.QuickActionRegistry)

Example 4 with QuickAction

use of net.osmand.plus.quickaction.QuickAction in project Osmand by osmandapp.

the class QuickActionsSettingsItem method writeItemsToJson.

@NonNull
@Override
JSONObject writeItemsToJson(@NonNull JSONObject json) {
    JSONArray jsonArray = new JSONArray();
    Gson gson = new Gson();
    Type type = new TypeToken<HashMap<String, String>>() {
    }.getType();
    if (!items.isEmpty()) {
        try {
            for (QuickAction action : items) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", action.hasCustomName(app) ? action.getName(app) : "");
                jsonObject.put("actionType", action.getActionType().getStringId());
                jsonObject.put("params", gson.toJson(action.getParams(), type));
                jsonArray.put(jsonObject);
            }
            json.put("items", jsonArray);
        } catch (JSONException e) {
            warnings.add(app.getString(R.string.settings_item_write_error, String.valueOf(getType())));
            SettingsHelper.LOG.error("Failed write to json", e);
        }
    }
    return json;
}
Also used : QuickAction(net.osmand.plus.quickaction.QuickAction) Type(java.lang.reflect.Type) SettingsItemType(net.osmand.plus.settings.backend.backup.SettingsItemType) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) JSONException(org.json.JSONException) NonNull(androidx.annotation.NonNull)

Example 5 with QuickAction

use of net.osmand.plus.quickaction.QuickAction in project Osmand by osmandapp.

the class SettingsHelper method getSettingsItems.

private Map<ExportSettingsType, List<?>> getSettingsItems(@Nullable List<ExportSettingsType> settingsTypes, boolean addEmptyItems) {
    Map<ExportSettingsType, List<?>> settingsItems = new LinkedHashMap<>();
    if (settingsTypes == null || settingsTypes.contains(ExportSettingsType.PROFILE)) {
        List<ApplicationModeBean> appModeBeans = new ArrayList<>();
        for (ApplicationMode mode : ApplicationMode.allPossibleValues()) {
            appModeBeans.add(mode.toModeBean());
        }
        settingsItems.put(ExportSettingsType.PROFILE, appModeBeans);
    }
    List<GlobalSettingsItem> globalSettingsList = settingsTypes == null || settingsTypes.contains(ExportSettingsType.GLOBAL) ? Collections.singletonList(new GlobalSettingsItem(app.getSettings())) : Collections.emptyList();
    if (!globalSettingsList.isEmpty() || addEmptyItems) {
        settingsItems.put(ExportSettingsType.GLOBAL, globalSettingsList);
    }
    QuickActionRegistry registry = app.getQuickActionRegistry();
    List<QuickAction> actionsList = settingsTypes == null || settingsTypes.contains(ExportSettingsType.QUICK_ACTIONS) ? registry.getQuickActions() : Collections.emptyList();
    if (!actionsList.isEmpty() || addEmptyItems) {
        settingsItems.put(ExportSettingsType.QUICK_ACTIONS, actionsList);
    }
    List<PoiUIFilter> poiList = settingsTypes == null || settingsTypes.contains(ExportSettingsType.POI_TYPES) ? app.getPoiFilters().getUserDefinedPoiFilters(false) : Collections.emptyList();
    if (!poiList.isEmpty() || addEmptyItems) {
        settingsItems.put(ExportSettingsType.POI_TYPES, poiList);
    }
    Map<LatLon, AvoidRoadInfo> impassableRoads = settingsTypes == null || settingsTypes.contains(ExportSettingsType.AVOID_ROADS) ? app.getAvoidSpecificRoads().getImpassableRoads() : Collections.emptyMap();
    if (!impassableRoads.isEmpty() || addEmptyItems) {
        settingsItems.put(ExportSettingsType.AVOID_ROADS, new ArrayList<>(impassableRoads.values()));
    }
    return settingsItems;
}
Also used : QuickAction(net.osmand.plus.quickaction.QuickAction) ApplicationModeBean(net.osmand.plus.settings.backend.ApplicationMode.ApplicationModeBean) ArrayList(java.util.ArrayList) AvoidRoadInfo(net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) LinkedHashMap(java.util.LinkedHashMap) PoiUIFilter(net.osmand.plus.poi.PoiUIFilter) LatLon(net.osmand.data.LatLon) GlobalSettingsItem(net.osmand.plus.settings.backend.backup.items.GlobalSettingsItem) List(java.util.List) ArrayList(java.util.ArrayList) QuickActionRegistry(net.osmand.plus.quickaction.QuickActionRegistry) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Aggregations

QuickAction (net.osmand.plus.quickaction.QuickAction)14 File (java.io.File)7 ArrayList (java.util.ArrayList)7 PoiUIFilter (net.osmand.plus.poi.PoiUIFilter)7 ApplicationMode (net.osmand.plus.settings.backend.ApplicationMode)7 ITileSource (net.osmand.map.ITileSource)6 AvoidRoadInfo (net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo)6 Gson (com.google.gson.Gson)5 Type (java.lang.reflect.Type)5 HashMap (java.util.HashMap)5 HistoryEntry (net.osmand.plus.helpers.SearchHistoryHelper.HistoryEntry)5 MapMarkersGroup (net.osmand.plus.mapmarkers.MapMarkersGroup)5 FavoriteGroup (net.osmand.plus.myplaces.FavoriteGroup)5 OnlineRoutingEngine (net.osmand.plus.onlinerouting.engine.OnlineRoutingEngine)5 ApplicationModeBean (net.osmand.plus.settings.backend.ApplicationMode.ApplicationModeBean)5 List (java.util.List)4 FavouritePoint (net.osmand.data.FavouritePoint)4 MapMarker (net.osmand.plus.mapmarkers.MapMarker)4 OpenstreetmapPoint (net.osmand.plus.plugins.osmedit.data.OpenstreetmapPoint)4 OsmNotesPoint (net.osmand.plus.plugins.osmedit.data.OsmNotesPoint)4