Save custom plugins second part
This commit is contained in:
parent
30a34b12a9
commit
69c3510fc0
3 changed files with 137 additions and 24 deletions
|
@ -1,14 +1,22 @@
|
|||
package net.osmand.plus;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import net.osmand.map.ITileSource;
|
||||
import net.osmand.osm.PoiCategory;
|
||||
import net.osmand.plus.helpers.AvoidSpecificRoads;
|
||||
import net.osmand.plus.poi.PoiUIFilter;
|
||||
import net.osmand.plus.quickaction.QuickAction;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
public class CustomOsmandPlugin extends OsmandPlugin {
|
||||
|
@ -76,9 +84,114 @@ public class CustomOsmandPlugin extends OsmandPlugin {
|
|||
json.put("name", getName());
|
||||
json.put("Description", getDescription());
|
||||
|
||||
saveAdditionalItemsToJson(json);
|
||||
|
||||
return json.toString();
|
||||
}
|
||||
|
||||
public void saveAdditionalItemsToJson(JSONObject json) throws JSONException {
|
||||
if (!appModes.isEmpty()) {
|
||||
List<String> appModesKeys = new ArrayList<>();
|
||||
for (ApplicationMode mode : appModes) {
|
||||
appModesKeys.add(mode.getStringKey());
|
||||
}
|
||||
JSONArray appModesJson = new JSONArray(appModesKeys);
|
||||
json.put("appModes", appModesJson);
|
||||
}
|
||||
if (!rendererNames.isEmpty()) {
|
||||
JSONArray rendererNamesJson = new JSONArray(rendererNames);
|
||||
json.put("rendererNames", rendererNamesJson);
|
||||
}
|
||||
if (!routerNames.isEmpty()) {
|
||||
JSONArray rendererNamesJson = new JSONArray(routerNames);
|
||||
json.put("routerNames", rendererNamesJson);
|
||||
}
|
||||
|
||||
savePoiUIFiltersToJson(json);
|
||||
saveMapSourcesToJson(json);
|
||||
saveQuickActionsToJson(json);
|
||||
saveAvoidRoadsToJson(json);
|
||||
}
|
||||
|
||||
private void savePoiUIFiltersToJson(JSONObject json) throws JSONException {
|
||||
if (!poiUIFilters.isEmpty()) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
Gson gson = new Gson();
|
||||
Type type = new TypeToken<HashMap<PoiCategory, LinkedHashSet<String>>>() {
|
||||
}.getType();
|
||||
for (PoiUIFilter filter : poiUIFilters) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("name", filter.getName());
|
||||
jsonObject.put("filterId", filter.getFilterId());
|
||||
jsonObject.put("acceptedTypes", gson.toJson(filter.getAcceptedTypes(), type));
|
||||
jsonArray.put(jsonObject);
|
||||
}
|
||||
json.put("poiUIFilters", jsonArray);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveMapSourcesToJson(JSONObject json) throws JSONException {
|
||||
if (!mapSources.isEmpty()) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (ITileSource template : mapSources) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
boolean sql = template instanceof SQLiteTileSource;
|
||||
jsonObject.put("sql", sql);
|
||||
jsonObject.put("name", template.getName());
|
||||
jsonObject.put("minZoom", template.getMinimumZoomSupported());
|
||||
jsonObject.put("maxZoom", template.getMaximumZoomSupported());
|
||||
jsonObject.put("url", template.getUrlTemplate());
|
||||
jsonObject.put("randoms", template.getRandoms());
|
||||
jsonObject.put("ellipsoid", template.isEllipticYTile());
|
||||
jsonObject.put("inverted_y", template.isInvertedYTile());
|
||||
jsonObject.put("referer", template.getReferer());
|
||||
jsonObject.put("timesupported", template.isTimeSupported());
|
||||
jsonObject.put("expire", template.getExpirationTimeMillis());
|
||||
jsonObject.put("inversiveZoom", template.getInversiveZoom());
|
||||
jsonObject.put("ext", template.getTileFormat());
|
||||
jsonObject.put("tileSize", template.getTileSize());
|
||||
jsonObject.put("bitDensity", template.getBitDensity());
|
||||
jsonObject.put("avgSize", template.getAvgSize());
|
||||
jsonObject.put("rule", template.getRule());
|
||||
jsonArray.put(jsonObject);
|
||||
}
|
||||
json.put("mapSources", jsonArray);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveAvoidRoadsToJson(JSONObject json) throws JSONException {
|
||||
if (!avoidRoadInfos.isEmpty()) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (AvoidSpecificRoads.AvoidRoadInfo avoidRoad : avoidRoadInfos) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("latitude", avoidRoad.latitude);
|
||||
jsonObject.put("longitude", avoidRoad.longitude);
|
||||
jsonObject.put("name", avoidRoad.name);
|
||||
jsonObject.put("appModeKey", avoidRoad.appModeKey);
|
||||
jsonArray.put(jsonObject);
|
||||
}
|
||||
json.put("avoidRoadInfos", jsonArray);
|
||||
}
|
||||
}
|
||||
|
||||
private void saveQuickActionsToJson(JSONObject json) throws JSONException {
|
||||
if (!quickActions.isEmpty()) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
Gson gson = new Gson();
|
||||
Type type = new TypeToken<HashMap<String, String>>() {
|
||||
}.getType();
|
||||
|
||||
for (QuickAction action : quickActions) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("name", action.hasCustomName(app) ? action.getName(app) : "");
|
||||
jsonObject.put("type", action.getType());
|
||||
jsonObject.put("params", gson.toJson(action.getParams(), type));
|
||||
jsonArray.put(jsonObject);
|
||||
}
|
||||
json.put("quickActions", jsonArray);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getRendererNames() {
|
||||
return rendererNames;
|
||||
|
|
|
@ -229,6 +229,7 @@ public class SettingsHelper {
|
|||
PluginSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
|
||||
super(SettingsItemType.PLUGIN, json);
|
||||
this.app = app;
|
||||
readFromJson(app, json);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
@ -257,8 +258,7 @@ public class SettingsHelper {
|
|||
return pluginItems;
|
||||
}
|
||||
|
||||
@Override
|
||||
void readFromJson(@NonNull JSONObject json) throws JSONException {
|
||||
void readFromJson(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
|
||||
String pluginId = json.getString("pluginId");
|
||||
String name = json.getString("name");
|
||||
String description = json.getString("Description");
|
||||
|
@ -554,6 +554,7 @@ public class SettingsHelper {
|
|||
public ProfileSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
|
||||
super(SettingsItemType.PROFILE, app.getSettings(), json);
|
||||
this.app = app;
|
||||
readFromJson(app, json);
|
||||
appModeBeanPrefsIds = new HashSet<>(Arrays.asList(app.getSettings().appModeBeanPrefsIds));
|
||||
}
|
||||
|
||||
|
@ -589,8 +590,7 @@ public class SettingsHelper {
|
|||
return "profile_" + getName() + ".json";
|
||||
}
|
||||
|
||||
void readFromJson(@NonNull JSONObject json) throws JSONException {
|
||||
super.readFromJson(json);
|
||||
void readFromJson(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
|
||||
String appModeJson = json.getString("appMode");
|
||||
modeBean = ApplicationMode.fromJson(appModeJson);
|
||||
builder = ApplicationMode.fromModeBean(app, modeBean);
|
||||
|
@ -1652,27 +1652,27 @@ public class SettingsHelper {
|
|||
Map<String, List<SettingsItem>> pluginItems = new HashMap<>();
|
||||
for (int i = 0; i < itemsJson.length(); i++) {
|
||||
JSONObject itemJson = itemsJson.getJSONObject(i);
|
||||
SettingsItem item = null;
|
||||
SettingsItem item;
|
||||
try {
|
||||
item = createItem(itemJson);
|
||||
if (item != null) {
|
||||
items.add(item);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalArgumentException 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) {
|
||||
throw new IllegalArgumentException("No items");
|
||||
|
|
|
@ -802,9 +802,7 @@ public class ImportHelper {
|
|||
}
|
||||
}
|
||||
for (SettingsHelper.PluginSettingsItem pluginItem : pluginSettingsItems) {
|
||||
final CustomOsmandPlugin plugin = pluginItem.getPlugin();
|
||||
OsmandPlugin.addCustomPlugin(app, activity, plugin);
|
||||
|
||||
CustomOsmandPlugin plugin = pluginItem.getPlugin();
|
||||
List<SettingsHelper.SettingsItem> pluginItems = pluginItem.getPluginItems();
|
||||
if (!Algorithms.isEmpty(pluginItems)) {
|
||||
for (SettingsHelper.SettingsItem item : pluginItems) {
|
||||
|
@ -825,10 +823,12 @@ public class ImportHelper {
|
|||
plugin.appModes.add(((SettingsHelper.ProfileSettingsItem) item).getAppMode());
|
||||
}
|
||||
}
|
||||
|
||||
OsmandPlugin.addCustomPlugin(app, activity, plugin);
|
||||
app.getSettingsHelper().importSettings(file, pluginItems, "", 1, new SettingsHelper.SettingsImportListener() {
|
||||
@Override
|
||||
public void onSettingsImportFinished(boolean succeed, @NonNull List<SettingsHelper.SettingsItem> items) {
|
||||
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, plugin.getName()));
|
||||
app.showShortToastMessage(app.getString(R.string.file_imported_successfully, ""));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue