From 0e63df108023f059e834f31ba207ea8cf21c5e15 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 21 Apr 2020 14:16:28 +0300 Subject: [PATCH] Create JsonUtils and remove duplicate gpx constant --- OsmAnd/src/net/osmand/JsonUtils.java | 78 ++++++++++++++++ .../net/osmand/plus/CustomOsmandPlugin.java | 92 ++++--------------- OsmAnd/src/net/osmand/plus/CustomRegion.java | 21 +++-- .../net/osmand/plus/GpxSelectionHelper.java | 3 +- .../src/net/osmand/plus/MapMarkersHelper.java | 4 +- .../plus/activities/MapActivityActions.java | 8 +- .../plus/activities/SavingTrackHelper.java | 7 +- .../osmand/plus/activities/TrackActivity.java | 3 +- .../osmand/plus/download/CustomIndexItem.java | 8 +- .../plus/download/DownloadActivityType.java | 2 +- .../net/osmand/plus/helpers/GpxUiHelper.java | 7 +- .../net/osmand/plus/helpers/ImportHelper.java | 17 ++-- .../builders/WptPtMenuBuilder.java | 3 +- .../controllers/WptPtMenuController.java | 3 +- ...dTracksGroupBottomSheetDialogFragment.java | 2 +- .../CoordinateInputDialogFragment.java | 4 +- .../SaveAsTrackBottomSheetDialogFragment.java | 5 +- ...ptCategoriesBottomSheetDialogFragment.java | 3 +- .../adapters/MapMarkersGroupsAdapter.java | 2 +- .../adapters/TracksGroupsAdapter.java | 3 +- .../MeasurementToolFragment.java | 19 ++-- .../OnSaveCurrentTrackFragment.java | 7 +- .../plus/myplaces/AvailableGPXFragment.java | 4 +- .../plus/myplaces/TrackPointFragment.java | 3 +- .../osmand/plus/osmedit/OsmEditsFragment.java | 3 +- .../plus/settings/DataStorageHelper.java | 2 +- .../plus/wikivoyage/data/TravelDbHelper.java | 3 +- 27 files changed, 174 insertions(+), 142 deletions(-) create mode 100644 OsmAnd/src/net/osmand/JsonUtils.java diff --git a/OsmAnd/src/net/osmand/JsonUtils.java b/OsmAnd/src/net/osmand/JsonUtils.java new file mode 100644 index 0000000000..7c8bb278fe --- /dev/null +++ b/OsmAnd/src/net/osmand/JsonUtils.java @@ -0,0 +1,78 @@ +package net.osmand; + +import android.content.Context; +import android.content.res.Configuration; + +import net.osmand.util.Algorithms; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +public class JsonUtils { + + public static String getLocalizedResFromMap(Context ctx, Map localizedMap, String defVal) { + if (!Algorithms.isEmpty(localizedMap)) { + Configuration config = ctx.getResources().getConfiguration(); + String lang = config.locale.getLanguage(); + String name = localizedMap.get(lang); + if (Algorithms.isEmpty(name)) { + name = localizedMap.get(""); + } + if (!Algorithms.isEmpty(name)) { + return name; + } + } + return defVal; + } + + public static List jsonArrayToList(String key, JSONObject json) throws JSONException { + List items = new ArrayList<>(); + JSONArray jsonArray = json.optJSONArray(key); + if (jsonArray != null) { + for (int i = 0; i < jsonArray.length(); i++) { + items.add(jsonArray.getString(i)); + } + } + return items; + } + + public static Map getLocalizedMapFromJson(String key, JSONObject json) throws JSONException { + Map localizedMap = new HashMap<>(); + JSONObject jsonObject = json.optJSONObject(key); + if (jsonObject != null) { + for (Iterator it = jsonObject.keys(); it.hasNext(); ) { + String localeKey = it.next(); + String name = jsonObject.getString(localeKey); + localizedMap.put(localeKey, name); + } + } + return localizedMap; + } + + public static void writeStringListToJson(String key, JSONObject json, List items) throws JSONException { + if (!Algorithms.isEmpty(items)) { + JSONArray jsonArray = new JSONArray(); + for (String render : items) { + jsonArray.put(render); + } + json.put(key, jsonArray); + } + } + + public static void writeLocalizedMapToJson(String jsonKey, JSONObject json, Map map) throws JSONException { + if (!Algorithms.isEmpty(map)) { + JSONObject jsonObject = new JSONObject(); + for (Map.Entry entry : map.entrySet()) { + jsonObject.put(entry.getKey(), entry.getValue()); + } + json.put(jsonKey, jsonObject); + } + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java index 65a40a4bec..3d6fb4b321 100644 --- a/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java +++ b/OsmAnd/src/net/osmand/plus/CustomOsmandPlugin.java @@ -1,8 +1,6 @@ package net.osmand.plus; import android.app.Activity; -import android.content.Context; -import android.content.res.Configuration; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; @@ -10,6 +8,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import net.osmand.IndexConstants; +import net.osmand.JsonUtils; import net.osmand.PlatformUtil; import net.osmand.data.LatLon; import net.osmand.map.ITileSource; @@ -83,12 +82,12 @@ public class CustomOsmandPlugin extends OsmandPlugin { @Override public String getName() { - return getLocalizedResFromMap(app, names, app.getString(R.string.custom_osmand_plugin)); + return JsonUtils.getLocalizedResFromMap(app, names, app.getString(R.string.custom_osmand_plugin)); } @Override public String getDescription() { - return getLocalizedResFromMap(app, descriptions, null); + return JsonUtils.getLocalizedResFromMap(app, descriptions, null); } public String getResourceDirName() { @@ -310,10 +309,10 @@ public class CustomOsmandPlugin extends OsmandPlugin { } public void readAdditionalDataFromJson(JSONObject json) throws JSONException { - iconNames = getLocalizedMapFromJson("icon", json); - imageNames = getLocalizedMapFromJson("image", json); - names = getLocalizedMapFromJson("name", json); - descriptions = getLocalizedMapFromJson("description", json); + iconNames = JsonUtils.getLocalizedMapFromJson("icon", json); + imageNames = JsonUtils.getLocalizedMapFromJson("image", json); + names = JsonUtils.getLocalizedMapFromJson("name", json); + descriptions = JsonUtils.getLocalizedMapFromJson("description", json); JSONArray regionsJson = json.optJSONArray("regionsJson"); if (regionsJson != null) { @@ -322,10 +321,10 @@ public class CustomOsmandPlugin extends OsmandPlugin { } public void writeAdditionalDataToJson(JSONObject json) throws JSONException { - writeLocalizedMapToJson("icon", json, iconNames); - writeLocalizedMapToJson("image", json, imageNames); - writeLocalizedMapToJson("name", json, names); - writeLocalizedMapToJson("description", json, descriptions); + JsonUtils.writeLocalizedMapToJson("icon", json, iconNames); + JsonUtils.writeLocalizedMapToJson("image", json, imageNames); + JsonUtils.writeLocalizedMapToJson("name", json, names); + JsonUtils.writeLocalizedMapToJson("description", json, descriptions); JSONArray regionsJson = new JSONArray(); for (WorldRegion region : getFlatCustomRegions()) { @@ -352,14 +351,14 @@ public class CustomOsmandPlugin extends OsmandPlugin { } public void readDependentFilesFromJson(JSONObject json) throws JSONException { - rendererNames = jsonArrayToList("rendererNames", json); - routerNames = jsonArrayToList("routerNames", json); + rendererNames = JsonUtils.jsonArrayToList("rendererNames", json); + routerNames = JsonUtils.jsonArrayToList("routerNames", json); resourceDirName = json.optString("pluginResDir"); } public void writeDependentFilesJson(JSONObject json) throws JSONException { - writeStringListToJson("rendererNames", json, rendererNames); - writeStringListToJson("routerNames", json, routerNames); + JsonUtils.writeStringListToJson("rendererNames", json, rendererNames); + JsonUtils.writeStringListToJson("routerNames", json, routerNames); json.put("pluginResDir", resourceDirName); } @@ -427,7 +426,7 @@ public class CustomOsmandPlugin extends OsmandPlugin { try { return DownloadResources.findIndexItemsAt(app, latLon, type); } catch (IOException e) { - e.printStackTrace(); + LOG.error(e); } return Collections.emptyList(); } @@ -436,65 +435,6 @@ public class CustomOsmandPlugin extends OsmandPlugin { return DownloadResources.findIndexItemsAt(app, names, type, false, limit); } - public static String getLocalizedResFromMap(Context ctx, Map localizedMap, String defVal) { - if (!Algorithms.isEmpty(localizedMap)) { - Configuration config = ctx.getResources().getConfiguration(); - String lang = config.locale.getLanguage(); - String name = localizedMap.get(lang); - if (Algorithms.isEmpty(name)) { - name = localizedMap.get(""); - } - if (!Algorithms.isEmpty(name)) { - return name; - } - } - return defVal; - } - - public static List jsonArrayToList(String key, JSONObject json) throws JSONException { - List items = new ArrayList<>(); - JSONArray jsonArray = json.optJSONArray(key); - if (jsonArray != null) { - for (int i = 0; i < jsonArray.length(); i++) { - items.add(jsonArray.getString(i)); - } - } - return items; - } - - public static Map getLocalizedMapFromJson(String key, JSONObject json) throws JSONException { - Map localizedMap = new HashMap<>(); - JSONObject jsonObject = json.optJSONObject(key); - if (jsonObject != null) { - for (Iterator it = jsonObject.keys(); it.hasNext(); ) { - String localeKey = it.next(); - String name = jsonObject.getString(localeKey); - localizedMap.put(localeKey, name); - } - } - return localizedMap; - } - - public static void writeStringListToJson(String key, JSONObject json, List items) throws JSONException { - if (!Algorithms.isEmpty(items)) { - JSONArray jsonArray = new JSONArray(); - for (String render : items) { - jsonArray.put(render); - } - json.put(key, jsonArray); - } - } - - public static void writeLocalizedMapToJson(String jsonKey, JSONObject json, Map map) throws JSONException { - if (!Algorithms.isEmpty(map)) { - JSONObject jsonObject = new JSONObject(); - for (Map.Entry entry : map.entrySet()) { - jsonObject.put(entry.getKey(), entry.getValue()); - } - json.put(jsonKey, jsonObject); - } - } - public interface PluginItemsListener { void onItemsRemoved(); diff --git a/OsmAnd/src/net/osmand/plus/CustomRegion.java b/OsmAnd/src/net/osmand/plus/CustomRegion.java index fe1c2d86b8..1838ca0c97 100644 --- a/OsmAnd/src/net/osmand/plus/CustomRegion.java +++ b/OsmAnd/src/net/osmand/plus/CustomRegion.java @@ -2,6 +2,7 @@ package net.osmand.plus; import androidx.annotation.ColorInt; +import net.osmand.JsonUtils; import net.osmand.PlatformUtil; import net.osmand.map.WorldRegion; import net.osmand.plus.download.CustomIndexItem; @@ -78,7 +79,7 @@ public class CustomRegion extends WorldRegion { region.parentPath = path.substring(0, index); } - region.names = CustomOsmandPlugin.getLocalizedMapFromJson("name", object); + region.names = JsonUtils.getLocalizedMapFromJson("name", object); if (!Algorithms.isEmpty(region.names)) { region.regionName = region.names.get(""); region.regionNameEn = region.names.get(""); @@ -86,8 +87,8 @@ public class CustomRegion extends WorldRegion { region.regionNameLocale = region.names.get(""); } - region.icons = CustomOsmandPlugin.getLocalizedMapFromJson("icon", object); - region.headers = CustomOsmandPlugin.getLocalizedMapFromJson("header", object); + region.icons = JsonUtils.getLocalizedMapFromJson("icon", object); + region.headers = JsonUtils.getLocalizedMapFromJson("header", object); region.headerButton = object.optString("header-button", null); region.downloadItemsJson = object.optJSONArray("items"); @@ -119,10 +120,10 @@ public class CustomRegion extends WorldRegion { String downloadUrl = itemJson.optString("downloadurl"); String size = new DecimalFormat("#.#").format(containerSize / (1024f * 1024f)); - List descrImageUrl = CustomOsmandPlugin.jsonArrayToList("image-description-url", itemJson); - Map indexNames = CustomOsmandPlugin.getLocalizedMapFromJson("name", itemJson); - Map descriptions = CustomOsmandPlugin.getLocalizedMapFromJson("description", itemJson); - Map webButtonText = CustomOsmandPlugin.getLocalizedMapFromJson("web-button-text", itemJson); + List descrImageUrl = JsonUtils.jsonArrayToList("image-description-url", itemJson); + Map indexNames = JsonUtils.getLocalizedMapFromJson("name", itemJson); + Map descriptions = JsonUtils.getLocalizedMapFromJson("description", itemJson); + Map webButtonText = JsonUtils.getLocalizedMapFromJson("web-button-text", itemJson); DownloadActivityType type = DownloadActivityType.getIndexType(indexType); if (type != null) { @@ -161,9 +162,9 @@ public class CustomRegion extends WorldRegion { jsonObject.putOpt("subfolder", subfolder); jsonObject.putOpt("header-button", headerButton); - CustomOsmandPlugin.writeLocalizedMapToJson("name", jsonObject, names); - CustomOsmandPlugin.writeLocalizedMapToJson("icon", jsonObject, icons); - CustomOsmandPlugin.writeLocalizedMapToJson("header", jsonObject, headers); + JsonUtils.writeLocalizedMapToJson("name", jsonObject, names); + JsonUtils.writeLocalizedMapToJson("icon", jsonObject, icons); + JsonUtils.writeLocalizedMapToJson("header", jsonObject, headers); jsonObject.putOpt("items", downloadItemsJson); diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java index ccbbbaad7a..ee2e00b632 100644 --- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java +++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java @@ -17,6 +17,7 @@ import net.osmand.GPXUtilities.Track; import net.osmand.GPXUtilities.TrkSegment; import net.osmand.GPXUtilities.WptPt; import net.osmand.IProgress; +import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.data.LatLon; import net.osmand.plus.GPXDatabase.GpxDataItem; @@ -241,7 +242,7 @@ public class GpxSelectionHelper { if (i >= 0) { name = name.substring(i + 1); } - if (name.toLowerCase().endsWith(".gpx")) { + if (name.toLowerCase().endsWith(IndexConstants.GPX_FILE_EXT)) { name = name.substring(0, name.length() - 4); } name = name.replace('_', ' '); diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index e27707b5e8..426161870c 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -1009,10 +1009,10 @@ public class MapMarkersHelper { if (!dir.exists()) { dir.mkdirs(); } - File fout = new File(dir, fileName + ".gpx"); + File fout = new File(dir, fileName + IndexConstants.GPX_FILE_EXT); int ind = 1; while (fout.exists()) { - fout = new File(dir, fileName + "_" + (++ind) + ".gpx"); + fout = new File(dir, fileName + "_" + (++ind) + IndexConstants.GPX_FILE_EXT); } GPXFile file = new GPXFile(Version.getFullVersion(ctx)); for (MapMarker marker : mapMarkers) { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 3fb019b46e..af66cc42ff 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -83,6 +83,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; +import static net.osmand.IndexConstants.GPX_FILE_EXT; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_CONFIGURE_MAP_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_CONFIGURE_SCREEN_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_DASHBOARD_ID; @@ -111,7 +112,6 @@ import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_S import static net.osmand.plus.ContextMenuAdapter.PROFILES_CHOSEN_PROFILE_TAG; import static net.osmand.plus.ContextMenuAdapter.PROFILES_CONTROL_BUTTON_TAG; import static net.osmand.plus.ContextMenuAdapter.PROFILES_NORMAL_PROFILE_TAG; -import static net.osmand.plus.helpers.ImportHelper.GPX_SUFFIX; public class MapActivityActions implements DialogProvider { @@ -282,8 +282,8 @@ public class MapActivityActions implements DialogProvider { fileDir.mkdirs(); File toSave = fileDir; if (name.length() > 0) { - if (!name.endsWith(GPX_SUFFIX)) { - name += GPX_SUFFIX; + if (!name.endsWith(GPX_FILE_EXT)) { + name += GPX_FILE_EXT; } toSave = new File(fileDir, name); } @@ -320,7 +320,7 @@ public class MapActivityActions implements DialogProvider { if (params.length > 0) { File file = params[0]; String fileName = file.getName(); - GPXFile gpx = app.getRoutingHelper().generateGPXFileWithRoute(fileName.substring(0,fileName.length()-GPX_SUFFIX.length())); + GPXFile gpx = app.getRoutingHelper().generateGPXFileWithRoute(fileName.substring(0,fileName.length()-GPX_FILE_EXT.length())); GPXUtilities.writeGpxFile(file, gpx); return app.getString(R.string.route_successfully_saved_at, file.getName()); } diff --git a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java index 52fe084e2e..ddf23106c7 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java @@ -5,6 +5,7 @@ import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.text.format.DateFormat; +import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.data.LatLon; import net.osmand.plus.GPXDatabase.GpxDataItem; @@ -209,7 +210,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper { // save file for (final String f : data.keySet()) { log.debug("Filename: " + f); - File fout = new File(dir, f + ".gpx"); //$NON-NLS-1$ + File fout = new File(dir, f + IndexConstants.GPX_FILE_EXT); if (!data.get(f).isEmpty()) { WptPt pt = data.get(f).findPointToShow(); String fileName = f + "_" + new SimpleDateFormat("HH-mm_EEE", Locale.US).format(new Date(pt.time)); //$NON-NLS-1$ @@ -227,10 +228,10 @@ public class SavingTrackHelper extends SQLiteOpenHelper { } } filenames.add(fileName); - fout = new File(dir, fileName + ".gpx"); //$NON-NLS-1$ + fout = new File(dir, fileName + IndexConstants.GPX_FILE_EXT); int ind = 1; while (fout.exists()) { - fout = new File(dir, fileName + "_" + (++ind) + ".gpx"); //$NON-NLS-1$ //$NON-NLS-2$ + fout = new File(dir, fileName + "_" + (++ind) + IndexConstants.GPX_FILE_EXT); //$NON-NLS-1$ } } diff --git a/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java b/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java index 522c334bb3..6659ca3ad2 100644 --- a/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/TrackActivity.java @@ -23,6 +23,7 @@ import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.TrkSegment; import net.osmand.GPXUtilities.WptPt; +import net.osmand.IndexConstants; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.data.QuadRect; @@ -92,7 +93,7 @@ public class TrackActivity extends TabActivity { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { if (file != null) { - String fn = file.getName().replace(".gpx", "").replace("/", " ").replace("_", " "); + String fn = file.getName().replace(IndexConstants.GPX_FILE_EXT, "").replace("/", " ").replace("_", " "); actionBar.setTitle(fn); } else { actionBar.setTitle(getString(R.string.shared_string_currently_recording_track)); diff --git a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java index e48179d1e0..6bacdb237a 100644 --- a/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/CustomIndexItem.java @@ -4,8 +4,8 @@ import android.content.Context; import androidx.annotation.NonNull; +import net.osmand.JsonUtils; import net.osmand.map.OsmandRegions; -import net.osmand.plus.CustomOsmandPlugin; import net.osmand.plus.OsmandApplication; import net.osmand.util.Algorithms; @@ -73,7 +73,7 @@ public class CustomIndexItem extends IndexItem { @Override public String getVisibleName(Context ctx, OsmandRegions osmandRegions, boolean includingParent) { String name = super.getVisibleName(ctx, osmandRegions, includingParent); - return CustomOsmandPlugin.getLocalizedResFromMap(ctx, names, name); + return JsonUtils.getLocalizedResFromMap(ctx, names, name); } public List getDescriptionImageUrl() { @@ -82,7 +82,7 @@ public class CustomIndexItem extends IndexItem { public String getLocalizedDescription(Context ctx) { String description = super.getDescription(); - return CustomOsmandPlugin.getLocalizedResFromMap(ctx, descriptions, description); + return JsonUtils.getLocalizedResFromMap(ctx, descriptions, description); } public String getWebUrl() { @@ -90,7 +90,7 @@ public class CustomIndexItem extends IndexItem { } public String getWebButtonText(Context ctx) { - return CustomOsmandPlugin.getLocalizedResFromMap(ctx, webButtonTexts, null); + return JsonUtils.getLocalizedResFromMap(ctx, webButtonTexts, null); } public static class CustomIndexItemBuilder { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index 7bde53b277..b5671e89c8 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -144,7 +144,7 @@ public class DownloadActivityType { } else if (GPX_FILE == this) { return fileName.endsWith(IndexConstants.GPX_FILE_EXT); } else if (SQLITE_FILE == this) { - return fileName.endsWith(IndexConstants.GPX_FILE_EXT); + return fileName.endsWith(IndexConstants.SQLITE_EXT); } return false; } diff --git a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java index a44e460a27..efb435cc5e 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/GpxUiHelper.java @@ -115,6 +115,7 @@ import java.util.List; import java.util.Map; import static com.github.mikephil.charting.components.XAxis.XAxisPosition.BOTTOM; +import static net.osmand.IndexConstants.GPX_FILE_EXT; import static net.osmand.binary.RouteDataObject.HEIGHT_UNDEFINED; import static net.osmand.plus.OsmAndFormatter.FEET_IN_ONE_METER; import static net.osmand.plus.OsmAndFormatter.METERS_IN_KILOMETER; @@ -307,8 +308,8 @@ public class GpxUiHelper { public static String getGpxTitle(String fileName) { String s = fileName; - if (s.toLowerCase().endsWith(".gpx")) { - s = s.substring(0, s.length() - ".gpx".length()); + if (s.toLowerCase().endsWith(GPX_FILE_EXT)) { + s = s.substring(0, s.length() - GPX_FILE_EXT.length()); } s = s.replace('_', ' '); return s; @@ -901,7 +902,7 @@ public class GpxUiHelper { File[] files = dir.listFiles(); if (files != null) { for (File f : files) { - if (f.getName().toLowerCase().endsWith(".gpx")) { //$NON-NLS-1$ + if (f.getName().toLowerCase().endsWith(GPX_FILE_EXT)) { list.add(new GPXInfo(absolutePath ? f.getAbsolutePath() : parent + f.getName(), f.lastModified(), f.length())); } else if (f.isDirectory()) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index 084e58ae08..1c2c1a4b88 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -84,6 +84,7 @@ import java.util.Map; import java.util.zip.ZipInputStream; import static android.app.Activity.RESULT_OK; +import static net.osmand.IndexConstants.GPX_FILE_EXT; import static net.osmand.IndexConstants.OSMAND_SETTINGS_FILE_EXT; import static net.osmand.IndexConstants.RENDERER_INDEX_EXT; import static net.osmand.IndexConstants.ROUTING_FILE_EXT; @@ -99,7 +100,7 @@ public class ImportHelper { public final static Log log = PlatformUtil.getLog(ImportHelper.class); public static final String KML_SUFFIX = ".kml"; public static final String KMZ_SUFFIX = ".kmz"; - public static final String GPX_SUFFIX = ".gpx"; + private final AppCompatActivity activity; private final OsmandApplication app; private final OsmandMapTileView mapView; @@ -146,8 +147,8 @@ public class ImportHelper { boolean isOsmandSubdir = isSubDirectory(app.getAppPath(IndexConstants.GPX_INDEX_DIR), new File(contentUri.getPath())); if (!isOsmandSubdir && name != null) { String nameLC = name.toLowerCase(); - if (nameLC.endsWith(GPX_SUFFIX)) { - name = name.substring(0, name.length() - 4) + GPX_SUFFIX; + if (nameLC.endsWith(GPX_FILE_EXT)) { + name = name.substring(0, name.length() - 4) + GPX_FILE_EXT; handleGpxImport(contentUri, name, true, useImportDir); return true; } else if (nameLC.endsWith(KML_SUFFIX)) { @@ -1046,14 +1047,14 @@ public class ImportHelper { private File getFileToSave(final String fileName, final File importDir, final WptPt pt) { final StringBuilder builder = new StringBuilder(fileName); if ("".equals(fileName)) { - builder.append("import_").append(new SimpleDateFormat("HH-mm_EEE", Locale.US).format(new Date(pt.time))).append(GPX_SUFFIX); //$NON-NLS-1$ + builder.append("import_").append(new SimpleDateFormat("HH-mm_EEE", Locale.US).format(new Date(pt.time))).append(GPX_FILE_EXT); //$NON-NLS-1$ } if (fileName.endsWith(KML_SUFFIX)) { - builder.replace(builder.length() - KML_SUFFIX.length(), builder.length(), GPX_SUFFIX); + builder.replace(builder.length() - KML_SUFFIX.length(), builder.length(), GPX_FILE_EXT); } else if (fileName.endsWith(KMZ_SUFFIX)) { - builder.replace(builder.length() - KMZ_SUFFIX.length(), builder.length(), GPX_SUFFIX); - } else if (!fileName.endsWith(GPX_SUFFIX)) { - builder.append(GPX_SUFFIX); + builder.replace(builder.length() - KMZ_SUFFIX.length(), builder.length(), GPX_FILE_EXT); + } else if (!fileName.endsWith(GPX_FILE_EXT)) { + builder.append(GPX_FILE_EXT); } return new File(importDir, builder.toString()); } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/WptPtMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/WptPtMenuBuilder.java index 68c19e8319..d9b2f6c7b9 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/WptPtMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/WptPtMenuBuilder.java @@ -11,6 +11,7 @@ import androidx.core.content.ContextCompat; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.WptPt; +import net.osmand.IndexConstants; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.plus.GpxSelectionHelper; @@ -113,7 +114,7 @@ public class WptPtMenuBuilder extends MenuBuilder { if (points.size() > 0) { String title = view.getContext().getString(R.string.context_menu_points_of_group); File file = new File(gpx.path); - String gpxName = file.getName().replace(".gpx", "").replace("/", " ").replace("_", " "); + String gpxName = file.getName().replace(IndexConstants.GPX_FILE_EXT, "").replace("/", " ").replace("_", " "); int color = getPointColor(wpt, getFileColor(selectedGpxFile)); buildRow(view, app.getUIUtilities().getPaintedIcon(R.drawable.ic_type_waypoints_group, color), null, title, 0, gpxName, true, getCollapsableWaypointsView(view.getContext(), true, gpx, wpt), diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/WptPtMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/WptPtMenuController.java index d692c153a4..d94079c977 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/WptPtMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/WptPtMenuController.java @@ -6,6 +6,7 @@ import androidx.annotation.NonNull; import androidx.core.content.ContextCompat; import net.osmand.GPXUtilities.WptPt; +import net.osmand.IndexConstants; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.plus.GpxSelectionHelper; @@ -115,7 +116,7 @@ public class WptPtMenuController extends MenuController { sb.append(", "); if (selectedGpxFile != null) { File file = new File(selectedGpxFile.getGpxFile().path); - String gpxName = file.getName().replace(".gpx", "").replace("/", " ").replace("_", " "); + String gpxName = file.getName().replace(IndexConstants.GPX_FILE_EXT, "").replace("/", " ").replace("_", " "); sb.append(gpxName); } return sb.toString(); diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java index d1005e35e8..b01548be00 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java @@ -172,7 +172,7 @@ public class AddTracksGroupBottomSheetDialogFragment extends AddGroupBottomSheet String sub = gpxSubfolder.length() == 0 ? gpxFile.getName() : gpxSubfolder + "/" + gpxFile.getName(); processGPXFolder(gpxFile, sub); - } else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) { + } else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(IndexConstants.GPX_FILE_EXT)) { GpxDataItem item = dbHelper.getItem(gpxFile, gpxDataItemCallback); publishProgress(item); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java index a4367a5cc0..232b73084c 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java @@ -1494,10 +1494,10 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm if (!dir.exists()) { dir.mkdirs(); } - File fout = new File(dir, fileName + ".gpx"); + File fout = new File(dir, fileName + IndexConstants.GPX_FILE_EXT); int ind = 1; while (fout.exists()) { - fout = new File(dir, fileName + "_" + (++ind) + ".gpx"); + fout = new File(dir, fileName + "_" + (++ind) + IndexConstants.GPX_FILE_EXT); } GPXUtilities.writeGpxFile(fout, gpx); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java index 1974f61cc7..5cdb3571c2 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java @@ -32,7 +32,6 @@ import net.osmand.plus.widgets.OsmandTextFieldBoxes; import java.io.File; import java.util.Date; -import static net.osmand.plus.helpers.ImportHelper.GPX_SUFFIX; import static net.osmand.plus.mapmarkers.CoordinateInputDialogFragment.ADDED_POINTS_NUMBER_KEY; public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragment { @@ -98,11 +97,11 @@ public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragm Date date = new Date(); final String suggestedName = app.getString(R.string.markers) + "_" + DateFormat.format("yyyy-MM-dd", date).toString(); String displayedName = suggestedName; - File fout = new File(dir, suggestedName + GPX_SUFFIX); + File fout = new File(dir, suggestedName + IndexConstants.GPX_FILE_EXT); int ind = 1; while (fout.exists()) { displayedName = suggestedName + "_" + (++ind); - fout = new File(dir, displayedName + GPX_SUFFIX); + fout = new File(dir, displayedName + IndexConstants.GPX_FILE_EXT); } final EditText nameEditText = (EditText) mainView.findViewById(R.id.name_edit_text); nameEditText.setText(displayedName); diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/SelectWptCategoriesBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/SelectWptCategoriesBottomSheetDialogFragment.java index f5255f0d25..0052bf655e 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/SelectWptCategoriesBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/SelectWptCategoriesBottomSheetDialogFragment.java @@ -10,6 +10,7 @@ import androidx.annotation.Nullable; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.WptPt; +import net.osmand.IndexConstants; import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.MapMarkersHelper; @@ -161,7 +162,7 @@ public class SelectWptCategoriesBottomSheetDialogFragment extends MenuBottomShee private String getGpxName(GPXFile gpxFile) { return new File(gpxFile.path).getName() - .replace(".gpx", "") + .replace(IndexConstants.GPX_FILE_EXT, "") .replace("/", " ") .replace("_", " "); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersGroupsAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersGroupsAdapter.java index c4f95fc9fa..0cd5421c3d 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersGroupsAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersGroupsAdapter.java @@ -404,7 +404,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter