From c9086f78f4c9b8a725da479583593ac7c1d1a5d2 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 24 Feb 2020 17:34:00 +0200 Subject: [PATCH] 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) {