Add restore for custom profiles

This commit is contained in:
Vitaliy 2020-02-26 10:47:25 +02:00
parent 9d857a9f98
commit f6e533d66e
3 changed files with 43 additions and 10 deletions

View file

@ -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<SettingsItem> 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<SettingsItem> 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<SettingsItem> items, String latestChanges, int version, @Nullable SettingsImportListener listener) {

View file

@ -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<SettingsHelper.SettingsItem> items) {
if (isActivityNotDestroyed(activity)) {

View file

@ -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<SettingsHelper.SettingsItem> 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,