Add ability to check exported profile files

This commit is contained in:
Vitaliy 2020-03-03 22:19:51 +02:00
parent ca742998dc
commit 6262c8ca71
3 changed files with 79 additions and 21 deletions

View file

@ -101,6 +101,7 @@ public class SettingsHelper {
private boolean importSuspended; private boolean importSuspended;
private boolean collectOnly; private boolean collectOnly;
private ImportAsyncTask importTask; private ImportAsyncTask importTask;
private Map<File, ExportAsyncTask> exportAsyncTasks = new HashMap<>();
public interface SettingsImportListener { public interface SettingsImportListener {
void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List<SettingsItem> items); void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List<SettingsItem> items);
@ -1930,6 +1931,17 @@ public class SettingsHelper {
return this.importTask.getFile(); 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") @SuppressLint("StaticFieldLeak")
private class ImportItemsAsyncTask extends AsyncTask<Void, Void, Boolean> { private class ImportItemsAsyncTask extends AsyncTask<Void, Void, Boolean> {
@ -2008,6 +2020,7 @@ public class SettingsHelper {
@Override @Override
protected void onPostExecute(Boolean success) { protected void onPostExecute(Boolean success) {
exportAsyncTasks.remove(file);
if (listener != null) { if (listener != null) {
listener.onSettingsExportFinished(file, success); listener.onSettingsExportFinished(file, success);
} }
@ -2022,11 +2035,11 @@ public class SettingsHelper {
new ImportAsyncTask(settingsFile, items, latestChanges, version, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); new ImportAsyncTask(settingsFile, items, latestChanges, version, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
public void exportSettings(@NonNull File fileDir, @NonNull String fileName, public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, @NonNull List<SettingsItem> items) {
@Nullable SettingsExportListener listener, File file = new File(fileDir, fileName + OSMAND_SETTINGS_FILE_EXT);
@NonNull List<SettingsItem> items) { ExportAsyncTask exportAsyncTask = new ExportAsyncTask(file, listener, items);
new ExportAsyncTask(new File(fileDir, fileName + OSMAND_SETTINGS_FILE_EXT), listener, items) exportAsyncTasks.put(file, exportAsyncTask);
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); exportAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener, public void exportSettings(@NonNull File fileDir, @NonNull String fileName, @Nullable SettingsExportListener listener,

View file

@ -168,7 +168,7 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co
public void resetAppModePrefs(ApplicationMode appMode) { public void resetAppModePrefs(ApplicationMode appMode) {
if (appMode != null) { if (appMode != null) {
if (appMode.isCustomProfile()) { if (appMode.isCustomProfile()) {
File file = getBackupFileForCustomMode(appMode); File file = getBackupFileForCustomMode(app, appMode.getStringKey());
if (file.exists()) { if (file.exists()) {
restoreCustomModeFromFile(file); 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() { private void updateCopiedOrResetPrefs() {
MapActivity mapActivity = getMapActivity(); MapActivity mapActivity = getMapActivity();
if (mapActivity != null) { if (mapActivity != null) {
@ -346,8 +336,8 @@ public class ConfigureProfileFragment extends BaseSettingsFragment implements Co
private void setupResetToDefaultPref() { private void setupResetToDefaultPref() {
Preference resetToDefault = findPreference(RESET_TO_DEFAULT); Preference resetToDefault = findPreference(RESET_TO_DEFAULT);
if (getSelectedAppMode().isCustomProfile() ApplicationMode mode = getSelectedAppMode();
&& !getBackupFileForCustomMode(getSelectedAppMode()).exists()) { if (mode.isCustomProfile() && !getBackupFileForCustomMode(app, mode.getStringKey()).exists()) {
resetToDefault.setVisible(false); resetToDefault.setVisible(false);
} else { } else {
resetToDefault.setIcon(app.getUIUtilities().getIcon(R.drawable.ic_action_reset_to_default_dark, 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);
}
} }

View file

@ -53,7 +53,6 @@ import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -69,6 +68,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
private static final Log LOG = PlatformUtil.getLog(ProfileAppearanceFragment.class); private static final Log LOG = PlatformUtil.getLog(ProfileAppearanceFragment.class);
public static final String TAG = ProfileAppearanceFragment.class.getName(); public static final String TAG = ProfileAppearanceFragment.class.getName();
private static final String MASTER_PROFILE = "master_profile"; private static final String MASTER_PROFILE = "master_profile";
private static final String PROFILE_NAME = "profile_name"; private static final String PROFILE_NAME = "profile_name";
private static final String SELECT_COLOR = "select_color"; 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 PROFILE_NAVIGATION_ICON_KEY = "profile_navigation_icon_key";
public static final String BASE_PROFILE_FOR_NEW = "base_profile_for_new"; 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_BASE_PROFILE_IMPORTED = "is_base_profile_imported";
public static final String IS_NEW_PROFILE_KEY = "is_new_profile_key";
private SelectProfileBottomSheetDialogFragment.SelectProfileListener parentProfileListener; private SelectProfileBottomSheetDialogFragment.SelectProfileListener parentProfileListener;
private SettingsHelper.SettingsExportListener exportListener;
private ProgressDialog progress;
private EditText baseProfileName; private EditText baseProfileName;
private ApplicationProfileObject profile; private ApplicationProfileObject profile;
private ApplicationProfileObject changedProfile; private ApplicationProfileObject changedProfile;
@ -108,6 +114,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
profile = new ApplicationProfileObject(); profile = new ApplicationProfileObject();
exportListener = getSettingsExportListener();
ApplicationMode baseModeForNewProfile = null; ApplicationMode baseModeForNewProfile = null;
if (getArguments() != null) { if (getArguments() != null) {
Bundle arguments = getArguments(); Bundle arguments = getArguments();
@ -139,9 +146,9 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
changedProfile.routeService = profile.routeService; changedProfile.routeService = profile.routeService;
changedProfile.locationIcon = profile.locationIcon; changedProfile.locationIcon = profile.locationIcon;
changedProfile.navigationIcon = profile.navigationIcon; changedProfile.navigationIcon = profile.navigationIcon;
}
isNewProfile = ApplicationMode.valueOfStringKey(changedProfile.stringKey, null) == null; isNewProfile = ApplicationMode.valueOfStringKey(changedProfile.stringKey, null) == null;
} }
}
public void setupAppProfileObjectFromAppMode(ApplicationMode baseModeForNewProfile) { public void setupAppProfileObjectFromAppMode(ApplicationMode baseModeForNewProfile) {
profile.stringKey = baseModeForNewProfile.getStringKey(); profile.stringKey = baseModeForNewProfile.getStringKey();
@ -275,6 +282,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
if (changedProfile.parent != null) { if (changedProfile.parent != null) {
outState.putString(PROFILE_PARENT_KEY, changedProfile.parent.getStringKey()); outState.putString(PROFILE_PARENT_KEY, changedProfile.parent.getStringKey());
} }
outState.putBoolean(IS_NEW_PROFILE_KEY, isNewProfile);
outState.putBoolean(IS_BASE_PROFILE_IMPORTED, isBaseProfileImported); outState.putBoolean(IS_BASE_PROFILE_IMPORTED, isBaseProfileImported);
outState.putSerializable(PROFILE_LOCATION_ICON_KEY, changedProfile.locationIcon); outState.putSerializable(PROFILE_LOCATION_ICON_KEY, changedProfile.locationIcon);
outState.putSerializable(PROFILE_NAVIGATION_ICON_KEY, changedProfile.navigationIcon); outState.putSerializable(PROFILE_NAVIGATION_ICON_KEY, changedProfile.navigationIcon);
@ -290,6 +298,7 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
isBaseProfileImported = savedInstanceState.getBoolean(IS_BASE_PROFILE_IMPORTED); isBaseProfileImported = savedInstanceState.getBoolean(IS_BASE_PROFILE_IMPORTED);
changedProfile.locationIcon = (LocationIcon) savedInstanceState.getSerializable(PROFILE_LOCATION_ICON_KEY); changedProfile.locationIcon = (LocationIcon) savedInstanceState.getSerializable(PROFILE_LOCATION_ICON_KEY);
changedProfile.navigationIcon = (NavigationIcon) savedInstanceState.getSerializable(PROFILE_NAVIGATION_ICON_KEY); changedProfile.navigationIcon = (NavigationIcon) savedInstanceState.getSerializable(PROFILE_NAVIGATION_ICON_KEY);
isNewProfile = savedInstanceState.getBoolean(IS_NEW_PROFILE_KEY);
} }
@Override @Override
@ -635,6 +644,27 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
return parentProfileListener; 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) { void updateParentProfile(String profileKey, boolean isBaseProfileImported) {
deleteImportedProfile(); deleteImportedProfile();
setupBaseProfileView(profileKey); setupBaseProfileView(profileKey);
@ -675,7 +705,10 @@ public class ProfileAppearanceFragment extends BaseSettingsFragment {
mode.setLocationIcon(changedProfile.locationIcon); mode.setLocationIcon(changedProfile.locationIcon);
mode.setNavigationIcon(changedProfile.navigationIcon); 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) { private String getUniqueStringKey(ApplicationMode mode) {
return mode.getStringKey() + "_" + System.currentTimeMillis(); return mode.getStringKey() + "_" + System.currentTimeMillis();
} }