quick actions some bug fixes

This commit is contained in:
Rosty 2017-01-06 12:49:53 +02:00
parent 43dec2b43b
commit dd888e73b0
3 changed files with 246 additions and 73 deletions

View file

@ -1,7 +1,6 @@
package net.osmand.plus.quickaction;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
@ -21,8 +20,6 @@ import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import net.osmand.data.FavouritePoint;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.IconsCache;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -191,7 +188,7 @@ public class CreateEditActionDialog extends DialogFragment {
}
});
name.setEnabled(!action.autogeneratedTitle);
name.setEnabled(action.isActionEditable());
action.setAutoGeneratedTitle(name);
if (savedInstanceState == null) name.setText(action.getName(getContext()));

View file

@ -2,26 +2,17 @@ package net.osmand.plus.quickaction;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import com.google.gson.Gson;
import com.google.gson.annotations.Expose;
import com.google.gson.reflect.TypeToken;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.MapActivity;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class QuickAction {
@ -33,10 +24,9 @@ public class QuickAction {
protected int type;
protected long id;
private @StringRes int nameRes;
private @DrawableRes int iconRes;
protected boolean autogeneratedTitle;
private @StringRes int nameRes;
private @DrawableRes int iconRes;
private boolean isActionEditable;
private String name;
private HashMap<String, String> params;
@ -56,16 +46,18 @@ public class QuickAction {
this.type = type;
this.nameRes = QuickActionFactory.getActionName(type);
this.iconRes = QuickActionFactory.getActionIcon(type);
this.isActionEditable = QuickActionFactory.isActionEditable(type);
}
public QuickAction(QuickAction quickAction) {
this.type = quickAction.type;
this.id = quickAction.id;
this.nameRes = quickAction.nameRes;
this.iconRes = quickAction.iconRes;
this.name = quickAction.name;
this.params = quickAction.params;
this.autogeneratedTitle = quickAction.autogeneratedTitle;
this.nameRes = QuickActionFactory.getActionName(type);
this.iconRes = QuickActionFactory.getActionIcon(type);
this.isActionEditable = QuickActionFactory.isActionEditable(type);
}
public int getNameRes() {
@ -80,6 +72,10 @@ public class QuickAction {
return id;
}
public boolean isActionEditable() {
return isActionEditable;
}
public String getName(Context context) {
return name == null || name.isEmpty() ? nameRes > 0 ? context.getString(nameRes) : "" : name;
}
@ -126,14 +122,18 @@ public class QuickAction {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (o == null) return false;
QuickAction action = (QuickAction) o;
if (o instanceof QuickAction) {
if (type != action.type) return false;
if (id != action.id) return false;
QuickAction action = (QuickAction) o;
return true;
if (type != action.type) return false;
if (id != action.id) return false;
return true;
} else return false;
}
@Override

View file

@ -8,6 +8,7 @@ import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.util.Pair;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
@ -46,6 +47,7 @@ import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.GeocodingLookupService;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.audionotes.AudioVideoNotesPlugin;
@ -59,6 +61,7 @@ import net.osmand.plus.osmedit.OsmEditingPlugin;
import net.osmand.plus.parkingpoint.ParkingPositionPlugin;
import net.osmand.plus.poi.PoiFiltersHelper;
import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin;
import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.render.RenderingIcons;
import net.osmand.plus.views.OsmandMapTileView;
@ -146,6 +149,11 @@ public class QuickActionFactory {
quickActions.add(new QuickAction(0, R.string.quick_action_add_configure_map));
quickActions.add(new MapStyleAction());
if (OsmandPlugin.getEnabledPlugin(OsmandRasterMapsPlugin.class) != null) {
quickActions.add(new MapSourceAction());
}
QuickAction favorites = new ShowHideFavoritesAction();
if (!favorites.hasInstanceInList(active)) {
@ -211,6 +219,9 @@ public class QuickActionFactory {
case MapStyleAction.TYPE:
return new MapStyleAction();
case MapSourceAction.TYPE:
return new MapSourceAction();
default:
return new QuickAction();
}
@ -262,6 +273,9 @@ public class QuickActionFactory {
case MapStyleAction.TYPE:
return new MapStyleAction(quickAction);
case MapSourceAction.TYPE:
return new MapSourceAction(quickAction);
default:
return quickAction;
}
@ -314,7 +328,7 @@ public class QuickActionFactory {
return R.drawable.ic_map;
case MapSourceAction.TYPE:
return R.drawable.ic_map;
return R.drawable.ic_world_globe_dark;
case MapOverlayAction.TYPE:
return R.drawable.ic_layer_top_dark;
@ -385,6 +399,25 @@ public class QuickActionFactory {
}
}
public static boolean isActionEditable(int type) {
switch (type) {
case NewAction.TYPE:
case MarkerAction.TYPE:
case ShowHideFavoritesAction.TYPE:
case ShowHidePoiAction.TYPE:
case ParkingAction.TYPE:
case TakeAudioNoteAction.TYPE:
case TakePhotoNoteAction.TYPE:
case TakeVideoNoteAction.TYPE:
case NavigationVoiceAction.TYPE:
return false;
default: return true;
}
}
public static class NewAction extends QuickAction {
public static final int TYPE = 1;
@ -647,7 +680,6 @@ public class QuickActionFactory {
protected ShowHideFavoritesAction() {
super(TYPE);
autogeneratedTitle = true;
}
public ShowHideFavoritesAction(QuickAction quickAction) {
@ -700,7 +732,6 @@ public class QuickActionFactory {
protected ShowHidePoiAction() {
super(TYPE);
autogeneratedTitle = true;
}
public ShowHidePoiAction(QuickAction quickAction) {
@ -1598,7 +1629,7 @@ public class QuickActionFactory {
}
}
public static class MapStyleAction extends SwitchableAction {
public static class MapStyleAction extends SwitchableAction<String> {
public static final int TYPE = 14;
@ -1649,10 +1680,10 @@ public class QuickActionFactory {
List<String> filtered = new ArrayList<>();
boolean enabled = OsmandPlugin.getEnabledPlugin(NauticalMapsPlugin.class) != null;
if (enabled) return loadMapStyles();
if (enabled) return loadListFromParams();
else {
for (String style : loadMapStyles()) {
for (String style : loadListFromParams()) {
if (!style.equals(RendererRegistry.NAUTICAL_RENDER)){
filtered.add(style);
@ -1732,9 +1763,40 @@ public class QuickActionFactory {
}
};
}
@Override protected void saveListToParams(List<String> styles) {
getParams().put(getListKey(), TextUtils.join(",", styles));
}
@Override protected List<String> loadListFromParams() {
List<String> styles = new ArrayList<>();
String filtersId = getParams().get(getListKey());
if (filtersId != null && !filtersId.trim().isEmpty()) {
Collections.addAll(styles, filtersId.split(","));
}
return styles;
}
@Override
protected String getItemName(String item) {
return item;
}
@Override protected String getTitle(List<String> filters) {
if (filters.isEmpty()) return "";
return filters.size() > 1
? filters.get(0) + " +" + (filters.size() - 1)
: filters.get(0);
}
}
public static class MapOverlayAction extends SwitchableAction {
public static class MapOverlayAction extends SwitchableAction<Pair<String, String>> {
public static final int TYPE = 15;
@ -1748,6 +1810,26 @@ public class QuickActionFactory {
super(quickAction);
}
@Override
protected String getTitle(List<Pair<String, String>> filters) {
return null;
}
@Override
protected void saveListToParams(List<Pair<String, String>> list) {
}
@Override
protected List<Pair<String, String>> loadListFromParams() {
return null;
}
@Override
protected String getItemName(Pair<String, String> item) {
return null;
}
@Override
public void execute(MapActivity activity) {
@ -1785,7 +1867,7 @@ public class QuickActionFactory {
}
public static class MapUnderlayAction extends SwitchableAction {
public static class MapUnderlayAction extends SwitchableAction<Pair<String, String>> {
public static final int TYPE = 16;
@ -1799,6 +1881,26 @@ public class QuickActionFactory {
super(quickAction);
}
@Override
protected String getTitle(List<Pair<String, String>> filters) {
return null;
}
@Override
protected void saveListToParams(List<Pair<String, String>> list) {
}
@Override
protected List<Pair<String, String>> loadListFromParams() {
return null;
}
@Override
protected String getItemName(Pair<String, String> item) {
return null;
}
@Override
public void execute(MapActivity activity) {
@ -1835,7 +1937,7 @@ public class QuickActionFactory {
}
}
public static class MapSourceAction extends SwitchableAction {
public static class MapSourceAction extends SwitchableAction<Pair<String, String>> {
public static final int TYPE = 17;
@ -1849,6 +1951,34 @@ public class QuickActionFactory {
super(quickAction);
}
@Override
protected String getTitle(List<Pair<String, String>> filters) {
if (filters.isEmpty()) return "";
return filters.size() > 1
? filters.get(0).second + " +" + (filters.size() - 1)
: filters.get(0).second;
}
@Override
protected void saveListToParams(List<Pair<String, String>> list) {
getParams().put(getListKey(), new Gson().toJson(list));
}
@Override
protected List<Pair<String, String>> loadListFromParams() {
String json = getParams().get(getListKey());
if (json == null || json.isEmpty()) return new ArrayList<>();
Type listType = new TypeToken<ArrayList<Pair<String, String>>>(){}.getType();
return new Gson().fromJson(json, listType);
}
@Override
public void execute(MapActivity activity) {
@ -1880,12 +2010,76 @@ public class QuickActionFactory {
@Override
public void onClick(View view) {
final OsmandSettings settings = activity.getMyApplication().getSettings();
final LinkedHashMap<String, String> entriesMap = new LinkedHashMap<>();
final String layerOsmVector = "LAYER_OSM_VECTOR";
entriesMap.put(layerOsmVector, activity.getString(R.string.vector_data));
entriesMap.putAll(settings.getTileSourceEntries());
final List<Entry<String, String>> entriesMapList = new ArrayList<>(entriesMap.entrySet());
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
String selectedTileSourceKey = settings.MAP_TILE_SOURCES.get();
int selectedItem = -1;
if (!settings.MAP_ONLINE_DATA.get()) {
selectedItem = 0;
} else {
Entry<String, String> selectedEntry = null;
for (Entry<String, String> entry : entriesMap.entrySet()) {
if (entry.getKey().equals(selectedTileSourceKey)) {
selectedEntry = entry;
break;
}
}
if (selectedEntry != null) {
selectedItem = 0;
entriesMapList.remove(selectedEntry);
entriesMapList.add(0, selectedEntry);
}
}
final String[] items = new String[entriesMapList.size()];
int i = 0;
for (Entry<String, String> entry : entriesMapList) {
items[i++] = entry.getValue();
}
builder.setSingleChoiceItems(items, selectedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Pair<String, String> layer = new Pair<String, String>(
entriesMapList.get(which).getKey(),
entriesMapList.get(which).getValue());
adapter.addItem(layer, activity);
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.shared_string_dismiss, null);
builder.show();
}
};
}
@Override
protected String getItemName(Pair<String, String> item) {
return item.second;
}
}
protected static abstract class SwitchableAction extends QuickAction{
protected static abstract class SwitchableAction<T> extends QuickAction{
private transient EditText title;
@ -1902,15 +2096,6 @@ public class QuickActionFactory {
this.title = title;
}
private String getTitle(List<String> filters) {
if (filters.isEmpty()) return "";
return filters.size() > 1
? filters.get(0) + " +" + (filters.size() - 1)
: filters.get(0);
}
@Override
public void drawUI(ViewGroup parent, final MapActivity activity) {
@ -1933,7 +2118,7 @@ public class QuickActionFactory {
touchHelper.attachToRecyclerView(list);
if (!getParams().isEmpty()){
adapter.addItems(loadMapStyles());
adapter.addItems(loadListFromParams());
}
list.setAdapter(adapter);
@ -1957,7 +2142,7 @@ public class QuickActionFactory {
protected class Adapter extends RecyclerView.Adapter<Adapter.ItemHolder> implements QuickActionItemTouchHelperCallback.OnItemMoveCallback {
private List<String> itemsList = new ArrayList<>();
private List<T> itemsList = new ArrayList<>();
private final QuickActionListFragment.OnStartDragListener onStartDragListener;
public Adapter(QuickActionListFragment.OnStartDragListener onStartDragListener) {
@ -1973,9 +2158,9 @@ public class QuickActionFactory {
@Override
public void onBindViewHolder(final Adapter.ItemHolder holder, final int position) {
final String item = itemsList.get(position);
final T item = itemsList.get(position);
holder.title.setText(item);
holder.title.setText(getItemName(item));
holder.handleView.setOnTouchListener(new View.OnTouchListener() {
@Override
@ -1993,10 +2178,11 @@ public class QuickActionFactory {
public void onClick(View v) {
String oldTitle = getTitle(itemsList);
String defaultName = holder.handleView.getContext().getString(getNameRes());
deleteItem(position);
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(getName(holder.handleView.getContext()))) {
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(defaultName)) {
String newTitle = getTitle(itemsList);
title.setText(newTitle);
@ -2017,34 +2203,35 @@ public class QuickActionFactory {
itemsList.remove(position);
saveMapStyles(itemsList);
saveListToParams(itemsList);
notifyItemRemoved(position);
}
public void addItems(List<String> data) {
public void addItems(List<T> data) {
if (!itemsList.containsAll(data)) {
itemsList.addAll(data);
saveMapStyles(itemsList);
saveListToParams(itemsList);
notifyDataSetChanged();
}
}
public void addItem(String item, Context context) {
public void addItem(T item, Context context) {
if (!itemsList.contains(item)) {
String oldTitle = getTitle(itemsList);
String defaultName = context.getString(getNameRes());
int oldSize = itemsList.size();
itemsList.add(item);
saveMapStyles(itemsList);
saveListToParams(itemsList);
notifyItemRangeInserted(oldSize, itemsList.size() - oldSize);
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(getName(context))) {
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(defaultName)) {
String newTitle = getTitle(itemsList);
title.setText(newTitle);
@ -2063,6 +2250,7 @@ public class QuickActionFactory {
}
String oldTitle = getTitle(itemsList);
String defaultName = recyclerView.getContext().getString(getNameRes());
Collections.swap(itemsList, selectedPosition, targetPosition);
if (selectedPosition - targetPosition < -1) {
@ -2083,7 +2271,7 @@ public class QuickActionFactory {
notifyItemChanged(selectedPosition);
notifyItemChanged(targetPosition);
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(getName(recyclerView.getContext()))) {
if (oldTitle.equals(title.getText().toString()) || title.getText().toString().equals(defaultName)) {
String newTitle = getTitle(itemsList);
title.setText(newTitle);
@ -2094,7 +2282,7 @@ public class QuickActionFactory {
@Override
public void onViewDropped(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
saveMapStyles(itemsList);
saveListToParams(itemsList);
}
public class ItemHolder extends RecyclerView.ViewHolder {
@ -2112,22 +2300,10 @@ public class QuickActionFactory {
}
}
protected void saveMapStyles(List<String> styles) {
getParams().put(getListKey(), TextUtils.join(",", styles));
}
protected List<String> loadMapStyles() {
List<String> styles = new ArrayList<>();
String filtersId = getParams().get(getListKey());
if (filtersId != null && !filtersId.trim().isEmpty()) {
Collections.addAll(styles, filtersId.split(","));
}
return styles;
}
protected abstract String getTitle(List<T> filters);
protected abstract void saveListToParams(List<T> list);
protected abstract List<T> loadListFromParams();
protected abstract String getItemName(T item);
protected abstract @StringRes int getAddBtnText();
protected abstract @StringRes int getDiscrHint();