Fix plugin dependent quick actions

This commit is contained in:
Vitaliy 2020-04-03 14:02:17 +03:00
parent 50577f3f32
commit 2fac0833cc
9 changed files with 67 additions and 33 deletions

View file

@ -56,6 +56,7 @@ import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
public abstract class OsmandPlugin { public abstract class OsmandPlugin {
@ -190,6 +191,10 @@ public abstract class OsmandPlugin {
return Collections.emptyList(); return Collections.emptyList();
} }
protected List<QuickActionType> getQuickActionTypes() {
return Collections.emptyList();
}
/** /**
* Plugin was installed * Plugin was installed
*/ */
@ -496,9 +501,6 @@ public abstract class OsmandPlugin {
return true; return true;
} }
protected void registerQuickActionTypes(List<QuickActionType> quickActionTypes) {
}
protected void registerLayerContextMenuActions(OsmandMapTileView mapView, ContextMenuAdapter adapter, MapActivity mapActivity) { protected void registerLayerContextMenuActions(OsmandMapTileView mapView, ContextMenuAdapter adapter, MapActivity mapActivity) {
} }
@ -787,14 +789,14 @@ public abstract class OsmandPlugin {
return false; return false;
} }
public static void registerQuickActionTypesPlugins(List<QuickActionType> quickActionTypes) { public static void registerQuickActionTypesPlugins(Map<QuickActionType, Boolean> availableQuickActionTypes) {
for (OsmandPlugin p : getEnabledPlugins()) { for (OsmandPlugin p : getAvailablePlugins()) {
p.registerQuickActionTypes(quickActionTypes); for (QuickActionType actionType : p.getQuickActionTypes()) {
availableQuickActionTypes.put(actionType, p.isActive());
}
} }
} }
public static void updateLocationPlugins(net.osmand.Location location) { public static void updateLocationPlugins(net.osmand.Location location) {
for (OsmandPlugin p : getEnabledPlugins()) { for (OsmandPlugin p : getEnabledPlugins()) {
p.updateLocation(location); p.updateLocation(location);

View file

@ -1379,6 +1379,7 @@ public class SettingsHelper {
} }
newActions.addAll(appliedItems); newActions.addAll(appliedItems);
actionRegistry.updateQuickActions(newActions); actionRegistry.updateQuickActions(newActions);
actionRegistry.updateActionTypes();
} }
} }
@ -1415,9 +1416,9 @@ public class SettingsHelper {
String name = object.getString("name"); String name = object.getString("name");
QuickAction quickAction = null; QuickAction quickAction = null;
if (object.has("actionType")) { if (object.has("actionType")) {
quickAction = quickActionRegistry.newActionByStringType(object.getString("actionType")); quickAction = quickActionRegistry.newActionByStringType(object.getString("actionType"), false);
} else if (object.has("type")) { } else if (object.has("type")) {
quickAction = quickActionRegistry.newActionByType(object.getInt("type")); quickAction = quickActionRegistry.newActionByType(object.getInt("type"), false);
} }
if (quickAction != null) { if (quickAction != null) {
String paramsString = object.getString("params"); String paramsString = object.getString("params");

View file

@ -872,10 +872,12 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
} }
@Override @Override
protected void registerQuickActionTypes(List<QuickActionType> quickActionTypes) { protected List<QuickActionType> getQuickActionTypes() {
ArrayList<QuickActionType> quickActionTypes = new ArrayList<>();
quickActionTypes.add(TakeAudioNoteAction.TYPE); quickActionTypes.add(TakeAudioNoteAction.TYPE);
quickActionTypes.add(TakePhotoNoteAction.TYPE); quickActionTypes.add(TakePhotoNoteAction.TYPE);
quickActionTypes.add(TakeVideoNoteAction.TYPE); quickActionTypes.add(TakeVideoNoteAction.TYPE);
return quickActionTypes;
} }
@Override @Override

View file

@ -49,6 +49,7 @@ import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_CREATE_POI; import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_CREATE_POI;
@ -139,10 +140,12 @@ public class OsmEditingPlugin extends OsmandPlugin {
// private EditingPOIDialogProvider poiActions; // private EditingPOIDialogProvider poiActions;
@Override @Override
protected void registerQuickActionTypes(List<QuickActionType> quickActionTypes) { protected List<QuickActionType> getQuickActionTypes() {
ArrayList<QuickActionType> quickActionTypes = new ArrayList<>();
quickActionTypes.add(AddPOIAction.TYPE); quickActionTypes.add(AddPOIAction.TYPE);
quickActionTypes.add(AddOSMBugAction.TYPE); quickActionTypes.add(AddOSMBugAction.TYPE);
quickActionTypes.add(ShowHideOSMBugAction.TYPE); quickActionTypes.add(ShowHideOSMBugAction.TYPE);
return quickActionTypes;
} }
@Override @Override

View file

@ -41,6 +41,7 @@ import net.osmand.plus.views.OsmandMapLayer.DrawSettings;
import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.plus.views.mapwidgets.TextInfoWidget; import net.osmand.plus.views.mapwidgets.TextInfoWidget;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List; import java.util.List;
@ -632,7 +633,9 @@ public class ParkingPositionPlugin extends OsmandPlugin {
@Override @Override
protected void registerQuickActionTypes(List<QuickActionType> quickActionTypes) { protected List<QuickActionType> getQuickActionTypes() {
ArrayList<QuickActionType> quickActionTypes = new ArrayList<>();
quickActionTypes.add(ParkingAction.TYPE); quickActionTypes.add(ParkingAction.TYPE);
return quickActionTypes;
} }
} }

View file

@ -124,7 +124,7 @@ public class CreateEditActionDialog extends DialogFragment {
: savedInstanceState.getBoolean(KEY_ACTION_IS_NEW); : savedInstanceState.getBoolean(KEY_ACTION_IS_NEW);
action = QuickActionRegistry.produceAction(isNew action = QuickActionRegistry.produceAction(isNew
? quickActionRegistry.newActionByType(type) ? quickActionRegistry.newActionByType(type, true)
: quickActionRegistry.getQuickAction(actionId)); : quickActionRegistry.getQuickAction(actionId));
setupToolbar(view); setupToolbar(view);

View file

@ -2,6 +2,8 @@ package net.osmand.plus.quickaction;
import android.content.Context; import android.content.Context;
import androidx.core.util.Pair;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
@ -70,8 +72,8 @@ public class QuickActionRegistry {
private final Map<String, Boolean> fabStateMap; private final Map<String, Boolean> fabStateMap;
private final Gson gson; private final Gson gson;
private List<QuickActionType> quickActionTypes = new ArrayList<>(); private List<QuickActionType> quickActionTypes = new ArrayList<>();
private Map<Integer, QuickActionType> quickActionTypesInt = new TreeMap<>(); private Map<Integer, Pair<QuickActionType, Boolean>> quickActionTypesInt = new TreeMap<>();
private Map<String, QuickActionType> quickActionTypesStr = new TreeMap<>(); private Map<String, Pair<QuickActionType, Boolean>> quickActionTypesStr = new TreeMap<>();
private QuickActionUpdatesListener updatesListener; private QuickActionUpdatesListener updatesListener;
@ -232,14 +234,23 @@ public class QuickActionRegistry {
quickActionTypes.add(NavAutoZoomMapAction.TYPE); quickActionTypes.add(NavAutoZoomMapAction.TYPE);
quickActionTypes.add(NavStartStopAction.TYPE); quickActionTypes.add(NavStartStopAction.TYPE);
quickActionTypes.add(NavResumePauseAction.TYPE); quickActionTypes.add(NavResumePauseAction.TYPE);
OsmandPlugin.registerQuickActionTypesPlugins(quickActionTypes);
Map<Integer, QuickActionType> quickActionTypesInt = new TreeMap<>(); Map<Integer, Pair<QuickActionType, Boolean>> quickActionTypesInt = new TreeMap<>();
Map<String, QuickActionType> quickActionTypesStr = new TreeMap<>(); Map<String, Pair<QuickActionType, Boolean>> quickActionTypesStr = new TreeMap<>();
for (QuickActionType qt : quickActionTypes) { for (QuickActionType qt : quickActionTypes) {
quickActionTypesInt.put(qt.getId(), qt); quickActionTypesInt.put(qt.getId(), new Pair<>(qt, true));
quickActionTypesStr.put(qt.getStringId(), qt); quickActionTypesStr.put(qt.getStringId(), new Pair<>(qt, true));
} }
Map<QuickActionType, Boolean> pluginActions = new HashMap<>();
OsmandPlugin.registerQuickActionTypesPlugins(pluginActions);
for (Map.Entry<QuickActionType, Boolean> entry : pluginActions.entrySet()) {
QuickActionType qt = entry.getKey();
boolean available = entry.getValue();
quickActionTypesInt.put(qt.getId(), new Pair<>(qt, available));
quickActionTypesStr.put(qt.getStringId(), new Pair<>(qt, available));
}
this.quickActionTypes = quickActionTypes; this.quickActionTypes = quickActionTypes;
this.quickActionTypesInt = quickActionTypesInt; this.quickActionTypesInt = quickActionTypesInt;
this.quickActionTypesStr = quickActionTypesStr; this.quickActionTypesStr = quickActionTypesStr;
@ -276,18 +287,22 @@ public class QuickActionRegistry {
} }
} }
public QuickAction newActionByStringType(String actionType) { public QuickAction newActionByStringType(String actionType, boolean onlyAvailable) {
QuickActionType quickActionType = quickActionTypesStr.get(actionType); Pair<QuickActionType, Boolean> quickActionTypeState = quickActionTypesStr.get(actionType);
if (quickActionType != null) { if (quickActionTypeState != null && quickActionTypeState.first != null) {
return quickActionType.createNew(); if (!onlyAvailable || quickActionTypeState.second != null && quickActionTypeState.second) {
return quickActionTypeState.first.createNew();
}
} }
return null; return null;
} }
public QuickAction newActionByType(int type) { public QuickAction newActionByType(int type, boolean onlyAvailable) {
QuickActionType quickActionType = quickActionTypesInt.get(type); Pair<QuickActionType, Boolean> quickActionTypeState = quickActionTypesInt.get(type);
if (quickActionType != null) { if (quickActionTypeState != null && quickActionTypeState.first != null) {
return quickActionType.createNew(); if (!onlyAvailable || quickActionTypeState.second != null && quickActionTypeState.second) {
return quickActionTypeState.first.createNew();
}
} }
return null; return null;
} }
@ -303,12 +318,16 @@ public class QuickActionRegistry {
public QuickAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { public QuickAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject(); JsonObject obj = json.getAsJsonObject();
QuickActionType found = null; QuickActionType found = null;
Pair<QuickActionType, Boolean> quickActionTypeState = null;
if (obj.has("actionType")) { if (obj.has("actionType")) {
String actionType = obj.get("actionType").getAsString(); String actionType = obj.get("actionType").getAsString();
found = quickActionTypesStr.get(actionType); quickActionTypeState = quickActionTypesStr.get(actionType);
} else if (obj.has("type")) { } else if (obj.has("type")) {
int type = obj.get("type").getAsInt(); int type = obj.get("type").getAsInt();
found = quickActionTypesInt.get(type); quickActionTypeState = quickActionTypesInt.get(type);
}
if (quickActionTypeState != null && quickActionTypeState.second != null && quickActionTypeState.second) {
found = quickActionTypeState.first;
} }
if (found != null) { if (found != null) {
QuickAction qa = found.createNew(); QuickAction qa = found.createNew();

View file

@ -683,9 +683,11 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
} }
@Override @Override
protected void registerQuickActionTypes(List<QuickActionType> quickActionTypes) { protected List<QuickActionType> getQuickActionTypes() {
ArrayList<QuickActionType> quickActionTypes = new ArrayList<>();
quickActionTypes.add(MapSourceAction.TYPE); quickActionTypes.add(MapSourceAction.TYPE);
quickActionTypes.add(MapOverlayAction.TYPE); quickActionTypes.add(MapOverlayAction.TYPE);
quickActionTypes.add(MapUnderlayAction.TYPE); quickActionTypes.add(MapUnderlayAction.TYPE);
return quickActionTypes;
} }
} }

View file

@ -520,8 +520,10 @@ public class SRTMPlugin extends OsmandPlugin {
} }
@Override @Override
protected void registerQuickActionTypes(List<QuickActionType> quickActionTypes) { protected List<QuickActionType> getQuickActionTypes() {
ArrayList<QuickActionType> quickActionTypes = new ArrayList<>();
quickActionTypes.add(ContourLinesAction.TYPE); quickActionTypes.add(ContourLinesAction.TYPE);
quickActionTypes.add(TerrainAction.TYPE); quickActionTypes.add(TerrainAction.TYPE);
return quickActionTypes;
} }
} }