use of net.osmand.plus.ContextMenuAdapter in project Osmand by osmandapp.
the class AvailableGPXFragment method onCreateOptionsMenu.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
MenuItem mi = createMenuItem(menu, SEARCH_ID, R.string.search_poi_filter, R.drawable.ic_action_search_dark, R.drawable.ic_action_search_dark, MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
SearchView searchView = new SearchView(getActivity());
FavoritesActivity.updateSearchView(getActivity(), searchView);
MenuItemCompat.setActionView(mi, searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
allGpxAdapter.getFilter().filter(query);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
allGpxAdapter.getFilter().filter(newText);
return true;
}
});
MenuItemCompat.setOnActionExpandListener(mi, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Needed to hide intermediate progress bar after closing action mode
new Handler().postDelayed(new Runnable() {
public void run() {
hideProgressBar();
}
}, 100);
return true;
}
});
if (AndroidUiHelper.isOrientationPortrait(getActivity())) {
menu = ((FavoritesActivity) getActivity()).getClearToolbar(true).getMenu();
} else {
((FavoritesActivity) getActivity()).getClearToolbar(false);
}
((FavoritesActivity) getActivity()).updateListViewFooter(footerView);
// TODO Rewrite without ContextMenuAdapter
optionsMenuAdapter = new ContextMenuAdapter();
ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() {
@Override
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, final int itemId, int pos, boolean isChecked, int[] viewCoordinates) {
if (itemId == R.string.local_index_mi_reload) {
reloadTracks();
} else if (itemId == R.string.shared_string_show_on_map) {
openShowOnMapMode();
} else if (itemId == R.string.shared_string_delete) {
openSelectionMode(itemId, R.drawable.ic_action_delete_dark, R.drawable.ic_action_delete_dark, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doAction(itemId);
}
});
} else if (itemId == R.string.gpx_add_track) {
addTrack();
}
return true;
}
};
optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.gpx_add_track, getActivity()).setIcon(R.drawable.ic_action_plus).setListener(listener).createItem());
optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.shared_string_show_on_map, getActivity()).setIcon(R.drawable.ic_show_on_map).setListener(listener).createItem());
optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.shared_string_delete, getActivity()).setIcon(R.drawable.ic_action_delete_dark).setListener(listener).createItem());
optionsMenuAdapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.local_index_mi_reload, getActivity()).setIcon(R.drawable.ic_action_refresh_dark).setListener(listener).createItem());
OsmandPlugin.onOptionsMenuActivity(getActivity(), this, optionsMenuAdapter);
for (int j = 0; j < optionsMenuAdapter.length(); j++) {
final MenuItem item;
ContextMenuItem contextMenuItem = optionsMenuAdapter.getItem(j);
item = menu.add(0, contextMenuItem.getTitleId(), j + 1, contextMenuItem.getTitle());
MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
if (AndroidUiHelper.isOrientationPortrait(getActivity())) {
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
onOptionsItemSelected(item);
return true;
}
});
}
if (contextMenuItem.getIcon() != -1) {
item.setIcon(contextMenuItem.getIcon());
}
}
}
use of net.osmand.plus.ContextMenuAdapter in project Osmand by osmandapp.
the class OsmEditingPlugin method registerMapContextMenuActions.
@Override
public void registerMapContextMenuActions(final MapActivity mapActivity, final double latitude, final double longitude, ContextMenuAdapter adapter, final Object selectedObj) {
ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() {
@Override
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, int resId, int pos, boolean isChecked, int[] viewCoordinates) {
if (resId == R.string.context_menu_item_create_poi) {
// getPoiActions(mapActivity).showCreateDialog(latitude, longitude);
EditPoiDialogFragment editPoiDialogFragment = EditPoiDialogFragment.createAddPoiInstance(latitude, longitude, mapActivity.getMyApplication());
editPoiDialogFragment.show(mapActivity.getSupportFragmentManager(), EditPoiDialogFragment.TAG);
} else if (resId == R.string.context_menu_item_open_note) {
openOsmNote(mapActivity, latitude, longitude);
} else if (resId == R.string.context_menu_item_modify_note) {
modifyOsmNote(mapActivity, (OsmNotesPoint) selectedObj);
} else if (resId == R.string.poi_context_menu_modify) {
EditPoiDialogFragment.showEditInstance((Amenity) selectedObj, mapActivity);
} else if (resId == R.string.poi_context_menu_modify_osm_change) {
final Node entity = ((OpenstreetmapPoint) selectedObj).getEntity();
EditPoiDialogFragment.createInstance(entity, false).show(mapActivity.getSupportFragmentManager(), EditPoiDialogFragment.TAG);
}
return true;
}
};
boolean isEditable = false;
if (selectedObj instanceof Amenity) {
Amenity amenity = (Amenity) selectedObj;
final PoiType poiType = amenity.getType().getPoiTypeByKeyName(amenity.getSubType());
isEditable = !amenity.getType().isWiki() && poiType != null && !poiType.isNotEditableOsm();
}
if (isEditable) {
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.poi_context_menu_modify, mapActivity).setIcon(R.drawable.ic_action_edit_dark).setOrder(MODIFY_POI_ITEM_ORDER).setListener(listener).createItem());
} else if (selectedObj instanceof OpenstreetmapPoint && ((OpenstreetmapPoint) selectedObj).getAction() != Action.DELETE) {
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.poi_context_menu_modify_osm_change, mapActivity).setIcon(R.drawable.ic_action_edit_dark).setOrder(MODIFY_OSM_CHANGE_ITEM_ORDER).setListener(listener).createItem());
} else {
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.context_menu_item_create_poi, mapActivity).setIcon(R.drawable.ic_action_plus_dark).setOrder(CREATE_POI_ITEM_ORDER).setListener(listener).createItem());
}
if (selectedObj instanceof OsmNotesPoint) {
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.context_menu_item_modify_note, mapActivity).setIcon(R.drawable.ic_action_edit_dark).setOrder(MODIFY_OSM_NOTE_ITEM_ORDER).setListener(listener).createItem());
} else {
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(R.string.context_menu_item_open_note, mapActivity).setIcon(R.drawable.ic_action_bug_dark).setOrder(OPEN_OSM_NOTE_ITEM_ORDER).setListener(listener).createItem());
}
}
use of net.osmand.plus.ContextMenuAdapter in project Osmand by osmandapp.
the class OsmNotesMenu method createLayersItems.
private static void createLayersItems(final ContextMenuAdapter adapter, final MapActivity mapActivity) {
final OsmandSettings settings = mapActivity.getMyApplication().getSettings();
final OsmEditingPlugin plugin = OsmandPlugin.getPlugin(OsmEditingPlugin.class);
if (plugin == null) {
return;
}
final int osmNotesStringId = R.string.layer_osm_bugs;
final int showZoomLevelStringId = R.string.show_from_zoom_level;
final int showClosedNotesStringId = R.string.show_closed_notes;
final OsmandPreference<Boolean> showOsmBugsPref = settings.SHOW_OSM_BUGS;
final CommonPreference<Boolean> showClosedOsmBugsPref = settings.SHOW_CLOSED_OSM_BUGS;
final CommonPreference<Integer> showOsmBugsZoomPref = settings.SHOW_OSM_BUGS_MIN_ZOOM;
final String[] zoomStrings = getZoomStrings(mapActivity);
ContextMenuAdapter.OnRowItemClick l = new ContextMenuAdapter.OnRowItemClick() {
@Override
public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> adapter, int itemId, final int position, boolean isChecked, int[] viewCoordinates) {
if (itemId == osmNotesStringId) {
showOsmBugsPref.set(isChecked);
plugin.updateLayers(mapActivity.getMapView(), mapActivity);
mapActivity.refreshMap();
mapActivity.getDashboard().refreshContent(true);
} else if (itemId == showZoomLevelStringId) {
int checked = Arrays.asList(zoomIntValues).indexOf(showOsmBugsZoomPref.get());
new AlertDialog.Builder(mapActivity).setTitle(R.string.show_from_zoom_level).setSingleChoiceItems(zoomStrings, checked, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showOsmBugsZoomPref.set(zoomIntValues[which]);
ContextMenuItem item = adapter.getItem(position);
if (item != null) {
item.setDescription(zoomStrings[which]);
adapter.notifyDataSetChanged();
}
mapActivity.refreshMap();
dialog.dismiss();
}
}).setNegativeButton(R.string.shared_string_dismiss, null).show();
} else if (itemId == showClosedNotesStringId) {
showClosedOsmBugsPref.set(isChecked);
mapActivity.refreshMap();
}
return false;
}
};
boolean showOsmBugs = showOsmBugsPref.get();
boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls();
int toggleIconColorId;
if (showOsmBugs) {
toggleIconColorId = nightMode ? R.color.color_dialog_buttons_dark : R.color.color_dialog_buttons_light;
} else {
toggleIconColorId = nightMode ? 0 : R.color.icon_color;
}
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(osmNotesStringId, mapActivity).setDescription(mapActivity.getString(R.string.switch_osm_notes_visibility_desc)).setIcon(R.drawable.ic_action_bug_dark).setColor(toggleIconColorId).setListener(l).setSelected(showOsmBugs).createItem());
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(showZoomLevelStringId, mapActivity).setDescription(zoomStrings[Arrays.asList(zoomIntValues).indexOf(showOsmBugsZoomPref.get())]).setLayout(R.layout.list_item_single_line_descrition_narrow).setIcon(R.drawable.ic_action_map_magnifier).setListener(l).setClickable(showOsmBugs).createItem());
adapter.addItem(new ContextMenuItem.ItemBuilder().setTitleId(showClosedNotesStringId, mapActivity).setIcon(R.drawable.ic_action_note_dark).setListener(l).setSelected(showClosedOsmBugsPref.get()).setClickable(showOsmBugs).hideDivider(true).createItem());
adapter.addItem(new ContextMenuItem.ItemBuilder().setLayout(R.layout.card_bottom_divider).setClickable(false).createItem());
}
use of net.osmand.plus.ContextMenuAdapter in project Osmand by osmandapp.
the class AvailableGPXFragment method moveGpx.
private void moveGpx(final GpxInfo info) {
final ContextMenuAdapter menuAdapter = new ContextMenuAdapter();
ContextMenuItem.ItemBuilder itemBuilder = new ContextMenuItem.ItemBuilder();
final List<File> dirs = new ArrayList<>();
collectDirs(app.getAppPath(IndexConstants.GPX_INDEX_DIR), dirs, info.file.getParentFile());
if (!info.file.getParentFile().equals(app.getAppPath(IndexConstants.GPX_INDEX_DIR))) {
dirs.add(0, app.getAppPath(IndexConstants.GPX_INDEX_DIR));
}
String gpxDir = app.getAppPath(IndexConstants.GPX_INDEX_DIR).getPath();
int i = 0;
for (File dir : dirs) {
String dirName = dir.getPath();
if (dirName.startsWith(gpxDir)) {
if (dirName.length() == gpxDir.length()) {
dirName = dir.getName();
} else {
dirName = dirName.substring(gpxDir.length() + 1);
}
}
menuAdapter.addItem(itemBuilder.setTitle(Algorithms.capitalizeFirstLetter(dirName)).setIcon(R.drawable.ic_action_folder_stroke).setTag(i).createItem());
i++;
}
menuAdapter.addItem(itemBuilder.setTitleId(R.string.add_new_folder, app).setIcon(R.drawable.map_zoom_in).setTag(-1).createItem());
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final ArrayAdapter<ContextMenuItem> listAdapter = menuAdapter.createListAdapter(getActivity(), app.getSettings().isLightContent());
builder.setTitle(R.string.select_gpx_folder);
builder.setAdapter(listAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContextMenuItem item = menuAdapter.getItem(which);
int index = item.getTag();
if (index == -1) {
Activity a = getActivity();
AlertDialog.Builder b = new AlertDialog.Builder(a);
b.setTitle(R.string.add_new_folder);
final EditText editText = new EditText(a);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Editable text = editText.getText();
if (text.length() >= 1) {
if (ILLEGAL_PATH_NAME_CHARACTERS.matcher(text).find()) {
editText.setError(app.getString(R.string.file_name_containes_illegal_char));
}
}
}
});
int leftPadding = AndroidUtils.dpToPx(a, 24f);
int topPadding = AndroidUtils.dpToPx(a, 4f);
b.setView(editText, leftPadding, topPadding, leftPadding, topPadding);
// Behaviour will be overwritten later;
b.setPositiveButton(R.string.shared_string_ok, null);
b.setNegativeButton(R.string.shared_string_cancel, null);
final AlertDialog alertDialog = b.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newName = editText.getText().toString();
if (ILLEGAL_PATH_NAME_CHARACTERS.matcher(newName).find()) {
Toast.makeText(app, R.string.file_name_containes_illegal_char, Toast.LENGTH_LONG).show();
return;
}
File destFolder = new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), newName);
if (destFolder.exists()) {
Toast.makeText(app, R.string.file_with_name_already_exists, Toast.LENGTH_LONG).show();
return;
} else if (destFolder.mkdirs()) {
File dest = new File(destFolder, info.fileName);
if (info.file.renameTo(dest)) {
app.getGpxDatabase().rename(info.file, dest);
asyncLoader = new LoadGpxTask();
asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity());
} else {
Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
}
alertDialog.dismiss();
}
});
}
});
alertDialog.show();
} else {
File dir = dirs.get(index);
File dest = new File(dir, info.file.getName());
if (dest.exists()) {
Toast.makeText(app, R.string.file_with_name_already_exists, Toast.LENGTH_LONG).show();
} else {
if (info.file.renameTo(dest)) {
app.getGpxDatabase().rename(info.file, dest);
asyncLoader = new LoadGpxTask();
asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity());
} else {
Toast.makeText(app, R.string.file_can_not_be_moved, Toast.LENGTH_LONG).show();
}
}
}
}
});
builder.setNegativeButton(R.string.shared_string_cancel, null);
builder.create().show();
}
use of net.osmand.plus.ContextMenuAdapter in project Osmand by osmandapp.
the class OsmNotesMenu method createListAdapter.
public static ContextMenuAdapter createListAdapter(final MapActivity mapActivity) {
ContextMenuAdapter adapter = new ContextMenuAdapter();
adapter.setDefaultLayoutId(R.layout.list_item_icon_and_menu);
createLayersItems(adapter, mapActivity);
return adapter;
}
Aggregations