This commit is contained in:
veliymolfar 2020-04-15 14:05:32 +03:00
parent 286ad614c6
commit b85b562cb6
4 changed files with 92 additions and 83 deletions

View file

@ -44,15 +44,11 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.APP_PROFILES_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.APP_PROFILES_ID;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.CONFIGURE_MAP_ITEM_ID_SCHEME;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.DRAWER_ITEM_ID_SCHEME;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_ACTIONS;
public class ContextMenuAdapter { public class ContextMenuAdapter {
private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class); private static final Log LOG = PlatformUtil.getLog(ContextMenuAdapter.class);
@ -65,7 +61,6 @@ public class ContextMenuAdapter {
@LayoutRes @LayoutRes
private int DEFAULT_LAYOUT_ID = R.layout.list_menu_item_native; private int DEFAULT_LAYOUT_ID = R.layout.list_menu_item_native;
List<ContextMenuItem> items = new ArrayList<>(); List<ContextMenuItem> items = new ArrayList<>();
List<ContextMenuItem> hiddenItems = new ArrayList<>();
private boolean profileDependent = false; private boolean profileDependent = false;
private boolean nightMode; private boolean nightMode;
private ConfigureMapMenu.OnClickListener changeAppModeListener = null; private ConfigureMapMenu.OnClickListener changeAppModeListener = null;
@ -94,11 +89,7 @@ public class ContextMenuAdapter {
item.setHidden(isItemHidden(id)); item.setHidden(isItemHidden(id));
item.setOrder(getItemOrder(id, item.getOrder())); item.setOrder(getItemOrder(id, item.getOrder()));
} }
if (item.isHidden()) { items.add(item.getPos(), item);
hiddenItems.add(item);
} else {
items.add(item.getPos(), item);
}
sortItemsByOrder(); sortItemsByOrder();
} catch (IndexOutOfBoundsException ex) { } catch (IndexOutOfBoundsException ex) {
items.add(item); items.add(item);
@ -164,7 +155,7 @@ public class ContextMenuAdapter {
if (contextMenuItemsPreference == null) { if (contextMenuItemsPreference == null) {
return false; return false;
} }
List<String> hiddenIds = contextMenuItemsPreference.getHiddenIds(); List<String> hiddenIds = contextMenuItemsPreference.get().getHiddenIds();
if (!Algorithms.isEmpty(hiddenIds)) { if (!Algorithms.isEmpty(hiddenIds)) {
return hiddenIds.contains(id); return hiddenIds.contains(id);
} }
@ -176,7 +167,7 @@ public class ContextMenuAdapter {
if (contextMenuItemsPreference == null) { if (contextMenuItemsPreference == null) {
return defaultOrder; return defaultOrder;
} }
List<String> orderIds = contextMenuItemsPreference.getOrderIds(); List<String> orderIds = contextMenuItemsPreference.get().getOrderIds();
if (!Algorithms.isEmpty(orderIds)) { if (!Algorithms.isEmpty(orderIds)) {
int order = orderIds.indexOf(id); int order = orderIds.indexOf(id);
if (order != -1) { if (order != -1) {
@ -190,12 +181,14 @@ public class ContextMenuAdapter {
final int layoutId = DEFAULT_LAYOUT_ID; final int layoutId = DEFAULT_LAYOUT_ID;
final OsmandApplication app = ((OsmandApplication) activity.getApplication()); final OsmandApplication app = ((OsmandApplication) activity.getApplication());
final OsmAndAppCustomization customization = app.getAppCustomization(); final OsmAndAppCustomization customization = app.getAppCustomization();
for (Iterator<ContextMenuItem> iterator = items.iterator(); iterator.hasNext(); ) { List<ContextMenuItem> itemsToRemove = new ArrayList<>();
String id = iterator.next().getId(); for (ContextMenuItem item : items) {
if (!TextUtils.isEmpty(id) && !customization.isFeatureEnabled(id)) { String id = item.getId();
iterator.remove(); if (item.isHidden() || !TextUtils.isEmpty(id) && !customization.isFeatureEnabled(id)) {
itemsToRemove.add(item);
} }
} }
items.removeAll(itemsToRemove);
return new ContextMenuArrayAdapter(activity, layoutId, R.id.title, return new ContextMenuArrayAdapter(activity, layoutId, R.id.title,
items.toArray(new ContextMenuItem[items.size()]), app, lightTheme, changeAppModeListener); items.toArray(new ContextMenuItem[items.size()]), app, lightTheme, changeAppModeListener);
} }
@ -603,24 +596,14 @@ public class ContextMenuAdapter {
return items; return items;
} }
public List<ContextMenuItem> getHiddenItems() {
return hiddenItems;
}
private String getIdScheme() { private String getIdScheme() {
String idScheme = ""; String idScheme = "";
for (ContextMenuItem item : items) { for (ContextMenuItem item : items) {
String id = item.getId(); String id = item.getId();
if (id != null) { if (id != null) {
if (id.startsWith(DRAWER_ITEM_ID_SCHEME)) { ContextMenuItemsPreference pref = app.getSettings().getContextMenuItemsPreference(id);
idScheme = DRAWER_ITEM_ID_SCHEME; if (pref != null) {
break; return pref.getIdScheme();
} else if (id.startsWith(CONFIGURE_MAP_ITEM_ID_SCHEME)) {
idScheme = CONFIGURE_MAP_ITEM_ID_SCHEME;
break;
} else if (id.startsWith(MAP_CONTEXT_MENU_ACTIONS)) {
idScheme = MAP_CONTEXT_MENU_ACTIONS;
break;
} }
} }
} }

View file

@ -64,6 +64,7 @@ import org.json.JSONObject;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.ArrayList; import java.util.ArrayList;
@ -451,6 +452,10 @@ public class OsmandSettings {
} }
return false; return false;
} }
} else if (preference instanceof ContextMenuItemsPreference) {
if (value instanceof ContextMenuItemsSettings) {
((ContextMenuItemsPreference) preference).setModeValue(mode, (ContextMenuItemsSettings) value);
}
} }
} }
return false; return false;
@ -1131,57 +1136,87 @@ public class OsmandSettings {
} }
} }
public class ContextMenuItemsPreference extends StringPreference { public class ContextMenuItemsPreference extends CommonPreference<ContextMenuItemsSettings> {
public static final String HIDDEN = "hidden"; @NonNull
public static final String ORDER = "order";
private List<String> hiddenIds;
private List<String> orderIds;
private Object cachedPreference;
private String idScheme; private String idScheme;
private ContextMenuItemsPreference(String id, String idScheme) { private ContextMenuItemsPreference(String id, @NonNull String idScheme) {
super(id, ""); super(id, new ContextMenuItemsSettings());
this.idScheme = idScheme; this.idScheme = idScheme;
} }
private void readValues() { @Override
hiddenIds = new ArrayList<>(); protected ContextMenuItemsSettings getValue(Object prefs, ContextMenuItemsSettings defaultValue) {
orderIds = new ArrayList<>(); String s = settingsAPI.getString(prefs, getId(), "");
cachedPreference = getPreferences(); return readValue(s);
String jsonString = get(); }
@Override
protected boolean setValue(Object prefs, ContextMenuItemsSettings val) {
return settingsAPI.edit(prefs).putString(getId(), val.writeToJsonString(idScheme)).commit();
}
@Override
public ContextMenuItemsSettings parseString(String s) {
return readValue(s);
}
private ContextMenuItemsSettings readValue(String s) {
ContextMenuItemsSettings value = new ContextMenuItemsSettings();
value.readFromJsonString(s, idScheme);
return value;
}
@NonNull
public String getIdScheme() {
return idScheme;
}
}
public static class ContextMenuItemsSettings implements Serializable {
public static final String HIDDEN = "hidden";
public static final String ORDER = "order";
private List<String> hiddenIds = new ArrayList<>();
private List<String> orderIds = new ArrayList<>();
public ContextMenuItemsSettings() {
}
public ContextMenuItemsSettings(@NonNull List<String> hiddenIds, @NonNull List<String> orderIds) {
this.hiddenIds = hiddenIds;
this.orderIds = orderIds;
}
public void readFromJsonString(String jsonString, @NonNull String idScheme) {
if (Algorithms.isEmpty(jsonString)) { if (Algorithms.isEmpty(jsonString)) {
return; return;
} }
try { try {
JSONObject json = new JSONObject(jsonString); JSONObject json = new JSONObject(jsonString);
JSONObject items = json.optJSONObject(getId()); hiddenIds = readIdsList(json.optJSONArray(HIDDEN), idScheme);
populateIdsList(items.optJSONArray(HIDDEN), hiddenIds); orderIds = readIdsList(json.optJSONArray(ORDER), idScheme);
populateIdsList(items.optJSONArray(ORDER), orderIds);
} catch (JSONException e) { } catch (JSONException e) {
LOG.error("Error converting to json string: " + e); LOG.error("Error converting to json string: " + e);
} }
} }
private void populateIdsList(JSONArray jsonArray, @NonNull List<String> list) { private List<String> readIdsList(JSONArray jsonArray, @NonNull String idScheme) {
List<String> list = new ArrayList<>();
if (jsonArray != null) { if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {
String id = jsonArray.optString(i); String id = jsonArray.optString(i);
list.add(idScheme + id); list.add(idScheme + id);
} }
} }
return list;
} }
public String convertToJsonString(List<String> hidden, List<String> order, String id) { public String writeToJsonString(@NonNull String idScheme) {
try { try {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
JSONObject items = new JSONObject(); json.put(HIDDEN, getJsonArray(hiddenIds, idScheme));
JSONArray hiddenItems = new JSONArray(); json.put(ORDER, getJsonArray(orderIds, idScheme));
JSONArray orderItems = new JSONArray();
addIdsToJsonArray(hiddenItems, hidden);
addIdsToJsonArray(orderItems, order);
items.put(HIDDEN, hiddenItems);
items.put(ORDER, orderItems);
json.put(id, items);
return json.toString(); return json.toString();
} catch (JSONException e) { } catch (JSONException e) {
LOG.error("Error converting to json string: " + e); LOG.error("Error converting to json string: " + e);
@ -1189,26 +1224,22 @@ public class OsmandSettings {
return ""; return "";
} }
private void addIdsToJsonArray(@NonNull JSONArray jsonArray, List<String> ids) { private JSONArray getJsonArray(List<String> ids, @NonNull String idScheme) {
JSONArray jsonArray = new JSONArray();
if (ids != null && !ids.isEmpty()) { if (ids != null && !ids.isEmpty()) {
for (String id : ids) { for (String id : ids) {
jsonArray.put(id.replace(idScheme, "")); jsonArray.put(id.replace(idScheme, ""));
} }
} }
return jsonArray;
} }
public List<String> getHiddenIds() { public List<String> getHiddenIds() {
if (cachedPreference != getPreferences() || hiddenIds == null) { return Collections.unmodifiableList(hiddenIds);
readValues();
}
return hiddenIds;
} }
public List<String> getOrderIds() { public List<String> getOrderIds() {
if (cachedPreference != getPreferences() || orderIds == null) { return Collections.unmodifiableList(orderIds);
readValues();
}
return orderIds;
} }
} }
@ -3545,11 +3576,11 @@ public class OsmandSettings {
public final ContextMenuItemsPreference CONTEXT_MENU_ACTIONS_ITEMS = public final ContextMenuItemsPreference CONTEXT_MENU_ACTIONS_ITEMS =
(ContextMenuItemsPreference) new ContextMenuItemsPreference("configure_map_items", MAP_CONTEXT_MENU_ACTIONS).makeProfile().cache(); (ContextMenuItemsPreference) new ContextMenuItemsPreference("configure_map_items", MAP_CONTEXT_MENU_ACTIONS).makeProfile().cache();
public final List<ContextMenuItemsPreference> contextMenuItemsPreferences = Arrays.asList(DRAWER_ITEMS, CONFIGURE_MAP_ITEMS, CONTEXT_MENU_ACTIONS_ITEMS); public final List<ContextMenuItemsPreference> CONTEXT_MENU_ITEMS_PREFERENCES = Arrays.asList(DRAWER_ITEMS, CONFIGURE_MAP_ITEMS, CONTEXT_MENU_ACTIONS_ITEMS);
@Nullable @Nullable
public ContextMenuItemsPreference getContextMenuItemsPreference(@NonNull String id) { public ContextMenuItemsPreference getContextMenuItemsPreference(@NonNull String id) {
for (ContextMenuItemsPreference preference : contextMenuItemsPreferences) { for (ContextMenuItemsPreference preference : CONTEXT_MENU_ITEMS_PREFERENCES) {
if (id.startsWith(preference.idScheme)) { if (id.startsWith(preference.idScheme)) {
return preference; return preference;
} }

View file

@ -43,9 +43,6 @@ import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem;
import net.osmand.plus.settings.RearrangeMenuItemsAdapter.MenuItemsAdapterListener; import net.osmand.plus.settings.RearrangeMenuItemsAdapter.MenuItemsAdapterListener;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -54,8 +51,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MORE_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.MAP_CONTEXT_MENU_MORE_ID;
import static net.osmand.plus.OsmandSettings.ContextMenuItemsPreference.HIDDEN;
import static net.osmand.plus.OsmandSettings.ContextMenuItemsPreference.ORDER;
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.BUTTON; import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.BUTTON;
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.DESCRIPTION; import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.DESCRIPTION;
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.DIVIDER; import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.DIVIDER;
@ -140,9 +135,9 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
hiddenMenuItems = savedInstanceState.getStringArrayList(HIDDEN_ITEMS_KEY); hiddenMenuItems = savedInstanceState.getStringArrayList(HIDDEN_ITEMS_KEY);
menuItemsOrder = (HashMap<String, Integer>) savedInstanceState.getSerializable(ITEMS_ORDER_KEY); menuItemsOrder = (HashMap<String, Integer>) savedInstanceState.getSerializable(ITEMS_ORDER_KEY);
} else { } else {
hiddenMenuItems = getSettingForScreen(app, screenType).getHiddenIds(); hiddenMenuItems = new ArrayList<>(getSettingForScreen(app, screenType).getModeValue(appMode).getHiddenIds());
menuItemsOrder = new HashMap<>(); menuItemsOrder = new HashMap<>();
List<String> orderIds = getSettingForScreen(app, screenType).getOrderIds(); List<String> orderIds = getSettingForScreen(app, screenType).getModeValue(appMode).getOrderIds();
for (int i = 0; i < orderIds.size(); i++) { for (int i = 0; i < orderIds.size(); i++) {
menuItemsOrder.put(orderIds.get(i), i); menuItemsOrder.put(orderIds.get(i), i);
} }
@ -261,19 +256,19 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
@Override @Override
public void onClick(View v) { public void onClick(View v) {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems(); List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
defItems.addAll(contextMenuAdapter.getHiddenItems());
sortByCustomOrder(defItems, menuItemsOrder);
List<String> ids = new ArrayList<>(); List<String> ids = new ArrayList<>();
for (ContextMenuItem item : defItems) { if (!menuItemsOrder.isEmpty()) {
ids.add(item.getId()); sortByCustomOrder(defItems, menuItemsOrder);
for (ContextMenuItem item : defItems) {
ids.add(item.getId());
}
} }
FragmentManager fm = getFragmentManager(); FragmentManager fm = getFragmentManager();
OsmandSettings.ContextMenuItemsPreference preference = getSettingForScreen(app, screenType); OsmandSettings.ContextMenuItemsSettings prefToSave = new OsmandSettings.ContextMenuItemsSettings(hiddenMenuItems, ids);
String stringToSave = preference.convertToJsonString(hiddenMenuItems, ids, preference.getId());
if (fm != null) { if (fm != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fm, ChangeGeneralProfilesPrefBottomSheet.showInstance(fm,
getSettingForScreen(app, screenType).getId(), getSettingForScreen(app, screenType).getId(),
stringToSave, prefToSave,
getTargetFragment(), getTargetFragment(),
false, false,
appMode, appMode,
@ -347,6 +342,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
menuItemsOrder.clear(); menuItemsOrder.clear();
wasReset = true; wasReset = true;
isChanged = true; isChanged = true;
getSettingForScreen(app, screenType).resetModeToDefault(appMode);
instantiateContextMenuAdapter(); instantiateContextMenuAdapter();
rearrangeAdapter.updateItems(getAdapterItems()); rearrangeAdapter.updateItems(getAdapterItems());
} }
@ -436,7 +432,6 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
public List<AdapterItem> getItemsForRearrangeAdapter(@Nullable List<String> hiddenItemsIds, @Nullable HashMap<String, Integer> itemsOrderIds, boolean hidden) { public List<AdapterItem> getItemsForRearrangeAdapter(@Nullable List<String> hiddenItemsIds, @Nullable HashMap<String, Integer> itemsOrderIds, boolean hidden) {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems(); List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
defItems.addAll(contextMenuAdapter.getHiddenItems());
if (itemsOrderIds == null || itemsOrderIds.isEmpty()) { if (itemsOrderIds == null || itemsOrderIds.isEmpty()) {
initDefaultOrders(defItems); initDefaultOrders(defItems);
} else { } else {

View file

@ -264,8 +264,8 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment {
contextMenuAdapter = menu.getActionsContextMenuAdapter(true); contextMenuAdapter = menu.getActionsContextMenuAdapter(true);
break; break;
} }
int hiddenCount = contextMenuAdapter.getHiddenItems().size(); int hiddenCount = ConfigureMenuItemsFragment.getSettingForScreen(app, type).getModeValue(appMode).getHiddenIds().size();
int allCount = contextMenuAdapter.getDefaultItems().size() + contextMenuAdapter.getHiddenItems().size(); int allCount = contextMenuAdapter.getDefaultItems().size();
String amount = getString(R.string.n_items_of_z, String.valueOf(allCount - hiddenCount), String.valueOf(allCount)); String amount = getString(R.string.n_items_of_z, String.valueOf(allCount - hiddenCount), String.valueOf(allCount));
return getString(R.string.ltr_or_rtl_combine_via_colon, getString(R.string.shared_string_items), amount); return getString(R.string.ltr_or_rtl_combine_via_colon, getString(R.string.shared_string_items), amount);
} }