Add plugin dependent settings items to plugin item
This commit is contained in:
parent
a5777b9ca2
commit
27a5daf2db
2 changed files with 52 additions and 20 deletions
|
@ -156,7 +156,6 @@ public class SettingsHelper {
|
||||||
public abstract static class SettingsItem {
|
public abstract static class SettingsItem {
|
||||||
|
|
||||||
private SettingsItemType type;
|
private SettingsItemType type;
|
||||||
private String pluginDependentId;
|
|
||||||
|
|
||||||
boolean shouldReplace = false;
|
boolean shouldReplace = false;
|
||||||
|
|
||||||
|
@ -183,11 +182,6 @@ public class SettingsHelper {
|
||||||
@NonNull
|
@NonNull
|
||||||
public abstract String getFileName();
|
public abstract String getFileName();
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public String getPluginDependentId() {
|
|
||||||
return pluginDependentId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean shouldReadOnCollecting() {
|
public boolean shouldReadOnCollecting() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -209,9 +203,6 @@ public class SettingsHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
void readFromJson(@NonNull JSONObject json) throws JSONException {
|
void readFromJson(@NonNull JSONObject json) throws JSONException {
|
||||||
if (json.has("pluginId")) {
|
|
||||||
pluginDependentId = json.getString("pluginId");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeToJson(@NonNull JSONObject json) throws JSONException {
|
void writeToJson(@NonNull JSONObject json) throws JSONException {
|
||||||
|
@ -256,6 +247,7 @@ public class SettingsHelper {
|
||||||
|
|
||||||
private OsmandApplication app;
|
private OsmandApplication app;
|
||||||
private CustomOsmandPlugin plugin;
|
private CustomOsmandPlugin plugin;
|
||||||
|
private List<SettingsItem> pluginItems;
|
||||||
|
|
||||||
PluginSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
|
PluginSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
|
||||||
super(SettingsItemType.PLUGIN, json);
|
super(SettingsItemType.PLUGIN, json);
|
||||||
|
@ -284,6 +276,10 @@ public class SettingsHelper {
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<SettingsItem> getPluginItems() {
|
||||||
|
return pluginItems;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void readFromJson(@NonNull JSONObject json) throws JSONException {
|
void readFromJson(@NonNull JSONObject json) throws JSONException {
|
||||||
String pluginId = json.getString("pluginId");
|
String pluginId = json.getString("pluginId");
|
||||||
|
@ -1666,23 +1662,45 @@ public class SettingsHelper {
|
||||||
|
|
||||||
SettingsItemsFactory(OsmandApplication app, String jsonStr) throws IllegalArgumentException, JSONException {
|
SettingsItemsFactory(OsmandApplication app, String jsonStr) throws IllegalArgumentException, JSONException {
|
||||||
this.app = app;
|
this.app = app;
|
||||||
JSONObject json = new JSONObject(jsonStr);
|
collectItems(new JSONObject(jsonStr));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void collectItems(JSONObject json) throws IllegalArgumentException, JSONException {
|
||||||
JSONArray itemsJson = json.getJSONArray("items");
|
JSONArray itemsJson = json.getJSONArray("items");
|
||||||
|
Map<String, List<SettingsItem>> pluginItems = new HashMap<>();
|
||||||
for (int i = 0; i < itemsJson.length(); i++) {
|
for (int i = 0; i < itemsJson.length(); i++) {
|
||||||
JSONObject itemJson = itemsJson.getJSONObject(i);
|
JSONObject itemJson = itemsJson.getJSONObject(i);
|
||||||
SettingsItem item;
|
SettingsItem item = null;
|
||||||
try {
|
try {
|
||||||
item = createItem(itemJson);
|
item = createItem(itemJson);
|
||||||
if (item != null) {
|
|
||||||
items.add(item);
|
|
||||||
}
|
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
LOG.error("Error creating item from json: " + itemJson, e);
|
LOG.error("Error creating item from json: " + itemJson, e);
|
||||||
}
|
}
|
||||||
|
if (item != null) {
|
||||||
|
if (itemJson.has("pluginId") && item.type != SettingsItemType.PLUGIN) {
|
||||||
|
String pluginId = itemJson.getString("pluginId");
|
||||||
|
List<SettingsItem> items = pluginItems.get(pluginId);
|
||||||
|
if (items != null) {
|
||||||
|
items.add(item);
|
||||||
|
} else {
|
||||||
|
items = new ArrayList<>();
|
||||||
|
items.add(item);
|
||||||
|
pluginItems.put(pluginId, items);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
items.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (items.size() == 0) {
|
if (items.size() == 0) {
|
||||||
throw new IllegalArgumentException("No items");
|
throw new IllegalArgumentException("No items");
|
||||||
}
|
}
|
||||||
|
for (SettingsItem item : items) {
|
||||||
|
if (item instanceof PluginSettingsItem) {
|
||||||
|
PluginSettingsItem pluginSettingsItem = ((PluginSettingsItem) item);
|
||||||
|
pluginSettingsItem.pluginItems = pluginItems.get(pluginSettingsItem.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|
|
@ -787,17 +787,31 @@ public class ImportHelper {
|
||||||
progress.dismiss();
|
progress.dismiss();
|
||||||
}
|
}
|
||||||
if (succeed) {
|
if (succeed) {
|
||||||
boolean pluginImport = false;
|
List<SettingsHelper.SettingsItem> pluginIndependentItems = new ArrayList<>();
|
||||||
|
List<SettingsHelper.PluginSettingsItem> pluginSettingsItems = new ArrayList<>();
|
||||||
for (SettingsHelper.SettingsItem item : items) {
|
for (SettingsHelper.SettingsItem item : items) {
|
||||||
if (item instanceof SettingsHelper.PluginSettingsItem) {
|
if (item instanceof SettingsHelper.PluginSettingsItem) {
|
||||||
pluginImport = true;
|
pluginSettingsItems.add((SettingsHelper.PluginSettingsItem) item);
|
||||||
CustomOsmandPlugin plugin = ((SettingsHelper.PluginSettingsItem) item).getPlugin();
|
} else {
|
||||||
OsmandPlugin.addCustomPlugin(app, activity, plugin);
|
pluginIndependentItems.add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!pluginImport) {
|
for (SettingsHelper.PluginSettingsItem pluginItem : pluginSettingsItems) {
|
||||||
|
final CustomOsmandPlugin plugin = pluginItem.getPlugin();
|
||||||
|
OsmandPlugin.addCustomPlugin(app, activity, plugin);
|
||||||
|
|
||||||
|
if (!Algorithms.isEmpty(pluginItem.getPluginItems())) {
|
||||||
|
app.getSettingsHelper().importSettings(file, pluginItem.getPluginItems(), "", 1, new SettingsHelper.SettingsImportListener() {
|
||||||
|
@Override
|
||||||
|
public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List<SettingsHelper.SettingsItem> items) {
|
||||||
|
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, plugin.getName()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!pluginIndependentItems.isEmpty()) {
|
||||||
FragmentManager fragmentManager = activity.getSupportFragmentManager();
|
FragmentManager fragmentManager = activity.getSupportFragmentManager();
|
||||||
ImportSettingsFragment.showInstance(fragmentManager, items, file);
|
ImportSettingsFragment.showInstance(fragmentManager, pluginIndependentItems, file);
|
||||||
}
|
}
|
||||||
} else if (empty) {
|
} else if (empty) {
|
||||||
app.showShortToastMessage(app.getString(R.string.file_import_error, name, app.getString(R.string.shared_string_unexpected_error)));
|
app.showShortToastMessage(app.getString(R.string.file_import_error, name, app.getString(R.string.shared_string_unexpected_error)));
|
||||||
|
|
Loading…
Reference in a new issue