From 13a17d3a5b2fd78bd1c36aa0091a77cc8df9eb1a Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 9 Oct 2020 14:16:25 +0300 Subject: [PATCH] Fix merging import helper --- .../plus/importfiles/SettingsImportTask.java | 13 ++- .../plus/importfiles/UriImportTask.java | 6 +- .../plus/settings/backend/SettingsHelper.java | 83 +++++++++++++++++++ .../fragments/ImportCompleteFragment.java | 8 +- .../fragments/ImportSettingsFragment.java | 17 ++-- 5 files changed, 102 insertions(+), 25 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java index 7800714610..55135832e5 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java @@ -9,6 +9,7 @@ import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.CallbackWithObject; +import net.osmand.FileUtils; import net.osmand.IndexConstants; import net.osmand.plus.CustomOsmandPlugin; import net.osmand.plus.R; @@ -25,7 +26,6 @@ import java.io.File; import java.util.ArrayList; import java.util.List; -import static net.osmand.IndexConstants.TEMP_DIR; import static net.osmand.plus.AppInitializer.loadRoutingFiles; class SettingsImportTask extends BaseImportAsyncTask { @@ -37,8 +37,8 @@ class SettingsImportTask extends BaseImportAsyncTask { private CallbackWithObject> callback; public SettingsImportTask(@NonNull FragmentActivity activity, @NonNull Uri uri, - @NonNull String name, String latestChanges, int version, - CallbackWithObject> callback) { + @NonNull String name, String latestChanges, int version, + CallbackWithObject> callback) { super(activity); this.uri = uri; this.name = name; @@ -49,17 +49,14 @@ class SettingsImportTask extends BaseImportAsyncTask { @Override protected String doInBackground(Void... voids) { - File tempDir = app.getAppPath(TEMP_DIR); - if (!tempDir.exists()) { - tempDir.mkdirs(); - } + File tempDir = FileUtils.getTempDir(app); File dest = new File(tempDir, name); return ImportHelper.copyFile(app, dest, uri, true); } @Override protected void onPostExecute(String error) { - File tempDir = app.getAppPath(TEMP_DIR); + File tempDir = FileUtils.getTempDir(app); final File file = new File(tempDir, name); if (error == null && file.exists()) { app.getSettingsHelper().collectSettings(file, latestChanges, version, new SettingsCollectListener() { diff --git a/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java index c2de2d072f..90c4ac9e8e 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/UriImportTask.java @@ -6,6 +6,7 @@ import androidx.annotation.NonNull; import androidx.fragment.app.FragmentActivity; import net.osmand.AndroidUtils; +import net.osmand.FileUtils; import net.osmand.plus.R; import net.osmand.plus.importfiles.ImportHelper.ImportType; import net.osmand.util.Algorithms; @@ -61,10 +62,7 @@ class UriImportTask extends BaseImportAsyncTask { if (is != null) { fileSignature = Algorithms.readInt(is); if (isSupportedFileSignature()) { - File tempDir = app.getAppPath(TEMP_DIR); - if (!tempDir.exists()) { - tempDir.mkdirs(); - } + File tempDir = FileUtils.getTempDir(app); tempFileName = getTempFileName(); File dest = new File(tempDir, tempFileName); diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/settings/backend/SettingsHelper.java index 60a411121a..a708f92a46 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/SettingsHelper.java @@ -2637,6 +2637,89 @@ public class SettingsHelper { } } + public static Map> getSettingsToOperate(List settingsItems, boolean importComplete) { + Map> settingsToOperate = new HashMap<>(); + List profiles = new ArrayList<>(); + List quickActions = new ArrayList<>(); + List poiUIFilters = new ArrayList<>(); + List tileSourceTemplates = new ArrayList<>(); + List routingFilesList = new ArrayList<>(); + List renderFilesList = new ArrayList<>(); + List avoidRoads = new ArrayList<>(); + for (SettingsItem item : settingsItems) { + switch (item.getType()) { + case PROFILE: + profiles.add(((ProfileSettingsItem) item).getModeBean()); + break; + case FILE: + FileSettingsItem fileItem = (FileSettingsItem) item; + if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.RENDERING_STYLE) { + renderFilesList.add(fileItem.getFile()); + } else if (fileItem.getSubtype() == FileSettingsItem.FileSubtype.ROUTING_CONFIG) { + routingFilesList.add(fileItem.getFile()); + } + break; + case QUICK_ACTIONS: + QuickActionsSettingsItem quickActionsItem = (QuickActionsSettingsItem) item; + if (importComplete) { + quickActions.addAll(quickActionsItem.getAppliedItems()); + } else { + quickActions.addAll(quickActionsItem.getItems()); + } + break; + case POI_UI_FILTERS: + PoiUiFiltersSettingsItem poiUiFilterItem = (PoiUiFiltersSettingsItem) item; + if (importComplete) { + poiUIFilters.addAll(poiUiFilterItem.getAppliedItems()); + } else { + poiUIFilters.addAll(poiUiFilterItem.getItems()); + } + break; + case MAP_SOURCES: + MapSourcesSettingsItem mapSourcesItem = (MapSourcesSettingsItem) item; + if (importComplete) { + tileSourceTemplates.addAll(mapSourcesItem.getAppliedItems()); + } else { + tileSourceTemplates.addAll(mapSourcesItem.getItems()); + } + break; + case AVOID_ROADS: + AvoidRoadsSettingsItem avoidRoadsItem = (AvoidRoadsSettingsItem) item; + if (importComplete) { + avoidRoads.addAll(avoidRoadsItem.getAppliedItems()); + } else { + avoidRoads.addAll(avoidRoadsItem.getItems()); + } + break; + default: + break; + } + } + + if (!profiles.isEmpty()) { + settingsToOperate.put(ExportSettingsType.PROFILE, profiles); + } + if (!quickActions.isEmpty()) { + settingsToOperate.put(ExportSettingsType.QUICK_ACTIONS, quickActions); + } + if (!poiUIFilters.isEmpty()) { + settingsToOperate.put(ExportSettingsType.POI_TYPES, poiUIFilters); + } + if (!tileSourceTemplates.isEmpty()) { + settingsToOperate.put(ExportSettingsType.MAP_SOURCES, tileSourceTemplates); + } + if (!renderFilesList.isEmpty()) { + settingsToOperate.put(ExportSettingsType.CUSTOM_RENDER_STYLE, renderFilesList); + } + if (!routingFilesList.isEmpty()) { + settingsToOperate.put(ExportSettingsType.CUSTOM_ROUTING, routingFilesList); + } + if (!avoidRoads.isEmpty()) { + settingsToOperate.put(ExportSettingsType.AVOID_ROADS, avoidRoads); + } + return settingsToOperate; + } + @SuppressLint("StaticFieldLeak") public class ImportAsyncTask extends AsyncTask> { diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java index e152d5ea68..a8e7acc8e3 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportCompleteFragment.java @@ -22,9 +22,6 @@ import androidx.recyclerview.widget.RecyclerView; import net.osmand.AndroidUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.helpers.ImportHelper; -import net.osmand.plus.settings.backend.ExportSettingsType; -import net.osmand.plus.settings.backend.SettingsHelper.SettingsItem; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.BaseOsmAndFragment; @@ -33,6 +30,9 @@ import net.osmand.plus.dialogs.SelectMapStyleBottomSheetDialogFragment; import net.osmand.plus.quickaction.QuickActionListFragment; import net.osmand.plus.routepreparationmenu.AvoidRoadsBottomSheetDialogFragment; import net.osmand.plus.search.QuickSearchDialogFragment; +import net.osmand.plus.settings.backend.ExportSettingsType; +import net.osmand.plus.settings.backend.SettingsHelper; +import net.osmand.plus.settings.backend.SettingsHelper.SettingsItem; import java.util.List; @@ -118,7 +118,7 @@ public class ImportCompleteFragment extends BaseOsmAndFragment { if (settingsItems != null) { ImportedSettingsItemsAdapter adapter = new ImportedSettingsItemsAdapter( app, - ImportHelper.getSettingsToOperate(settingsItems, true), + SettingsHelper.getSettingsToOperate(settingsItems, true), nightMode, new ImportedSettingsItemsAdapter.OnItemClickListener() { @Override diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java index 7e8f6bb934..dd02f3f716 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ImportSettingsFragment.java @@ -34,8 +34,14 @@ import net.osmand.plus.AppInitializer; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.SQLiteTileSource; -import net.osmand.plus.settings.backend.ExportSettingsType; +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.settings.backend.ApplicationMode.ApplicationModeBean; +import net.osmand.plus.settings.backend.ExportSettingsType; import net.osmand.plus.settings.backend.SettingsHelper; import net.osmand.plus.settings.backend.SettingsHelper.AvoidRoadsSettingsItem; import net.osmand.plus.settings.backend.SettingsHelper.FileSettingsItem; @@ -47,12 +53,6 @@ import net.osmand.plus.settings.backend.SettingsHelper.ProfileSettingsItem; import net.osmand.plus.settings.backend.SettingsHelper.QuickActionsSettingsItem; import net.osmand.plus.settings.backend.SettingsHelper.SettingsItem; import net.osmand.plus.settings.backend.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.util.Algorithms; @@ -64,7 +64,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import static net.osmand.plus.helpers.ImportHelper.getSettingsToOperate; public class ImportSettingsFragment extends BaseOsmAndFragment implements View.OnClickListener { @@ -183,7 +182,7 @@ public class ImportSettingsFragment extends BaseOsmAndFragment adapter = new ExportImportSettingsAdapter(app, nightMode, true); Map> itemsMap = new HashMap<>(); if (settingsItems != null) { - itemsMap = getSettingsToOperate(settingsItems, false); + itemsMap = SettingsHelper.getSettingsToOperate(settingsItems, false); adapter.updateSettingsList(itemsMap); } expandableList.setAdapter(adapter);