Fix import/export settings

This commit is contained in:
max-klaus 2019-10-11 21:40:06 +03:00
parent 5a860b7096
commit 781e8d01ac
3 changed files with 147 additions and 65 deletions

View file

@ -630,7 +630,7 @@ public class ApplicationMode {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
ApplicationModeBean mb = gson.fromJson(json, ApplicationModeBean.class);
ApplicationModeBuilder b = createCustomMode(valueOfStringKey(mb.parent, CAR),
ApplicationModeBuilder b = createCustomMode(valueOfStringKey(mb.parent, null),
mb.userProfileName, mb.stringKey);
b.setRouteService(mb.routeService).setRoutingProfile(mb.routingProfile);
b.icon(app, mb.iconName);

View file

@ -539,10 +539,6 @@ public class OsmandSettings {
return this;
}
public boolean isGlobal() {
return global;
}
public CommonPreference<T> cache() {
cache = true;
return this;
@ -665,42 +661,46 @@ public class OsmandSettings {
@Override
public boolean writeToJson(JSONObject json, ApplicationMode appMode) throws JSONException {
if (appMode != null) {
if (isSetForMode(appMode)) {
json.put(getId(), asStringModeValue(appMode));
if (!global) {
String value = asStringModeValue(appMode);
if (value != null) {
json.put(getId(), value);
}
return true;
}
} else if (global) {
if (isSet()) {
json.put(getId(), asString());
return true;
String value = asString();
if (value != null) {
json.put(getId(), value);
}
return true;
}
return false;
}
@Override
public void readFromJson(JSONObject json, ApplicationMode appMode) throws JSONException {
if (isGlobal()) {
String globalValue = json.getString(getId());
if (globalValue != null) {
set(parseString(globalValue));
}
} else {
String modeValue = json.getString(getId());
if (modeValue != null) {
if (appMode != null) {
if (!global) {
String modeValue = json.getString(getId());
setModeValue(appMode, parseString(modeValue));
}
} else if (global) {
String globalValue = json.getString(getId());
set(parseString(globalValue));
}
}
@Override
public String asString() {
return get().toString();
T o = get();
return o != null ? o.toString() : null;
}
@Override
public String asStringModeValue(ApplicationMode m) {
return getModeValue(m).toString();
T v = getModeValue(m);
return v != null ? v.toString() : null;
}
}

View file

@ -2,6 +2,7 @@ package net.osmand.plus;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
@ -49,6 +50,7 @@ public class SettingsHelper {
private Activity activity;
private boolean importing;
private boolean importSuspended;
private ImportAsyncTask importTask;
public SettingsHelper(OsmandApplication app) {
@ -68,6 +70,10 @@ public class SettingsHelper {
public void resetActivity(Activity activity) {
if (this.activity == activity) {
if (importing) {
importTask.suspendImport();
importSuspended = true;
}
this.activity = null;
}
}
@ -105,6 +111,9 @@ public class SettingsHelper {
@NonNull
public abstract String getName();
@NonNull
public abstract String getPublicName(@NonNull Context ctx);
@NonNull
public abstract String getFileName();
@ -232,23 +241,29 @@ public class SettingsHelper {
if (Algorithms.isEmpty(jsonStr)) {
throw new IllegalArgumentException("Cannot find json body");
}
JSONObject json;
final JSONObject json;
try {
json = new JSONObject(jsonStr);
} catch (JSONException e) {
throw new IllegalArgumentException("Json parse error", e);
}
Map<String, OsmandPreference<?>> prefs = settings.getRegisteredPreferences();
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
OsmandPreference<?> p = prefs.get(key);
try {
readPreferenceFromJson(p, json);
} catch (JSONException e) {
LOG.error("Failed to read preference: " + p.getId(), e);
settings.getContext().runInUIThread(new Runnable() {
@Override
public void run() {
Map<String, OsmandPreference<?>> prefs = settings.getRegisteredPreferences();
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
OsmandPreference<?> p = prefs.get(key);
try {
readPreferenceFromJson(p, json);
} catch (JSONException e) {
LOG.error("Failed to read preference: " + p.getId(), e);
}
}
}
}
});
}
}
@ -300,6 +315,12 @@ public class SettingsHelper {
return "general_settings";
}
@NonNull
@Override
public String getPublicName(@NonNull Context ctx) {
return ctx.getString(R.string.general_settings_2);
}
@NonNull
@Override
public String getFileName() {
@ -355,6 +376,18 @@ public class SettingsHelper {
return appMode.getStringKey();
}
@NonNull
@Override
public String getPublicName(@NonNull Context ctx) {
if (appMode.isCustomProfile()) {
return appMode.getCustomProfileName();
} else if (appMode.getNameKeyResource() != -1) {
return ctx.getString(appMode.getNameKeyResource());
} else {
return getName();
}
}
@NonNull
@Override
public String getFileName() {
@ -364,7 +397,11 @@ public class SettingsHelper {
void readFromJson(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
String appModeJson = json.getString("appMode");
builder = ApplicationMode.fromJson(app, appModeJson);
this.appMode = builder.getApplicationMode();
ApplicationMode appMode = builder.getApplicationMode();
if (!appMode.isCustomProfile()) {
appMode = ApplicationMode.valueOfStringKey(appMode.getStringKey(), appMode);
}
this.appMode = appMode;
}
@Override
@ -374,7 +411,9 @@ public class SettingsHelper {
@Override
public void apply() {
appMode = ApplicationMode.saveCustomProfile(builder, getSettings().getContext());
if (appMode.isCustomProfile()) {
appMode = ApplicationMode.saveCustomProfile(builder, getSettings().getContext());
}
}
@Override
@ -476,6 +515,12 @@ public class SettingsHelper {
return name;
}
@NonNull
@Override
public String getPublicName(@NonNull Context ctx) {
return getName();
}
@Override
void readFromJson(@NonNull JSONObject json) throws JSONException {
super.readFromJson(json);
@ -583,10 +628,14 @@ public class SettingsHelper {
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[BUFFER];
int count;
while ((count = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, count);
try {
while ((count = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, count);
}
output.flush();
} finally {
Algorithms.closeStream(output);
}
output.flush();
}
};
}
@ -770,7 +819,7 @@ public class SettingsHelper {
LOG.error("Error parsing items: " + itemsJson, e);
throw new IllegalArgumentException("No items");
}
while ((entry = zis.getNextEntry()) != null && !collecting) {
while (!collecting && (entry = zis.getNextEntry()) != null) {
String fileName = entry.getName();
SettingsItem item = itemsFactory.getItemByFileName(fileName);
if (item != null) {
@ -805,6 +854,8 @@ public class SettingsHelper {
private SettingsImporter importer;
private List<SettingsItem> items;
private List<SettingsItem> processedItems = new ArrayList<>();
private SettingsItem currentItem;
private AlertDialog dialog;
ImportAsyncTask(@NonNull File zipFile) {
this.zipFile = zipFile;
@ -817,6 +868,7 @@ public class SettingsHelper {
finishImport(false);
}
importing = true;
importSuspended = false;
importTask = this;
}
@ -844,43 +896,72 @@ public class SettingsHelper {
if (activity == null) {
return;
}
if (items.size() == 0) {
if (items.size() == 0 && !importSuspended) {
if (processedItems.size() > 0) {
new ImportItemsAsyncTask(zipFile, processedItems).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
finishImport(false);
}
return;
}
final SettingsItem item = items.remove(0);
if (item.exists()) {
switch (item.getType()) {
case PROFILE: {
AlertDialog.Builder b = new AlertDialog.Builder(activity);
b.setMessage("Profile \"" + item.getName() + "\" is already exists. Overwrite?");
b.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
acceptItem(item);
}
});
b.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
processNextItem();
}
});
b.setCancelable(false);
b.show();
break;
final SettingsItem item;
if (importSuspended && currentItem != null) {
item = currentItem;
} else if (items.size() > 0) {
item = items.remove(0);
currentItem = item;
} else {
item = null;
}
importSuspended = false;
if (item != null) {
if (item.exists()) {
switch (item.getType()) {
case PROFILE: {
AlertDialog.Builder b = new AlertDialog.Builder(activity);
b.setMessage("Profile \"" + item.getPublicName(app) + "\" is already exists. Overwrite?");
b.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
acceptItem(item);
}
});
b.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
processNextItem();
}
});
b.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
ImportAsyncTask.this.dialog = null;
}
});
b.setCancelable(false);
dialog = b.show();
break;
}
case FILE:
// overwrite now
acceptItem(item);
break;
default:
acceptItem(item);
break;
}
case FILE:
break;
default:
acceptItem(item);
break;
} else {
acceptItem(item);
}
} else {
acceptItem(item);
processNextItem();
}
}
private void suspendImport() {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}
@ -925,6 +1006,7 @@ public class SettingsHelper {
private void finishImport(boolean success) {
importing = false;
importSuspended = false;
importTask = null;
if (success) {
app.showShortToastMessage("Import succeed");