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

View file

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

View file

@ -43,9 +43,6 @@ import net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItem;
import net.osmand.plus.settings.RearrangeMenuItemsAdapter.MenuItemsAdapterListener;
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.Collections;
@ -54,8 +51,6 @@ import java.util.HashMap;
import java.util.List;
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.DESCRIPTION;
import static net.osmand.plus.settings.RearrangeMenuItemsAdapter.AdapterItemType.DIVIDER;
@ -140,9 +135,9 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
hiddenMenuItems = savedInstanceState.getStringArrayList(HIDDEN_ITEMS_KEY);
menuItemsOrder = (HashMap<String, Integer>) savedInstanceState.getSerializable(ITEMS_ORDER_KEY);
} else {
hiddenMenuItems = getSettingForScreen(app, screenType).getHiddenIds();
hiddenMenuItems = new ArrayList<>(getSettingForScreen(app, screenType).getModeValue(appMode).getHiddenIds());
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++) {
menuItemsOrder.put(orderIds.get(i), i);
}
@ -261,19 +256,19 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
@Override
public void onClick(View v) {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
defItems.addAll(contextMenuAdapter.getHiddenItems());
sortByCustomOrder(defItems, menuItemsOrder);
List<String> ids = new ArrayList<>();
for (ContextMenuItem item : defItems) {
ids.add(item.getId());
if (!menuItemsOrder.isEmpty()) {
sortByCustomOrder(defItems, menuItemsOrder);
for (ContextMenuItem item : defItems) {
ids.add(item.getId());
}
}
FragmentManager fm = getFragmentManager();
OsmandSettings.ContextMenuItemsPreference preference = getSettingForScreen(app, screenType);
String stringToSave = preference.convertToJsonString(hiddenMenuItems, ids, preference.getId());
OsmandSettings.ContextMenuItemsSettings prefToSave = new OsmandSettings.ContextMenuItemsSettings(hiddenMenuItems, ids);
if (fm != null) {
ChangeGeneralProfilesPrefBottomSheet.showInstance(fm,
getSettingForScreen(app, screenType).getId(),
stringToSave,
prefToSave,
getTargetFragment(),
false,
appMode,
@ -347,6 +342,7 @@ public class ConfigureMenuItemsFragment extends BaseOsmAndFragment
menuItemsOrder.clear();
wasReset = true;
isChanged = true;
getSettingForScreen(app, screenType).resetModeToDefault(appMode);
instantiateContextMenuAdapter();
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) {
List<ContextMenuItem> defItems = contextMenuAdapter.getDefaultItems();
defItems.addAll(contextMenuAdapter.getHiddenItems());
if (itemsOrderIds == null || itemsOrderIds.isEmpty()) {
initDefaultOrders(defItems);
} else {

View file

@ -264,8 +264,8 @@ public class ConfigureMenuRootFragment extends BaseOsmAndFragment {
contextMenuAdapter = menu.getActionsContextMenuAdapter(true);
break;
}
int hiddenCount = contextMenuAdapter.getHiddenItems().size();
int allCount = contextMenuAdapter.getDefaultItems().size() + contextMenuAdapter.getHiddenItems().size();
int hiddenCount = ConfigureMenuItemsFragment.getSettingForScreen(app, type).getModeValue(appMode).getHiddenIds().size();
int allCount = contextMenuAdapter.getDefaultItems().size();
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);
}