From c9086f78f4c9b8a725da479583593ac7c1d1a5d2 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 24 Feb 2020 17:34:00 +0200 Subject: [PATCH 01/16] Backup created profile --- OsmAnd/res/values/strings.xml | 2 + .../src/net/osmand/plus/SettingsHelper.java | 17 +-- .../settings/ExportProfileBottomSheet.java | 19 ++- .../settings/ProfileAppearanceFragment.java | 131 +++++++++++++----- 4 files changed, 119 insertions(+), 50 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 577fbd1a68..e3ee4c52d0 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,8 @@ Thx - Hardy --> + Saving new profile + Could not back up profile. Custom profile Angle: %s° Angle diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index ab8c98c0c7..912110e8e6 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -2,7 +2,6 @@ package net.osmand.plus; import android.annotation.SuppressLint; import android.app.Activity; -import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import android.support.annotation.NonNull; @@ -1831,7 +1830,6 @@ public class SettingsHelper { private SettingsExporter exporter; private File file; private SettingsExportListener listener; - private ProgressDialog progress; ExportAsyncTask(@NonNull File settingsFile, @Nullable SettingsExportListener listener, @@ -1844,14 +1842,6 @@ public class SettingsHelper { } } - @Override - protected void onPreExecute() { - super.onPreExecute(); - if (activity != null) { - progress = ProgressDialog.show(activity, app.getString(R.string.export_profile), app.getString(R.string.shared_string_preparing)); - } - } - @Override protected Boolean doInBackground(Void... voids) { try { @@ -1867,11 +1857,8 @@ public class SettingsHelper { @Override protected void onPostExecute(Boolean success) { - if (activity != null) { - progress.dismiss(); - if (listener != null) { - listener.onSettingsExportFinished(file, success); - } + if (listener != null) { + listener.onSettingsExportFinished(file, success); } } } diff --git a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java index 2966884c90..7b762b79f9 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java @@ -1,5 +1,6 @@ package net.osmand.plus.settings; +import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.res.ColorStateList; @@ -15,7 +16,6 @@ import android.support.v7.widget.SwitchCompat; import android.view.LayoutInflater; import android.view.View; import android.widget.ExpandableListView; -import android.widget.LinearLayout.LayoutParams; import android.widget.Toast; import net.osmand.AndroidUtils; @@ -42,6 +42,7 @@ import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; import org.apache.commons.logging.Log; import java.io.File; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedHashMap; @@ -287,15 +288,29 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { } private void prepareFile() { - if (app != null) { + Context context = getContext(); + if (app != null && context != null) { File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); if (!tempDir.exists()) { tempDir.mkdirs(); } String fileName = profile.toHumanString(); + + ProgressDialog progress = new ProgressDialog(context); + progress.setTitle(app.getString(R.string.export_profile)); + progress.setMessage(app.getString(R.string.shared_string_preparing)); + progress.setCancelable(false); + progress.show(); + + final WeakReference progressRef = new WeakReference<>(progress); + app.getSettingsHelper().exportSettings(tempDir, fileName, new SettingsHelper.SettingsExportListener() { @Override public void onSettingsExportFinished(@NonNull File file, boolean succeed) { + ProgressDialog progress = progressRef.get(); + if (progress != null) { + progress.dismiss(); + } if (succeed) { shareProfile(file, profile); } else { diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index fb8c17708d..4e2a646391 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -2,6 +2,7 @@ package net.osmand.plus.settings; import android.annotation.SuppressLint; import android.app.Activity; +import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Matrix; @@ -10,6 +11,7 @@ import android.graphics.PorterDuffColorFilter; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.LayerDrawable; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; @@ -31,9 +33,11 @@ import android.widget.ImageView; import android.widget.TextView; import net.osmand.AndroidUtils; +import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.plus.ApplicationMode; import net.osmand.plus.R; +import net.osmand.plus.SettingsHelper; import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities.DialogButtonType; import net.osmand.plus.profiles.LocationIcon; @@ -48,6 +52,8 @@ import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; +import java.io.File; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; @@ -241,17 +247,8 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { public void onClick(View v) { if (getActivity() != null) { hideKeyboard(); - if (isChanged()) { - if (saveProfile()) { - profile = changedProfile; - if (isNewProfile) { - ProfileAppearanceFragment.this.dismiss(); - BaseSettingsFragment.showInstance(getMapActivity(), SettingsScreenType.CONFIGURE_PROFILE, - ApplicationMode.valueOfStringKey(changedProfile.stringKey, null)); - } else { - getActivity().onBackPressed(); - } - } + if (isChanged() && checkProfileName()) { + saveProfile(); } } } @@ -652,31 +649,21 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { baseProfileName.setText(Algorithms.capitalizeFirstLetter(mode.toHumanString())); } - private boolean saveProfile() { - if (changedProfile.name.trim().isEmpty()) { - if (getActivity() != null) { - createWarningDialog(getActivity(), - R.string.profile_alert_need_profile_name_title, R.string.profile_alert_need_profile_name_msg, R.string.shared_string_dismiss).show(); + private boolean checkProfileName() { + if (Algorithms.isBlank(changedProfile.name)) { + Activity activity = getActivity(); + if (activity != null) { + createWarningDialog(activity, R.string.profile_alert_need_profile_name_title, + R.string.profile_alert_need_profile_name_msg, R.string.shared_string_dismiss).show(); } return false; } - if (isNewProfile) { - changedProfile.stringKey = getUniqueStringKey(changedProfile.parent); - ApplicationMode.ApplicationModeBuilder builder = ApplicationMode - .createCustomMode(changedProfile.parent, changedProfile.stringKey, app) - .setIconResName(ProfileIcons.getResStringByResId(changedProfile.iconRes)) - .setUserProfileName(changedProfile.name.trim()) - .setRoutingProfile(changedProfile.routingProfile) - .setRouteService(changedProfile.routeService) - .setIconColor(changedProfile.color) - .setLocationIcon(changedProfile.locationIcon) - .setNavigationIcon(changedProfile.navigationIcon); + return true; + } - app.getSettings().copyPreferencesFromProfile(changedProfile.parent, builder.getApplicationMode()); - ApplicationMode mode = ApplicationMode.saveProfile(builder, app); - if (!ApplicationMode.values(app).contains(mode)) { - ApplicationMode.changeProfileAvailability(mode, true, app); - } + private void saveProfile() { + if (isNewProfile) { + showNewProfileSavingDialog(); } else { ApplicationMode mode = getSelectedAppMode(); mode.setParentAppMode(changedProfile.parent); @@ -687,8 +674,86 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { mode.setIconColor(changedProfile.color); mode.setLocationIcon(changedProfile.locationIcon); mode.setNavigationIcon(changedProfile.navigationIcon); + + profileSaved(); + } + } + + private ApplicationMode saveNewProfile() { + changedProfile.stringKey = getUniqueStringKey(changedProfile.parent); + + ApplicationMode.ApplicationModeBuilder builder = ApplicationMode + .createCustomMode(changedProfile.parent, changedProfile.stringKey, app) + .setIconResName(ProfileIcons.getResStringByResId(changedProfile.iconRes)) + .setUserProfileName(changedProfile.name.trim()) + .setRoutingProfile(changedProfile.routingProfile) + .setRouteService(changedProfile.routeService) + .setIconColor(changedProfile.color) + .setLocationIcon(changedProfile.locationIcon) + .setNavigationIcon(changedProfile.navigationIcon); + + app.getSettings().copyPreferencesFromProfile(changedProfile.parent, builder.getApplicationMode()); + ApplicationMode mode = ApplicationMode.saveProfile(builder, app); + if (!ApplicationMode.values(app).contains(mode)) { + ApplicationMode.changeProfileAvailability(mode, true, app); + } + return mode; + } + + private void saveProfileBackup(ApplicationMode mode, SettingsHelper.SettingsExportListener listener) { + if (app != null) { + File tempDir = app.getAppPath(IndexConstants.BACKUP_INDEX_DIR); + if (!tempDir.exists()) { + tempDir.mkdirs(); + } + app.getSettingsHelper().exportSettings(tempDir, mode.getStringKey(), listener, new SettingsHelper.ProfileSettingsItem(app, mode)); + } + } + + private void showNewProfileSavingDialog() { + ProgressDialog progress = new ProgressDialog(getContext()); + progress.setMessage(getString(R.string.saving_new_profile)); + progress.setCancelable(false); + + final WeakReference progressRef = new WeakReference<>(progress); + final SettingsHelper.SettingsExportListener listener = new SettingsHelper.SettingsExportListener() { + + @Override + public void onSettingsExportFinished(@NonNull File file, boolean succeed) { + ProgressDialog progress = progressRef.get(); + if (progress != null) { + progress.dismiss(); + } + if (succeed) { + profileSaved(); + } else { + app.showToastMessage(R.string.profile_backup_failed); + } + } + }; + + progress.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + ApplicationMode mode = saveNewProfile(); + saveProfileBackup(mode, listener); + } + }); + progress.show(); + } + + private void profileSaved() { + FragmentActivity activity = getActivity(); + if (activity != null) { + profile = changedProfile; + if (isNewProfile) { + ProfileAppearanceFragment.this.dismiss(); + BaseSettingsFragment.showInstance(activity, SettingsScreenType.CONFIGURE_PROFILE, + ApplicationMode.valueOfStringKey(changedProfile.stringKey, null)); + } else { + activity.onBackPressed(); + } } - return true; } private String getUniqueStringKey(ApplicationMode mode) { From 9d857a9f98ea76d7cf92a23db5b5eff4c179d37d Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 25 Feb 2020 18:34:26 +0200 Subject: [PATCH 02/16] Ui fixes --- .../plus/settings/ConfigureProfileFragment.java | 16 ---------------- .../plus/settings/ProfileAppearanceFragment.java | 9 +++++++-- .../plus/settings/VoiceAnnouncesFragment.java | 2 +- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java index aa190ab5f7..d043ba9a21 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java @@ -323,22 +323,6 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co isNightMode() ? R.color.active_color_primary_dark : R.color.active_color_primary_light)); } - private void shareProfile(@NonNull File file, @NonNull ApplicationMode profile) { - try { - Context ctx = requireContext(); - final Intent sendIntent = new Intent(); - sendIntent.setAction(Intent.ACTION_SEND); - sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.exported_osmand_profile, profile.toHumanString())); - sendIntent.putExtra(Intent.EXTRA_STREAM, AndroidUtils.getUriForFile(getMyApplication(), file)); - sendIntent.setType("*/*"); - sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - startActivity(sendIntent); - } catch (Exception e) { - app.showToastMessage(R.string.export_profile_failed); - LOG.error("Share profile error", e); - } - } - private void setupOsmandPluginsPref(PreferenceCategory preferenceCategory) { Context ctx = getContext(); if (ctx == null) { diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index 4e2a646391..7310cc85cf 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -735,8 +735,13 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { progress.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { - ApplicationMode mode = saveNewProfile(); - saveProfileBackup(mode, listener); + app.runInUIThread(new Runnable() { + @Override + public void run() { + ApplicationMode mode = saveNewProfile(); + saveProfileBackup(mode, listener); + } + }); } }); progress.show(); diff --git a/OsmAnd/src/net/osmand/plus/settings/VoiceAnnouncesFragment.java b/OsmAnd/src/net/osmand/plus/settings/VoiceAnnouncesFragment.java index ef9c6e38f2..f1caeda45f 100644 --- a/OsmAnd/src/net/osmand/plus/settings/VoiceAnnouncesFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/VoiceAnnouncesFragment.java @@ -212,7 +212,7 @@ public class VoiceAnnouncesFragment extends BaseSettingsFragment { if (ctx == null) { return; } - AlertDialog.Builder bld = new AlertDialog.Builder(ctx); + AlertDialog.Builder bld = new AlertDialog.Builder(UiUtilities.getThemedContext(ctx, isNightMode())); bld.setMessage(R.string.confirm_usage_speed_cameras); bld.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { From f6e533d66e8a7b57e11e184815ba420cb5eb398a Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 26 Feb 2020 10:47:25 +0200 Subject: [PATCH 03/16] Add restore for custom profiles --- .../src/net/osmand/plus/SettingsHelper.java | 17 +++++++--- .../net/osmand/plus/helpers/ImportHelper.java | 2 +- .../settings/ConfigureProfileFragment.java | 34 ++++++++++++++++--- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 912110e8e6..bbbc28649f 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -1650,6 +1650,7 @@ public class SettingsHelper { private File file; private String latestChanges; private boolean askBeforeImport; + private boolean forceImport; private int version; private SettingsImportListener listener; @@ -1660,12 +1661,13 @@ public class SettingsHelper { private AlertDialog dialog; ImportAsyncTask(@NonNull File settingsFile, String latestChanges, int version, boolean askBeforeImport, - @Nullable SettingsImportListener listener) { + boolean forceImport, @Nullable SettingsImportListener listener) { this.file = settingsFile; this.listener = listener; this.latestChanges = latestChanges; this.version = version; this.askBeforeImport = askBeforeImport; + this.forceImport = forceImport; importer = new SettingsImporter(app); collectOnly = true; } @@ -1692,7 +1694,7 @@ public class SettingsHelper { @Override protected List doInBackground(Void... voids) { - if (collectOnly) { + if (collectOnly || forceImport) { try { return importer.collectItems(file); } catch (IllegalArgumentException e) { @@ -1709,10 +1711,15 @@ public class SettingsHelper { @Override protected void onPostExecute(List items) { this.items = items; - if (collectOnly) { + if (collectOnly && !forceImport) { listener.onSettingsImportFinished(true, false, items); } else { if (items != null && items.size() > 0) { + if (forceImport) { + for (SettingsItem item : items) { + item.setShouldReplace(true); + } + } processNextItem(); } } @@ -1864,8 +1871,8 @@ public class SettingsHelper { } public void importSettings(@NonNull File settingsFile, String latestChanges, int version, - boolean askBeforeImport, @Nullable SettingsImportListener listener) { - new ImportAsyncTask(settingsFile, latestChanges, version, askBeforeImport, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + boolean askBeforeImport, boolean forceImport, @Nullable SettingsImportListener listener) { + new ImportAsyncTask(settingsFile, latestChanges, version, askBeforeImport, forceImport, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } public void importSettings(@NonNull File settingsFile, @NonNull List items, String latestChanges, int version, @Nullable SettingsImportListener listener) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index b93a803b8a..18acda5a91 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -771,7 +771,7 @@ public class ImportHelper { File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); final File file = new File(tempDir, name); if (error == null && file.exists()) { - app.getSettingsHelper().importSettings(file, latestChanges, version, askBeforeImport, new SettingsImportListener() { + app.getSettingsHelper().importSettings(file, latestChanges, version, askBeforeImport, false, new SettingsImportListener() { @Override public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { if (isActivityNotDestroyed(activity)) { diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java index d043ba9a21..b9c32cc34e 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java @@ -28,11 +28,13 @@ import android.widget.TextView; import android.widget.Toast; import net.osmand.AndroidUtils; +import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; +import net.osmand.plus.SettingsHelper; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.helpers.AndroidUiHelper; @@ -165,12 +167,35 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co @Override public void resetAppModePrefs(ApplicationMode appMode) { if (appMode != null) { - app.getSettings().resetPreferencesForProfile(appMode); - app.showToastMessage(R.string.profile_prefs_reset_successful); - updateCopiedOrResetPrefs(); + if (appMode.isCustomProfile()) { + File file = getBackupFileForCustomMode(appMode); + if (file.exists()) { + app.getSettingsHelper().importSettings(file, "", 1, false, true, new SettingsHelper.SettingsImportListener() { + @Override + public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { + app.showToastMessage(R.string.profile_prefs_reset_successful); + updateCopiedOrResetPrefs(); + } + }); + } + } else { + app.getSettings().resetPreferencesForProfile(appMode); + 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); + if (!backupDir.exists()) { + backupDir.mkdirs(); + } + + return new File(backupDir, fileName); + } + private void updateCopiedOrResetPrefs() { MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { @@ -303,7 +328,8 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co private void setupResetToDefaultPref() { Preference resetToDefault = findPreference(RESET_TO_DEFAULT); - if (getSelectedAppMode().isCustomProfile()) { + if (getSelectedAppMode().isCustomProfile() + && !getBackupFileForCustomMode(getSelectedAppMode()).exists()) { resetToDefault.setVisible(false); } else { resetToDefault.setIcon(app.getUIUtilities().getIcon(R.drawable.ic_action_reset_to_default_dark, From d791e9eb5d7b0fa091e9d0ec651173f63fd6c6a5 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 28 Feb 2020 10:20:44 +0200 Subject: [PATCH 04/16] Fix possible npe --- .../search/QuickSearchCustomPoiFragment.java | 25 +++++++------------ .../settings/ProfileAppearanceFragment.java | 2 +- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchCustomPoiFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchCustomPoiFragment.java index 35ab9b2a8e..2da25b5552 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchCustomPoiFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchCustomPoiFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.search; +import android.annotation.SuppressLint; import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.Drawable; @@ -26,10 +27,10 @@ import android.widget.TextView; import net.osmand.osm.PoiCategory; import net.osmand.osm.PoiType; import net.osmand.plus.DialogListItemAdapter; -import net.osmand.plus.UiUtilities; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; +import net.osmand.plus.UiUtilities; import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.render.RenderingIcons; @@ -170,8 +171,9 @@ public class QuickSearchCustomPoiFragment extends DialogFragment { public void onDismiss(DialogInterface dialog) { if (editMode) { QuickSearchDialogFragment quickSearchDialogFragment = getQuickSearchDialogFragment(); - if (quickSearchDialogFragment != null) { - getMyApplication().getSearchUICore().refreshCustomPoiFilters(); + OsmandApplication app = getMyApplication(); + if (app != null && quickSearchDialogFragment != null) { + app.getSearchUICore().refreshCustomPoiFilters(); quickSearchDialogFragment.replaceQueryWithUiFilter(filter, ""); quickSearchDialogFragment.reloadCategories(); } @@ -313,25 +315,16 @@ public class QuickSearchCustomPoiFragment extends DialogFragment { } } + @SuppressLint("SetTextI18n") private void saveFilter() { helper.editPoiFilter(filter); - if (!editMode) { + Context ctx = getContext(); + if (ctx != null) { if (filter.isEmpty()) { bottomBarShadow.setVisibility(View.GONE); bottomBar.setVisibility(View.GONE); } else { - barTitle.setText(getContext().getString(R.string.selected_categories) + ": " + filter - .getAcceptedTypesCount()); - bottomBarShadow.setVisibility(View.VISIBLE); - bottomBar.setVisibility(View.VISIBLE); - } - } else { - if (filter.isEmpty()) { - bottomBarShadow.setVisibility(View.GONE); - bottomBar.setVisibility(View.GONE); - } else { - barTitle.setText(getContext().getString(R.string.selected_categories) + ": " + filter - .getAcceptedTypesCount()); + barTitle.setText(ctx.getString(R.string.selected_categories) + ": " + filter.getAcceptedTypesCount()); bottomBarShadow.setVisibility(View.VISIBLE); bottomBar.setVisibility(View.VISIBLE); } diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index 7310cc85cf..911297c380 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -721,7 +721,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { @Override public void onSettingsExportFinished(@NonNull File file, boolean succeed) { ProgressDialog progress = progressRef.get(); - if (progress != null) { + if (progress != null && progress.isShowing()) { progress.dismiss(); } if (succeed) { From 5c0f88b37a76881d2670e40f6e1a9c420da95085 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Fri, 28 Feb 2020 15:30:24 +0200 Subject: [PATCH 05/16] Remove unnecessary boolean flags --- .../src/net/osmand/plus/SettingsHelper.java | 28 +++++-------------- .../net/osmand/plus/helpers/ImportHelper.java | 18 ++++++------ ...electProfileBottomSheetDialogFragment.java | 15 +++++----- .../settings/ConfigureProfileFragment.java | 2 +- .../plus/settings/MainSettingsFragment.java | 7 +++-- 5 files changed, 28 insertions(+), 42 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 48a070bbb4..814db90a06 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -49,7 +49,6 @@ import java.io.OutputStream; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -1804,8 +1803,6 @@ public class SettingsHelper { private File file; private String latestChanges; - private boolean askBeforeImport; - private boolean forceImport; private int version; private SettingsImportListener listener; @@ -1815,14 +1812,11 @@ public class SettingsHelper { private SettingsItem currentItem; private AlertDialog dialog; - ImportAsyncTask(@NonNull File settingsFile, String latestChanges, int version, boolean askBeforeImport, - boolean forceImport, @Nullable SettingsImportListener listener) { + ImportAsyncTask(@NonNull File settingsFile, String latestChanges, int version, @Nullable SettingsImportListener listener) { this.file = settingsFile; this.listener = listener; this.latestChanges = latestChanges; this.version = version; - this.askBeforeImport = askBeforeImport; - this.forceImport = forceImport; importer = new SettingsImporter(app); collectOnly = true; } @@ -1849,7 +1843,7 @@ public class SettingsHelper { @Override protected List doInBackground(Void... voids) { - if (collectOnly || forceImport) { + if (collectOnly) { try { return importer.collectItems(file); } catch (IllegalArgumentException e) { @@ -1868,17 +1862,10 @@ public class SettingsHelper { if (items != null) { this.items = items; } - if (collectOnly && !forceImport) { + if (collectOnly) { listener.onSettingsImportFinished(true, false, this.items); - } else { - if (items != null && items.size() > 0) { - if (forceImport) { - for (SettingsItem item : items) { - item.setShouldReplace(true); - } - } - processNextItem(); - } + } else if (items != null && items.size() > 0) { + processNextItem(); } } @@ -2027,9 +2014,8 @@ public class SettingsHelper { } } - public void importSettings(@NonNull File settingsFile, String latestChanges, int version, - boolean askBeforeImport, boolean forceImport, @Nullable SettingsImportListener listener) { - new ImportAsyncTask(settingsFile, latestChanges, version, askBeforeImport, forceImport, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + public void importSettings(@NonNull File settingsFile, String latestChanges, int version, @Nullable SettingsImportListener listener) { + new ImportAsyncTask(settingsFile, latestChanges, version, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } public void importSettings(@NonNull File settingsFile, @NonNull List items, String latestChanges, int version, @Nullable SettingsImportListener listener) { diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index 0b92796a13..dc78b7a0f3 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -183,7 +183,7 @@ public class ImportHelper { } else if (fileName != null && fileName.endsWith(IndexConstants.SQLITE_EXT)) { handleSqliteTileImport(intentUri, fileName); } else if (fileName != null && fileName.endsWith(OSMAND_SETTINGS_FILE_EXT)) { - handleOsmAndSettingsImport(intentUri, fileName, extras, true, null); + handleOsmAndSettingsImport(intentUri, fileName, extras, null); } else if (fileName != null && fileName.endsWith(ROUTING_FILE_EXT)) { handleRoutingFileImport(intentUri, fileName, null); } else { @@ -607,7 +607,7 @@ public class ImportHelper { }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - public void chooseFileToImport(final ImportType importType, final boolean askBeforeImport, final CallbackWithObject callback) { + public void chooseFileToImport(final ImportType importType, final CallbackWithObject callback) { final MapActivity mapActivity = getMapActivity(); if (mapActivity == null) { return; @@ -645,7 +645,7 @@ public class ImportHelper { if (fileName.endsWith(importType.getExtension())) { if (importType.equals(ImportType.SETTINGS)) { - handleOsmAndSettingsImport(data, fileName, resultData.getExtras(), askBeforeImport, callback); + handleOsmAndSettingsImport(data, fileName, resultData.getExtras(), callback); } else if (importType.equals(ImportType.ROUTING)){ handleRoutingFileImport(data, fileName, callback); } @@ -733,20 +733,20 @@ public class ImportHelper { routingImportTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } - - private void handleOsmAndSettingsImport(Uri intentUri, String fileName, Bundle extras, boolean askBeforeImport, CallbackWithObject> callback) { + + 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)) { int version = extras.getInt(SettingsHelper.SETTINGS_VERSION_KEY, -1); String latestChanges = extras.getString(SettingsHelper.SETTINGS_LATEST_CHANGES_KEY); - handleOsmAndSettingsImport(intentUri, fileName, latestChanges, version, askBeforeImport, callback); + handleOsmAndSettingsImport(intentUri, fileName, latestChanges, version, callback); } else { - handleOsmAndSettingsImport(intentUri, fileName, null, -1, askBeforeImport, callback); + handleOsmAndSettingsImport(intentUri, fileName, null, -1, callback); } } @SuppressLint("StaticFieldLeak") private void handleOsmAndSettingsImport(final Uri uri, final String name, final String latestChanges, final int version, - final boolean askBeforeImport, final CallbackWithObject> callback) { + final CallbackWithObject> callback) { final AsyncTask settingsImportTask = new AsyncTask() { ProgressDialog progress; @@ -771,7 +771,7 @@ public class ImportHelper { File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); final File file = new File(tempDir, name); if (error == null && file.exists()) { - app.getSettingsHelper().importSettings(file, latestChanges, version, askBeforeImport, false, new SettingsImportListener() { + app.getSettingsHelper().importSettings(file, latestChanges, version, new SettingsImportListener() { @Override public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items) { if (isActivityNotDestroyed(activity)) { diff --git a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheetDialogFragment.java index b9148e9b12..887289bfdc 100644 --- a/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/profiles/SelectProfileBottomSheetDialogFragment.java @@ -160,14 +160,13 @@ public class SelectProfileBottomSheetDialogFragment extends BasePreferenceBottom if (mapActivity == null) { return; } - mapActivity.getImportHelper().chooseFileToImport(ROUTING, false, - new CallbackWithObject() { - @Override - public boolean processResult(RoutingConfiguration.Builder builder) { - refreshView(); - return false; - } - }); + mapActivity.getImportHelper().chooseFileToImport(ROUTING, new CallbackWithObject() { + @Override + public boolean processResult(RoutingConfiguration.Builder builder) { + refreshView(); + return false; + } + }); } }); items.add(new BaseBottomSheetItem.Builder() diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java index b9c32cc34e..d0a3dff90b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java @@ -170,7 +170,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co if (appMode.isCustomProfile()) { File file = getBackupFileForCustomMode(appMode); if (file.exists()) { - app.getSettingsHelper().importSettings(file, "", 1, false, true, new SettingsHelper.SettingsImportListener() { + 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); diff --git a/OsmAnd/src/net/osmand/plus/settings/MainSettingsFragment.java b/OsmAnd/src/net/osmand/plus/settings/MainSettingsFragment.java index ca6f0dcdab..d452cbad90 100644 --- a/OsmAnd/src/net/osmand/plus/settings/MainSettingsFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/MainSettingsFragment.java @@ -16,7 +16,8 @@ import net.osmand.CallbackWithObject; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.SettingsHelper.*; +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.helpers.AndroidUiHelper; @@ -127,8 +128,8 @@ public class MainSettingsFragment extends BaseSettingsFragment { } else if (IMPORT_PROFILE.equals(prefId)) { final MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { - mapActivity.getImportHelper().chooseFileToImport(SETTINGS, false, new CallbackWithObject>() { - + mapActivity.getImportHelper().chooseFileToImport(SETTINGS, new CallbackWithObject>() { + @Override public boolean processResult(List result) { for (SettingsItem item : result) { From 2a225f8659fd37a5bc73ddd7ae9a78bda00d82e9 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 2 Mar 2020 12:10:15 +0200 Subject: [PATCH 06/16] 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); From 56eda77a2cc49ff36fd8ac11fcf596d70df19d41 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 2 Mar 2020 17:39:19 +0200 Subject: [PATCH 07/16] Fix compilation --- OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java index ba1aad79a8..13748cd821 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ImportHelper.java @@ -915,7 +915,7 @@ public class ImportHelper { } else { app.showShortToastMessage(app.getString(R.string.file_import_error, mFileName, error)); } - if (isActivityNotDestroyed(activity)) { + if (AndroidUtils.isActivityNotDestroyed(activity)) { progress.dismiss(); } } From ca742998dc632d80deff870bef609a00064449f2 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 3 Mar 2020 21:45:43 +0200 Subject: [PATCH 08/16] Update restore and reset dialog UI --- OsmAnd/res/values/strings.xml | 2 ++ .../ResetProfilePrefsBottomSheet.java | 20 ++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 20baa92d0d..44e972a76f 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,8 @@ Thx - Hardy --> + All profile settings will be restored to their original state after creating/importing this profile. + Restore all profile settings? Saving new profile Could not back up profile. Import rendering file diff --git a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/ResetProfilePrefsBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/ResetProfilePrefsBottomSheet.java index 70bd98036e..7a15cf9862 100644 --- a/OsmAnd/src/net/osmand/plus/settings/bottomsheets/ResetProfilePrefsBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/bottomsheets/ResetProfilePrefsBottomSheet.java @@ -31,9 +31,12 @@ public class ResetProfilePrefsBottomSheet extends BasePreferenceBottomSheet { return; } - items.add(new TitleItem(getString(R.string.reset_all_profile_settings))); - ApplicationMode mode = getAppMode(); + boolean customProfile = mode.isCustomProfile(); + + String title = getString(customProfile ? R.string.restore_all_profile_settings : R.string.reset_all_profile_settings); + items.add(new TitleItem(title)); + int profileColor = mode.getIconColorInfo().getColor(nightMode); int colorNoAlpha = ContextCompat.getColor(ctx, profileColor); @@ -52,12 +55,15 @@ public class ResetProfilePrefsBottomSheet extends BasePreferenceBottomSheet { .create(); items.add(profileItem); - StringBuilder description = new StringBuilder(getString(R.string.reset_confirmation_descr, getString(R.string.shared_string_reset))); - description.append("\n\n"); - description.append(getString(R.string.reset_all_profile_settings_descr)); + String restoreDescr = getString(customProfile ? R.string.shared_string_restore : R.string.shared_string_reset); + String description = getString(customProfile ? R.string.restore_all_profile_settings_descr : R.string.reset_all_profile_settings_descr); + + StringBuilder stringBuilder = new StringBuilder(description); + stringBuilder.append("\n\n"); + stringBuilder.append(getString(R.string.reset_confirmation_descr, restoreDescr)); BaseBottomSheetItem resetAllSettings = new BottomSheetItemWithDescription.Builder() - .setDescription(description) + .setDescription(stringBuilder) .setLayoutId(R.layout.bottom_sheet_item_pref_info) .create(); items.add(resetAllSettings); @@ -65,7 +71,7 @@ public class ResetProfilePrefsBottomSheet extends BasePreferenceBottomSheet { @Override protected int getRightBottomButtonTextId() { - return R.string.shared_string_reset; + return getAppMode().isCustomProfile() ? R.string.shared_string_restore : R.string.shared_string_reset; } @Override From 6262c8ca71bf744da1a3aa73850560934a40cb40 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 3 Mar 2020 22:19:51 +0200 Subject: [PATCH 09/16] Add ability to check exported profile files --- .../src/net/osmand/plus/SettingsHelper.java | 23 +++++++-- .../settings/ConfigureProfileFragment.java | 26 +++++----- .../settings/ProfileAppearanceFragment.java | 51 +++++++++++++++++-- 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/SettingsHelper.java index 814db90a06..45568aa58c 100644 --- a/OsmAnd/src/net/osmand/plus/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/SettingsHelper.java @@ -101,6 +101,7 @@ public class SettingsHelper { private boolean importSuspended; private boolean collectOnly; private ImportAsyncTask importTask; + private Map exportAsyncTasks = new HashMap<>(); public interface SettingsImportListener { void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List items); @@ -1930,6 +1931,17 @@ public class SettingsHelper { return this.importTask.getFile(); } + public boolean isFileExporting(File file) { + return exportAsyncTasks.containsKey(file); + } + + public void updateExportListener(File file, SettingsExportListener listener) { + ExportAsyncTask exportAsyncTask = exportAsyncTasks.get(file); + if (exportAsyncTask != null) { + exportAsyncTask.listener = listener; + } + } + @SuppressLint("StaticFieldLeak") private class ImportItemsAsyncTask extends AsyncTask { @@ -2008,6 +2020,7 @@ public class SettingsHelper { @Override protected void onPostExecute(Boolean success) { + exportAsyncTasks.remove(file); if (listener != null) { listener.onSettingsExportFinished(file, success); } @@ -2022,11 +2035,11 @@ public class SettingsHelper { new ImportAsyncTask(settingsFile, items, latestChanges, version, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - public void exportSettings(@NonNull File fileDir, @NonNull String fileName, - @Nullable SettingsExportListener listener, - @NonNull List items) { - new ExportAsyncTask(new File(fileDir, fileName + OSMAND_SETTINGS_FILE_EXT), listener, items) - .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, @NonNull List items) { + File file = new File(fileDir, fileName + OSMAND_SETTINGS_FILE_EXT); + ExportAsyncTask exportAsyncTask = new ExportAsyncTask(file, listener, items); + exportAsyncTasks.put(file, exportAsyncTask); + exportAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, diff --git a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java index 86cba3e90c..005ae747c9 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ConfigureProfileFragment.java @@ -168,7 +168,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co public void resetAppModePrefs(ApplicationMode appMode) { if (appMode != null) { if (appMode.isCustomProfile()) { - File file = getBackupFileForCustomMode(appMode); + File file = getBackupFileForCustomMode(app, appMode.getStringKey()); if (file.exists()) { restoreCustomModeFromFile(file); } @@ -204,16 +204,6 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co }); } - private File getBackupFileForCustomMode(ApplicationMode appMode) { - String fileName = appMode.getStringKey() + IndexConstants.OSMAND_SETTINGS_FILE_EXT; - File backupDir = app.getAppPath(IndexConstants.BACKUP_INDEX_DIR); - if (!backupDir.exists()) { - backupDir.mkdirs(); - } - - return new File(backupDir, fileName); - } - private void updateCopiedOrResetPrefs() { MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { @@ -346,8 +336,8 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co private void setupResetToDefaultPref() { Preference resetToDefault = findPreference(RESET_TO_DEFAULT); - if (getSelectedAppMode().isCustomProfile() - && !getBackupFileForCustomMode(getSelectedAppMode()).exists()) { + ApplicationMode mode = getSelectedAppMode(); + if (mode.isCustomProfile() && !getBackupFileForCustomMode(app, mode.getStringKey()).exists()) { resetToDefault.setVisible(false); } else { resetToDefault.setIcon(app.getUIUtilities().getIcon(R.drawable.ic_action_reset_to_default_dark, @@ -470,4 +460,14 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co } } } + + public static File getBackupFileForCustomMode(OsmandApplication app, String appModeKey) { + String fileName = appModeKey + IndexConstants.OSMAND_SETTINGS_FILE_EXT; + File backupDir = app.getAppPath(IndexConstants.BACKUP_INDEX_DIR); + if (!backupDir.exists()) { + backupDir.mkdirs(); + } + + return new File(backupDir, fileName); + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index 911297c380..a2fb253be5 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -53,7 +53,6 @@ import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; import java.io.File; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; @@ -69,6 +68,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { private static final Log LOG = PlatformUtil.getLog(ProfileAppearanceFragment.class); public static final String TAG = ProfileAppearanceFragment.class.getName(); + private static final String MASTER_PROFILE = "master_profile"; private static final String PROFILE_NAME = "profile_name"; private static final String SELECT_COLOR = "select_color"; @@ -89,7 +89,13 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { public static final String PROFILE_NAVIGATION_ICON_KEY = "profile_navigation_icon_key"; public static final String BASE_PROFILE_FOR_NEW = "base_profile_for_new"; public static final String IS_BASE_PROFILE_IMPORTED = "is_base_profile_imported"; + public static final String IS_NEW_PROFILE_KEY = "is_new_profile_key"; + private SelectProfileBottomSheetDialogFragment.SelectProfileListener parentProfileListener; + private SettingsHelper.SettingsExportListener exportListener; + + private ProgressDialog progress; + private EditText baseProfileName; private ApplicationProfileObject profile; private ApplicationProfileObject changedProfile; @@ -108,6 +114,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); profile = new ApplicationProfileObject(); + exportListener = getSettingsExportListener(); ApplicationMode baseModeForNewProfile = null; if (getArguments() != null) { Bundle arguments = getArguments(); @@ -139,8 +146,8 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { changedProfile.routeService = profile.routeService; changedProfile.locationIcon = profile.locationIcon; changedProfile.navigationIcon = profile.navigationIcon; + isNewProfile = ApplicationMode.valueOfStringKey(changedProfile.stringKey, null) == null; } - isNewProfile = ApplicationMode.valueOfStringKey(changedProfile.stringKey, null) == null; } public void setupAppProfileObjectFromAppMode(ApplicationMode baseModeForNewProfile) { @@ -275,6 +282,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { if (changedProfile.parent != null) { outState.putString(PROFILE_PARENT_KEY, changedProfile.parent.getStringKey()); } + outState.putBoolean(IS_NEW_PROFILE_KEY, isNewProfile); outState.putBoolean(IS_BASE_PROFILE_IMPORTED, isBaseProfileImported); outState.putSerializable(PROFILE_LOCATION_ICON_KEY, changedProfile.locationIcon); outState.putSerializable(PROFILE_NAVIGATION_ICON_KEY, changedProfile.navigationIcon); @@ -290,6 +298,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { isBaseProfileImported = savedInstanceState.getBoolean(IS_BASE_PROFILE_IMPORTED); changedProfile.locationIcon = (LocationIcon) savedInstanceState.getSerializable(PROFILE_LOCATION_ICON_KEY); changedProfile.navigationIcon = (NavigationIcon) savedInstanceState.getSerializable(PROFILE_NAVIGATION_ICON_KEY); + isNewProfile = savedInstanceState.getBoolean(IS_NEW_PROFILE_KEY); } @Override @@ -635,6 +644,27 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { return parentProfileListener; } + public SettingsHelper.SettingsExportListener getSettingsExportListener() { + if (exportListener == null) { + exportListener = new SettingsHelper.SettingsExportListener() { + + @Override + public void onSettingsExportFinished(@NonNull File file, boolean succeed) { + FragmentActivity activity = getActivity(); + if (progress != null && activity != null && AndroidUtils.isActivityNotDestroyed(activity)) { + progress.dismiss(); + } + if (succeed) { + customProfileSaved(); + } else { + app.showToastMessage(R.string.profile_backup_failed); + } + } + }; + } + return exportListener; + } + void updateParentProfile(String profileKey, boolean isBaseProfileImported) { deleteImportedProfile(); setupBaseProfileView(profileKey); @@ -675,7 +705,10 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { mode.setLocationIcon(changedProfile.locationIcon); mode.setNavigationIcon(changedProfile.navigationIcon); - profileSaved(); + FragmentActivity activity = getActivity(); + if (activity != null) { + activity.onBackPressed(); + } } } @@ -761,6 +794,18 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { } } + private void customProfileSaved() { + FragmentActivity activity = getActivity(); + if (activity != null) { + FragmentManager fragmentManager = activity.getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(); + BaseSettingsFragment.showInstance(activity, SettingsScreenType.CONFIGURE_PROFILE, + ApplicationMode.valueOfStringKey(changedProfile.stringKey, null)); + } + } + } + private String getUniqueStringKey(ApplicationMode mode) { return mode.getStringKey() + "_" + System.currentTimeMillis(); } From cdbdf4a78baf775ede4cec9a9d098ee5c6e174e2 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Wed, 4 Mar 2020 12:38:50 +0200 Subject: [PATCH 10/16] Show progress dialog after screen rotation --- .../settings/ProfileAppearanceFragment.java | 73 ++++++++----------- 1 file changed, 29 insertions(+), 44 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index a2fb253be5..472f8700d2 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -15,6 +15,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v7.app.AlertDialog; @@ -132,6 +133,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { changedProfile = new ApplicationProfileObject(); if (savedInstanceState != null) { restoreState(savedInstanceState); + checkSavingProfile(); } else { changedProfile.stringKey = profile.stringKey; changedProfile.parent = profile.parent; @@ -693,7 +695,19 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { private void saveProfile() { if (isNewProfile) { - showNewProfileSavingDialog(); + DialogInterface.OnShowListener showListener = new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + app.runInUIThread(new Runnable() { + @Override + public void run() { + ApplicationMode mode = saveNewProfile(); + saveProfileBackup(mode, exportListener); + } + }); + } + }; + showNewProfileSavingDialog(showListener); } else { ApplicationMode mode = getSelectedAppMode(); mode.setParentAppMode(changedProfile.parent); @@ -743,54 +757,25 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { } } - private void showNewProfileSavingDialog() { - ProgressDialog progress = new ProgressDialog(getContext()); + private void showNewProfileSavingDialog(@Nullable DialogInterface.OnShowListener showListener) { + if (progress != null) { + progress.dismiss(); + } + progress = new ProgressDialog(getContext()); progress.setMessage(getString(R.string.saving_new_profile)); progress.setCancelable(false); - - final WeakReference progressRef = new WeakReference<>(progress); - final SettingsHelper.SettingsExportListener listener = new SettingsHelper.SettingsExportListener() { - - @Override - public void onSettingsExportFinished(@NonNull File file, boolean succeed) { - ProgressDialog progress = progressRef.get(); - if (progress != null && progress.isShowing()) { - progress.dismiss(); - } - if (succeed) { - profileSaved(); - } else { - app.showToastMessage(R.string.profile_backup_failed); - } - } - }; - - progress.setOnShowListener(new DialogInterface.OnShowListener() { - @Override - public void onShow(DialogInterface dialog) { - app.runInUIThread(new Runnable() { - @Override - public void run() { - ApplicationMode mode = saveNewProfile(); - saveProfileBackup(mode, listener); - } - }); - } - }); + progress.setOnShowListener(showListener); progress.show(); } - private void profileSaved() { - FragmentActivity activity = getActivity(); - if (activity != null) { - profile = changedProfile; - if (isNewProfile) { - ProfileAppearanceFragment.this.dismiss(); - BaseSettingsFragment.showInstance(activity, SettingsScreenType.CONFIGURE_PROFILE, - ApplicationMode.valueOfStringKey(changedProfile.stringKey, null)); - } else { - activity.onBackPressed(); - } + private void checkSavingProfile() { + File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey); + boolean fileExporting = app.getSettingsHelper().isFileExporting(file); + if (fileExporting) { + showNewProfileSavingDialog(null); + app.getSettingsHelper().updateExportListener(file, exportListener); + } else if (isNewProfile) { + customProfileSaved(); } } From a3006a5127636da38ad7c18b71a6c1df76a1496d Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 5 Mar 2020 11:01:29 +0200 Subject: [PATCH 11/16] Fix compilation --- .../src/net/osmand/plus/settings/ProfileAppearanceFragment.java | 1 + 1 file changed, 1 insertion(+) diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index 559aaf08b6..59bdcb2050 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -23,6 +23,7 @@ import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; From 19e58c33a538ff65b8bd3f54f7eb8feb15885bb1 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 5 Mar 2020 14:48:21 +0200 Subject: [PATCH 12/16] Clear export listener after pause --- .../settings/ProfileAppearanceFragment.java | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index 59bdcb2050..c05ce99db9 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -82,16 +82,16 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { private static final String SELECT_NAV_ICON = "select_nav_icon"; private static final String NAV_ICON_ITEMS = "nav_icon_items"; - public static final String PROFILE_NAME_KEY = "profile_name_key"; - public static final String PROFILE_STRINGKEY_KEY = "profile_stringkey_key"; - public static final String PROFILE_ICON_RES_KEY = "profile_icon_res_key"; - public static final String PROFILE_COLOR_KEY = "profile_color_key"; - public static final String PROFILE_PARENT_KEY = "profile_parent_key"; - public static final String PROFILE_LOCATION_ICON_KEY = "profile_location_icon_key"; - public static final String PROFILE_NAVIGATION_ICON_KEY = "profile_navigation_icon_key"; - public static final String BASE_PROFILE_FOR_NEW = "base_profile_for_new"; - public static final String IS_BASE_PROFILE_IMPORTED = "is_base_profile_imported"; - public static final String IS_NEW_PROFILE_KEY = "is_new_profile_key"; + private static final String PROFILE_NAME_KEY = "profile_name_key"; + private static final String PROFILE_STRINGKEY_KEY = "profile_stringkey_key"; + private static final String PROFILE_ICON_RES_KEY = "profile_icon_res_key"; + private static final String PROFILE_COLOR_KEY = "profile_color_key"; + private static final String PROFILE_PARENT_KEY = "profile_parent_key"; + private static final String PROFILE_LOCATION_ICON_KEY = "profile_location_icon_key"; + private static final String PROFILE_NAVIGATION_ICON_KEY = "profile_navigation_icon_key"; + private static final String BASE_PROFILE_FOR_NEW = "base_profile_for_new"; + private static final String IS_BASE_PROFILE_IMPORTED = "is_base_profile_imported"; + private static final String IS_NEW_PROFILE_KEY = "is_new_profile_key"; private SelectProfileBottomSheetDialogFragment.SelectProfileListener parentProfileListener; private SettingsHelper.SettingsExportListener exportListener; @@ -424,6 +424,26 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { } } + @Override + public void onResume() { + super.onResume(); + updateExportListener(exportListener); + } + + @Override + public void onPause() { + super.onPause(); + updateExportListener(null); + } + + private void updateExportListener(SettingsHelper.SettingsExportListener listener) { + File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey); + boolean fileExporting = app.getSettingsHelper().isFileExporting(file); + if (fileExporting) { + app.getSettingsHelper().updateExportListener(file, listener); + } + } + private View createColorItemView(final ProfileIconColors colorRes, ViewGroup rootView) { FrameLayout colorItemView = (FrameLayout) UiUtilities.getInflater(getContext(), isNightMode()) .inflate(R.layout.preference_circle_item, rootView, false); @@ -647,7 +667,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { return parentProfileListener; } - public SettingsHelper.SettingsExportListener getSettingsExportListener() { + private SettingsHelper.SettingsExportListener getSettingsExportListener() { if (exportListener == null) { exportListener = new SettingsHelper.SettingsExportListener() { @@ -668,7 +688,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { return exportListener; } - void updateParentProfile(String profileKey, boolean isBaseProfileImported) { + private void updateParentProfile(String profileKey, boolean isBaseProfileImported) { deleteImportedProfile(); setupBaseProfileView(profileKey); changedProfile.parent = ApplicationMode.valueOfStringKey(profileKey, ApplicationMode.DEFAULT); @@ -703,7 +723,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { @Override public void run() { ApplicationMode mode = saveNewProfile(); - saveProfileBackup(mode, exportListener); + saveProfileBackup(mode); } }); } @@ -748,13 +768,13 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { return mode; } - private void saveProfileBackup(ApplicationMode mode, SettingsHelper.SettingsExportListener listener) { + private void saveProfileBackup(ApplicationMode mode) { if (app != null) { File tempDir = app.getAppPath(IndexConstants.BACKUP_INDEX_DIR); if (!tempDir.exists()) { tempDir.mkdirs(); } - app.getSettingsHelper().exportSettings(tempDir, mode.getStringKey(), listener, new SettingsHelper.ProfileSettingsItem(app, mode)); + app.getSettingsHelper().exportSettings(tempDir, mode.getStringKey(), exportListener, new SettingsHelper.ProfileSettingsItem(app, mode)); } } From 2e89b0f3ab78dce44eeaa811533d4c2dbc0f5835 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 5 Mar 2020 17:39:36 +0200 Subject: [PATCH 13/16] Fix progress dialog in export screen --- .../settings/ExportProfileBottomSheet.java | 123 ++++++++++++++---- .../settings/ProfileAppearanceFragment.java | 47 ++++--- 2 files changed, 123 insertions(+), 47 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java index 777cfad65b..8df2fa2fd0 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java @@ -17,6 +17,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.widget.SwitchCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; @@ -45,7 +46,6 @@ import net.osmand.plus.settings.bottomsheets.BasePreferenceBottomSheet; import org.apache.commons.logging.Log; import java.io.File; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -54,29 +54,42 @@ import java.util.Set; public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { - private static final Log LOG = PlatformUtil.getLog(ExportProfileBottomSheet.class); public static final String TAG = ExportProfileBottomSheet.class.getSimpleName(); + + private static final Log LOG = PlatformUtil.getLog(ExportProfileBottomSheet.class); + private static final String INCLUDE_ADDITIONAL_DATA_KEY = "INCLUDE_ADDITIONAL_DATA_KEY"; - private boolean includeAdditionalData = false; + private static final String EXPORTING_PROFILE_KEY = "exporting_profile_key"; + private OsmandApplication app; private ApplicationMode profile; private List dataList = new ArrayList<>(); private ExportImportSettingsAdapter adapter; + private SettingsHelper.SettingsExportListener exportListener; + private ProgressDialog progress; + + private boolean includeAdditionalData = false; + private boolean exportingProfile = false; + @Override public void onCreate(Bundle savedInstanceState) { - if (savedInstanceState != null) { - includeAdditionalData = savedInstanceState.getBoolean(INCLUDE_ADDITIONAL_DATA_KEY); - } super.onCreate(savedInstanceState); app = requiredMyApplication(); + profile = getAppMode(); dataList = getAdditionalData(); + exportListener = getSettingsExportListener(); + if (savedInstanceState != null) { + includeAdditionalData = savedInstanceState.getBoolean(INCLUDE_ADDITIONAL_DATA_KEY); + exportingProfile = savedInstanceState.getBoolean(EXPORTING_PROFILE_KEY); + } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean(INCLUDE_ADDITIONAL_DATA_KEY, includeAdditionalData); + outState.putBoolean(EXPORTING_PROFILE_KEY, exportingProfile); } @Override @@ -87,8 +100,6 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { } LayoutInflater inflater = UiUtilities.getInflater(app, nightMode); - profile = getAppMode(); - int profileColor = profile.getIconColorInfo().getColor(nightMode); int colorNoAlpha = ContextCompat.getColor(context, profileColor); @@ -189,6 +200,21 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { return true; } + @Override + public void onResume() { + super.onResume(); + checkExportingFile(); + } + + @Override + public void onPause() { + super.onPause(); + if (exportingProfile) { + File file = getExportFile(); + app.getSettingsHelper().updateExportListener(file, null); + } + } + private List getAdditionalData() { List dataList = new ArrayList<>(); @@ -304,37 +330,82 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { } private void prepareFile() { - Context context = getContext(); - if (app != null && context != null) { - File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); - if (!tempDir.exists()) { - tempDir.mkdirs(); - } + if (app != null) { + exportingProfile = true; + showExportProgressDialog(); + File tempDir = getTempDir(); String fileName = profile.toHumanString(); + app.getSettingsHelper().exportSettings(tempDir, fileName, exportListener, prepareSettingsItemsForExport()); + } + } - ProgressDialog progress = new ProgressDialog(context); - progress.setTitle(app.getString(R.string.export_profile)); - progress.setMessage(app.getString(R.string.shared_string_preparing)); - progress.setCancelable(false); - progress.show(); + private void showExportProgressDialog() { + Context context = getContext(); + if (context == null) { + return; + } + if (progress != null) { + progress.dismiss(); + } + progress = new ProgressDialog(context); + progress.setTitle(app.getString(R.string.export_profile)); + progress.setMessage(app.getString(R.string.shared_string_preparing)); + progress.setCancelable(false); + progress.show(); + } - final WeakReference progressRef = new WeakReference<>(progress); + private SettingsHelper.SettingsExportListener getSettingsExportListener() { + if (exportListener == null) { + exportListener = new SettingsHelper.SettingsExportListener() { - app.getSettingsHelper().exportSettings(tempDir, fileName, new SettingsHelper.SettingsExportListener() { @Override public void onSettingsExportFinished(@NonNull File file, boolean succeed) { - ProgressDialog progress = progressRef.get(); - if (progress != null) { - progress.dismiss(); - } + dismissExportProgressDialog(); + exportingProfile = false; if (succeed) { shareProfile(file, profile); } else { app.showToastMessage(R.string.export_profile_failed); } } - }, prepareSettingsItemsForExport()); + }; } + return exportListener; + } + + private void checkExportingFile() { + if (exportingProfile) { + File file = getExportFile(); + boolean fileExporting = app.getSettingsHelper().isFileExporting(file); + if (fileExporting) { + showExportProgressDialog(); + app.getSettingsHelper().updateExportListener(file, exportListener); + } else if (file.exists()) { + dismissExportProgressDialog(); + shareProfile(file, profile); + } + } + } + + private void dismissExportProgressDialog() { + FragmentActivity activity = getActivity(); + if (progress != null && activity != null && AndroidUtils.isActivityNotDestroyed(activity)) { + progress.dismiss(); + } + } + + private File getExportFile() { + File tempDir = getTempDir(); + String fileName = profile.toHumanString(); + return new File(tempDir, fileName + IndexConstants.OSMAND_SETTINGS_FILE_EXT); + } + + private File getTempDir() { + File tempDir = app.getAppPath(IndexConstants.TEMP_DIR); + if (!tempDir.exists()) { + tempDir.mkdirs(); + } + return tempDir; } private void shareProfile(@NonNull File file, @NonNull ApplicationMode profile) { diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index c05ce99db9..11a865696d 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -134,7 +134,6 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { changedProfile = new ApplicationProfileObject(); if (savedInstanceState != null) { restoreState(savedInstanceState); - checkSavingProfile(); } else { changedProfile.stringKey = profile.stringKey; changedProfile.parent = profile.parent; @@ -427,20 +426,18 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { @Override public void onResume() { super.onResume(); - updateExportListener(exportListener); + checkSavingProfile(); } @Override public void onPause() { super.onPause(); - updateExportListener(null); - } - - private void updateExportListener(SettingsHelper.SettingsExportListener listener) { - File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey); - boolean fileExporting = app.getSettingsHelper().isFileExporting(file); - if (fileExporting) { - app.getSettingsHelper().updateExportListener(file, listener); + if (isNewProfile) { + File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey); + boolean fileExporting = app.getSettingsHelper().isFileExporting(file); + if (fileExporting) { + app.getSettingsHelper().updateExportListener(file, null); + } } } @@ -673,10 +670,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { @Override public void onSettingsExportFinished(@NonNull File file, boolean succeed) { - FragmentActivity activity = getActivity(); - if (progress != null && activity != null && AndroidUtils.isActivityNotDestroyed(activity)) { - progress.dismiss(); - } + dismissProfileSavingDialog(); if (succeed) { customProfileSaved(); } else { @@ -717,6 +711,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { private void saveProfile() { if (isNewProfile) { DialogInterface.OnShowListener showListener = new DialogInterface.OnShowListener() { + @Override public void onShow(DialogInterface dialog) { app.runInUIThread(new Runnable() { @@ -790,13 +785,23 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { } private void checkSavingProfile() { - File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey); - boolean fileExporting = app.getSettingsHelper().isFileExporting(file); - if (fileExporting) { - showNewProfileSavingDialog(null); - app.getSettingsHelper().updateExportListener(file, exportListener); - } else if (isNewProfile) { - customProfileSaved(); + if (isNewProfile) { + File file = ConfigureProfileFragment.getBackupFileForCustomMode(app, changedProfile.stringKey); + boolean fileExporting = app.getSettingsHelper().isFileExporting(file); + if (fileExporting) { + showNewProfileSavingDialog(null); + app.getSettingsHelper().updateExportListener(file, exportListener); + } else if (file.exists()) { + dismissProfileSavingDialog(); + customProfileSaved(); + } + } + } + + private void dismissProfileSavingDialog() { + FragmentActivity activity = getActivity(); + if (progress != null && activity != null && AndroidUtils.isActivityNotDestroyed(activity)) { + progress.dismiss(); } } From 2c7430e5676c096e7cc358c6d1f5f7cb17575da6 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 5 Mar 2020 18:06:10 +0200 Subject: [PATCH 14/16] Remove unnecessary call to listener and fix possible crash --- .../mapcontextmenu/other/MapMultiSelectionMenuFragment.java | 6 +++++- .../net/osmand/plus/settings/ExportProfileBottomSheet.java | 5 ++--- .../net/osmand/plus/settings/ProfileAppearanceFragment.java | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapMultiSelectionMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapMultiSelectionMenuFragment.java index d7aefdab98..edcb722eea 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapMultiSelectionMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapMultiSelectionMenuFragment.java @@ -18,6 +18,7 @@ import android.widget.TextView; import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; import com.github.ksoichiro.android.observablescrollview.ObservableListView; import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks; @@ -252,7 +253,10 @@ public class MapMultiSelectionMenuFragment extends Fragment implements MultiSele if (menu.getMapActivity().getContextMenu().isVisible()) { menu.getMapActivity().getContextMenu().hide(); } else { - menu.getMapActivity().getSupportFragmentManager().popBackStack(); + FragmentManager fragmentManager = menu.getMapActivity().getSupportFragmentManager(); + if (!fragmentManager.isStateSaved()) { + fragmentManager.popBackStack(); + } } } } diff --git a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java index 8df2fa2fd0..5f4788b585 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/settings/ExportProfileBottomSheet.java @@ -78,7 +78,6 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { app = requiredMyApplication(); profile = getAppMode(); dataList = getAdditionalData(); - exportListener = getSettingsExportListener(); if (savedInstanceState != null) { includeAdditionalData = savedInstanceState.getBoolean(INCLUDE_ADDITIONAL_DATA_KEY); exportingProfile = savedInstanceState.getBoolean(EXPORTING_PROFILE_KEY); @@ -335,7 +334,7 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { showExportProgressDialog(); File tempDir = getTempDir(); String fileName = profile.toHumanString(); - app.getSettingsHelper().exportSettings(tempDir, fileName, exportListener, prepareSettingsItemsForExport()); + app.getSettingsHelper().exportSettings(tempDir, fileName, getSettingsExportListener(), prepareSettingsItemsForExport()); } } @@ -379,7 +378,7 @@ public class ExportProfileBottomSheet extends BasePreferenceBottomSheet { boolean fileExporting = app.getSettingsHelper().isFileExporting(file); if (fileExporting) { showExportProgressDialog(); - app.getSettingsHelper().updateExportListener(file, exportListener); + app.getSettingsHelper().updateExportListener(file, getSettingsExportListener()); } else if (file.exists()) { dismissExportProgressDialog(); shareProfile(file, profile); diff --git a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java index 11a865696d..209a85aa75 100644 --- a/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/ProfileAppearanceFragment.java @@ -116,7 +116,6 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); profile = new ApplicationProfileObject(); - exportListener = getSettingsExportListener(); ApplicationMode baseModeForNewProfile = null; if (getArguments() != null) { Bundle arguments = getArguments(); @@ -769,7 +768,8 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { if (!tempDir.exists()) { tempDir.mkdirs(); } - app.getSettingsHelper().exportSettings(tempDir, mode.getStringKey(), exportListener, new SettingsHelper.ProfileSettingsItem(app, mode)); + app.getSettingsHelper().exportSettings(tempDir, mode.getStringKey(), + getSettingsExportListener(), new SettingsHelper.ProfileSettingsItem(app, mode)); } } @@ -790,7 +790,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment { boolean fileExporting = app.getSettingsHelper().isFileExporting(file); if (fileExporting) { showNewProfileSavingDialog(null); - app.getSettingsHelper().updateExportListener(file, exportListener); + app.getSettingsHelper().updateExportListener(file, getSettingsExportListener()); } else if (file.exists()) { dismissProfileSavingDialog(); customProfileSaved(); From febe5e33610deed6fa5c5a7e401d8f9f3777635d Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 5 Mar 2020 19:30:07 +0200 Subject: [PATCH 15/16] Fix possible crash and migrate file provider name in manifests --- OsmAnd/AndroidManifest-free.xml | 2 +- OsmAnd/AndroidManifest-freecustom.xml | 2 +- OsmAnd/AndroidManifest-freedev.xml | 2 +- OsmAnd/AndroidManifest-freehuawei.xml | 2 +- OsmAnd/AndroidManifest-huawei.xml | 2 +- .../other/DestinationReachedMenuFragment.java | 8 ++++++-- 6 files changed, 11 insertions(+), 7 deletions(-) diff --git a/OsmAnd/AndroidManifest-free.xml b/OsmAnd/AndroidManifest-free.xml index 0cc15e429d..873848e1e9 100644 --- a/OsmAnd/AndroidManifest-free.xml +++ b/OsmAnd/AndroidManifest-free.xml @@ -20,7 +20,7 @@ android:process="net.osmand"/> diff --git a/OsmAnd/AndroidManifest-freecustom.xml b/OsmAnd/AndroidManifest-freecustom.xml index 3b9368a468..66ced1602a 100644 --- a/OsmAnd/AndroidManifest-freecustom.xml +++ b/OsmAnd/AndroidManifest-freecustom.xml @@ -18,7 +18,7 @@ android:process="net.osmand.freecustom" tools:replace="android:process"/> diff --git a/OsmAnd/AndroidManifest-freedev.xml b/OsmAnd/AndroidManifest-freedev.xml index 0ef9c72e9c..09ecf8a880 100644 --- a/OsmAnd/AndroidManifest-freedev.xml +++ b/OsmAnd/AndroidManifest-freedev.xml @@ -17,7 +17,7 @@ android:process="net.osmand.dev" tools:replace="android:process"/> diff --git a/OsmAnd/AndroidManifest-freehuawei.xml b/OsmAnd/AndroidManifest-freehuawei.xml index 9fe9a01ccf..e96bb7fe47 100644 --- a/OsmAnd/AndroidManifest-freehuawei.xml +++ b/OsmAnd/AndroidManifest-freehuawei.xml @@ -13,7 +13,7 @@ Date: Thu, 5 Mar 2020 19:59:08 +0200 Subject: [PATCH 16/16] Update material components library --- OsmAnd/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OsmAnd/build.gradle b/OsmAnd/build.gradle index e27eddcf0d..7dac21bbef 100644 --- a/OsmAnd/build.gradle +++ b/OsmAnd/build.gradle @@ -459,7 +459,7 @@ dependencies { implementation 'androidx.gridlayout:gridlayout:1.0.0' implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'com.google.android.material:material:1.0.0' + implementation 'com.google.android.material:material:1.2.0-alpha05' implementation 'androidx.browser:browser:1.0.0' implementation 'androidx.preference:preference:1.1.0' implementation fileTree(include: ['gnu-trove-osmand.jar', 'icu4j-49_1_patched.jar'], dir: 'libs') @@ -495,7 +495,7 @@ dependencies { exclude group: 'com.android.support' } implementation 'com.github.PhilJay:MPAndroidChart:v3.0.1' - implementation ("com.github.HITGIF:TextFieldBoxes:1.3.5"){ + implementation ("com.github.HITGIF:TextFieldBoxes:1.4.5"){ exclude group: 'com.android.support' }