Search in sources :

Example 6 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class SettingsImporter method processItems.

private List<SettingsItem> processItems(@NonNull File file, @Nullable List<SettingsItem> items) throws IllegalArgumentException, IOException {
    boolean collecting = items == null;
    if (collecting) {
        items = getItemsFromJson(file);
    }
    if (items.isEmpty()) {
        throw new IllegalArgumentException("No items");
    }
    ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
    InputStream ois = new BufferedInputStream(zis);
    try {
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            String fileName = checkEntryName(entry.getName());
            SettingsItem item = null;
            for (SettingsItem settingsItem : items) {
                if (settingsItem != null && settingsItem.applyFileName(fileName)) {
                    item = settingsItem;
                    break;
                }
            }
            if (item != null && collecting && item.shouldReadOnCollecting() || item != null && !collecting && !item.shouldReadOnCollecting()) {
                try {
                    SettingsItemReader<? extends SettingsItem> reader = item.getReader();
                    if (reader != null) {
                        reader.readFromStream(ois, fileName);
                    }
                    item.applyAdditionalParams(reader);
                } catch (IllegalArgumentException | IOException e) {
                    item.getWarnings().add(app.getString(R.string.settings_item_read_error, item.getName()));
                    SettingsHelper.LOG.error("Error reading item data: " + item.getName(), e);
                } finally {
                    zis.closeEntry();
                }
            }
        }
    } catch (IOException ex) {
        SettingsHelper.LOG.error("Failed to read next entry", ex);
    } finally {
        Algorithms.closeStream(ois);
        Algorithms.closeStream(zis);
    }
    return items;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 7 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class FileSettingsHelper method finishImport.

void finishImport(@Nullable ImportListener listener, boolean success, @NonNull List<SettingsItem> items, boolean needRestart) {
    importTask = null;
    List<String> warnings = new ArrayList<>();
    for (SettingsItem item : items) {
        warnings.addAll(item.getWarnings());
    }
    if (!warnings.isEmpty()) {
        getApp().showToastMessage(AndroidUtils.formatWarnings(warnings).toString());
    }
    if (listener != null) {
        listener.onImportFinished(success, needRestart, items);
    }
}
Also used : SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) ArrayList(java.util.ArrayList)

Example 8 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class ExportSettingsFragment method prepareFile.

private void prepareFile() {
    if (app != null) {
        exportingStarted = true;
        exportStartTime = System.currentTimeMillis();
        showExportProgressDialog();
        File tempDir = FileUtils.getTempDir(app);
        String fileName = getFileName();
        List<SettingsItem> items = app.getFileSettingsHelper().prepareSettingsItems(adapter.getData(), Collections.emptyList(), true);
        progress.setMax(getMaxProgress(items));
        app.getFileSettingsHelper().exportSettings(tempDir, fileName, getSettingsExportListener(), items, true);
    }
}
Also used : SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) FileSettingsItem(net.osmand.plus.settings.backend.backup.items.FileSettingsItem) File(java.io.File)

Example 9 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class OsmandAidlApi method exportProfile.

public boolean exportProfile(String appModeKey, List<String> settingsTypesKeys) {
    ApplicationMode appMode = ApplicationMode.valueOfStringKey(appModeKey, null);
    if (app != null && appMode != null) {
        List<ExportSettingsType> settingsTypes = new ArrayList<>();
        for (String key : settingsTypesKeys) {
            settingsTypes.add(ExportSettingsType.valueOf(key));
        }
        settingsTypes.remove(ExportSettingsType.PROFILE);
        List<SettingsItem> settingsItems = new ArrayList<>();
        settingsItems.add(new ProfileSettingsItem(app, appMode));
        File exportDir = app.getSettings().getExternalStorageDirectory();
        String fileName = appMode.toHumanString();
        FileSettingsHelper settingsHelper = app.getFileSettingsHelper();
        settingsItems.addAll(settingsHelper.getFilteredSettingsItems(settingsTypes, true, false));
        settingsHelper.exportSettings(exportDir, fileName, null, settingsItems, true);
        return true;
    }
    return false;
}
Also used : ProfileSettingsItem(net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) ProfileSettingsItem(net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem) ArrayList(java.util.ArrayList) ApplicationMode(net.osmand.plus.settings.backend.ApplicationMode) GPXFile(net.osmand.GPXUtilities.GPXFile) SelectedGpxFile(net.osmand.plus.track.helpers.GpxSelectionHelper.SelectedGpxFile) ASqliteDbFile(net.osmand.aidl.tiles.ASqliteDbFile) File(java.io.File) ASelectedGpxFile(net.osmand.aidl.gpx.ASelectedGpxFile) AGpxFile(net.osmand.aidl.gpx.AGpxFile) FileSettingsHelper(net.osmand.plus.settings.backend.backup.FileSettingsHelper) ExportSettingsType(net.osmand.plus.settings.backend.ExportSettingsType)

Example 10 with SettingsItem

use of net.osmand.plus.settings.backend.backup.items.SettingsItem in project Osmand by osmandapp.

the class SettingsImportTask method handlePluginImport.

private void handlePluginImport(final PluginSettingsItem pluginItem, final File file) {
    FragmentActivity activity = activityRef.get();
    final ProgressDialog progress;
    if (AndroidUtils.isActivityNotDestroyed(activity)) {
        progress = new ProgressDialog(activity);
        progress.setTitle(app.getString(R.string.loading_smth, ""));
        progress.setMessage(app.getString(R.string.importing_from, pluginItem.getPublicName(app)));
        progress.setIndeterminate(true);
        progress.setCancelable(false);
        progress.show();
    } else {
        progress = null;
    }
    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) {
            FragmentActivity activity = activityRef.get();
            if (progress != null && AndroidUtils.isActivityNotDestroyed(activity)) {
                progress.dismiss();
            }
            AudioVideoNotesPlugin pluginAudioVideo = OsmandPlugin.getPlugin(AudioVideoNotesPlugin.class);
            if (pluginAudioVideo != null) {
                pluginAudioVideo.indexingFiles(true, true);
            }
            CustomOsmandPlugin plugin = pluginItem.getPlugin();
            plugin.loadResources();
            if (!Algorithms.isEmpty(plugin.getDownloadMaps())) {
                app.getDownloadThread().runReloadIndexFilesSilent();
            }
            if (!Algorithms.isEmpty(plugin.getRendererNames())) {
                app.getRendererRegistry().updateExternalRenderers();
            }
            if (!Algorithms.isEmpty(plugin.getRouterNames())) {
                loadRoutingFiles(app, null);
            }
            if (!silentImport && activity != null) {
                plugin.onInstall(app, activity);
            }
            String pluginId = pluginItem.getPluginId();
            File pluginDir = app.getAppPath(IndexConstants.PLUGINS_DIR + pluginId);
            if (!pluginDir.exists()) {
                pluginDir.mkdirs();
            }
            app.getFileSettingsHelper().exportSettings(pluginDir, "items", null, items, false);
        }
    };
    List<SettingsItem> pluginItems = new ArrayList<>(pluginItem.getPluginDependentItems());
    pluginItems.add(0, pluginItem);
    app.getFileSettingsHelper().checkDuplicates(file, pluginItems, pluginItems, new CheckDuplicatesListener() {

        @Override
        public void onDuplicatesChecked(@NonNull List<Object> duplicates, List<SettingsItem> items) {
            for (SettingsItem item : items) {
                item.setShouldReplace(true);
            }
            app.getFileSettingsHelper().importSettings(file, items, "", 1, importListener);
        }
    });
}
Also used : AudioVideoNotesPlugin(net.osmand.plus.plugins.audionotes.AudioVideoNotesPlugin) CustomOsmandPlugin(net.osmand.plus.plugins.CustomOsmandPlugin) ArrayList(java.util.ArrayList) ProgressDialog(android.app.ProgressDialog) ImportListener(net.osmand.plus.settings.backend.backup.SettingsHelper.ImportListener) FragmentActivity(androidx.fragment.app.FragmentActivity) SettingsItem(net.osmand.plus.settings.backend.backup.items.SettingsItem) PluginSettingsItem(net.osmand.plus.settings.backend.backup.items.PluginSettingsItem) NonNull(androidx.annotation.NonNull) ArrayList(java.util.ArrayList) List(java.util.List) CallbackWithObject(net.osmand.CallbackWithObject) File(java.io.File) CheckDuplicatesListener(net.osmand.plus.settings.backend.backup.SettingsHelper.CheckDuplicatesListener)

Aggregations

SettingsItem (net.osmand.plus.settings.backend.backup.items.SettingsItem)37 ArrayList (java.util.ArrayList)19 FileSettingsItem (net.osmand.plus.settings.backend.backup.items.FileSettingsItem)18 File (java.io.File)11 List (java.util.List)11 IOException (java.io.IOException)10 GpxSettingsItem (net.osmand.plus.settings.backend.backup.items.GpxSettingsItem)10 ProfileSettingsItem (net.osmand.plus.settings.backend.backup.items.ProfileSettingsItem)10 NonNull (androidx.annotation.NonNull)8 AvoidRoadsSettingsItem (net.osmand.plus.settings.backend.backup.items.AvoidRoadsSettingsItem)7 MapSourcesSettingsItem (net.osmand.plus.settings.backend.backup.items.MapSourcesSettingsItem)7 PoiUiFiltersSettingsItem (net.osmand.plus.settings.backend.backup.items.PoiUiFiltersSettingsItem)7 QuickActionsSettingsItem (net.osmand.plus.settings.backend.backup.items.QuickActionsSettingsItem)7 Nullable (androidx.annotation.Nullable)6 HashMap (java.util.HashMap)6 PluginSettingsItem (net.osmand.plus.settings.backend.backup.items.PluginSettingsItem)6 HashSet (java.util.HashSet)5 FileInputStream (java.io.FileInputStream)4 FragmentActivity (androidx.fragment.app.FragmentActivity)3 Map (java.util.Map)3