From 1d98082fac711a6a390d45df64e514f92b66006f Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Tue, 25 Feb 2020 16:56:16 +0200 Subject: [PATCH 1/2] import fix --- .../src/net/osmand/plus/SettingsHelper.java | 44 +++++++++---------- .../settings/ExportProfileBottomSheet.java | 8 ++-- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index af26bfaa5b..9459e4b42b 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -245,7 +245,7 @@ public class SettingsHelper { public abstract static class CollectionSettingsItem extends SettingsItem { - protected List items; + protected List items = new ArrayList<>(); protected List duplicateItems = new ArrayList<>(); protected List existingItems; @@ -931,7 +931,7 @@ public class SettingsHelper { @NonNull @Override SettingsItemReader getReader() { - return new SettingsItemReader(this) { + return new SettingsItemReader(this) { @Override public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException { StringBuilder buf = new StringBuilder(); @@ -950,7 +950,6 @@ public class SettingsHelper { } final JSONObject json; try { - items = new ArrayList<>(); Gson gson = new Gson(); Type type = new TypeToken>() { }.getType(); @@ -979,7 +978,7 @@ public class SettingsHelper { @NonNull @Override SettingsItemWriter getWriter() { - return new SettingsItemWriter(this) { + return new SettingsItemWriter(this) { @Override public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException { JSONObject json = new JSONObject(); @@ -1098,7 +1097,7 @@ public class SettingsHelper { @NonNull @Override SettingsItemReader getReader() { - return new SettingsItemReader(this) { + return new SettingsItemReader(this) { @Override public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException { StringBuilder buf = new StringBuilder(); @@ -1117,7 +1116,6 @@ public class SettingsHelper { } final JSONObject json; try { - items = new ArrayList<>(); json = new JSONObject(jsonStr); JSONArray jsonArray = json.getJSONArray("items"); Gson gson = new Gson(); @@ -1148,7 +1146,7 @@ public class SettingsHelper { @NonNull @Override SettingsItemWriter getWriter() { - return new SettingsItemWriter(this) { + return new SettingsItemWriter(this) { @Override public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException { JSONObject json = new JSONObject(); @@ -1193,15 +1191,13 @@ public class SettingsHelper { public MapSourcesSettingsItem(@NonNull OsmandApplication app, @NonNull List items) { super(SettingsItemType.MAP_SOURCES, items); this.app = app; - Collection values = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true)).values(); - existingItemsNames = new ArrayList(values); + existingItemsNames = new ArrayList<>(app.getSettings().getTileSourceEntries().values()); } MapSourcesSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { super(SettingsItemType.MAP_SOURCES, json); this.app = app; - Collection values = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true)).values(); - existingItemsNames = new ArrayList(values); + existingItemsNames = new ArrayList<>(app.getSettings().getTileSourceEntries().values()); } @Override @@ -1301,7 +1297,7 @@ public class SettingsHelper { @NonNull @Override SettingsItemReader getReader() { - return new SettingsItemReader(this) { + return new SettingsItemReader(this) { @Override public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException { StringBuilder buf = new StringBuilder(); @@ -1320,7 +1316,6 @@ public class SettingsHelper { } final JSONObject json; try { - items = new ArrayList<>(); json = new JSONObject(jsonStr); JSONArray jsonArray = json.getJSONArray("items"); for (int i = 0; i < jsonArray.length(); i++) { @@ -1361,7 +1356,7 @@ public class SettingsHelper { @NonNull @Override SettingsItemWriter getWriter() { - return new SettingsItemWriter(this) { + return new SettingsItemWriter(this) { @Override public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException { JSONObject json = new JSONObject(); @@ -1503,7 +1498,7 @@ public class SettingsHelper { @NonNull @Override SettingsItemReader getReader() { - return new SettingsItemReader(this) { + return new SettingsItemReader(this) { @Override public void readFromStream(@NonNull InputStream inputStream) throws IOException, IllegalArgumentException { StringBuilder buf = new StringBuilder(); @@ -1522,7 +1517,6 @@ public class SettingsHelper { } final JSONObject json; try { - items = new ArrayList<>(); json = new JSONObject(jsonStr); JSONArray jsonArray = json.getJSONArray("items"); for (int i = 0; i < jsonArray.length(); i++) { @@ -1553,7 +1547,7 @@ public class SettingsHelper { @NonNull @Override SettingsItemWriter getWriter() { - return new SettingsItemWriter(this) { + return new SettingsItemWriter(this) { @Override public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException { JSONObject json = new JSONObject(); @@ -1599,9 +1593,15 @@ public class SettingsHelper { JSONArray itemsJson = json.getJSONArray("items"); for (int i = 0; i < itemsJson.length(); i++) { JSONObject itemJson = itemsJson.getJSONObject(i); - SettingsItem item = createItem(itemJson); - if (item != null) { - items.add(item); + SettingsItem item = null; + try { + item = createItem(itemJson); + } catch (IllegalArgumentException e) { + LOG.error("Error creating item from json: " + itemJson, e); + } finally { + if (item != null) { + items.add(item); + } } } if (items.size() == 0) { @@ -1811,7 +1811,7 @@ public class SettingsHelper { private SettingsImportListener listener; private SettingsImporter importer; - private List items; + private List items = new ArrayList<>(); private List processedItems = new ArrayList<>(); private SettingsItem currentItem; private AlertDialog dialog; @@ -1972,7 +1972,7 @@ public class SettingsHelper { } } - private void finishImport(@Nullable SettingsImportListener listener, boolean success, boolean empty, List items) { + private void finishImport(@Nullable SettingsImportListener listener, boolean success, boolean empty, @NonNull List items) { importing = false; importSuspended = false; importTask = null; diff --git a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java index ba52799f3b..0a29c604c1 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java @@ -45,9 +45,9 @@ import org.apache.commons.logging.Log; import java.io.File; import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { @@ -205,9 +205,9 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { } List iTileSources = new ArrayList<>(); - final LinkedHashMap tileSourceEntries = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true)); - for (Map.Entry entry : tileSourceEntries.entrySet()) { - File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + entry.getKey()); + Set tileSourceKeys = app.getSettings().getTileSourceEntries(true).keySet(); + for (String key : tileSourceKeys) { + File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + key); if (f != null) { ITileSource template; if (f.getName().endsWith(SQLiteTileSource.EXT)) { From 0eecb08a4a0606b8a4e279b857664d6a12c67ed5 Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Tue, 25 Feb 2020 18:19:25 +0200 Subject: [PATCH 2/2] refactor, fix null items list --- OsmAnd/src/net/osmand/plus/SettingsHelper.java | 15 ++++++++------- .../plus/settings/ExportProfileBottomSheet.java | 6 +++--- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 9459e4b42b..de63be3c15 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -1593,15 +1593,14 @@ public class SettingsHelper { JSONArray itemsJson = json.getJSONArray("items"); for (int i = 0; i < itemsJson.length(); i++) { JSONObject itemJson = itemsJson.getJSONObject(i); - SettingsItem item = null; + SettingsItem item; try { item = createItem(itemJson); - } catch (IllegalArgumentException e) { - LOG.error("Error creating item from json: " + itemJson, e); - } finally { if (item != null) { items.add(item); } + } catch (IllegalArgumentException e) { + LOG.error("Error creating item from json: " + itemJson, e); } } if (items.size() == 0) { @@ -1864,10 +1863,12 @@ public class SettingsHelper { } @Override - protected void onPostExecute(List items) { - this.items = items; + protected void onPostExecute(@Nullable List items) { + if (items != null) { + this.items = items; + } if (collectOnly) { - listener.onSettingsImportFinished(true, false, items); + listener.onSettingsImportFinished(true, false, this.items); } else { if (items != null && items.size() > 0) { processNextItem(); diff --git a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java index 0a29c604c1..2c58ee66a6 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java @@ -205,9 +205,9 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { } List iTileSources = new ArrayList<>(); - Set tileSourceKeys = app.getSettings().getTileSourceEntries(true).keySet(); - for (String key : tileSourceKeys) { - File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + key); + Set tileSourceNames = app.getSettings().getTileSourceEntries(true).keySet(); + for (String name : tileSourceNames) { + File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + name); if (f != null) { ITileSource template; if (f.getName().endsWith(SQLiteTileSource.EXT)) {