From 6262c8ca71bf744da1a3aa73850560934a40cb40 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 3 Mar 2020 22:19:51 +0200 Subject: [PATCH] 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(); }