refactor/wip

This commit is contained in:
veliymolfar 2020-02-04 19:04:25 +02:00
parent feca38ac4b
commit ccd638f2ed
3 changed files with 133 additions and 201 deletions

View file

@ -139,9 +139,9 @@ public class SettingsHelper {
PLUGIN,
DATA,
FILE,
QUICK_ACTION_LIST,
POI_UI_FILTERS_LIST,
MAP_SOURCES_LIST,
QUICK_ACTION,
POI_UI_FILTERS,
MAP_SOURCES,
}
public abstract static class SettingsItem {
@ -171,6 +171,10 @@ public class SettingsHelper {
@NonNull
public abstract String getFileName();
public Boolean shouldReadInCollecting() {
return false;
}
static SettingsItemType parseItemType(@NonNull JSONObject json) throws IllegalArgumentException, JSONException {
return SettingsItemType.valueOf(json.getString("type"));
}
@ -271,8 +275,6 @@ public class SettingsHelper {
private OsmandSettings settings;
JSONObject json;
public OsmandSettingsItemReader(@NonNull OsmandSettingsItem item, @NonNull OsmandSettings settings) {
super(item);
this.settings = settings;
@ -297,33 +299,32 @@ public class SettingsHelper {
if (Algorithms.isEmpty(jsonStr)) {
throw new IllegalArgumentException("Cannot find json body");
}
// final JSONObject json;
final JSONObject json;
try {
json = new JSONObject(jsonStr);
} catch (JSONException e) {
throw new IllegalArgumentException("Json parse error", 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);
// if (p != null) {
// try {
// readPreferenceFromJson(p, json);
// } catch (Exception e) {
// LOG.error("Failed to read preference: " + key, e);
// }
// } else {
// LOG.warn("No preference while importing settings: " + key);
// }
// }
// }
// });
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);
if (p != null) {
try {
readPreferenceFromJson(p, json);
} catch (Exception e) {
LOG.error("Failed to read preference: " + key, e);
}
} else {
LOG.warn("No preference while importing settings: " + key);
}
}
}
});
}
}
@ -484,27 +485,6 @@ public class SettingsHelper {
if (appMode.isCustomProfile()) {
appMode = ApplicationMode.saveProfile(builder, getSettings().getContext());
}
getSettings().getContext().runInUIThread(new Runnable() {
@Override
public void run() {
Map<String, OsmandPreference<?>> prefs = getSettings().getRegisteredPreferences();
Iterator<String> iter = reader.json.keys();
while (iter.hasNext()) {
String key = iter.next();
OsmandPreference<?> p = prefs.get(key);
if (p != null) {
try {
reader.readPreferenceFromJson(p, reader.json);
} catch (Exception e) {
LOG.error("Failed to read preference: " + key, e);
}
} else {
LOG.warn("No preference while importing settings: " + key);
}
}
}
});
}
@Override
@ -513,8 +493,10 @@ public class SettingsHelper {
json.put("appMode", new JSONObject(appMode.toJson()));
}
OsmandSettingsItemReader reader = new OsmandSettingsItemReader(this, getSettings()) {
@NonNull
@Override
SettingsItemReader getReader() {
return new OsmandSettingsItemReader(this, getSettings()) {
@Override
protected void readPreferenceFromJson(@NonNull OsmandPreference<?> preference, @NonNull JSONObject json) throws JSONException {
if (!appModeBeanPrefsIds.contains(preference.getId())) {
@ -522,11 +504,6 @@ public class SettingsHelper {
}
}
};
@NonNull
@Override
SettingsItemReader getReader() {
return reader;
}
@NonNull
@ -757,14 +734,14 @@ public class SettingsHelper {
public QuickActionSettingsItem(@NonNull OsmandApplication app,
@NonNull List<QuickAction> quickActions) {
super(SettingsItemType.QUICK_ACTION_LIST, app.getSettings());
super(SettingsItemType.QUICK_ACTION, app.getSettings());
this.app = app;
this.quickActions = quickActions;
}
public QuickActionSettingsItem(@NonNull OsmandSettings settings,
@NonNull JSONObject jsonObject) throws JSONException {
super(SettingsItemType.QUICK_ACTION_LIST, settings, jsonObject);
super(SettingsItemType.QUICK_ACTION, settings, jsonObject);
}
public List<QuickAction> getQuickActions() {
@ -781,6 +758,11 @@ public class SettingsHelper {
}
}
@Override
public Boolean shouldReadInCollecting() {
return true;
}
@NonNull
@Override
public String getName() {
@ -874,13 +856,13 @@ public class SettingsHelper {
private OsmandApplication app;
public PoiUiFilterSettingsItem(OsmandApplication app, List<PoiUIFilter> poiUIFilters) {
super(SettingsItemType.POI_UI_FILTERS_LIST, app.getSettings());
super(SettingsItemType.POI_UI_FILTERS, app.getSettings());
this.app = app;
this.poiUIFilters = poiUIFilters;
}
public PoiUiFilterSettingsItem(OsmandApplication app, JSONObject jsonObject) throws JSONException {
super(SettingsItemType.POI_UI_FILTERS_LIST, app.getSettings(), jsonObject);
super(SettingsItemType.POI_UI_FILTERS, app.getSettings(), jsonObject);
this.app = app;
readFromJson(jsonObject);
}
@ -897,6 +879,11 @@ public class SettingsHelper {
return null;
}
@Override
public Boolean shouldReadInCollecting() {
return true;
}
@NonNull
@Override
public String getFileName() {
@ -982,12 +969,12 @@ public class SettingsHelper {
private List<ExportImportProfileBottomSheet.MapSourceWrapper> mapSources;
public MapSourcesSettingsItem(OsmandSettings settings, List<ExportImportProfileBottomSheet.MapSourceWrapper> mapSources) {
super(SettingsItemType.MAP_SOURCES_LIST, settings);
super(SettingsItemType.MAP_SOURCES, settings);
this.mapSources = mapSources;
}
public MapSourcesSettingsItem(OsmandSettings settings, JSONObject jsonObject) throws JSONException {
super(SettingsItemType.MAP_SOURCES_LIST, settings, jsonObject);
super(SettingsItemType.MAP_SOURCES, settings, jsonObject);
}
@NonNull
@ -1002,6 +989,11 @@ public class SettingsHelper {
return null;
}
@Override
public Boolean shouldReadInCollecting() {
return true;
}
@NonNull
@Override
public String getFileName() {
@ -1096,13 +1088,13 @@ public class SettingsHelper {
case FILE:
item = new FileSettingsItem(app, json);
break;
case QUICK_ACTION_LIST:
case QUICK_ACTION:
item = new QuickActionSettingsItem(settings, json);
break;
case POI_UI_FILTERS_LIST:
case POI_UI_FILTERS:
item = new PoiUiFilterSettingsItem(app, json);
break;
case MAP_SOURCES_LIST:
case MAP_SOURCES:
item = new MapSourcesSettingsItem(settings, json);
break;
}
@ -1181,15 +1173,14 @@ public class SettingsHelper {
}
private List<SettingsItem> processItems(@NonNull File file, @Nullable List<SettingsItem> items) throws IllegalArgumentException, IOException {
// boolean collecting = items == null;
// if (collecting) {
// items = new ArrayList<>();
// } else {
// if (items.size() == 0) {
// throw new IllegalArgumentException("No items");
// }
// }
boolean collecting = items == null;
if (collecting) {
items = new ArrayList<>();
} else {
if (items.size() == 0) {
throw new IllegalArgumentException("No items");
}
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
InputStream ois = new BufferedInputStream(zis);
try {
@ -1207,9 +1198,9 @@ public class SettingsHelper {
SettingsItemsFactory itemsFactory;
try {
itemsFactory = new SettingsItemsFactory(app, itemsJson);
// if (collecting) {
if (collecting) {
items.addAll(itemsFactory.getItems());
// }
}
} catch (IllegalArgumentException e) {
LOG.error("Error parsing items: " + itemsJson, e);
throw new IllegalArgumentException("No items");
@ -1220,7 +1211,8 @@ public class SettingsHelper {
while ((entry = zis.getNextEntry()) != null) {
String fileName = entry.getName();
SettingsItem item = itemsFactory.getItemByFileName(fileName);
if (item != null) {
if (item != null && collecting && item.shouldReadInCollecting()
|| item != null && !collecting && !item.shouldReadInCollecting()) {
try {
item.getReader().readFromStream(ois);
} catch (IllegalArgumentException e) {
@ -1251,6 +1243,7 @@ public class SettingsHelper {
private File file;
private String latestChanges;
private int version;
private Boolean isCollecting;
private SettingsImportListener listener;
private SettingsImporter importer;
@ -1265,6 +1258,17 @@ public class SettingsHelper {
this.latestChanges = latestChanges;
this.version = version;
importer = new SettingsImporter(app);
isCollecting = true;
}
ImportAsyncTask(@NonNull File settingsFile, @NonNull List<SettingsItem> items, String latestChanges, int version, @Nullable SettingsImportListener listener) {
this.file = settingsFile;
this.listener = listener;
this.items = items;
this.latestChanges = latestChanges;
this.version = version;
importer = new SettingsImporter(app);
isCollecting = false;
}
@Override
@ -1279,6 +1283,7 @@ public class SettingsHelper {
@Override
protected List<SettingsItem> doInBackground(Void... voids) {
if (isCollecting) {
try {
return importer.collectItems(file);
} catch (IllegalArgumentException e) {
@ -1286,16 +1291,23 @@ public class SettingsHelper {
} catch (IOException e) {
LOG.error("Failed to collect items from: " + file.getName(), e);
}
} else {
return this.items;
}
return null;
}
@Override
protected void onPostExecute(List<SettingsItem> items) {
if (isCollecting) {
listener.onSettingsImportFinished(true, false, items);
} else {
this.items = items;
if (items != null && items.size() > 0) {
processNextItem();
}
}
}
private void processNextItem() {
if (activity == null) {
@ -1433,46 +1445,6 @@ public class SettingsHelper {
}
}
@SuppressLint("StaticFieldLeak")
public class PreImportAsyncTask extends AsyncTask<Void, Void, List<SettingsItem>> {
private String latestChanges;
private int version;
private File file;
private SettingsPreImportListener listener;
private SettingsImporter importer;
PreImportAsyncTask(@NonNull File file, String latestChanges, int version, SettingsPreImportListener listener) {
this.file = file;
this.listener = listener;
this.latestChanges = latestChanges;
this.version = version;
importer = new SettingsImporter(app);
}
@Override
protected List<SettingsItem> doInBackground(Void... voids) {
try {
return importer.collectItems(file);
} catch (IllegalArgumentException e) {
LOG.error("Failed to collect items from: " + file.getName(), e);
} catch (IOException e) {
LOG.error("Failed to collect items from: " + file.getName(), e);
}
return null;
}
@Override
protected void onPostExecute(List<SettingsItem> settingsItems) {
super.onPostExecute(settingsItems);
if (settingsItems != null && settingsItems.size() > 0) {
listener.onSettingsPreImported(true, settingsItems);
} else {
listener.onSettingsPreImported(false, settingsItems);
}
}
}
@SuppressLint("StaticFieldLeak")
private class ExportAsyncTask extends AsyncTask<Void, Void, Boolean> {
@ -1516,6 +1488,10 @@ public class SettingsHelper {
new ImportAsyncTask(settingsFile, latestChanges, version, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
public void importSettings(@NonNull File settingsFile, @NonNull List<SettingsItem> items, String latestChanges, int version, @Nullable SettingsImportListener listener) {
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<SettingsItem> items) {
@ -1527,59 +1503,4 @@ public class SettingsHelper {
@NonNull SettingsItem... items) {
exportSettings(fileDir, fileName, listener, new ArrayList<>(Arrays.asList(items)));
}
public void preImportSettings(File file, String latestChanges, int version, SettingsPreImportListener listener) {
new PreImportAsyncTask(file, latestChanges, version, listener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
public void importSettingsItems(List<SettingsItem> list) {
for (SettingsItem item : list) {
if (item.exists()) {
switch (item.getType()) {
case PROFILE: {
String title = activity.getString(R.string.overwrite_profile_q, item.getPublicName(app));
showConfirmDialog(item, title, "");
break;
}
default:
item.apply();
break;
}
} else {
if (item.getType() == SettingsItemType.PROFILE) {
String title = activity.getString(R.string.add_new_profile_q, item.getPublicName(app));
showConfirmDialog(item, title, "");
} else {
item.apply();
// acceptItem(item);
}
}
}
}
private AlertDialog showConfirmDialog(final SettingsItem item, String title, String message) {
AlertDialog.Builder b = new AlertDialog.Builder(activity);
b.setTitle(title);
b.setMessage(message);
b.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
item.apply();
}
});
b.setNegativeButton(R.string.shared_string_no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
b.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
}
});
b.setCancelable(false);
return b.show();
}
}

View file

@ -776,18 +776,19 @@ public class ImportHelper {
File tempDir = app.getAppPath(IndexConstants.TEMP_DIR);
final File file = new File(tempDir, name);
if (error == null && file.exists()) {
app.getSettingsHelper().preImportSettings(file, latestChanges, version, new SettingsHelper.SettingsPreImportListener() {
app.getSettingsHelper().importSettings(file, latestChanges, version, new SettingsImportListener() {
@Override
public void onSettingsPreImported(boolean isSuccessful, List<SettingsHelper.SettingsItem> items) {
public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List<SettingsHelper.SettingsItem> items) {
if (isActivityNotDestroyed(activity)) {
progress.dismiss();
}
if (isSuccessful) {
if (succeed) {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
if (fragmentManager != null) {
ExportImportProfileBottomSheet.showInstance(
fragmentManager,
ExportImportProfileBottomSheet.State.IMPORT,
file,
items);
}
} else {

View file

@ -87,6 +87,8 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet {
private SettingsHelper.ProfileSettingsItem profileSettingsItem;
private File file;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -191,11 +193,11 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet {
List<PoiUIFilter> poiUIFilters = new ArrayList<>();
List<MapSourceWrapper> mapSourceWrappers = new ArrayList<>();
for (SettingsHelper.SettingsItem item : settingsItems) {
if (item.getType().equals(SettingsHelper.SettingsItemType.QUICK_ACTION_LIST)) {
if (item.getType().equals(SettingsHelper.SettingsItemType.QUICK_ACTION)) {
quickActions.addAll(((SettingsHelper.QuickActionSettingsItem) item).getQuickActions());
} else if (item.getType().equals(SettingsHelper.SettingsItemType.POI_UI_FILTERS_LIST)) {
} else if (item.getType().equals(SettingsHelper.SettingsItemType.POI_UI_FILTERS)) {
// poiUIFilters.addAll(((SettingsHelper.PoiUiFilterSettingsItem) item).)
} else if (item.getType().equals(SettingsHelper.SettingsItemType.MAP_SOURCES_LIST)) {
} else if (item.getType().equals(SettingsHelper.SettingsItemType.MAP_SOURCES)) {
// mapSourceWrappers.addAll(((SettingsHelper.MapSourcesSettingsItem) item).)
}
}
@ -219,21 +221,14 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet {
dataToOperate.addAll(dataWrapper.getItems());
}
adapter.updateList(dataList);
// app.getSettingsHelper().importSettings(settingsItems, file, new SettingsHelper.SettingsImportListener() {
// @Override
// public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List<SettingsHelper.SettingsItem> items) {
//
// }
// });
}
private Boolean checkAdditionalDataContains() {
boolean containsData = false;
for (SettingsHelper.SettingsItem item : settingsItems) {
containsData = item.getType().equals(SettingsHelper.SettingsItemType.QUICK_ACTION_LIST)
|| item.getType().equals(SettingsHelper.SettingsItemType.POI_UI_FILTERS_LIST)
|| item.getType().equals(SettingsHelper.SettingsItemType.MAP_SOURCES_LIST);
containsData = item.getType().equals(SettingsHelper.SettingsItemType.QUICK_ACTION)
|| item.getType().equals(SettingsHelper.SettingsItemType.POI_UI_FILTERS)
|| item.getType().equals(SettingsHelper.SettingsItemType.MAP_SOURCES);
if (containsData) {
break;
}
@ -299,7 +294,12 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet {
if (includeAdditionalData) {
list.addAll(prepareAdditionalSettingsItems());
}
app.getSettingsHelper().importSettingsItems(list);
app.getSettingsHelper().importSettings(file, list, "", 1, new SettingsHelper.SettingsImportListener() {
@Override
public void onSettingsImportFinished(boolean succeed, boolean empty, @NonNull List<SettingsHelper.SettingsItem> items) {
}
});
dismiss();
}
@ -410,6 +410,7 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet {
public static boolean showInstance(@NonNull FragmentManager fragmentManager,
State state,
File file,
List<SettingsHelper.SettingsItem> items) {
try {
Bundle bundle = new Bundle();
@ -417,6 +418,7 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet {
ExportImportProfileBottomSheet fragment = new ExportImportProfileBottomSheet();
fragment.setArguments(bundle);
fragment.setSettingsItems(items);
fragment.setFile(file);
fragment.show(fragmentManager, TAG);
return true;
} catch (RuntimeException e) {
@ -424,6 +426,14 @@ public class ExportImportProfileBottomSheet extends BasePreferenceBottomSheet {
}
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public enum State {
EXPORT,
IMPORT