From 12fcff9af8c22ea9be343a961a8a7917cda20f9c Mon Sep 17 00:00:00 2001 From: max-klaus Date: Sun, 22 Mar 2020 12:24:01 +0300 Subject: [PATCH] Added subtypes to FileSettingsItem --- .../src/net/osmand/plus/SettingsHelper.java | 88 +++++++++++++++++-- .../settings/ExportProfileBottomSheet.java | 20 +++-- .../plus/settings/ImportSettingsFragment.java | 47 ++++++---- 3 files changed, 126 insertions(+), 29 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index b009910a2d..f8e825ab7e 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -901,18 +901,80 @@ public class SettingsHelper { public static class FileSettingsItem extends StreamSettingsItem { - private File file; - private String subtype; + public enum FileSubtype { + UNKNOWN("", null), + ROUTING_CONFIG("routing_config", IndexConstants.ROUTING_PROFILES_DIR), + RENDERING_STYLE("rendering_style", IndexConstants.RENDERERS_DIR), + OBF_MAP("obf_map", IndexConstants.MAPS_PATH), + TILES_MAP("tiles_map", IndexConstants.TILES_INDEX_DIR), + GPX("gpx", IndexConstants.GPX_INDEX_DIR), + VOICE("voice", IndexConstants.VOICE_INDEX_DIR); - public FileSettingsItem(@NonNull OsmandApplication app, @NonNull File file) { + private String subtypeName; + private String subtypeFolder; + + FileSubtype(String subtypeName, String subtypeFolder) { + this.subtypeName = subtypeName; + this.subtypeFolder = subtypeFolder; + } + + public String getSubtypeName() { + return subtypeName; + } + + public String getSubtypeFolder() { + return subtypeFolder; + } + + public static FileSubtype getSubtypeByName(@NonNull String name) { + for (FileSubtype subtype : FileSubtype.values()) { + if (name.equals(subtype.subtypeName)) { + return subtype; + } + } + return null; + } + + public static FileSubtype getSubtypeByFileName(@NonNull String fileName) { + String name = fileName.substring(1); + for (FileSubtype subtype : FileSubtype.values()) { + if (subtype != UNKNOWN && name.startsWith(subtype.subtypeFolder)) { + return subtype; + } + } + return UNKNOWN; + } + + @Override + public String toString() { + return subtypeName; + } + } + + private File file; + private FileSubtype subtype; + + public FileSettingsItem(@NonNull OsmandApplication app, @NonNull File file) throws IllegalArgumentException { super(SettingsItemType.FILE, file.getPath().replace(app.getAppPath(null).getPath(), "")); this.file = file; + this.subtype = FileSubtype.getSubtypeByFileName(getFileName()); + if (subtype == FileSubtype.UNKNOWN) { + throw new IllegalArgumentException("Unknown file subtype: " + getFileName()); + } + } + + public FileSettingsItem(@NonNull OsmandApplication app, @NonNull FileSubtype subtype, @NonNull File file) throws IllegalArgumentException { + super(SettingsItemType.FILE, file.getPath().replace(app.getAppPath(null).getPath(), "")); + this.file = file; + this.subtype = subtype; + if (subtype == FileSubtype.UNKNOWN) { + throw new IllegalArgumentException("Unknown file subtype: " + getFileName()); + } } FileSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { super(SettingsItemType.FILE, json); this.file = new File(app.getAppPath(null), name); - this.subtype = json.has("subtype") ? json.getString("subtype") : null; } @Override @@ -922,6 +984,22 @@ public class SettingsHelper { if (!Algorithms.isEmpty(fileName)) { name = fileName; } + String subtypeStr = json.has("subtype") ? json.getString("subtype") : null; + if (!Algorithms.isEmpty(subtypeStr)) { + subtype = FileSubtype.getSubtypeByName(subtypeStr); + } else if (!Algorithms.isEmpty(fileName)) { + subtype = FileSubtype.getSubtypeByFileName(fileName); + } else { + subtype = FileSubtype.UNKNOWN; + } + } + + @Override + void writeToJson(@NonNull JSONObject json) throws JSONException { + super.writeToJson(json); + if (subtype != null) { + json.put("subtype", subtype.getSubtypeName()); + } } @NonNull @@ -934,7 +1012,7 @@ public class SettingsHelper { return file; } - public String getSubtype() { + public FileSubtype getSubtype() { return subtype; } diff --git a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java index ac1a7970ee..13ab129d17 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java @@ -31,6 +31,8 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.SQLiteTileSource; import net.osmand.plus.SettingsHelper; +import net.osmand.plus.SettingsHelper.FileSettingsItem; +import net.osmand.plus.SettingsHelper.SettingsItem; import net.osmand.plus.UiUtilities; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; @@ -40,8 +42,8 @@ import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.quickaction.QuickAction; import net.osmand.plus.quickaction.QuickActionFactory; -import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; import net.osmand.plus.settings.ExportImportSettingsAdapter.Type; +import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; import org.apache.commons.logging.Log; @@ -240,7 +242,7 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { } else { template = TileSourceManager.createTileSourceTemplate(f); } - if (template != null && template.getUrlTemplate() != null) { + if (template.getUrlTemplate() != null) { iTileSources.add(template); } } @@ -269,8 +271,8 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { return dataList; } - private List prepareSettingsItemsForExport() { - List settingsItems = new ArrayList<>(); + private List prepareSettingsItemsForExport() { + List settingsItems = new ArrayList<>(); settingsItems.add(new SettingsHelper.ProfileSettingsItem(app, profile)); if (includeAdditionalData) { settingsItems.addAll(prepareAdditionalSettingsItems()); @@ -278,8 +280,8 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { return settingsItems; } - private List prepareAdditionalSettingsItems() { - List settingsItems = new ArrayList<>(); + private List prepareAdditionalSettingsItems() { + List settingsItems = new ArrayList<>(); List quickActions = new ArrayList<>(); List poiUIFilters = new ArrayList<>(); List tileSourceTemplates = new ArrayList<>(); @@ -293,7 +295,11 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { || object instanceof SQLiteTileSource) { tileSourceTemplates.add((ITileSource) object); } else if (object instanceof File) { - settingsItems.add(new SettingsHelper.FileSettingsItem(app, (File) object)); + try { + settingsItems.add(new FileSettingsItem(app, (File) object)); + } catch (IllegalArgumentException e) { + LOG.warn("Trying to export unsuported file type", e); + } } else if (object instanceof AvoidRoadInfo) { avoidRoads.add((AvoidRoadInfo) object); } diff --git a/OsmAnd/src/net/osmand/plus/settings/ImportSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/ImportSettingsFragment.java index f55c1b9291..f3e53c7d91 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ImportSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ImportSettingsFragment.java @@ -26,7 +26,6 @@ import androidx.fragment.app.FragmentManager; import com.google.android.material.appbar.CollapsingToolbarLayout; import net.osmand.AndroidUtils; -import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.map.ITileSource; import net.osmand.map.TileSourceManager; @@ -36,16 +35,25 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.SQLiteTileSource; import net.osmand.plus.SettingsHelper; -import net.osmand.plus.SettingsHelper.*; +import net.osmand.plus.SettingsHelper.AvoidRoadsSettingsItem; +import net.osmand.plus.SettingsHelper.FileSettingsItem; +import net.osmand.plus.SettingsHelper.FileSettingsItem.FileSubtype; +import net.osmand.plus.SettingsHelper.ImportAsyncTask; +import net.osmand.plus.SettingsHelper.ImportType; +import net.osmand.plus.SettingsHelper.MapSourcesSettingsItem; +import net.osmand.plus.SettingsHelper.PoiUiFilterSettingsItem; +import net.osmand.plus.SettingsHelper.ProfileSettingsItem; +import net.osmand.plus.SettingsHelper.QuickActionsSettingsItem; +import net.osmand.plus.SettingsHelper.SettingsItem; +import net.osmand.plus.SettingsHelper.SettingsItemType; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.quickaction.QuickAction; -import net.osmand.plus.widgets.TextViewEx; import net.osmand.plus.settings.ExportImportSettingsAdapter.Type; - +import net.osmand.plus.widgets.TextViewEx; import org.apache.commons.logging.Log; @@ -322,7 +330,7 @@ public class ImportSettingsFragment extends BaseOsmAndFragment || object instanceof SQLiteTileSource) { tileSourceTemplates.add((ITileSource) object); } else if (object instanceof File) { - settingsItems.add(new SettingsHelper.FileSettingsItem(app, (File) object)); + settingsItems.add(new FileSettingsItem(app, (File) object)); } else if (object instanceof AvoidRoadInfo) { avoidRoads.add((AvoidRoadInfo) object); } @@ -356,23 +364,28 @@ public class ImportSettingsFragment extends BaseOsmAndFragment if (item.getType().equals(SettingsItemType.PROFILE)) { profiles.add(((ProfileSettingsItem) item).getModeBean()); } else if (item.getType().equals(SettingsItemType.QUICK_ACTIONS)) { - quickActions.addAll(((QuickActionsSettingsItem) item).getItems()); - quickActions.addAll(((QuickActionsSettingsItem) item).getDuplicateItems()); + QuickActionsSettingsItem quickActionsItem = (QuickActionsSettingsItem) item; + quickActions.addAll(quickActionsItem.getItems()); + quickActions.addAll(quickActionsItem.getDuplicateItems()); } else if (item.getType().equals(SettingsItemType.POI_UI_FILTERS)) { - poiUIFilters.addAll(((PoiUiFilterSettingsItem) item).getItems()); - poiUIFilters.addAll(((PoiUiFilterSettingsItem) item).getDuplicateItems()); + PoiUiFilterSettingsItem poiUiFilterItem = (PoiUiFilterSettingsItem) item; + poiUIFilters.addAll(poiUiFilterItem.getItems()); + poiUIFilters.addAll(poiUiFilterItem.getDuplicateItems()); } else if (item.getType().equals(SettingsItemType.MAP_SOURCES)) { - tileSourceTemplates.addAll(((MapSourcesSettingsItem) item).getItems()); - tileSourceTemplates.addAll(((MapSourcesSettingsItem) item).getDuplicateItems()); + MapSourcesSettingsItem mapSourcesItem = (MapSourcesSettingsItem) item; + tileSourceTemplates.addAll(mapSourcesItem.getItems()); + tileSourceTemplates.addAll(mapSourcesItem.getDuplicateItems()); } else if (item.getType().equals(SettingsItemType.FILE)) { - if (item.getName().contains(IndexConstants.RENDERERS_DIR)) { - renderFilesList.add(((FileSettingsItem) item).getFile()); - } else if (item.getName().contains(IndexConstants.ROUTING_PROFILES_DIR)) { - routingFilesList.add(((FileSettingsItem) item).getFile()); + FileSettingsItem fileItem = (FileSettingsItem) item; + if (fileItem.getSubtype() == FileSubtype.RENDERING_STYLE) { + renderFilesList.add(fileItem.getFile()); + } else if (fileItem.getSubtype() == FileSubtype.ROUTING_CONFIG) { + routingFilesList.add(fileItem.getFile()); } } else if (item.getType().equals(SettingsItemType.AVOID_ROADS)) { - avoidRoads.addAll(((AvoidRoadsSettingsItem) item).getItems()); - avoidRoads.addAll(((AvoidRoadsSettingsItem) item).getDuplicateItems()); + AvoidRoadsSettingsItem avoidRoadsItem = (AvoidRoadsSettingsItem) item; + avoidRoads.addAll(avoidRoadsItem.getItems()); + avoidRoads.addAll(avoidRoadsItem.getDuplicateItems()); } }