Initial refactoring

This commit is contained in:
Victor Shcherb 2020-03-20 16:51:29 +01:00
parent c7bbb80213
commit 8c4ac76b98
7 changed files with 206 additions and 145 deletions

View file

@ -941,6 +941,7 @@ public class SettingsHelper {
JSONObject object = itemsJson.getJSONObject(i);
String name = object.getString("name");
int actionType = object.getInt("type");
// TODO
String paramsString = object.getString("params");
HashMap<String, String> params = gson.fromJson(paramsString, type);
QuickAction quickAction = new QuickAction(actionType);

View file

@ -86,7 +86,7 @@ public class AddQuickActionDialog extends DialogFragment {
private static final int HEADER = 1;
private static final int ITEM = 2;
private List<QuickAction> data;
private List<QuickActionType> data;
public class ItemViewHolder extends RecyclerView.ViewHolder {
@ -114,7 +114,7 @@ public class AddQuickActionDialog extends DialogFragment {
}
}
public Adapter(List<QuickAction> data) {
public Adapter(List<QuickActionType> data) {
this.data = data;
}
@ -138,7 +138,7 @@ public class AddQuickActionDialog extends DialogFragment {
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final QuickAction action = data.get(position);
final QuickActionType action = data.get(position);
if (getItemViewType(position) == HEADER) {
@ -179,7 +179,7 @@ public class AddQuickActionDialog extends DialogFragment {
@Override
public int getItemViewType(int position) {
if (data.get(position).type == 0)
if (data.get(position).getId() == 0)
return HEADER;
return ITEM;

View file

@ -2,6 +2,7 @@ package net.osmand.plus.quickaction;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
@ -26,52 +27,38 @@ public class QuickAction {
protected int type;
protected long id;
private @StringRes Integer nameRes;
private @DrawableRes int iconRes;
private boolean isActionEditable;
private String name;
private HashMap<String, String> params;
private QuickActionType actionType;
protected QuickAction() {
this.id = System.currentTimeMillis();
}
protected QuickAction(int type, @StringRes int nameRes) {
public QuickAction(QuickActionType type) {
// FIXME id
this.id = System.currentTimeMillis();
this.nameRes = nameRes;
this.type = type;
}
public QuickAction(int type) {
this.id = System.currentTimeMillis();
this.type = type;
this.nameRes = QuickActionFactory.getActionName(type);
this.iconRes = QuickActionFactory.getActionIcon(type);
this.isActionEditable = QuickActionFactory.isActionEditable(type);
this.actionType = type;
}
public QuickAction(QuickAction quickAction) {
this.type = quickAction.type;
this.actionType = quickAction.actionType;
this.id = quickAction.id;
this.name = quickAction.name;
this.params = quickAction.params;
this.nameRes = QuickActionFactory.getActionName(type);
this.iconRes = QuickActionFactory.getActionIcon(type);
this.isActionEditable = QuickActionFactory.isActionEditable(type);
}
public int getNameRes() {
return nameRes;
return actionType == null ? 0 : actionType.getNameRes();
}
public int getIconRes() {
return iconRes;
return actionType == null ? 0 : actionType.getIconRes();
}
public int getIconRes(Context context) {
return iconRes;
return actionType == null ? 0 : actionType.getIconRes();
}
public long getId() {
@ -83,7 +70,7 @@ public class QuickAction {
}
public boolean isActionEditable() {
return isActionEditable;
return actionType == null ? false : actionType.isActionEditable();
}
public boolean isActionEnable(OsmandApplication app) {
@ -92,13 +79,18 @@ public class QuickAction {
public String getName(Context context) {
if (Algorithms.isEmpty(name)) {
return nameRes != null ? context.getString(nameRes) : "";
} else {
return getDefaultName(context);
} else {
return name;
}
}
public HashMap<String, String> getParams() {
@NonNull
private String getDefaultName(Context context) {
return getNameRes() != 0 ? context.getString(getNameRes()) : "";
}
public HashMap<String, String> getParams() {
if (params == null) params = new HashMap<>();
@ -121,7 +113,11 @@ public class QuickAction {
return getName(application);
}
public void setAutoGeneratedTitle(EditText title){
public QuickActionType getActionType() {
return actionType;
}
public void setAutoGeneratedTitle(EditText title){
}
public void execute(MapActivity activity){};
@ -158,13 +154,10 @@ public class QuickAction {
public int hashCode() {
int result = type;
result = 31 * result + (int) (id ^ (id >>> 32));
result = 31 * result + nameRes;
result = 31 * result + iconRes;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
public boolean hasCustomName(Context context) {
return !getName(context).equals(context.getString(nameRes));
return !getName(context).equals(context.getString(getDefaultName()));
}
}

View file

@ -42,6 +42,7 @@ import net.osmand.plus.quickaction.actions.ShowHideOSMBugAction;
import net.osmand.plus.quickaction.actions.ShowHidePoiAction;
import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin;
import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.util.MapUtils;
import java.lang.reflect.Type;
import java.util.ArrayList;
@ -49,6 +50,13 @@ import java.util.List;
public class QuickActionFactory {
public static final QuickActionType TYPE_ADD_ITEMS = new QuickActionType(0, "").
nameRes(R.string.quick_action_add_create_items).category(QuickActionType.CREATE_CATEGORY);
public static final QuickActionType TYPE_CONFIGURE_MAP = new QuickActionType(0, "").
nameRes(R.string.quick_action_add_configure_map).category(QuickActionType.CONFIGURE_MAP);
public static final QuickActionType TYPE_NAVIGATION = new QuickActionType(0, "").
nameRes(R.string.quick_action_add_navigation).category(QuickActionType.NAVIGATION);
)
public String quickActionListToString(List<QuickAction> quickActions) {
return new Gson().toJson(quickActions);
}
@ -60,125 +68,99 @@ public class QuickActionFactory {
return quickActions != null ? quickActions : new ArrayList<QuickAction>();
}
public static List<QuickAction> produceTypeActionsListWithHeaders(List<QuickAction> active) {
ArrayList<QuickAction> quickActions = new ArrayList<>();
quickActions.add(new QuickAction(0, R.string.quick_action_add_create_items));
quickActions.add(new FavoriteAction());
quickActions.add(new GPXAction());
QuickAction marker = new MarkerAction();
if (!marker.hasInstanceInList(active)) {
quickActions.add(marker);
}
public static List<QuickActionType> getActionTypes() {
List<QuickActionType> quickActionsTypes = new ArrayList<>();
quickActionsTypes.add(FavoriteAction.TYPE);
quickActionsTypes.add(GPXAction.TYPE);
quickActionsTypes.add(MarkerAction.TYPE);
// FIXME plugins
if (OsmandPlugin.getEnabledPlugin(AudioVideoNotesPlugin.class) != null) {
QuickAction audio = new TakeAudioNoteAction();
QuickAction photo = new TakePhotoNoteAction();
QuickAction video = new TakeVideoNoteAction();
if (!audio.hasInstanceInList(active)) {
quickActions.add(audio);
}
if (!photo.hasInstanceInList(active)) {
quickActions.add(photo);
}
if (!video.hasInstanceInList(active)) {
quickActions.add(video);
}
quickActionsTypes.add(TakeAudioNoteAction.TYPE);
quickActionsTypes.add(TakePhotoNoteAction.TYPE);
quickActionsTypes.add(TakeVideoNoteAction.TYPE);
}
if (OsmandPlugin.getEnabledPlugin(OsmEditingPlugin.class) != null) {
quickActions.add(new AddPOIAction());
quickActions.add(new AddOSMBugAction());
quickActionsTypes.add(AddPOIAction.TYPE);
quickActionsTypes.add(AddOSMBugAction.TYPE);
}
if (OsmandPlugin.getEnabledPlugin(ParkingPositionPlugin.class) != null) {
QuickAction parking = new ParkingAction();
if (!parking.hasInstanceInList(active)) {
quickActions.add(parking);
}
quickActionsTypes.add(ParkingAction.TYPE);
}
// configure map
quickActionsTypes.add(ShowHideFavoritesAction.TYPE);
quickActionsTypes.add(ShowHideGpxTracksAction.TYPE);
quickActionsTypes.add(ShowHidePoiAction.TYPE);
quickActions.add(new QuickAction(0, R.string.quick_action_add_configure_map));
QuickAction favorites = new ShowHideFavoritesAction();
if (!favorites.hasInstanceInList(active)) {
quickActions.add(favorites);
}
quickActions.add(new ShowHideGpxTracksAction());
quickActions.add(new ShowHidePoiAction());
if (OsmandPlugin.getEnabledPlugin(OsmEditingPlugin.class) != null) {
QuickAction showHideOSMBugAction = new ShowHideOSMBugAction();
if (!showHideOSMBugAction.hasInstanceInList(active)) {
quickActions.add(showHideOSMBugAction);
}
quickActionsTypes.add(ShowHideOSMBugAction.TYPE);
}
quickActions.add(new MapStyleAction());
quickActionsTypes.add(MapStyleAction.TYPE);
if (OsmandPlugin.getEnabledPlugin(OsmandRasterMapsPlugin.class) != null) {
quickActions.add(new MapSourceAction());
quickActions.add(new MapOverlayAction());
quickActions.add(new MapUnderlayAction());
quickActionsTypes.add(MapSourceAction.TYPE);
quickActionsTypes.add(MapOverlayAction.TYPE);
quickActionsTypes.add(MapUnderlayAction.TYPE);
}
if (OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null) {
quickActions.add(new ContourLinesAction());
quickActions.add(new HillshadeAction());
quickActionsTypes.add(ContourLinesAction.TYPE);
quickActionsTypes.add(HillshadeAction.TYPE);
}
quickActions.add(new DayNightModeAction());
quickActionsTypes.add(DayNightModeAction.TYPE);
// navigation
quickActionsTypes.add(NavVoiceAction.TYPE);
quickActionsTypes.add(NavDirectionsFromAction.TYPE);
quickActionsTypes.add(NavAddDestinationAction.TYPE);
quickActionsTypes.add(NavAddFirstIntermediateAction.TYPE);
quickActionsTypes.add(NavReplaceDestinationAction.TYPE);
quickActionsTypes.add(NavAutoZoomMapAction.TYPE);
quickActionsTypes.add(NavStartStopAction.TYPE);
quickActionsTypes.add(NavResumePauseAction.TYPE);
return quickActionsTypes;
}
QuickAction voice = new NavVoiceAction();
QuickAction directionFrom = new NavDirectionsFromAction();
QuickAction addDestination = new NavAddDestinationAction();
QuickAction addFirstIntermediate = new NavAddFirstIntermediateAction();
QuickAction replaceDestination = new NavReplaceDestinationAction();
QuickAction autoZoomMap = new NavAutoZoomMapAction();
QuickAction startStopNavigation = new NavStartStopAction();
QuickAction resumePauseNavigation = new NavResumePauseAction();
ArrayList<QuickAction> navigationQuickActions = new ArrayList<>();
if (!voice.hasInstanceInList(active)) {
navigationQuickActions.add(voice);
}
if (!directionFrom.hasInstanceInList(active)) {
navigationQuickActions.add(directionFrom);
}
if (!addDestination.hasInstanceInList(active)) {
navigationQuickActions.add(addDestination);
}
if (!addFirstIntermediate.hasInstanceInList(active)) {
navigationQuickActions.add(addFirstIntermediate);
}
if (!replaceDestination.hasInstanceInList(active)) {
navigationQuickActions.add(replaceDestination);
}
if (!autoZoomMap.hasInstanceInList(active)) {
navigationQuickActions.add(autoZoomMap);
}
if (!startStopNavigation.hasInstanceInList(active)) {
navigationQuickActions.add(startStopNavigation);
}
if (!resumePauseNavigation.hasInstanceInList(active)) {
navigationQuickActions.add(resumePauseNavigation);
}
if (navigationQuickActions.size() > 0) {
quickActions.add(new QuickAction(0, R.string.quick_action_add_navigation));
quickActions.addAll(navigationQuickActions);
}
public static List<QuickActionType> produceTypeActionsListWithHeaders(List<QuickAction> active) {
List<QuickActionType> quickActions = new ArrayList<>();
List<QuickActionType> tps = getActionTypes();
filterQuickActions(active, tps, TYPE_ADD_ITEMS, quickActions);
filterQuickActions(active, tps, TYPE_CONFIGURE_MAP, quickActions);
filterQuickActions(active, tps, TYPE_NAVIGATION, quickActions);
return quickActions;
}
public static QuickAction newActionByType(int type) {
private static void filterQuickActions(List<QuickAction> active, List<QuickActionType> allTypes, QuickActionType filter,
List<QuickActionType> result) {
result.add(filter);
for(QuickActionType t : allTypes) {
if(t.getCategory() == filter.getCategory()) {
if(t.isActionEditable()) {
boolean instanceInList = false;
for(QuickAction qa : active) {
if(qa.getActionType().getId() == t.getId()) {
instanceInList = true;
break;
}
}
if (!instanceInList) {
result.add(t);
}
} else {
result.add(t);
}
}
}
}
public static QuickAction newActionByType(int type) {
// FIXME
switch (type) {
case NewAction.TYPE:
@ -217,9 +199,6 @@ public class QuickActionFactory {
case ShowHideOSMBugAction.TYPE:
return new ShowHideOSMBugAction();
case AddOSMBugAction.TYPE:
return new AddOSMBugAction();
case AddPOIAction.TYPE:
return new AddPOIAction();
@ -274,7 +253,7 @@ public class QuickActionFactory {
}
public static QuickAction produceAction(QuickAction quickAction) {
// FIXME
switch (quickAction.type) {
case NewAction.TYPE:
@ -313,9 +292,6 @@ public class QuickActionFactory {
case ShowHideOSMBugAction.TYPE:
return new ShowHideOSMBugAction(quickAction);
case AddOSMBugAction.TYPE:
return new AddOSMBugAction(quickAction);
case AddPOIAction.TYPE:
return new AddPOIAction(quickAction);
@ -409,9 +385,6 @@ public class QuickActionFactory {
case ShowHideOSMBugAction.TYPE:
return R.drawable.ic_action_bug_dark;
case AddOSMBugAction.TYPE:
return R.drawable.ic_action_bug_dark;
case AddPOIAction.TYPE:
return R.drawable.ic_action_gabout_dark;
@ -505,9 +478,6 @@ public class QuickActionFactory {
case ShowHideOSMBugAction.TYPE:
return R.string.quick_action_showhide_osmbugs_title;
case AddOSMBugAction.TYPE:
return R.string.quick_action_add_osm_bug;
case AddPOIAction.TYPE:
return R.string.quick_action_add_poi;

View file

@ -67,11 +67,13 @@ public class QuickActionRegistry {
}
public List<QuickAction> getQuickActions() {
List<QuickAction> actions = new ArrayList<>();
actions.addAll(quickActions);
return actions;
return new ArrayList<>(quickActions);
}
public List<QuickActionType> getQuickActionTypes() {
List<QuickActionType> list = new ArrayList<>();
}
public List<QuickAction> getFilteredQuickActions() {
List<QuickAction> actions = getQuickActions();

View file

@ -0,0 +1,90 @@
package net.osmand.plus.quickaction;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
public class QuickActionType {
public static final int CREATE_CATEGORY = 0;
public static final int CONFIGURE_MAP = 1;
public static final int NAVIGATION = 2;
private final int id;
private final String stringId;
private boolean actionEditable;
private @StringRes int nameRes;
private @DrawableRes int iconRes;
private Class<? extends QuickAction> cl;
private int category;
public QuickActionType(int id, String stringId) {
this.id = id;
this.stringId = stringId;
}
public QuickActionType(int id, String stringId, Class<? extends QuickAction> cl) {
this.id = id;
this.stringId = stringId;
this.cl = cl;
}
public QuickActionType nameRes(int nameRes) {
this.nameRes = nameRes;
return this;
}
public QuickActionType category(int cat) {
this.category = cat;
return this;
}
public QuickActionType iconRes(int iconRes) {
this.iconRes = iconRes;
return this;
}
public QuickActionType editable() {
actionEditable = true;
return this;
}
public QuickAction createNew() {
if(cl != null) {
try {
return cl.newInstance();
} catch (InstantiationException e) {
throw new UnsupportedOperationException(e);
} catch (IllegalAccessException e) {
throw new UnsupportedOperationException(e);
}
} else {
throw new UnsupportedOperationException();
}
}
public int getId() {
return id;
}
public String getStringId() {
return stringId;
}
public boolean isActionEditable() {
return actionEditable;
}
public int getNameRes() {
return nameRes;
}
public int getIconRes() {
return iconRes;
}
public int getCategory() {
return category;
}
}

View file

@ -13,14 +13,19 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.osmedit.OsmEditingPlugin;
import net.osmand.plus.quickaction.QuickAction;
import net.osmand.plus.quickaction.QuickActionType;
public class AddOSMBugAction extends QuickAction {
public static final int TYPE = 12;
private static final String KEY_MESSAGE = "message";
private static final String KEY_SHO_DIALOG = "dialog";
public static final QuickActionType TYPE = new QuickActionType(12,
"osmbug.add", AddOSMBugAction.class).
nameRes(R.string.quick_action_add_osm_bug).iconRes(R.drawable.ic_action_bug_dark).
category(QuickActionType.CREATE_CATEGORY);
public AddOSMBugAction() {
super(TYPE);
}