Search in sources :

Example 1 with CollectListener

use of net.osmand.plus.settings.backend.backup.SettingsHelper.CollectListener in project Osmand by osmandapp.

the class CustomOsmandPlugin method removePluginItemsFromFile.

private void removePluginItemsFromFile(final File file, final PluginItemsListener itemsListener) {
    app.getFileSettingsHelper().collectSettings(file, "", 1, new CollectListener() {

        @Override
        public void onCollectFinished(boolean succeed, boolean empty, @NonNull List<SettingsItem> items) {
            if (succeed && !items.isEmpty()) {
                for (SettingsItem item : items) {
                    if (item instanceof QuickActionsSettingsItem) {
                        QuickActionsSettingsItem quickActionsSettingsItem = (QuickActionsSettingsItem) item;
                        List<QuickAction> quickActions = quickActionsSettingsItem.getItems();
                        QuickActionRegistry actionRegistry = app.getQuickActionRegistry();
                        for (QuickAction action : quickActions) {
                            QuickAction savedAction = actionRegistry.getQuickAction(app, action.getType(), action.getName(app), action.getParams());
                            if (savedAction != null) {
                                actionRegistry.deleteQuickAction(savedAction);
                            }
                        }
                    } else if (item instanceof MapSourcesSettingsItem) {
                        MapSourcesSettingsItem mapSourcesSettingsItem = (MapSourcesSettingsItem) item;
                        List<ITileSource> mapSources = mapSourcesSettingsItem.getItems();
                        for (ITileSource tileSource : mapSources) {
                            String tileSourceName = tileSource.getName();
                            if (tileSource instanceof SQLiteTileSource) {
                                tileSourceName += SQLITE_EXT;
                            }
                            ITileSource savedTileSource = app.getSettings().getTileSourceByName(tileSourceName, false);
                            if (savedTileSource != null) {
                                if (savedTileSource instanceof SQLiteTileSource) {
                                    SQLiteTileSource sqLiteTileSource = ((SQLiteTileSource) savedTileSource);
                                    sqLiteTileSource.closeDB();
                                }
                                File tPath = app.getAppPath(IndexConstants.TILES_INDEX_DIR);
                                File dir = new File(tPath, tileSourceName);
                                Algorithms.removeAllFiles(dir);
                            }
                        }
                    } else if (item instanceof PoiUiFiltersSettingsItem) {
                        PoiUiFiltersSettingsItem poiUiFiltersSettingsItem = (PoiUiFiltersSettingsItem) item;
                        List<PoiUIFilter> poiUIFilters = poiUiFiltersSettingsItem.getItems();
                        for (PoiUIFilter filter : poiUIFilters) {
                            app.getPoiFilters().removePoiFilter(filter);
                        }
                        app.getPoiFilters().reloadAllPoiFilters();
                        app.getPoiFilters().loadSelectedPoiFilters();
                        app.getSearchUICore().refreshCustomPoiFilters();
                    } else if (item instanceof AvoidRoadsSettingsItem) {
                        AvoidRoadsSettingsItem avoidRoadsSettingsItem = (AvoidRoadsSettingsItem) item;
                        List<AvoidSpecificRoads.AvoidRoadInfo> avoidRoadInfos = avoidRoadsSettingsItem.getItems();
                        for (AvoidSpecificRoads.AvoidRoadInfo avoidRoad : avoidRoadInfos) {
                            app.getAvoidSpecificRoads().removeImpassableRoad(avoidRoad);
                        }
                    } else if (item instanceof ProfileSettingsItem) {
                        ProfileSettingsItem profileSettingsItem = (ProfileSettingsItem) item;
                        ApplicationMode mode = profileSettingsItem.getAppMode();
                        ApplicationMode savedMode = ApplicationMode.valueOfStringKey(mode.getStringKey(), null);
                        if (savedMode != null) {
                            ApplicationMode.changeProfileAvailability(savedMode, false, app);
                        }
                    }
                }
            }
            if (itemsListener != null) {
                itemsListener.onItemsRemoved();
            }
        }
    });
}
Also used : QuickAction(net.osmand.plus.quickaction.QuickAction) ProfileSettingsItem(net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem) AvoidSpecificRoads(net.osmand.plus.helpers.AvoidSpecificRoads) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) SQLiteTileSource(net.osmand.plus.resources.SQLiteTileSource) PoiUIFilter(net.osmand.plus.poi.PoiUIFilter) QuickActionsSettingsItem(net.osmand.plus.settings.backend.backup.items.QuickActionsSettingsItem) PoiUiFiltersSettingsItem(net.osmand.plus.settings.backend.backup.items.PoiUiFiltersSettingsItem) CollectListener(net.osmand.plus.settings.backend.backup.SettingsHelper.CollectListener) AvoidRoadsSettingsItem(net.osmand.plus.settings.backend.backup.items.AvoidRoadsSettingsItem) ProfileSettingsItem(net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem) MapSourcesSettingsItem(net.osmand.plus.settings.backend.backup.items.MapSourcesSettingsItem) PoiUiFiltersSettingsItem(net.osmand.plus.settings.backend.backup.items.PoiUiFiltersSettingsItem) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) PluginSettingsItem(net.osmand.plus.settings.backend.backup.items.PluginSettingsItem) QuickActionsSettingsItem(net.osmand.plus.settings.backend.backup.items.QuickActionsSettingsItem) ITileSource(net.osmand.map.ITileSource) List(java.util.List) ArrayList(java.util.ArrayList) MapSourcesSettingsItem(net.osmand.plus.settings.backend.backup.items.MapSourcesSettingsItem) QuickActionRegistry(net.osmand.plus.quickaction.QuickActionRegistry) File(java.io.File) AvoidRoadsSettingsItem(net.osmand.plus.settings.backend.backup.items.AvoidRoadsSettingsItem)

Example 2 with CollectListener

use of net.osmand.plus.settings.backend.backup.SettingsHelper.CollectListener in project Osmand by osmandapp.

the class CustomOsmandPlugin method addPluginItemsFromFile.

private void addPluginItemsFromFile(final File file, final Activity activity) {
    final ProgressDialog progress = new ProgressDialog(activity);
    progress.setTitle(app.getString(R.string.loading_smth, ""));
    progress.setMessage(app.getString(R.string.loading_data));
    progress.setIndeterminate(true);
    progress.setCancelable(false);
    if (AndroidUtils.isActivityNotDestroyed(activity)) {
        progress.show();
    }
    final ImportListener importListener = new ImportListener() {

        @Override
        public void onImportItemStarted(@NonNull String type, @NonNull String fileName, int work) {
        }

        @Override
        public void onImportItemProgress(@NonNull String type, @NonNull String fileName, int value) {
        }

        @Override
        public void onImportItemFinished(@NonNull String type, @NonNull String fileName) {
        }

        @Override
        public void onImportFinished(boolean succeed, boolean needRestart, @NonNull List<SettingsItem> items) {
            if (AndroidUtils.isActivityNotDestroyed(activity)) {
                progress.dismiss();
            }
        }
    };
    app.getFileSettingsHelper().collectSettings(file, "", 1, new CollectListener() {

        @Override
        public void onCollectFinished(boolean succeed, boolean empty, @NonNull List<SettingsItem> items) {
            if (succeed && !items.isEmpty()) {
                for (Iterator<SettingsItem> iterator = items.iterator(); iterator.hasNext(); ) {
                    SettingsItem item = iterator.next();
                    if (item instanceof ProfileSettingsItem) {
                        ProfileSettingsItem profileSettingsItem = (ProfileSettingsItem) item;
                        ApplicationMode mode = profileSettingsItem.getAppMode();
                        ApplicationMode savedMode = ApplicationMode.valueOfStringKey(mode.getStringKey(), null);
                        if (savedMode != null) {
                            ApplicationMode.changeProfileAvailability(savedMode, true, app);
                        }
                        iterator.remove();
                    } else if (!(item instanceof PluginSettingsItem)) {
                        item.setShouldReplace(true);
                    }
                }
                app.getFileSettingsHelper().importSettings(file, items, "", 1, importListener);
            }
        }
    });
}
Also used : ProfileSettingsItem(net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) ProgressDialog(android.app.ProgressDialog) ImportListener(net.osmand.plus.settings.backend.backup.SettingsHelper.ImportListener) CollectListener(net.osmand.plus.settings.backend.backup.SettingsHelper.CollectListener) AvoidRoadsSettingsItem(net.osmand.plus.settings.backend.backup.items.AvoidRoadsSettingsItem) ProfileSettingsItem(net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem) MapSourcesSettingsItem(net.osmand.plus.settings.backend.backup.items.MapSourcesSettingsItem) PoiUiFiltersSettingsItem(net.osmand.plus.settings.backend.backup.items.PoiUiFiltersSettingsItem) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) PluginSettingsItem(net.osmand.plus.settings.backend.backup.items.PluginSettingsItem) QuickActionsSettingsItem(net.osmand.plus.settings.backend.backup.items.QuickActionsSettingsItem) NonNull(androidx.annotation.NonNull) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) PluginSettingsItem(net.osmand.plus.settings.backend.backup.items.PluginSettingsItem)

Example 3 with CollectListener

use of net.osmand.plus.settings.backend.backup.SettingsHelper.CollectListener in project Osmand by osmandapp.

the class SettingsImportTask method onPostExecute.

@Override
protected void onPostExecute(String error) {
    File tempDir = FileUtils.getTempDir(app);
    final File file = new File(tempDir, name);
    if (error == null && file.exists()) {
        final FileSettingsHelper settingsHelper = app.getFileSettingsHelper();
        settingsHelper.collectSettings(file, latestChanges, version, new CollectListener() {

            @Override
            public void onCollectFinished(boolean succeed, boolean empty, @NonNull List<SettingsItem> items) {
                hideProgress();
                if (succeed) {
                    List<SettingsItem> pluginIndependentItems = new ArrayList<>();
                    List<PluginSettingsItem> pluginSettingsItems = new ArrayList<>();
                    for (SettingsItem item : items) {
                        if (item instanceof PluginSettingsItem) {
                            pluginSettingsItems.add((PluginSettingsItem) item);
                        } else if (Algorithms.isEmpty(item.getPluginId())) {
                            pluginIndependentItems.add(item);
                        }
                    }
                    for (PluginSettingsItem pluginItem : pluginSettingsItems) {
                        handlePluginImport(pluginItem, file);
                    }
                    if (!pluginIndependentItems.isEmpty()) {
                        if (settingsTypes == null) {
                            FragmentActivity activity = activityRef.get();
                            if (!silentImport && activity != null) {
                                FragmentManager fragmentManager = activity.getSupportFragmentManager();
                                FileImportSettingsFragment.showInstance(fragmentManager, pluginIndependentItems, file);
                            }
                        } else {
                            Map<ExportSettingsType, List<?>> allSettingsMap = getSettingsToOperate(pluginIndependentItems, false, false);
                            List<SettingsItem> settingsList = settingsHelper.getFilteredSettingsItems(allSettingsMap, settingsTypes, pluginIndependentItems, false);
                            settingsHelper.checkDuplicates(file, settingsList, settingsList, getDuplicatesListener(file, replace));
                        }
                    }
                } else if (empty) {
                    app.showShortToastMessage(app.getString(R.string.file_import_error, name, app.getString(R.string.shared_string_unexpected_error)));
                }
            }
        });
    } else {
        hideProgress();
        app.showShortToastMessage(app.getString(R.string.file_import_error, name, error));
    }
}
Also used : FragmentManager(androidx.fragment.app.FragmentManager) FragmentActivity(androidx.fragment.app.FragmentActivity) CollectListener(net.osmand.plus.settings.backend.backup.SettingsHelper.CollectListener) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) PluginSettingsItem(net.osmand.plus.settings.backend.backup.items.PluginSettingsItem) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) FileSettingsHelper(net.osmand.plus.settings.backend.backup.FileSettingsHelper) Map(java.util.Map) PluginSettingsItem(net.osmand.plus.settings.backend.backup.items.PluginSettingsItem)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 CollectListener (net.osmand.plus.settings.backend.backup.SettingsHelper.CollectListener)3 PluginSettingsItem (net.osmand.plus.settings.backend.backup.items.PluginSettingsItem)3 SettingsItem (net.osmand.plus.settings.backend.backup.items.SettingsItem)3 File (java.io.File)2 ApplicationMode (net.osmand.plus.settings.backend.ApplicationMode)2 AvoidRoadsSettingsItem (net.osmand.plus.settings.backend.backup.items.AvoidRoadsSettingsItem)2 MapSourcesSettingsItem (net.osmand.plus.settings.backend.backup.items.MapSourcesSettingsItem)2 PoiUiFiltersSettingsItem (net.osmand.plus.settings.backend.backup.items.PoiUiFiltersSettingsItem)2 ProfileSettingsItem (net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem)2 QuickActionsSettingsItem (net.osmand.plus.settings.backend.backup.items.QuickActionsSettingsItem)2 ProgressDialog (android.app.ProgressDialog)1 NonNull (androidx.annotation.NonNull)1 FragmentActivity (androidx.fragment.app.FragmentActivity)1 FragmentManager (androidx.fragment.app.FragmentManager)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 ITileSource (net.osmand.map.ITileSource)1 AvoidSpecificRoads (net.osmand.plus.helpers.AvoidSpecificRoads)1