diff --git a/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java b/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java index 09d2064284..c7cad00b0c 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/ImportHelper.java @@ -34,6 +34,7 @@ import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment; import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.helpers.GpxUiHelper.GPXInfo; import net.osmand.plus.measurementtool.MeasurementToolFragment; +import net.osmand.plus.settings.backend.ExportSettingsType; import net.osmand.plus.settings.backend.backup.SettingsHelper; import net.osmand.plus.settings.backend.backup.SettingsItem; import net.osmand.plus.views.OsmandMapTileView; @@ -67,6 +68,8 @@ import static net.osmand.IndexConstants.WPT_CHART_FILE_EXT; import static net.osmand.data.FavouritePoint.DEFAULT_BACKGROUND_TYPE; import static net.osmand.plus.myplaces.FavoritesActivity.GPX_TAB; import static net.osmand.plus.myplaces.FavoritesActivity.TAB_ID; +import static net.osmand.plus.settings.backend.backup.SettingsHelper.REPLACE_KEY; +import static net.osmand.plus.settings.backend.backup.SettingsHelper.SETTINGS_TYPE_LIST_KEY; /** * @author Koen Rabaey @@ -203,10 +206,11 @@ public class ImportHelper { } } + @Nullable public static String getNameFromContentUri(OsmandApplication app, Uri contentUri) { try { String name; - Cursor returnCursor = app.getContentResolver().query(contentUri, new String[] {OpenableColumns.DISPLAY_NAME}, null, null, null); + Cursor returnCursor = app.getContentResolver().query(contentUri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null); if (returnCursor != null && returnCursor.moveToFirst()) { int columnIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); if (columnIndex != -1) { @@ -257,18 +261,32 @@ public class ImportHelper { } private void handleOsmAndSettingsImport(Uri intentUri, String fileName, Bundle extras, CallbackWithObject> callback) { - if (extras != null && extras.containsKey(SettingsHelper.SETTINGS_VERSION_KEY) && extras.containsKey(SettingsHelper.SETTINGS_LATEST_CHANGES_KEY)) { + if (extras != null + && extras.containsKey(SettingsHelper.SETTINGS_VERSION_KEY) + && extras.containsKey(SettingsHelper.SETTINGS_LATEST_CHANGES_KEY)) { int version = extras.getInt(SettingsHelper.SETTINGS_VERSION_KEY, -1); String latestChanges = extras.getString(SettingsHelper.SETTINGS_LATEST_CHANGES_KEY); - handleOsmAndSettingsImport(intentUri, fileName, latestChanges, version, callback); + boolean replace = extras.getBoolean(REPLACE_KEY); + ArrayList settingsTypeKeys = extras.getStringArrayList(SETTINGS_TYPE_LIST_KEY); + List settingsTypes = null; + if (settingsTypeKeys != null) { + settingsTypes = new ArrayList<>(); + for (String key : settingsTypeKeys) { + settingsTypes.add(ExportSettingsType.valueOf(key)); + } + } + handleOsmAndSettingsImport(intentUri, fileName, settingsTypes, replace, latestChanges, version, callback); } else { - handleOsmAndSettingsImport(intentUri, fileName, null, -1, callback); + handleOsmAndSettingsImport(intentUri, fileName, null, false, null, -1, + callback); } } - protected void handleOsmAndSettingsImport(Uri uri, String name, String latestChanges, int version, - CallbackWithObject> callback) { - executeImportTask(new SettingsImportTask(activity, uri, name, latestChanges, version, callback)); + protected void handleOsmAndSettingsImport(Uri uri, String name, final List settingsTypes, + final boolean replace, String latestChanges, int version, + CallbackWithObject> callback) { + executeImportTask(new SettingsImportTask(activity, uri, name, settingsTypes, replace, latestChanges, version, + callback)); } protected void handleXmlFileImport(Uri intentUri, String fileName, CallbackWithObject routingCallback) { diff --git a/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java index 991d8a83c4..75c8d55015 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java @@ -11,38 +11,50 @@ import net.osmand.AndroidUtils; import net.osmand.CallbackWithObject; import net.osmand.FileUtils; import net.osmand.IndexConstants; +import net.osmand.plus.AppInitializer; import net.osmand.plus.CustomOsmandPlugin; import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.BaseLoadAsyncTask; +import net.osmand.plus.settings.backend.ExportSettingsType; +import net.osmand.plus.settings.backend.backup.SettingsHelper; import net.osmand.plus.settings.backend.backup.SettingsHelper.CheckDuplicatesListener; import net.osmand.plus.settings.backend.backup.PluginSettingsItem; import net.osmand.plus.settings.backend.backup.ProfileSettingsItem; import net.osmand.plus.settings.backend.backup.SettingsHelper.SettingsCollectListener; import net.osmand.plus.settings.backend.backup.SettingsHelper.SettingsImportListener; import net.osmand.plus.settings.backend.backup.SettingsItem; +import net.osmand.plus.settings.fragments.ImportCompleteFragment; import net.osmand.plus.settings.fragments.ImportSettingsFragment; import net.osmand.util.Algorithms; import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Map; import static net.osmand.plus.AppInitializer.loadRoutingFiles; +import static net.osmand.plus.settings.backend.backup.SettingsHelper.getSettingsToOperate; class SettingsImportTask extends BaseLoadAsyncTask { private Uri uri; private String name; + private List settingsTypes; + private boolean replace; private String latestChanges; private int version; private CallbackWithObject> callback; public SettingsImportTask(@NonNull FragmentActivity activity, @NonNull Uri uri, - @NonNull String name, String latestChanges, int version, - CallbackWithObject> callback) { + @NonNull String name, List settingsTypes, + boolean replace, String latestChanges, int version, + CallbackWithObject> callback) { super(activity); this.uri = uri; this.name = name; + this.settingsTypes = settingsTypes; + this.replace = replace; this.latestChanges = latestChanges; this.version = version; this.callback = callback; @@ -60,7 +72,8 @@ class SettingsImportTask extends BaseLoadAsyncTask { 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() { + final SettingsHelper settingsHelper = app.getSettingsHelper(); + settingsHelper.collectSettings(file, latestChanges, version, new SettingsCollectListener() { @Override public void onSettingsCollectFinished(boolean succeed, boolean empty, @NonNull List items) { hideProgress(); @@ -78,10 +91,16 @@ class SettingsImportTask extends BaseLoadAsyncTask { handlePluginImport(pluginItem, file); } if (!pluginIndependentItems.isEmpty()) { - FragmentActivity activity = activityRef.get(); - if (activity != null) { - FragmentManager fragmentManager = activity.getSupportFragmentManager(); - ImportSettingsFragment.showInstance(fragmentManager, pluginIndependentItems, file); + if (settingsTypes == null) { + FragmentActivity activity = activityRef.get(); + if (activity != null) { + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + ImportSettingsFragment.showInstance(fragmentManager, pluginIndependentItems, file); + } + } else { + Map> allSettingsMap = getSettingsToOperate(pluginIndependentItems, false); + List settingsList = settingsHelper.getFilteredSettingsItems(allSettingsMap, settingsTypes); + settingsHelper.checkDuplicates(file, settingsList, settingsList, getDuplicatesListener(file, replace)); } } } else if (empty) { @@ -95,6 +114,42 @@ class SettingsImportTask extends BaseLoadAsyncTask { } } + private CheckDuplicatesListener getDuplicatesListener(final File file, final boolean replace) { + return new CheckDuplicatesListener() { + @Override + public void onDuplicatesChecked(@NonNull List duplicates, List items) { + if (replace) { + for (SettingsItem item : items) { + item.setShouldReplace(true); + } + } + app.getSettingsHelper().importSettings(file, items, "", 1, getImportListener(file)); + } + }; + } + + private SettingsImportListener getImportListener(final File file) { + return new SettingsImportListener() { + @Override + public void onSettingsImportFinished(boolean succeed, @NonNull List items) { + if (succeed) { + app.getRendererRegistry().updateExternalRenderers(); + app.getPoiFilters().loadSelectedPoiFilters(); + AppInitializer.loadRoutingFiles(app, null); + FragmentActivity activity = activityRef.get(); + if (activity instanceof MapActivity) { + ((MapActivity) activity).getMapLayers().getMapWidgetRegistry().updateVisibleWidgets(); + ((MapActivity) activity).updateApplicationModeSettings(); + } + if (file != null && activity != null) { + FragmentManager fm = activity.getSupportFragmentManager(); + ImportCompleteFragment.showInstance(fm, items, file.getName()); + } + } + } + }; + } + private void handlePluginImport(final PluginSettingsItem pluginItem, final File file) { FragmentActivity activity = activityRef.get(); final ProgressDialog progress; diff --git a/OsmAnd/src/net/osmand/plus/importfiles/ZipImportTask.java b/OsmAnd/src/net/osmand/plus/importfiles/ZipImportTask.java index e9bac73258..afdb7a32f2 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/ZipImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/ZipImportTask.java @@ -85,7 +85,8 @@ public class ZipImportTask extends BaseLoadAsyncTask { importHelper.handleKmzImport(uri, name + GPX_FILE_EXT, save, useImportDir); } else if (importType == ImportType.SETTINGS) { String name = createUniqueFileName(app, "settings", TEMP_DIR, OSMAND_SETTINGS_FILE_EXT); - importHelper.handleOsmAndSettingsImport(uri, name + OSMAND_SETTINGS_FILE_EXT, null, -1, null); + importHelper.handleOsmAndSettingsImport(uri, name + OSMAND_SETTINGS_FILE_EXT, + null, false, null, -1, null); } } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/OsmAndAppCustomization.java b/OsmAnd/src/net/osmand/plus/settings/backend/OsmAndAppCustomization.java index 8a43f650ee..742d9b26e1 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/OsmAndAppCustomization.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/OsmAndAppCustomization.java @@ -287,20 +287,17 @@ public class OsmAndAppCustomization { if (!connectedAppDir.exists()) { connectedAppDir.mkdirs(); } - OutputStream fout = new FileOutputStream(new File(connectedAppDir, iconName)); + OutputStream fout = null; + if (!Algorithms.isEmpty(iconName)) { + fout = new FileOutputStream(new File(connectedAppDir, iconName)); + } try { - Algorithms.streamCopy(is, fout); + if (fout != null) { + Algorithms.streamCopy(is, fout); + } } finally { - try { - is.close(); - } catch (IOException e) { - LOG.error(e); - } - try { - fout.close(); - } catch (IOException e) { - LOG.error(e); - } + Algorithms.closeStream(is); + Algorithms.closeStream(fout); } JSONObject json = new JSONObject(); json.put("", connectedAppDirPath + "/" + iconName); @@ -406,8 +403,7 @@ public class OsmAndAppCustomization { return set.contains(appMode); } - public boolean setNavDrawerLogoWithParams(String imageUri, @Nullable String packageName, - @Nullable String intent) { + public boolean setNavDrawerLogoWithParams(String imageUri, @Nullable String packageName, @Nullable String intent) { return setNavDrawerLogo(imageUri, packageName, intent); } diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java index babb91a140..6c3cf25178 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java @@ -481,24 +481,19 @@ public class SettingsHelper { return getFilteredSettingsItems(typesMap, settingsTypes); } - public List getFilteredSettingsItems(Map> additionalData, - List settingsTypes) { + public List getFilteredSettingsItems(Map> allSettingsMap, + List settingsTypes) { List settingsItems = new ArrayList<>(); for (ExportSettingsType settingsType : settingsTypes) { - List settingsDataObjects = additionalData.get(settingsType); + List settingsDataObjects = allSettingsMap.get(settingsType); if (settingsDataObjects != null) { - for (Object object : settingsDataObjects) { - if (object instanceof ApplicationModeBean) { - settingsItems.add(new ProfileSettingsItem(app, null, (ApplicationModeBean) object)); - } - } - settingsItems.addAll(prepareAdditionalSettingsItems(new ArrayList<>(settingsDataObjects))); + settingsItems.addAll(prepareSettingsItems(new ArrayList<>(settingsDataObjects))); } } return settingsItems; } - public Map getAdditionalData(boolean globalExport) { + public Map getSettingsByCategory(boolean globalExport) { Map dataList = new LinkedHashMap<>(); Map> settingsItems = getSettingsItems(globalExport); @@ -692,7 +687,7 @@ public class SettingsHelper { return files; } - public List prepareAdditionalSettingsItems(List data) { + public List prepareSettingsItems(List data) { List settingsItems = new ArrayList<>(); List quickActions = new ArrayList<>(); List poiUIFilters = new ArrayList<>(); @@ -754,10 +749,7 @@ public class SettingsHelper { } if (!appModeBeans.isEmpty()) { for (ApplicationModeBean modeBean : appModeBeans) { - ApplicationMode mode = ApplicationMode.valueOfStringKey(modeBean.stringKey, null); - if (mode != null) { - settingsItems.add(new ProfileSettingsItem(app, mode)); - } + settingsItems.add(new ProfileSettingsItem(app, null, modeBean)); } } if (!osmNotesPointList.isEmpty()) { diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java index 0a310e5c16..3ced930a17 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java @@ -276,7 +276,7 @@ public class ExportItemsBottomSheet extends MenuBottomSheetDialogFragment { String profileName = modeBean.userProfileName; if (Algorithms.isEmpty(profileName)) { ApplicationMode appMode = ApplicationMode.valueOfStringKey(modeBean.stringKey, null); - profileName = getString(appMode.getNameKeyResource()); + profileName = appMode.toHumanString(); } builder.setTitle(profileName); diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportSettingsFragment.java index af954db7c5..ad4e23aa11 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportSettingsFragment.java @@ -43,12 +43,9 @@ public class ExportSettingsFragment extends BaseSettingsListFragment { public static final String TAG = ImportSettingsFragment.class.getSimpleName(); public static final Log LOG = PlatformUtil.getLog(ImportSettingsFragment.class.getSimpleName()); - private static final String EXPORT_SETTINGS_TAG = "import_settings_tag"; private static final String GLOBAL_EXPORT_KEY = "global_export_key"; private static final String EXPORT_START_TIME_KEY = "export_start_time_key"; private static final String EXPORTING_STARTED_KEY = "exporting_started_key"; - private static final String INCLUDE_ADDITIONAL_DATA_KEY = "include_additional_data_key"; - private static final String INCLUDE_GLOBAL_SETTINGS_KEY = "include_global_settings_key"; private static final String PROGRESS_MAX_KEY = "progress_max_key"; private static final String PROGRESS_VALUE_KEY = "progress_value_key"; @@ -76,7 +73,7 @@ public class ExportSettingsFragment extends BaseSettingsListFragment { progressValue = savedInstanceState.getInt(PROGRESS_VALUE_KEY); } exportMode = true; - dataList = app.getSettingsHelper().getAdditionalData(globalExport); + dataList = app.getSettingsHelper().getSettingsByCategory(globalExport); } @Nullable @@ -132,7 +129,7 @@ public class ExportSettingsFragment extends BaseSettingsListFragment { showExportProgressDialog(); File tempDir = FileUtils.getTempDir(app); String fileName = getFileName(); - List items = app.getSettingsHelper().prepareAdditionalSettingsItems(adapter.getData()); + List items = app.getSettingsHelper().prepareSettingsItems(adapter.getData()); progress.setMax(getMaxProgress(items)); app.getSettingsHelper().exportSettings(tempDir, fileName, getSettingsExportListener(), items, true); } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java index 8e4dfc14c0..0374a4a42f 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ProfileAppearanceFragment.java @@ -40,6 +40,7 @@ import androidx.recyclerview.widget.RecyclerView; import net.osmand.AndroidUtils; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; +import net.osmand.plus.activities.MapActivity; import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.R; import net.osmand.plus.settings.backend.backup.ProfileSettingsItem; @@ -873,6 +874,10 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { private void customProfileSaved() { FragmentActivity activity = getActivity(); if (activity != null) { + if (activity instanceof MapActivity) { + ((MapActivity) activity).getMapLayers().getMapWidgetRegistry().updateVisibleWidgets(); + ((MapActivity) activity).updateApplicationModeSettings(); + } FragmentManager fragmentManager = activity.getSupportFragmentManager(); if (!fragmentManager.isStateSaved()) { fragmentManager.popBackStack();