refactor & corrections

This commit is contained in:
veliymolfar 2020-02-19 19:16:22 +02:00
parent 5256ac14b2
commit 8a9b8c81d7
3 changed files with 64 additions and 66 deletions

View file

@ -40,6 +40,7 @@ import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.monitoring.LiveMonitoringHelper; import net.osmand.plus.monitoring.LiveMonitoringHelper;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.poi.PoiFiltersHelper; import net.osmand.plus.poi.PoiFiltersHelper;
import net.osmand.plus.quickaction.QuickActionRegistry;
import net.osmand.plus.render.MapRenderRepositories; import net.osmand.plus.render.MapRenderRepositories;
import net.osmand.plus.render.NativeOsmandLibrary; import net.osmand.plus.render.NativeOsmandLibrary;
import net.osmand.plus.render.RendererRegistry; import net.osmand.plus.render.RendererRegistry;
@ -527,6 +528,7 @@ public class AppInitializer implements IProgress {
app.travelDbHelper = startupInit(app.travelDbHelper, TravelDbHelper.class); app.travelDbHelper = startupInit(app.travelDbHelper, TravelDbHelper.class);
app.lockHelper = startupInit(new LockHelper(app), LockHelper.class); app.lockHelper = startupInit(new LockHelper(app), LockHelper.class);
app.settingsHelper = startupInit(new SettingsHelper(app), SettingsHelper.class); app.settingsHelper = startupInit(new SettingsHelper(app), SettingsHelper.class);
app.quickActionRegistry = startupInit(new QuickActionRegistry(app.getSettings()), QuickActionRegistry.class);
initOpeningHoursParser(); initOpeningHoursParser();

View file

@ -200,7 +200,6 @@ public class OsmandApplication extends MultiDexApplication {
System.out.println("Time to init plugins " + (System.currentTimeMillis() - timeToStart) + " ms. Should be less < 800 ms"); System.out.println("Time to init plugins " + (System.currentTimeMillis() - timeToStart) + " ms. Should be less < 800 ms");
SearchUICore.setDebugMode(OsmandPlugin.isDevelopment()); SearchUICore.setDebugMode(OsmandPlugin.isDevelopment());
quickActionRegistry = new QuickActionRegistry(getSettings());
} }
public boolean isPlusVersionInApp() { public boolean isPlusVersionInApp() {

View file

@ -23,7 +23,6 @@ import net.osmand.plus.ApplicationMode.ApplicationModeBuilder;
import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.OsmandSettings.OsmandPreference;
import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.plus.quickaction.QuickAction; import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionFactory;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -90,8 +89,6 @@ public class SettingsHelper {
public static final String SETTINGS_LATEST_CHANGES_KEY = "settings_latest_changes"; public static final String SETTINGS_LATEST_CHANGES_KEY = "settings_latest_changes";
public static final String SETTINGS_VERSION_KEY = "settings_version"; public static final String SETTINGS_VERSION_KEY = "settings_version";
private static final String COPY_PREFIX = "copy_";
private static final String COPY_SUFFIX = "_copy";
private static final Log LOG = PlatformUtil.getLog(SettingsHelper.class); private static final Log LOG = PlatformUtil.getLog(SettingsHelper.class);
private static final int BUFFER = 1024; private static final int BUFFER = 1024;
@ -242,8 +239,8 @@ public class SettingsHelper {
public abstract static class CollectionSettingsItem<T> extends SettingsItem { public abstract static class CollectionSettingsItem<T> extends SettingsItem {
protected List<T> items; protected List<T> items;
List<T> duplicateItems; protected List<T> duplicateItems;
List<T> savedItems; protected List<T> existingItems;
CollectionSettingsItem(@NonNull SettingsItemType type, @NonNull List<T> items) { CollectionSettingsItem(@NonNull SettingsItemType type, @NonNull List<T> items) {
super(type); super(type);
@ -254,6 +251,7 @@ public class SettingsHelper {
super(type, json); super(type, json);
} }
@NonNull
public List<T> getItems() { public List<T> getItems() {
return items; return items;
} }
@ -263,6 +261,7 @@ public class SettingsHelper {
public abstract boolean isDuplicate(T item); public abstract boolean isDuplicate(T item);
@NonNull
public abstract T renameItem(T item); public abstract T renameItem(T item);
} }
@ -833,48 +832,40 @@ public class SettingsHelper {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static class QuickActionSettingsItem extends CollectionSettingsItem { public static class QuickActionSettingsItem extends CollectionSettingsItem<QuickAction> {
private OsmandApplication app; private OsmandApplication app;
private QuickActionFactory factory;
public QuickActionSettingsItem(@NonNull OsmandApplication app, public QuickActionSettingsItem(@NonNull OsmandApplication app,
@NonNull List<QuickAction> items) { @NonNull List<QuickAction> items) {
super(SettingsItemType.QUICK_ACTION, items); super(SettingsItemType.QUICK_ACTION, items);
this.app = app; this.app = app;
this.factory = new QuickActionFactory(); existingItems = app.getQuickActionRegistry().getQuickActions();
savedItems = factory.parseActiveActionsList(app.getSettings().QUICK_ACTION_LIST.get());
} }
QuickActionSettingsItem(@NonNull OsmandApplication app, QuickActionSettingsItem(@NonNull OsmandApplication app,
@NonNull JSONObject json) throws JSONException { @NonNull JSONObject json) throws JSONException {
super(SettingsItemType.QUICK_ACTION, json); super(SettingsItemType.QUICK_ACTION, json);
this.app = app; this.app = app;
this.factory = new QuickActionFactory(); existingItems = app.getQuickActionRegistry().getQuickActions();
savedItems = factory.parseActiveActionsList(app.getSettings().QUICK_ACTION_LIST.get());
} }
@Override @Override
public Object renameItem(Object item) { public boolean isDuplicate(QuickAction item) {
return app.getQuickActionRegistry().generateUniqueName((QuickAction) item, app); return !app.getQuickActionRegistry().isNameUnique(item, app);
} }
@NonNull
@Override @Override
public boolean isDuplicate(Object item) { public QuickAction renameItem(QuickAction item) {
String actionName = ((QuickAction) item).getName(app); return app.getQuickActionRegistry().generateUniqueName(item, app);
for (QuickAction savedItem : (List<QuickAction>) savedItems) {
if (savedItem.getName(app).equals(actionName)) {
return true;
}
}
return false;
} }
@NonNull @NonNull
@Override @Override
public List excludeDuplicateItems() { public List excludeDuplicateItems() {
duplicateItems = new ArrayList<>(); duplicateItems = new ArrayList<>();
for (Object item : items) { for (QuickAction item : items) {
if (isDuplicate(item)) { if (isDuplicate(item)) {
duplicateItems.add(item); duplicateItems.add(item);
} }
@ -886,18 +877,18 @@ public class SettingsHelper {
@Override @Override
public void apply() { public void apply() {
if (!items.isEmpty() || !duplicateItems.isEmpty()) { if (!items.isEmpty() || !duplicateItems.isEmpty()) {
List<QuickAction> newActions = new ArrayList<>(savedItems); List<QuickAction> newActions = new ArrayList<>(existingItems);
if (!duplicateItems.isEmpty()) { if (!duplicateItems.isEmpty()) {
if (shouldReplace) { if (shouldReplace) {
for (QuickAction duplicateItem : (List<QuickAction>) duplicateItems) { for (QuickAction duplicateItem : duplicateItems) {
for (QuickAction savedAction : (List<QuickAction>) savedItems) { for (QuickAction savedAction : existingItems) {
if (duplicateItem.getName(app).equals(savedAction.getName(app))) { if (duplicateItem.getName(app).equals(savedAction.getName(app))) {
newActions.remove(savedAction); newActions.remove(savedAction);
} }
} }
} }
} else { } else {
for (Object duplicateItem : duplicateItems) { for (QuickAction duplicateItem : duplicateItems) {
renameItem(duplicateItem); renameItem(duplicateItem);
} }
} }
@ -953,7 +944,7 @@ public class SettingsHelper {
} }
final JSONObject json; final JSONObject json;
try { try {
items = new ArrayList<QuickAction>(); items = new ArrayList<>();
Gson gson = new Gson(); Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, String>>() { Type type = new TypeToken<HashMap<String, String>>() {
}.getType(); }.getType();
@ -990,7 +981,7 @@ public class SettingsHelper {
}.getType(); }.getType();
if (!items.isEmpty()) { if (!items.isEmpty()) {
try { try {
for (QuickAction action : (List<QuickAction>) items) { for (QuickAction action : items) {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("name", action.getName(app)); jsonObject.put("name", action.getName(app));
jsonObject.put("type", action.getType()); jsonObject.put("type", action.getType());
@ -1018,29 +1009,29 @@ public class SettingsHelper {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static class PoiUiFilterSettingsItem extends CollectionSettingsItem { public static class PoiUiFilterSettingsItem extends CollectionSettingsItem<PoiUIFilter> {
private OsmandApplication app; private OsmandApplication app;
public PoiUiFilterSettingsItem(@NonNull OsmandApplication app, @NonNull List items) { public PoiUiFilterSettingsItem(@NonNull OsmandApplication app, @NonNull List items) {
super(SettingsItemType.POI_UI_FILTERS, items); super(SettingsItemType.POI_UI_FILTERS, items);
this.app = app; this.app = app;
savedItems = app.getPoiFilters().getUserDefinedPoiFilters(false); existingItems = app.getPoiFilters().getUserDefinedPoiFilters(false);
} }
PoiUiFilterSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { PoiUiFilterSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
super(SettingsItemType.POI_UI_FILTERS, json); super(SettingsItemType.POI_UI_FILTERS, json);
this.app = app; this.app = app;
savedItems = app.getPoiFilters().getUserDefinedPoiFilters(false); existingItems = app.getPoiFilters().getUserDefinedPoiFilters(false);
} }
@Override @Override
public void apply() { public void apply() {
if (!items.isEmpty() || !duplicateItems.isEmpty()) { if (!items.isEmpty() || !duplicateItems.isEmpty()) {
for (Object duplicate : duplicateItems) { for (PoiUIFilter duplicate : duplicateItems) {
items.add(shouldReplace ? duplicate : renameItem(duplicate)); items.add(shouldReplace ? duplicate : renameItem(duplicate));
} }
for (PoiUIFilter filter : (List<PoiUIFilter>) items) { for (PoiUIFilter filter : items) {
app.getPoiFilters().createPoiFilter(filter, false); app.getPoiFilters().createPoiFilter(filter, false);
} }
app.getSearchUICore().refreshCustomPoiFilters(); app.getSearchUICore().refreshCustomPoiFilters();
@ -1048,9 +1039,9 @@ public class SettingsHelper {
} }
@Override @Override
public boolean isDuplicate(Object item) { public boolean isDuplicate(PoiUIFilter item) {
String savedName = ((PoiUIFilter) item).getName(); String savedName = item.getName();
for (PoiUIFilter filter : (List<PoiUIFilter>) savedItems) { for (PoiUIFilter filter : existingItems) {
if (filter.getName().equals(savedName)) { if (filter.getName().equals(savedName)) {
return true; return true;
} }
@ -1063,9 +1054,9 @@ public class SettingsHelper {
public List excludeDuplicateItems() { public List excludeDuplicateItems() {
duplicateItems = new ArrayList<>(); duplicateItems = new ArrayList<>();
if (!items.isEmpty()) { if (!items.isEmpty()) {
for (Object object : items) { for (PoiUIFilter item : items) {
if (isDuplicate(object)) { if (isDuplicate(item)) {
duplicateItems.add(object); duplicateItems.add(item);
} }
} }
} }
@ -1073,16 +1064,17 @@ public class SettingsHelper {
return duplicateItems; return duplicateItems;
} }
@NonNull
@Override @Override
public Object renameItem(Object item) { public PoiUIFilter renameItem(PoiUIFilter item) {
int number = 0; int number = 0;
PoiUIFilter oldItem = (PoiUIFilter) item;
while (true) { while (true) {
number++; number++;
PoiUIFilter renamedItem = new PoiUIFilter( PoiUIFilter renamedItem = new PoiUIFilter(
oldItem.getName() + "_" + number, item.getName() + "_" + number,
oldItem.getFilterId() + "_" + number, item.getFilterId() + "_" + number,
oldItem.getAcceptedTypes(), app); item.getAcceptedTypes(), app);
if (!isDuplicate(renamedItem)) { if (!isDuplicate(renamedItem)) {
return renamedItem; return renamedItem;
} }
@ -1175,7 +1167,7 @@ public class SettingsHelper {
}.getType(); }.getType();
if (!items.isEmpty()) { if (!items.isEmpty()) {
try { try {
for (PoiUIFilter filter : (List<PoiUIFilter>) items) { for (PoiUIFilter filter : items) {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put("name", filter.getName()); jsonObject.put("name", filter.getName());
jsonObject.put("filterId", filter.getFilterId()); jsonObject.put("filterId", filter.getFilterId());
@ -1203,29 +1195,30 @@ public class SettingsHelper {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static class MapSourcesSettingsItem extends CollectionSettingsItem { public static class MapSourcesSettingsItem extends CollectionSettingsItem<ITileSource> {
private OsmandApplication app; private OsmandApplication app;
private List<String> existingItemsNames;
public MapSourcesSettingsItem(@NonNull OsmandApplication app, @NonNull List items) { public MapSourcesSettingsItem(@NonNull OsmandApplication app, @NonNull List items) {
super(SettingsItemType.MAP_SOURCES, items); super(SettingsItemType.MAP_SOURCES, items);
this.app = app; this.app = app;
Collection values = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true)).values(); Collection values = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true)).values();
savedItems = new ArrayList(values); existingItemsNames = new ArrayList(values);
} }
MapSourcesSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { MapSourcesSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException {
super(SettingsItemType.MAP_SOURCES, json); super(SettingsItemType.MAP_SOURCES, json);
this.app = app; this.app = app;
Collection values = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true)).values(); Collection values = new LinkedHashMap<>(app.getSettings().getTileSourceEntries(true)).values();
savedItems = new ArrayList(values); existingItemsNames = new ArrayList(values);
} }
@Override @Override
public void apply() { public void apply() {
if (!items.isEmpty() || !duplicateItems.isEmpty()) { if (!items.isEmpty() || !duplicateItems.isEmpty()) {
if (shouldReplace) { if (shouldReplace) {
for (ITileSource tileSource : (List<ITileSource>) duplicateItems) { for (ITileSource tileSource : duplicateItems) {
if (tileSource instanceof SQLiteTileSource) { if (tileSource instanceof SQLiteTileSource) {
File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + tileSource.getName() + IndexConstants.SQLITE_EXT); File f = app.getAppPath(IndexConstants.TILES_INDEX_DIR + tileSource.getName() + IndexConstants.SQLITE_EXT);
if (f != null && f.exists()) { if (f != null && f.exists()) {
@ -1243,15 +1236,15 @@ public class SettingsHelper {
} }
} }
} else { } else {
for (ITileSource tileSource : (List<ITileSource>) duplicateItems) { for (ITileSource tileSource : duplicateItems) {
items.add(renameItem(tileSource)); items.add(renameItem(tileSource));
} }
} }
for (Object template : items) { for (ITileSource tileSource : items) {
if (template instanceof TileSourceManager.TileSourceTemplate) { if (tileSource instanceof TileSourceManager.TileSourceTemplate) {
app.getSettings().installTileSource((TileSourceManager.TileSourceTemplate) template); app.getSettings().installTileSource((TileSourceManager.TileSourceTemplate) tileSource);
} else if (template instanceof SQLiteTileSource) { } else if (tileSource instanceof SQLiteTileSource) {
((SQLiteTileSource) template).createDataBase(); ((SQLiteTileSource) tileSource).createDataBase();
} }
} }
} }
@ -1262,8 +1255,8 @@ public class SettingsHelper {
@Override @Override
public List excludeDuplicateItems() { public List excludeDuplicateItems() {
duplicateItems = new ArrayList<>(); duplicateItems = new ArrayList<>();
for (String name : (List<String>) savedItems) { for (String name : existingItemsNames) {
for (ITileSource tileSource : (List<ITileSource>) items) { for (ITileSource tileSource : items) {
if (name.equals(tileSource.getName())) { if (name.equals(tileSource.getName())) {
duplicateItems.add(tileSource); duplicateItems.add(tileSource);
} }
@ -1273,8 +1266,9 @@ public class SettingsHelper {
return duplicateItems; return duplicateItems;
} }
@NonNull
@Override @Override
public Object renameItem(Object item) { public ITileSource renameItem(ITileSource item) {
int number = 0; int number = 0;
while (true) { while (true) {
number++; number++;
@ -1304,10 +1298,11 @@ public class SettingsHelper {
} }
} }
@Override @Override
public boolean isDuplicate(Object item) { public boolean isDuplicate(ITileSource item) {
for (String name : (List<String>) savedItems) { for (String name : existingItemsNames) {
if (name.equals(((ITileSource) item).getName())) { if (name.equals(item.getName())) {
return true; return true;
} }
} }
@ -1407,7 +1402,7 @@ public class SettingsHelper {
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
if (!items.isEmpty()) { if (!items.isEmpty()) {
try { try {
for (ITileSource template : (List<ITileSource>) items) { for (ITileSource template : items) {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
boolean sql = template instanceof SQLiteTileSource; boolean sql = template instanceof SQLiteTileSource;
jsonObject.put("sql", sql); jsonObject.put("sql", sql);
@ -1877,12 +1872,14 @@ public class SettingsHelper {
@Override @Override
protected void onPostExecute(Boolean success) { protected void onPostExecute(Boolean success) {
if (activity != null) {
progress.dismiss(); progress.dismiss();
if (listener != null) { if (listener != null) {
listener.onSettingsExportFinished(file, success); listener.onSettingsExportFinished(file, success);
} }
} }
} }
}
public void importSettings(@NonNull File settingsFile, String latestChanges, int version, public void importSettings(@NonNull File settingsFile, String latestChanges, int version,
boolean askBeforeImport, @Nullable SettingsImportListener listener) { boolean askBeforeImport, @Nullable SettingsImportListener listener) {