From 2eb9b7d143027c320974bee848288c8e747a3a0b Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 25 Feb 2020 11:09:33 +0200 Subject: [PATCH 1/7] Fix possible npe (cherry picked from commit 71c0167b1976c24b5d26ad2ad4218151b3fe2aa2) --- OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 00a155c20f..bdea697a31 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -728,7 +728,10 @@ public class GpxSelectionHelper { } public List getPointsToDisplay() { - return joinSegments ? gpxFile.getGeneralTrack().segments : processedPointsToDisplay; + if (joinSegments && gpxFile != null && gpxFile.getGeneralTrack() != null) { + return gpxFile.getGeneralTrack().segments; + } + return processedPointsToDisplay; } public List getModifiablePointsToDisplay() { From 318ffc541f4549ba5c61055b0c34892e0e0aeb47 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 25 Feb 2020 12:07:01 +0200 Subject: [PATCH 2/7] Add check for general track (cherry picked from commit 10c4e06aa1f167b12f397a6714fe09bcbdd86b04) --- OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index bdea697a31..41d62c6c49 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -35,6 +35,7 @@ import org.json.JSONObject; import java.io.File; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -599,6 +600,9 @@ public class GpxSelectionHelper { if (dataItem.getColor() != 0) { gpx.setColor(dataItem.getColor()); } + if (gpx.getGeneralTrack() == null) { + app.getGpxDbHelper().updateJoinSegments(dataItem, false); + } sf.setJoinSegments(dataItem.isJoinSegments()); } sf.setGpxFile(gpx, app); @@ -728,8 +732,12 @@ public class GpxSelectionHelper { } public List getPointsToDisplay() { - if (joinSegments && gpxFile != null && gpxFile.getGeneralTrack() != null) { - return gpxFile.getGeneralTrack().segments; + if (joinSegments) { + if (gpxFile != null && gpxFile.getGeneralTrack() != null) { + return gpxFile.getGeneralTrack().segments; + } else { + return Collections.emptyList(); + } } return processedPointsToDisplay; } From af95ad96b67b290b02342dd934375a3ba2edb1b3 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 25 Feb 2020 12:12:54 +0200 Subject: [PATCH 3/7] Remove unnecessary changes (cherry picked from commit 6e2285874984ead67ee44570e5634d06e3822fe0) --- OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index 41d62c6c49..59d100e619 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -600,9 +600,6 @@ public class GpxSelectionHelper { if (dataItem.getColor() != 0) { gpx.setColor(dataItem.getColor()); } - if (gpx.getGeneralTrack() == null) { - app.getGpxDbHelper().updateJoinSegments(dataItem, false); - } sf.setJoinSegments(dataItem.isJoinSegments()); } sf.setGpxFile(gpx, app); From e8541e7103e163bf34db6d151f4ee0bb7051b2a3 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 25 Feb 2020 14:14:45 +0200 Subject: [PATCH 4/7] Fix dateFormat for different threads (cherry picked from commit a60584d0d4f6c76a446cd09569a116a2812002c4) --- OsmAnd/src/net/osmand/plus/resources/ResourceManager.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 1d8c687035..5eb9feb502 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -219,7 +219,6 @@ public class ResourceManager { private HandlerThread renderingBufferImageThread; protected boolean internetIsNotAccessible = false; - private java.text.DateFormat dateFormat; private boolean depthContours; public ResourceManager(OsmandApplication context) { @@ -237,7 +236,6 @@ public class ResourceManager { renderingBufferImageThread.start(); tileDownloader = MapTileDownloader.getInstance(Version.getFullVersion(context)); - dateFormat = DateFormat.getDateFormat(context); resetStoreDirectory(); WindowManager mgr = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); @@ -296,7 +294,7 @@ public class ResourceManager { } public java.text.DateFormat getDateFormat() { - return dateFormat; + return DateFormat.getDateFormat(context); } public OsmandApplication getContext() { @@ -405,6 +403,7 @@ public class ResourceManager { if (file.exists() && file.canRead()) { File[] lf = file.listFiles(); if (lf != null) { + java.text.DateFormat dateFormat = DateFormat.getDateFormat(context); for (File f : lf) { if (f.isDirectory()) { String lang = f.getName().replace("-tts", ""); @@ -430,6 +429,7 @@ public class ResourceManager { if (file.exists() && file.canRead()) { File[] lf = file.listFiles(); if (lf != null) { + java.text.DateFormat dateFormat = DateFormat.getDateFormat(context); for (File f : lf) { if (!f.isDirectory()) { indexFileNames.put(f.getName(), dateFormat.format(f.lastModified())); @@ -671,6 +671,7 @@ public class ResourceManager { if (hasWorldBasemap && worldBasemapMini != null) { files.remove(worldBasemapMini); } + java.text.DateFormat dateFormat = DateFormat.getDateFormat(context); for (File f : files) { progress.startTask(context.getString(R.string.indexing_map) + " " + f.getName(), -1); //$NON-NLS-1$ try { From 90651bd29045dc28f75d04565ae90ced925201bf Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 25 Feb 2020 15:07:22 +0200 Subject: [PATCH 5/7] Remove unnecessary changes (cherry picked from commit 8194d599c504d039f85279a71e7def6eee468cd6) --- OsmAnd/src/net/osmand/plus/resources/ResourceManager.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 5eb9feb502..dca3f943c2 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -403,7 +403,7 @@ public class ResourceManager { if (file.exists() && file.canRead()) { File[] lf = file.listFiles(); if (lf != null) { - java.text.DateFormat dateFormat = DateFormat.getDateFormat(context); + java.text.DateFormat dateFormat = getDateFormat(); for (File f : lf) { if (f.isDirectory()) { String lang = f.getName().replace("-tts", ""); @@ -429,7 +429,7 @@ public class ResourceManager { if (file.exists() && file.canRead()) { File[] lf = file.listFiles(); if (lf != null) { - java.text.DateFormat dateFormat = DateFormat.getDateFormat(context); + java.text.DateFormat dateFormat = getDateFormat(); for (File f : lf) { if (!f.isDirectory()) { indexFileNames.put(f.getName(), dateFormat.format(f.lastModified())); @@ -671,7 +671,7 @@ public class ResourceManager { if (hasWorldBasemap && worldBasemapMini != null) { files.remove(worldBasemapMini); } - java.text.DateFormat dateFormat = DateFormat.getDateFormat(context); + java.text.DateFormat dateFormat = getDateFormat(); for (File f : files) { progress.startTask(context.getString(R.string.indexing_map) + " " + f.getName(), -1); //$NON-NLS-1$ try { From 1d98082fac711a6a390d45df64e514f92b66006f Mon Sep 17 00:00:00 2001 From: veliymolfar Date: Tue, 25 Feb 2020 16:56:16 +0200 Subject: [PATCH 6/7] 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 7/7] 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)) {