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++) {
JSONObject object = itemsJson.getJSONObject(i);
String name = object.getString("name");
// FIXME QA: make type string
int actionType = object.getInt("type");
QuickAction quickAction = QuickActionRegistry.newActionByType(actionType);
if (quickAction.getType() != 0) {
QuickAction quickAction = null;
if(object.has("actionType")) {
quickAction = QuickActionRegistry.newActionByStringType(object.getString("actionType"));
} else if(object.has("type")) {
quickAction = QuickActionRegistry.newActionByType(object.getInt("type"));
}
if (quickAction != null) {
String paramsString = object.getString("params");
HashMap<String, String> params = gson.fromJson(paramsString, type);

View file

@ -139,7 +139,7 @@ public class CreateEditActionDialog extends DialogFragment {
super.onSaveInstanceState(outState);
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);
}

View file

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

View file

@ -271,7 +271,7 @@ public class QuickActionListFragment extends BaseOsmAndFragment implements Quick
@Override
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) {

View file

@ -3,7 +3,18 @@ package net.osmand.plus.quickaction;
import android.content.Context;
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.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import net.osmand.plus.OsmandPlugin;
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.ShowHideGpxTracksAction;
import net.osmand.plus.quickaction.actions.ShowHidePoiAction;
import net.osmand.util.Algorithms;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -57,15 +71,73 @@ public class QuickActionRegistry {
private final List<QuickAction> quickActions;
private final Map<String, Boolean> fabStateMap;
private final Gson gson;
private QuickActionUpdatesListener updatesListener;
private QuickActionUpdatesListener updatesListener;
public QuickActionRegistry(OsmandSettings settings) {
this.settings = settings;
quickActions = parseActiveActionsList(settings.QUICK_ACTION_LIST.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) {
this.updatesListener = updatesListener;
@ -186,18 +258,23 @@ public class QuickActionRegistry {
}
private String quickActionListToString(List<QuickAction> quickActions) {
// FIXME QA: backward compatibiltiy
return new Gson().toJson(quickActions);
return gson.toJson(quickActions);
}
public List<QuickAction> parseActiveActionsList(String json) {
// FIXME QA: backward compatibiltiy
Type type = new TypeToken<List<QuickAction>>() {
}.getType();
ArrayList<QuickAction> quickActions = new Gson().fromJson(json, type);
return quickActions != null ? quickActions : new ArrayList<QuickAction>();
Type type = new TypeToken<List<QuickAction>>() {}.getType();
List<QuickAction> quickActions = gson.fromJson(json, type);
List<QuickActionType> actionTypes = QuickActionRegistry.getActionTypes();
List<QuickAction> rquickActions = new ArrayList<>(quickActions.size());
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) {
for (QuickActionType t : getActionTypes()) {
if (t.getId() == type) {
return t.createNew();
}
}
return new QuickAction();
return null;
}
public static QuickAction produceAction(QuickAction quickAction) {

View file

@ -10,7 +10,7 @@ import net.osmand.plus.quickaction.QuickActionType;
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).
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
public void onActionSelected(QuickAction action) {
QuickActionFactory.produceAction(action).execute(mapActivity);
QuickActionRegistry.produceAction(action).execute(mapActivity);
setLayerState(false);
}