From ca2b6f4d0e61eca55a29b7b7c43c64345766cb30 Mon Sep 17 00:00:00 2001 From: Dima-1 Date: Thu, 26 Nov 2020 19:22:33 +0200 Subject: [PATCH] Fix profiles import --- .../osmand/plus/importfiles/ImportHelper.java | 32 +++++++--- .../plus/importfiles/SettingsImportTask.java | 59 +++++++++++++++++-- .../plus/importfiles/ZipImportTask.java | 3 +- .../backend/OsmAndAppCustomization.java | 24 ++++---- .../fragments/ExportItemsBottomSheet.java | 2 +- 5 files changed, 91 insertions(+), 29 deletions(-) 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..833ad34c3d 100644 --- a/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java +++ b/OsmAnd/src/net/osmand/plus/importfiles/SettingsImportTask.java @@ -11,38 +11,49 @@ 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.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 +71,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(); @@ -77,11 +89,15 @@ class SettingsImportTask extends BaseLoadAsyncTask { for (PluginSettingsItem pluginItem : pluginSettingsItems) { handlePluginImport(pluginItem, file); } - if (!pluginIndependentItems.isEmpty()) { - FragmentActivity activity = activityRef.get(); - if (activity != null) { + FragmentActivity activity = activityRef.get(); + if (!pluginIndependentItems.isEmpty() && activity != null) { + if (settingsTypes == null) { FragmentManager fragmentManager = activity.getSupportFragmentManager(); ImportSettingsFragment.showInstance(fragmentManager, pluginIndependentItems, file); + } else { + Map> allSettingsList = getSettingsToOperate(pluginIndependentItems, false); + List settingsList = settingsHelper.getFilteredSettingsItems(allSettingsList, settingsTypes); + settingsHelper.checkDuplicates(file, settingsList, settingsList, getDuplicatesListener(file, replace)); } } } else if (empty) { @@ -95,6 +111,37 @@ 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) { + FragmentActivity activity = activityRef.get(); + if (activity != null && succeed) { + FragmentManager fm = activity.getSupportFragmentManager(); + app.getRendererRegistry().updateExternalRenderers(); + AppInitializer.loadRoutingFiles(app, null); + if (file != null) { + 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/fragments/ExportItemsBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java index 8c62e04ccf..93dff80cce 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/ExportItemsBottomSheet.java @@ -217,7 +217,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);