Fixes of SettingsHelper

This commit is contained in:
max-klaus 2020-03-20 22:12:19 +03:00
parent a2c5624930
commit 1110dd5166
4 changed files with 428 additions and 437 deletions

View file

@ -87,6 +87,8 @@ import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT;
public class SettingsHelper { public class SettingsHelper {
public static final int VERSION = 1;
public static final String SETTINGS_LATEST_CHANGES_KEY = "settings_latest_changes"; public static final String SETTINGS_LATEST_CHANGES_KEY = "settings_latest_changes";
public static final String SETTINGS_VERSION_KEY = "settings_version"; public static final String SETTINGS_VERSION_KEY = "settings_version";
@ -124,7 +126,7 @@ public class SettingsHelper {
PLUGIN, PLUGIN,
DATA, DATA,
FILE, FILE,
QUICK_ACTION, QUICK_ACTIONS,
POI_UI_FILTERS, POI_UI_FILTERS,
MAP_SOURCES, MAP_SOURCES,
AVOID_ROADS AVOID_ROADS
@ -133,6 +135,7 @@ public class SettingsHelper {
public abstract static class SettingsItem { public abstract static class SettingsItem {
private SettingsItemType type; private SettingsItemType type;
private String fileName;
boolean shouldReplace = false; boolean shouldReplace = false;
@ -157,7 +160,16 @@ public class SettingsHelper {
public abstract String getPublicName(@NonNull Context ctx); public abstract String getPublicName(@NonNull Context ctx);
@NonNull @NonNull
public abstract String getFileName(); public abstract String getDefaultFileName();
@NonNull
public String getFileName() {
return !Algorithms.isEmpty(fileName) ? fileName : getDefaultFileName();
}
public void setFileName(@NonNull String fileName) {
this.fileName = fileName;
}
public boolean shouldReadOnCollecting() { public boolean shouldReadOnCollecting() {
return false; return false;
@ -180,11 +192,14 @@ public class SettingsHelper {
} }
void readFromJson(@NonNull JSONObject json) throws JSONException { void readFromJson(@NonNull JSONObject json) throws JSONException {
fileName = json.getString("file");
readItemsFromJson(json);
} }
void writeToJson(@NonNull JSONObject json) throws JSONException { void writeToJson(@NonNull JSONObject json) throws JSONException {
json.put("type", type.name()); json.put("type", type.name());
json.put("name", getName()); json.put("file", getFileName());
writeItemsToJson(json);
} }
String toJson() throws JSONException { String toJson() throws JSONException {
@ -193,12 +208,69 @@ public class SettingsHelper {
return json.toString(); return json.toString();
} }
@NonNull void readItemsFromJson(@NonNull JSONObject json) throws IllegalArgumentException {
// override
}
void writeItemsToJson(@NonNull JSONObject json) {
// override
}
@Nullable
abstract SettingsItemReader getReader(); abstract SettingsItemReader getReader();
@NonNull @Nullable
abstract SettingsItemWriter getWriter(); abstract SettingsItemWriter getWriter();
@NonNull
SettingsItemReader getJsonReader() {
return new SettingsItemReader<SettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
StringBuilder buf = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
} catch (IOException e) {
throw new IOException("Cannot read json body", e);
}
String json = buf.toString();
if (json.length() == 0) {
throw new IllegalArgumentException("Json body is empty");
}
try {
readItemsFromJson(new JSONObject(json));
} catch (JSONException e) {
throw new IllegalArgumentException("Json parsing error", e);
}
}
};
}
@NonNull
SettingsItemWriter getJsonWriter() {
return new SettingsItemWriter<SettingsItem>(this) {
@Override
public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException {
JSONObject json = new JSONObject();
writeItemsToJson(json);
if (json.length() > 0) {
try {
String s = json.toString(2);
outputStream.write(s.getBytes("UTF-8"));
} catch (JSONException e) {
LOG.error("Failed to write json to stream", e);
}
return true;
}
return false;
}
};
}
@Override @Override
public int hashCode() { public int hashCode() {
return (getType().name() + getName()).hashCode(); return (getType().name() + getName()).hashCode();
@ -246,7 +318,7 @@ public class SettingsHelper {
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return getName() + ".zip"; return getName() + ".zip";
} }
@ -262,27 +334,16 @@ public class SettingsHelper {
plugin = new CustomOsmandPlugin(app, json); plugin = new CustomOsmandPlugin(app, json);
} }
@NonNull @Nullable
@Override @Override
SettingsItemReader getReader() { SettingsItemReader getReader() {
return new SettingsItemReader<PluginSettingsItem>(this) { return null;
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
}
};
} }
@NonNull @Nullable
@Override @Override
SettingsItemWriter getWriter() { SettingsItemWriter getWriter() {
return new SettingsItemWriter<PluginSettingsItem>(this) { return null;
@Override
public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException {
return false;
}
};
} }
} }
@ -488,7 +549,7 @@ public class SettingsHelper {
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return getName() + ".json"; return getName() + ".json";
} }
@ -497,7 +558,7 @@ public class SettingsHelper {
return true; return true;
} }
@NonNull @Nullable
@Override @Override
SettingsItemReader getReader() { SettingsItemReader getReader() {
return new OsmandSettingsItemReader(this, getSettings()) { return new OsmandSettingsItemReader(this, getSettings()) {
@ -508,7 +569,7 @@ public class SettingsHelper {
}; };
} }
@NonNull @Nullable
@Override @Override
SettingsItemWriter getWriter() { SettingsItemWriter getWriter() {
return new OsmandSettingsItemWriter(this, getSettings()) { return new OsmandSettingsItemWriter(this, getSettings()) {
@ -579,7 +640,7 @@ public class SettingsHelper {
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return "profile_" + getName() + ".json"; return "profile_" + getName() + ".json";
} }
@ -645,7 +706,7 @@ public class SettingsHelper {
json.put("appMode", new JSONObject(appMode.toJson())); json.put("appMode", new JSONObject(appMode.toJson()));
} }
@NonNull @Nullable
@Override @Override
SettingsItemReader getReader() { SettingsItemReader getReader() {
return new OsmandSettingsItemReader(this, getSettings()) { return new OsmandSettingsItemReader(this, getSettings()) {
@ -658,7 +719,7 @@ public class SettingsHelper {
}; };
} }
@NonNull @Nullable
@Override @Override
SettingsItemWriter getWriter() { SettingsItemWriter getWriter() {
return new OsmandSettingsItemWriter(this, getSettings()) { return new OsmandSettingsItemWriter(this, getSettings()) {
@ -677,7 +738,6 @@ public class SettingsHelper {
public StreamSettingsItemReader(@NonNull StreamSettingsItem item) { public StreamSettingsItemReader(@NonNull StreamSettingsItem item) {
super(item); super(item);
} }
} }
public static class StreamSettingsItemWriter extends SettingsItemWriter<StreamSettingsItem> { public static class StreamSettingsItemWriter extends SettingsItemWriter<StreamSettingsItem> {
@ -780,7 +840,7 @@ public class SettingsHelper {
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return getName() + ".dat"; return getName() + ".dat";
} }
@ -789,7 +849,16 @@ public class SettingsHelper {
return data; return data;
} }
@NonNull @Override
void readFromJson(@NonNull JSONObject json) throws JSONException {
super.readFromJson(json);
String fileName = getFileName();
if (Algorithms.isEmpty(name) && !Algorithms.isEmpty(fileName)) {
name = Algorithms.getFileNameWithoutExtension(new File(fileName));
}
}
@Nullable
@Override @Override
SettingsItemReader getReader() { SettingsItemReader getReader() {
return new StreamSettingsItemReader(this) { return new StreamSettingsItemReader(this) {
@ -808,7 +877,7 @@ public class SettingsHelper {
}; };
} }
@NonNull @Nullable
@Override @Override
public SettingsItemWriter getWriter() { public SettingsItemWriter getWriter() {
setInputStream(new ByteArrayInputStream(data)); setInputStream(new ByteArrayInputStream(data));
@ -819,6 +888,7 @@ public class SettingsHelper {
public static class FileSettingsItem extends StreamSettingsItem { public static class FileSettingsItem extends StreamSettingsItem {
private File file; private File file;
private String subtype;
public FileSettingsItem(@NonNull OsmandApplication app, @NonNull File file) { public FileSettingsItem(@NonNull OsmandApplication app, @NonNull File file) {
super(SettingsItemType.FILE, file.getPath().replace(app.getAppPath(null).getPath(), "")); super(SettingsItemType.FILE, file.getPath().replace(app.getAppPath(null).getPath(), ""));
@ -828,11 +898,12 @@ public class SettingsHelper {
FileSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { FileSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
super(SettingsItemType.FILE, json); super(SettingsItemType.FILE, json);
this.file = new File(app.getAppPath(null), name); this.file = new File(app.getAppPath(null), name);
this.subtype = json.getString("subtype");
} }
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return getName(); return getName();
} }
@ -840,6 +911,10 @@ public class SettingsHelper {
return file; return file;
} }
public String getSubtype() {
return subtype;
}
@Override @Override
public boolean exists() { public boolean exists() {
return file.exists(); return file.exists();
@ -858,7 +933,7 @@ public class SettingsHelper {
} }
} }
@NonNull @Nullable
@Override @Override
SettingsItemReader getReader() { SettingsItemReader getReader() {
return new StreamSettingsItemReader(this) { return new StreamSettingsItemReader(this) {
@ -884,7 +959,7 @@ public class SettingsHelper {
}; };
} }
@NonNull @Nullable
@Override @Override
public SettingsItemWriter getWriter() { public SettingsItemWriter getWriter() {
try { try {
@ -896,22 +971,22 @@ public class SettingsHelper {
} }
} }
public static class QuickActionSettingsItem extends CollectionSettingsItem<QuickAction> { public static class QuickActionsSettingsItem extends CollectionSettingsItem<QuickAction> {
private OsmandApplication app; private OsmandApplication app;
private QuickActionRegistry actionRegistry; private QuickActionRegistry actionRegistry;
public QuickActionSettingsItem(@NonNull OsmandApplication app, public QuickActionsSettingsItem(@NonNull OsmandApplication app,
@NonNull List<QuickAction> items) { @NonNull List<QuickAction> items) {
super(SettingsItemType.QUICK_ACTION, items); super(SettingsItemType.QUICK_ACTIONS, items);
this.app = app; this.app = app;
actionRegistry = app.getQuickActionRegistry(); actionRegistry = app.getQuickActionRegistry();
existingItems = actionRegistry.getQuickActions(); existingItems = actionRegistry.getQuickActions();
} }
QuickActionSettingsItem(@NonNull OsmandApplication app, QuickActionsSettingsItem(@NonNull OsmandApplication app,
@NonNull JSONObject json) throws JSONException { @NonNull JSONObject json) throws JSONException {
super(SettingsItemType.QUICK_ACTION, json); super(SettingsItemType.QUICK_ACTIONS, json);
this.app = app; this.app = app;
actionRegistry = app.getQuickActionRegistry(); actionRegistry = app.getQuickActionRegistry();
existingItems = actionRegistry.getQuickActions(); existingItems = actionRegistry.getQuickActions();
@ -972,36 +1047,16 @@ public class SettingsHelper {
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return getName() + ".json"; return getName() + ".json";
} }
@NonNull
@Override @Override
SettingsItemReader getReader() { void readItemsFromJson(@NonNull JSONObject json) throws IllegalArgumentException {
return new SettingsItemReader<QuickActionSettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
StringBuilder buf = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
} catch (IOException e) {
throw new IOException("Cannot read json body", e);
}
String jsonStr = buf.toString();
if (Algorithms.isEmpty(jsonStr)) {
throw new IllegalArgumentException("Cannot find json body");
}
final JSONObject json;
try { try {
Gson gson = new Gson(); Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, String>>() { Type type = new TypeToken<HashMap<String, String>>() {
}.getType(); }.getType();
json = new JSONObject(jsonStr);
JSONArray itemsJson = json.getJSONArray("items"); JSONArray itemsJson = json.getJSONArray("items");
for (int i = 0; i < itemsJson.length(); i++) { for (int i = 0; i < itemsJson.length(); i++) {
JSONObject object = itemsJson.getJSONObject(i); JSONObject object = itemsJson.getJSONObject(i);
@ -1020,16 +1075,9 @@ public class SettingsHelper {
throw new IllegalArgumentException("Json parse error", e); throw new IllegalArgumentException("Json parse error", e);
} }
} }
};
}
@NonNull
@Override @Override
SettingsItemWriter getWriter() { void writeItemsToJson(@NonNull JSONObject json) {
return new SettingsItemWriter<QuickActionSettingsItem>(this) {
@Override
public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException {
JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
Gson gson = new Gson(); Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, String>>() { Type type = new TypeToken<HashMap<String, String>>() {
@ -1049,18 +1097,18 @@ public class SettingsHelper {
LOG.error("Failed write to json", e); LOG.error("Failed write to json", e);
} }
} }
if (json.length() > 0) {
try {
String s = json.toString(2);
outputStream.write(s.getBytes("UTF-8"));
} catch (JSONException e) {
LOG.error("Failed to write json to stream", e);
} }
return true;
@Nullable
@Override
SettingsItemReader getReader() {
return getJsonReader();
} }
return false;
} @Nullable
}; @Override
SettingsItemWriter getWriter() {
return null;
} }
} }
@ -1138,33 +1186,13 @@ public class SettingsHelper {
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return getName() + ".json"; return getName() + ".json";
} }
@NonNull
@Override @Override
SettingsItemReader getReader() { void readItemsFromJson(@NonNull JSONObject json) throws IllegalArgumentException {
return new SettingsItemReader<PoiUiFilterSettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
StringBuilder buf = new StringBuilder();
try { try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
} catch (IOException e) {
throw new IOException("Cannot read json body", e);
}
String jsonStr = buf.toString();
if (Algorithms.isEmpty(jsonStr)) {
throw new IllegalArgumentException("Cannot find json body");
}
final JSONObject json;
try {
json = new JSONObject(jsonStr);
JSONArray jsonArray = json.getJSONArray("items"); JSONArray jsonArray = json.getJSONArray("items");
Gson gson = new Gson(); Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, LinkedHashSet<String>>>() { Type type = new TypeToken<HashMap<String, LinkedHashSet<String>>>() {
@ -1188,16 +1216,9 @@ public class SettingsHelper {
throw new IllegalArgumentException("Json parse error", e); throw new IllegalArgumentException("Json parse error", e);
} }
} }
};
}
@NonNull
@Override @Override
SettingsItemWriter getWriter() { void writeItemsToJson(@NonNull JSONObject json) {
return new SettingsItemWriter<PoiUiFilterSettingsItem>(this) {
@Override
public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException {
JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
Gson gson = new Gson(); Gson gson = new Gson();
Type type = new TypeToken<HashMap<PoiCategory, LinkedHashSet<String>>>() { Type type = new TypeToken<HashMap<PoiCategory, LinkedHashSet<String>>>() {
@ -1216,18 +1237,18 @@ public class SettingsHelper {
LOG.error("Failed write to json", e); LOG.error("Failed write to json", e);
} }
} }
if (json.length() > 0) {
try {
String s = json.toString(2);
outputStream.write(s.getBytes("UTF-8"));
} catch (JSONException e) {
LOG.error("Failed to write json to stream", e);
} }
return true;
@Nullable
@Override
SettingsItemReader getReader() {
return getJsonReader();
} }
return false;
} @Nullable
}; @Override
SettingsItemWriter getWriter() {
return null;
} }
} }
@ -1338,33 +1359,13 @@ public class SettingsHelper {
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return getName() + ".json"; return getName() + ".json";
} }
@NonNull
@Override @Override
SettingsItemReader getReader() { void readItemsFromJson(@NonNull JSONObject json) throws IllegalArgumentException {
return new SettingsItemReader<MapSourcesSettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
StringBuilder buf = new StringBuilder();
try { try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
} catch (IOException e) {
throw new IOException("Cannot read json body", e);
}
String jsonStr = buf.toString();
if (Algorithms.isEmpty(jsonStr)) {
throw new IllegalArgumentException("Cannot find json body");
}
final JSONObject json;
try {
json = new JSONObject(jsonStr);
JSONArray jsonArray = json.getJSONArray("items"); JSONArray jsonArray = json.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i); JSONObject object = jsonArray.getJSONObject(i);
@ -1398,16 +1399,9 @@ public class SettingsHelper {
throw new IllegalArgumentException("Json parse error", e); throw new IllegalArgumentException("Json parse error", e);
} }
} }
};
}
@NonNull
@Override @Override
SettingsItemWriter getWriter() { void writeItemsToJson(@NonNull JSONObject json) {
return new SettingsItemWriter<MapSourcesSettingsItem>(this) {
@Override
public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException {
JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
if (!items.isEmpty()) { if (!items.isEmpty()) {
try { try {
@ -1439,18 +1433,18 @@ public class SettingsHelper {
LOG.error("Failed write to json", e); LOG.error("Failed write to json", e);
} }
} }
if (json.length() > 0) {
try {
String s = json.toString(2);
outputStream.write(s.getBytes("UTF-8"));
} catch (JSONException e) {
LOG.error("Failed to write json to stream", e);
} }
return true;
@Nullable
@Override
SettingsItemReader getReader() {
return getJsonReader();
} }
return false;
} @Nullable
}; @Override
SettingsItemWriter getWriter() {
return null;
} }
} }
@ -1490,7 +1484,7 @@ public class SettingsHelper {
@NonNull @NonNull
@Override @Override
public String getFileName() { public String getDefaultFileName() {
return getName() + ".json"; return getName() + ".json";
} }
@ -1543,29 +1537,9 @@ public class SettingsHelper {
} }
} }
@NonNull
@Override @Override
SettingsItemReader getReader() { void readItemsFromJson(@NonNull JSONObject json) throws IllegalArgumentException {
return new SettingsItemReader<AvoidRoadsSettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException {
StringBuilder buf = new StringBuilder();
try { try {
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
} catch (IOException e) {
throw new IOException("Cannot read json body", e);
}
String jsonStr = buf.toString();
if (Algorithms.isEmpty(jsonStr)) {
throw new IllegalArgumentException("Cannot find json body");
}
final JSONObject json;
try {
json = new JSONObject(jsonStr);
JSONArray jsonArray = json.getJSONArray("items"); JSONArray jsonArray = json.getJSONArray("items");
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i); JSONObject object = jsonArray.getJSONObject(i);
@ -1589,16 +1563,9 @@ public class SettingsHelper {
throw new IllegalArgumentException("Json parse error", e); throw new IllegalArgumentException("Json parse error", e);
} }
} }
};
}
@NonNull
@Override @Override
SettingsItemWriter getWriter() { void writeItemsToJson(@NonNull JSONObject json) {
return new SettingsItemWriter<AvoidRoadsSettingsItem>(this) {
@Override
public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException {
JSONObject json = new JSONObject();
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
if (!items.isEmpty()) { if (!items.isEmpty()) {
try { try {
@ -1615,18 +1582,18 @@ public class SettingsHelper {
LOG.error("Failed write to json", e); LOG.error("Failed write to json", e);
} }
} }
if (json.length() > 0) {
try {
String s = json.toString(2);
outputStream.write(s.getBytes("UTF-8"));
} catch (JSONException e) {
LOG.error("Failed to write json to stream", e);
} }
return true;
@Nullable
@Override
SettingsItemReader getReader() {
return getJsonReader();
} }
return false;
} @Nullable
}; @Override
SettingsItemWriter getWriter() {
return null;
} }
} }
@ -1642,6 +1609,10 @@ public class SettingsHelper {
private void collectItems(JSONObject json) throws IllegalArgumentException, JSONException { private void collectItems(JSONObject json) throws IllegalArgumentException, JSONException {
JSONArray itemsJson = json.getJSONArray("items"); JSONArray itemsJson = json.getJSONArray("items");
int version = json.getInt("version");
if (version > VERSION) {
throw new IllegalArgumentException("Unsupported osf version: " + version);
}
Map<String, List<SettingsItem>> pluginItems = new HashMap<>(); 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);
@ -1714,8 +1685,8 @@ public class SettingsHelper {
case FILE: case FILE:
item = new FileSettingsItem(app, json); item = new FileSettingsItem(app, json);
break; break;
case QUICK_ACTION: case QUICK_ACTIONS:
item = new QuickActionSettingsItem(app, json); item = new QuickActionsSettingsItem(app, json);
break; break;
case POI_UI_FILTERS: case POI_UI_FILTERS:
item = new PoiUiFilterSettingsItem(app, json); item = new PoiUiFilterSettingsItem(app, json);
@ -1754,7 +1725,7 @@ public class SettingsHelper {
void exportSettings(File file) throws JSONException, IOException { void exportSettings(File file) throws JSONException, IOException {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("osmand_settings_version", OsmandSettings.VERSION); json.put("version", VERSION);
for (Map.Entry<String, String> param : additionalParams.entrySet()) { for (Map.Entry<String, String> param : additionalParams.entrySet()) {
json.put(param.getKey(), param.getValue()); json.put(param.getKey(), param.getValue());
} }
@ -1771,11 +1742,14 @@ public class SettingsHelper {
zos.write(json.toString(2).getBytes("UTF-8")); zos.write(json.toString(2).getBytes("UTF-8"));
zos.closeEntry(); zos.closeEntry();
for (SettingsItem item : items.values()) { for (SettingsItem item : items.values()) {
SettingsItemWriter writer = item.getWriter();
if (writer != null) {
entry = new ZipEntry(item.getFileName()); entry = new ZipEntry(item.getFileName());
zos.putNextEntry(entry); zos.putNextEntry(entry);
item.getWriter().writeToStream(zos); writer.writeToStream(zos);
zos.closeEntry(); zos.closeEntry();
} }
}
zos.flush(); zos.flush();
zos.finish(); zos.finish();
} finally { } finally {
@ -1801,20 +1775,14 @@ public class SettingsHelper {
processItems(file, items); processItems(file, items);
} }
private List<SettingsItem> processItems(@NonNull File file, @Nullable List<SettingsItem> items) throws IllegalArgumentException, IOException { private List<SettingsItem> getItemsFromJson(@NonNull File file) throws IOException {
boolean collecting = items == null; List<SettingsItem> items = new ArrayList<>();
if (collecting) {
items = new ArrayList<>();
} else {
if (items.size() == 0) {
throw new IllegalArgumentException("No items");
}
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(file)); ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
InputStream ois = new BufferedInputStream(zis); InputStream ois = new BufferedInputStream(zis);
try { try {
ZipEntry entry = zis.getNextEntry(); ZipEntry entry;
if (entry != null && entry.getName().equals("items.json")) { while ((entry = zis.getNextEntry()) != null) {
if (entry.getName().equals("items.json")) {
String itemsJson = null; String itemsJson = null;
try { try {
itemsJson = Algorithms.readFromInputStream(ois).toString(); itemsJson = Algorithms.readFromInputStream(ois).toString();
@ -1824,7 +1792,6 @@ public class SettingsHelper {
} finally { } finally {
zis.closeEntry(); zis.closeEntry();
} }
if (collecting) {
try { try {
SettingsItemsFactory itemsFactory = new SettingsItemsFactory(app, itemsJson); SettingsItemsFactory itemsFactory = new SettingsItemsFactory(app, itemsJson);
items.addAll(itemsFactory.getItems()); items.addAll(itemsFactory.getItems());
@ -1835,7 +1802,31 @@ public class SettingsHelper {
LOG.error("Error parsing items: " + itemsJson, e); LOG.error("Error parsing items: " + itemsJson, e);
throw new IllegalArgumentException("No items"); throw new IllegalArgumentException("No items");
} }
break;
} }
}
} catch (IOException ex) {
LOG.error("Failed to read next entry", ex);
} finally {
Algorithms.closeStream(ois);
Algorithms.closeStream(zis);
}
return items;
}
private List<SettingsItem> processItems(@NonNull File file, @Nullable List<SettingsItem> items) throws IllegalArgumentException, IOException {
boolean collecting = items == null;
if (collecting) {
items = getItemsFromJson(file);
} else {
if (items.size() == 0) {
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) { while ((entry = zis.getNextEntry()) != null) {
String fileName = entry.getName(); String fileName = entry.getName();
SettingsItem item = null; SettingsItem item = null;
@ -1848,7 +1839,10 @@ public class SettingsHelper {
if (item != null && collecting && item.shouldReadOnCollecting() if (item != null && collecting && item.shouldReadOnCollecting()
|| item != null && !collecting && !item.shouldReadOnCollecting()) { || item != null && !collecting && !item.shouldReadOnCollecting()) {
try { try {
item.getReader().readFromStream(ois); SettingsItemReader reader = item.getReader();
if (reader != null) {
reader.readFromStream(ois);
}
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
LOG.error("Error reading item data: " + item.getName(), e); LOG.error("Error reading item data: " + item.getName(), e);
} catch (IOException e) { } catch (IOException e) {
@ -1858,9 +1852,6 @@ public class SettingsHelper {
} }
} }
} }
} else {
throw new IllegalArgumentException("No items found");
}
} catch (IOException ex) { } catch (IOException ex) {
LOG.error("Failed to read next entry", ex); LOG.error("Failed to read next entry", ex);
} finally { } finally {

View file

@ -808,8 +808,8 @@ public class ImportHelper {
pluginIndependentItems.removeAll(pluginItems); pluginIndependentItems.removeAll(pluginItems);
for (SettingsHelper.SettingsItem item : pluginItems) { for (SettingsHelper.SettingsItem item : pluginItems) {
item.setShouldReplace(true); item.setShouldReplace(true);
if (item instanceof SettingsHelper.QuickActionSettingsItem) { if (item instanceof SettingsHelper.QuickActionsSettingsItem) {
plugin.quickActions = ((SettingsHelper.QuickActionSettingsItem) item).getItems(); plugin.quickActions = ((SettingsHelper.QuickActionsSettingsItem) item).getItems();
} }
if (item instanceof SettingsHelper.PoiUiFilterSettingsItem) { if (item instanceof SettingsHelper.PoiUiFilterSettingsItem) {
plugin.poiUIFilters = ((SettingsHelper.PoiUiFilterSettingsItem) item).getItems(); plugin.poiUIFilters = ((SettingsHelper.PoiUiFilterSettingsItem) item).getItems();

View file

@ -299,7 +299,7 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet {
} }
} }
if (!quickActions.isEmpty()) { if (!quickActions.isEmpty()) {
settingsItems.add(new SettingsHelper.QuickActionSettingsItem(app, quickActions)); settingsItems.add(new SettingsHelper.QuickActionsSettingsItem(app, quickActions));
} }
if (!poiUIFilters.isEmpty()) { if (!poiUIFilters.isEmpty()) {
settingsItems.add(new SettingsHelper.PoiUiFilterSettingsItem(app, poiUIFilters)); settingsItems.add(new SettingsHelper.PoiUiFilterSettingsItem(app, poiUIFilters));

View file

@ -328,7 +328,7 @@ public class ImportSettingsFragment extends BaseOsmAndFragment
} }
} }
if (!quickActions.isEmpty()) { if (!quickActions.isEmpty()) {
settingsItems.add(new SettingsHelper.QuickActionSettingsItem(app, quickActions)); settingsItems.add(new QuickActionsSettingsItem(app, quickActions));
} }
if (!poiUIFilters.isEmpty()) { if (!poiUIFilters.isEmpty()) {
settingsItems.add(new SettingsHelper.PoiUiFilterSettingsItem(app, poiUIFilters)); settingsItems.add(new SettingsHelper.PoiUiFilterSettingsItem(app, poiUIFilters));
@ -355,9 +355,9 @@ public class ImportSettingsFragment extends BaseOsmAndFragment
for (SettingsItem item : settingsItems) { for (SettingsItem item : settingsItems) {
if (item.getType().equals(SettingsItemType.PROFILE)) { if (item.getType().equals(SettingsItemType.PROFILE)) {
profiles.add(((ProfileSettingsItem) item).getModeBean()); profiles.add(((ProfileSettingsItem) item).getModeBean());
} else if (item.getType().equals(SettingsItemType.QUICK_ACTION)) { } else if (item.getType().equals(SettingsItemType.QUICK_ACTIONS)) {
quickActions.addAll(((QuickActionSettingsItem) item).getItems()); quickActions.addAll(((QuickActionsSettingsItem) item).getItems());
quickActions.addAll(((QuickActionSettingsItem) item).getDuplicateItems()); quickActions.addAll(((QuickActionsSettingsItem) item).getDuplicateItems());
} else if (item.getType().equals(SettingsItemType.POI_UI_FILTERS)) { } else if (item.getType().equals(SettingsItemType.POI_UI_FILTERS)) {
poiUIFilters.addAll(((PoiUiFilterSettingsItem) item).getItems()); poiUIFilters.addAll(((PoiUiFilterSettingsItem) item).getItems());
poiUIFilters.addAll(((PoiUiFilterSettingsItem) item).getDuplicateItems()); poiUIFilters.addAll(((PoiUiFilterSettingsItem) item).getDuplicateItems());