Fix compilation

This commit is contained in:
Victor Shcherb 2020-03-22 02:09:44 +01:00
parent d2fd6a6cc1
commit adf9026d78
7 changed files with 136 additions and 29 deletions

View file

@ -941,11 +941,13 @@ public class SettingsHelper {
for (int i = 0; i < itemsJson.length(); i++) { for (int i = 0; i < itemsJson.length(); i++) {
JSONObject object = itemsJson.getJSONObject(i); JSONObject object = itemsJson.getJSONObject(i);
String name = object.getString("name"); String name = object.getString("name");
// FIXME QA: make type string QuickAction quickAction = null;
int actionType = object.getInt("type"); if(object.has("actionType")) {
QuickAction quickAction = QuickActionRegistry.newActionByType(actionType); quickAction = QuickActionRegistry.newActionByStringType(object.getString("actionType"));
if (quickAction.getType() != 0) { } else if(object.has("type")) {
quickAction = QuickActionRegistry.newActionByType(object.getInt("type"));
}
if (quickAction != null) {
String paramsString = object.getString("params"); String paramsString = object.getString("params");
HashMap<String, String> params = gson.fromJson(paramsString, type); HashMap<String, String> params = gson.fromJson(paramsString, type);

View file

@ -139,7 +139,7 @@ public class CreateEditActionDialog extends DialogFragment {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putLong(KEY_ACTION_ID, action.getId()); outState.putLong(KEY_ACTION_ID, action.getId());
outState.putInt(KEY_ACTION_TYPE, action.type); outState.putInt(KEY_ACTION_TYPE, action.getType());
outState.putBoolean(KEY_ACTION_IS_NEW, isNew); outState.putBoolean(KEY_ACTION_IS_NEW, isNew);
} }

View file

@ -7,6 +7,11 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.EditText; import android.widget.EditText;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import androidx.annotation.DrawableRes; import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
@ -15,8 +20,10 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.quickaction.actions.NewAction; import net.osmand.plus.quickaction.actions.NewAction;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class QuickAction { public class QuickAction {
@ -24,19 +31,19 @@ public class QuickAction {
void onActionSelected(QuickAction action); void onActionSelected(QuickAction action);
} }
private static int SEQ = 0;
protected long id; protected long id;
private String name; private String name;
private HashMap<String, String> params; private Map<String, String> params;
private QuickActionType actionType; private QuickActionType actionType;
protected QuickAction() { protected QuickAction() {
this(NewAction.TYPE); this(QuickActionRegistry.TYPE_ADD_ITEMS);
} }
public QuickAction(QuickActionType type) { public QuickAction(QuickActionType type) {
// FIXME QA: id this.id = System.currentTimeMillis() + (SEQ++);
this.id = System.currentTimeMillis();
this.actionType = type; this.actionType = type;
} }
@ -67,6 +74,10 @@ public class QuickAction {
return actionType.getId(); return actionType.getId();
} }
public void setActionType(QuickActionType actionType) {
this.actionType = actionType;
}
public boolean isActionEditable() { public boolean isActionEditable() {
return actionType == null ? false : actionType.isActionEditable(); return actionType == null ? false : actionType.isActionEditable();
} }
@ -83,15 +94,19 @@ public class QuickAction {
} }
} }
public String getRawName() {
return name;
}
@NonNull @NonNull
private String getDefaultName(Context context) { private String getDefaultName(Context context) {
return getNameRes() != 0 ? context.getString(getNameRes()) : ""; return getNameRes() != 0 ? context.getString(getNameRes()) : "";
} }
public HashMap<String, String> getParams() { public Map<String, String> getParams() {
if (params == null) {
if (params == null) params = new HashMap<>(); params = new HashMap<>();
}
return params; return params;
} }
@ -99,7 +114,11 @@ public class QuickAction {
this.name = name; this.name = name;
} }
public void setParams(HashMap<String, String> params) { public void setId(long id) {
this.id = id;
}
public void setParams(Map<String, String> params) {
this.params = params; this.params = params;
} }

View file

@ -271,7 +271,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
return itemsList.get(position).type == 0 ? SCREEN_HEADER_TYPE : SCREEN_ITEM_TYPE; return itemsList.get(position).getType() == 0 ? SCREEN_HEADER_TYPE : SCREEN_ITEM_TYPE;
} }
public void deleteItem(int position) { public void deleteItem(int position) {

View file

@ -3,7 +3,18 @@ package net.osmand.plus.quickaction;
import android.content.Context; import android.content.Context;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
@ -25,9 +36,12 @@ import net.osmand.plus.quickaction.actions.NewAction;
import net.osmand.plus.quickaction.actions.ShowHideFavoritesAction; import net.osmand.plus.quickaction.actions.ShowHideFavoritesAction;
import net.osmand.plus.quickaction.actions.ShowHideGpxTracksAction; import net.osmand.plus.quickaction.actions.ShowHideGpxTracksAction;
import net.osmand.plus.quickaction.actions.ShowHidePoiAction; import net.osmand.plus.quickaction.actions.ShowHidePoiAction;
import net.osmand.util.Algorithms;
import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -57,6 +71,7 @@ public class QuickActionRegistry {
private final List<QuickAction> quickActions; private final List<QuickAction> quickActions;
private final Map<String, Boolean> fabStateMap; private final Map<String, Boolean> fabStateMap;
private final Gson gson;
private QuickActionUpdatesListener updatesListener; private QuickActionUpdatesListener updatesListener;
@ -65,6 +80,63 @@ public class QuickActionRegistry {
this.settings = settings; this.settings = settings;
quickActions = parseActiveActionsList(settings.QUICK_ACTION_LIST.get()); quickActions = parseActiveActionsList(settings.QUICK_ACTION_LIST.get());
fabStateMap = getQuickActionFabStateMapFromJson(settings.QUICK_ACTION.get()); fabStateMap = getQuickActionFabStateMapFromJson(settings.QUICK_ACTION.get());
gson = new GsonBuilder().registerTypeAdapter(QuickAction.class, new JsonSerializer<QuickAction>() {
@Override
public JsonElement serialize(QuickAction src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject el = new JsonObject();
el.addProperty("actionType", src.getActionType().getStringId());
el.addProperty("id", src.getId());
if (src.getRawName() != null) {
el.addProperty("name", src.getRawName());
}
el.add("params", context.serialize(src.getParams()));
return el;
}
}).registerTypeAdapter(QuickAction.class, new JsonDeserializer<QuickAction>() {
@Override
public QuickAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject();
// TODO iteration
List<QuickActionType> types = QuickActionRegistry.getActionTypes();
QuickActionType found = null;
if(obj.has("actionType")) {
String actionType = obj.get("actionType").getAsString();
for(QuickActionType q : types) {
if(Algorithms.stringsEqual(q.getStringId(), actionType)) {
found = q;
break;
}
}
} else if(obj.has("type")) {
int type = obj.get("type").getAsInt();
for(QuickActionType q : types) {
if(q.getId() == type) {
found = q;
break;
}
}
}
if(found != null) {
QuickAction qa = found.createNew();
if(obj.has("name")) {
qa.setName(obj.get("name").getAsString());
}
if(obj.has("id")) {
qa.setId(obj.get("id").getAsLong());
}
if(obj.has("params")) {
qa.setParams(
(Map<String, String>) context.deserialize(obj.get("params"),
new TypeToken<HashMap<String, String>>(){}.getType())
);
}
return qa;
}
return null;
}
}).create();
} }
public void setUpdatesListener(QuickActionUpdatesListener updatesListener) { public void setUpdatesListener(QuickActionUpdatesListener updatesListener) {
@ -186,18 +258,23 @@ public class QuickActionRegistry {
} }
private String quickActionListToString(List<QuickAction> quickActions) { private String quickActionListToString(List<QuickAction> quickActions) {
// FIXME QA: backward compatibiltiy return gson.toJson(quickActions);
return new Gson().toJson(quickActions);
} }
public List<QuickAction> parseActiveActionsList(String json) { public List<QuickAction> parseActiveActionsList(String json) {
// FIXME QA: backward compatibiltiy Type type = new TypeToken<List<QuickAction>>() {}.getType();
Type type = new TypeToken<List<QuickAction>>() { List<QuickAction> quickActions = gson.fromJson(json, type);
}.getType(); List<QuickActionType> actionTypes = QuickActionRegistry.getActionTypes();
ArrayList<QuickAction> quickActions = new Gson().fromJson(json, type); List<QuickAction> rquickActions = new ArrayList<>(quickActions.size());
return quickActions != null ? quickActions : new ArrayList<QuickAction>(); if(quickActions != null) {
for(QuickAction qa : rquickActions) {
if(qa != null) {
rquickActions.add(qa);
}
}
}
return rquickActions;
} }
@ -258,13 +335,22 @@ public class QuickActionRegistry {
} }
} }
public static QuickAction newActionByStringType(String actionType) {
for (QuickActionType t : getActionTypes()) {
if (Algorithms.objectEquals(actionType, t.getStringId())) {
return t.createNew();
}
}
return null;
}
public static QuickAction newActionByType(int type) { public static QuickAction newActionByType(int type) {
for (QuickActionType t : getActionTypes()) { for (QuickActionType t : getActionTypes()) {
if (t.getId() == type) { if (t.getId() == type) {
return t.createNew(); return t.createNew();
} }
} }
return new QuickAction(); return null;
} }
public static QuickAction produceAction(QuickAction quickAction) { public static QuickAction produceAction(QuickAction quickAction) {

View file

@ -10,7 +10,7 @@ import net.osmand.plus.quickaction.QuickActionType;
public class NewAction extends QuickAction { public class NewAction extends QuickAction {
public static final QuickActionType TYPE = new QuickActionType(0, "new", public static final QuickActionType TYPE = new QuickActionType(1, "new",
NewAction.class). NewAction.class).
nonEditable().iconRes(R.drawable.ic_action_plus).nameRes(R.string.quick_action_new_action); nonEditable().iconRes(R.drawable.ic_action_plus).nameRes(R.string.quick_action_new_action);

View file

@ -430,7 +430,7 @@ public class MapQuickActionLayer extends OsmandMapLayer implements QuickActionRe
@Override @Override
public void onActionSelected(QuickAction action) { public void onActionSelected(QuickAction action) {
QuickActionFactory.produceAction(action).execute(mapActivity); QuickActionRegistry.produceAction(action).execute(mapActivity);
setLayerState(false); setLayerState(false);
} }