use of net.osmand.plus.settings.backend.backup.items.PluginSettingsItem in project Osmand by osmandapp.
the class SettingsItemsFactory method collectItems.
private void collectItems(JSONObject json) throws IllegalArgumentException, JSONException {
JSONArray itemsJson = json.getJSONArray("items");
int version = json.has("version") ? json.getInt("version") : 1;
if (version > SettingsHelper.VERSION) {
throw new IllegalArgumentException("Unsupported osf version: " + version);
}
Map<String, List<SettingsItem>> pluginItems = new HashMap<>();
for (int i = 0; i < itemsJson.length(); i++) {
JSONObject itemJson = itemsJson.getJSONObject(i);
SettingsItem item;
try {
item = createItem(itemJson);
// unknown type
if (item == null) {
continue;
}
items.add(item);
String pluginId = item.getPluginId();
if (pluginId != null && item.getType() != SettingsItemType.PLUGIN) {
List<SettingsItem> items = pluginItems.get(pluginId);
if (items != null) {
items.add(item);
} else {
items = new ArrayList<>();
items.add(item);
pluginItems.put(pluginId, items);
}
}
} catch (IllegalArgumentException e) {
SettingsHelper.LOG.error("Error creating item from json: " + itemJson, e);
}
}
for (SettingsItem item : items) {
if (item instanceof PluginSettingsItem) {
PluginSettingsItem pluginSettingsItem = ((PluginSettingsItem) item);
List<SettingsItem> pluginDependentItems = pluginItems.get(pluginSettingsItem.getName());
if (!Algorithms.isEmpty(pluginDependentItems)) {
pluginSettingsItem.getPluginDependentItems().addAll(pluginDependentItems);
}
}
}
}
use of net.osmand.plus.settings.backend.backup.items.PluginSettingsItem in project Osmand by osmandapp.
the class SettingsItemsFactory method createItem.
@Nullable
private SettingsItem createItem(@NonNull JSONObject json) throws IllegalArgumentException, JSONException {
SettingsItem item = null;
SettingsItemType type = SettingsItem.parseItemType(json);
if (type == null) {
return null;
}
OsmandSettings settings = app.getSettings();
switch(type) {
case GLOBAL:
item = new GlobalSettingsItem(settings, json);
break;
case PROFILE:
item = new ProfileSettingsItem(app, json);
break;
case PLUGIN:
item = new PluginSettingsItem(app, json);
break;
case DATA:
item = new DataSettingsItem(app, json);
break;
case FILE:
item = new FileSettingsItem(app, json);
break;
case RESOURCES:
item = new ResourcesSettingsItem(app, json);
break;
case QUICK_ACTIONS:
item = new QuickActionsSettingsItem(app, json);
break;
case POI_UI_FILTERS:
item = new PoiUiFiltersSettingsItem(app, json);
break;
case MAP_SOURCES:
item = new MapSourcesSettingsItem(app, json);
break;
case AVOID_ROADS:
item = new AvoidRoadsSettingsItem(app, json);
break;
case SUGGESTED_DOWNLOADS:
item = new SuggestedDownloadsItem(app, json);
break;
case DOWNLOADS:
item = new DownloadsItem(app, json);
break;
case OSM_NOTES:
item = new OsmNotesSettingsItem(app, json);
break;
case OSM_EDITS:
item = new OsmEditsSettingsItem(app, json);
break;
case FAVOURITES:
item = new FavoritesSettingsItem(app, json);
break;
case ACTIVE_MARKERS:
item = new MarkersSettingsItem(app, json);
break;
case HISTORY_MARKERS:
item = new HistoryMarkersSettingsItem(app, json);
break;
case SEARCH_HISTORY:
item = new SearchHistorySettingsItem(app, json);
break;
case GPX:
item = new GpxSettingsItem(app, json);
break;
case ONLINE_ROUTING_ENGINES:
item = new OnlineRoutingSettingsItem(app, json);
break;
case ITINERARY_GROUPS:
item = new ItinerarySettingsItem(app, json);
break;
}
return item;
}
use of net.osmand.plus.settings.backend.backup.items.PluginSettingsItem 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);
}
});
}
use of net.osmand.plus.settings.backend.backup.items.PluginSettingsItem 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);
}
}
});
}
use of net.osmand.plus.settings.backend.backup.items.PluginSettingsItem 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));
}
}
Aggregations