From 2a225f8659fd37a5bc73ddd7ae9a78bda00d82e9 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 2 Mar 2020 12:10:15 +0200 Subject: [PATCH] Import backup profile settings items --- OsmAnd/src/net/osmand/AndroidUtils.java | 7 ++++ .../net/osmand/plus/helpers/ImportHelper.java | 29 +++++++---------- .../settings/ConfigureProfileFragment.java | 32 +++++++++++++++---- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/OsmAnd/src/net/osmand/AndroidUtils.java b/OsmAnd/src/net/osmand/AndroidUtils.java index 8633f61abf..e77b957645 100644 --- a/OsmAnd/src/net/osmand/AndroidUtils.java +++ b/OsmAnd/src/net/osmand/AndroidUtils.java @@ -162,6 +162,13 @@ public class AndroidUtils { return intent.resolveActivity(context.getPackageManager()) != null; } + public static boolean isActivityNotDestroyed(Activity activity) { + if (Build.VERSION.SDK_INT >= 17) { + return !activity.isFinishing() && !activity.isDestroyed(); + } + return !activity.isFinishing(); + } + public static Spannable replaceCharsWithIcon(String text, Drawable icon, String[] chars) { Spannable spannable = new SpannableString(text); for (String entry : chars) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index dc78b7a0f3..3cd7f37869 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -243,7 +243,7 @@ public class ImportHelper { @Override protected void onPostExecute(GPXFile result) { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } handleResult(result, fileName, save, useImportDir, false); @@ -315,7 +315,7 @@ public class ImportHelper { @Override protected void onPostExecute(final GPXFile result) { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } @@ -353,7 +353,7 @@ public class ImportHelper { @Override protected void onPostExecute(GPXFile result) { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } Toast.makeText(activity, R.string.fav_imported_sucessfully, Toast.LENGTH_LONG) @@ -426,7 +426,7 @@ public class ImportHelper { @Override protected void onPostExecute(GPXFile result) { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } handleResult(result, name, save, useImportDir, false); @@ -474,7 +474,7 @@ public class ImportHelper { @Override protected void onPostExecute(GPXFile result) { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } handleResult(result, name, save, useImportDir, false); @@ -506,7 +506,7 @@ public class ImportHelper { @Override protected void onPostExecute(String message) { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } Toast.makeText(app, message, Toast.LENGTH_SHORT).show(); @@ -587,7 +587,7 @@ public class ImportHelper { @Override protected void onPostExecute(String error) { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } if (error == null) { @@ -696,7 +696,7 @@ public class ImportHelper { loadRoutingFiles(app, new AppInitializer.LoadRoutingFilesCallback() { @Override public void onRoutingFilesLoaded() { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } RoutingConfiguration.Builder builder = app.getCustomRoutingConfig(mFileName); @@ -711,7 +711,7 @@ public class ImportHelper { } }); } else { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } app.showShortToastMessage(app.getString(R.string.file_import_error, mFileName, error)); @@ -774,7 +774,7 @@ public class ImportHelper { app.getSettingsHelper().importSettings(file, latestChanges, version, new SettingsImportListener() { @Override public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } if (succeed) { @@ -788,7 +788,7 @@ public class ImportHelper { } }); } else { - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } app.showShortToastMessage(app.getString(R.string.file_import_error, name, error)); @@ -811,13 +811,6 @@ public class ImportHelper { } } - private boolean isActivityNotDestroyed(Activity activity) { - if (Build.VERSION.SDK_INT >= 17) { - return !activity.isFinishing() && !activity.isDestroyed(); - } - return !activity.isFinishing(); - } - private void handleResult(final GPXFile result, final String name, final boolean save, final boolean useImportDir, boolean forceImportFavourites) { if (result != null) { diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java index d0a3dff90b..86cba3e90c 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java @@ -170,13 +170,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co if (appMode.isCustomProfile()) { File file = getBackupFileForCustomMode(appMode); if (file.exists()) { - app.getSettingsHelper().importSettings(file, "", 1, new SettingsHelper.SettingsImportListener() { - @Override - public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { - app.showToastMessage(R.string.profile_prefs_reset_successful); - updateCopiedOrResetPrefs(); - } - }); + restoreCustomModeFromFile(file); } } else { app.getSettings().resetPreferencesForProfile(appMode); @@ -186,6 +180,30 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co } } + private void restoreCustomModeFromFile(final File file) { + app.getSettingsHelper().importSettings(file, "", 1, new SettingsHelper.SettingsImportListener() { + @Override + public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { + if (succeed) { + for (SettingsHelper.SettingsItem item : items) { + item.setShouldReplace(true); + } + importBackupSettingsItems(file, items); + } + } + }); + } + + private void importBackupSettingsItems(File file, List items) { + app.getSettingsHelper().importSettings(file, items, "", 1, new SettingsHelper.SettingsImportListener() { + @Override + public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { + app.showToastMessage(R.string.profile_prefs_reset_successful); + updateCopiedOrResetPrefs(); + } + }); + } + private File getBackupFileForCustomMode(ApplicationMode appMode) { String fileName = appMode.getStringKey() + IndexConstants.OSMAND_SETTINGS_FILE_EXT; File backupDir = app.getAppPath(IndexConstants.BACKUP_INDEX_DIR);