use of net.osmand.plus.dialogs.ConfigureMapMenu.AppearanceListItem in project Osmand by osmandapp.
the class GpxUiHelper method createDialog.
private static AlertDialog createDialog(final Activity activity, final boolean showCurrentGpx, final boolean multipleChoice, final boolean showAppearanceSetting, final CallbackWithObject<GPXFile[]> callbackWithObject, final List<GPXInfo> list, final ContextMenuAdapter adapter) {
final OsmandApplication app = (OsmandApplication) activity.getApplication();
final DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(activity);
final File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR);
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final int layout = R.layout.gpx_track_item;
final Map<String, String> gpxAppearanceParams = new HashMap<>();
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(activity, layout, R.id.title, adapter.getItemNames()) {
List<GpxDataItem> dataItems = null;
@Override
public int getItemViewType(int position) {
return showCurrentGpx && position == 0 ? 1 : 0;
}
@Override
public int getViewTypeCount() {
return 2;
}
private GpxDataItem getDataItem(GPXInfo info) {
if (dataItems != null) {
for (GpxDataItem item : dataItems) {
if (item.getFile().getAbsolutePath().endsWith(info.fileName)) {
return item;
}
}
}
return null;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// User super class to create the View
View v = convertView;
boolean checkLayout = getItemViewType(position) == 0;
if (v == null) {
v = activity.getLayoutInflater().inflate(layout, null);
}
if (dataItems == null) {
dataItems = app.getGpxDatabase().getItems();
}
final ContextMenuItem item = adapter.getItem(position);
GPXInfo info = list.get(position);
updateGpxInfoView(v, item, info, getDataItem(info), showCurrentGpx && position == 0, app);
if (item.getSelected() == null) {
v.findViewById(R.id.check_item).setVisibility(View.GONE);
v.findViewById(R.id.check_local_index).setVisibility(View.GONE);
} else {
if (checkLayout) {
final CheckBox ch = ((CheckBox) v.findViewById(R.id.check_local_index));
ch.setVisibility(View.VISIBLE);
v.findViewById(R.id.toggle_item).setVisibility(View.GONE);
ch.setOnCheckedChangeListener(null);
ch.setChecked(item.getSelected());
ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.setSelected(isChecked);
}
});
} else {
final SwitchCompat ch = ((SwitchCompat) v.findViewById(R.id.toggle_item));
ch.setVisibility(View.VISIBLE);
v.findViewById(R.id.toggle_checkbox_item).setVisibility(View.GONE);
ch.setOnCheckedChangeListener(null);
ch.setChecked(item.getSelected());
ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
item.setSelected(isChecked);
}
});
}
v.findViewById(R.id.check_item).setVisibility(View.VISIBLE);
}
return v;
}
};
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
}
};
builder.setAdapter(listAdapter, onClickListener);
if (multipleChoice) {
if (showAppearanceSetting) {
final RenderingRuleProperty trackWidthProp;
final RenderingRuleProperty trackColorProp;
final RenderingRulesStorage renderer = app.getRendererRegistry().getCurrentSelectedRenderer();
if (renderer != null) {
trackWidthProp = renderer.PROPS.getCustomRule(CURRENT_TRACK_WIDTH_ATTR);
trackColorProp = renderer.PROPS.getCustomRule(CURRENT_TRACK_COLOR_ATTR);
} else {
trackWidthProp = null;
trackColorProp = null;
}
if (trackWidthProp == null || trackColorProp == null) {
builder.setTitle(R.string.show_gpx);
} else {
final View apprTitleView = activity.getLayoutInflater().inflate(R.layout.select_gpx_appearance_title, null);
final OsmandSettings.CommonPreference<String> prefWidth = app.getSettings().getCustomRenderProperty(CURRENT_TRACK_WIDTH_ATTR);
final OsmandSettings.CommonPreference<String> prefColor = app.getSettings().getCustomRenderProperty(CURRENT_TRACK_COLOR_ATTR);
updateAppearanceTitle(activity, app, trackWidthProp, renderer, apprTitleView, prefWidth.get(), prefColor.get());
apprTitleView.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ListPopupWindow popup = new ListPopupWindow(activity);
popup.setAnchorView(apprTitleView);
popup.setContentWidth(AndroidUtils.dpToPx(activity, 200f));
popup.setModal(true);
popup.setDropDownGravity(Gravity.RIGHT | Gravity.TOP);
popup.setVerticalOffset(AndroidUtils.dpToPx(activity, -48f));
popup.setHorizontalOffset(AndroidUtils.dpToPx(activity, -6f));
final GpxAppearanceAdapter gpxApprAdapter = new GpxAppearanceAdapter(activity, gpxAppearanceParams.containsKey(CURRENT_TRACK_COLOR_ATTR) ? gpxAppearanceParams.get(CURRENT_TRACK_COLOR_ATTR) : prefColor.get(), GpxAppearanceAdapter.GpxAppearanceAdapterType.TRACK_WIDTH_COLOR);
popup.setAdapter(gpxApprAdapter);
popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AppearanceListItem item = gpxApprAdapter.getItem(position);
if (item != null) {
if (item.getAttrName() == CURRENT_TRACK_WIDTH_ATTR) {
gpxAppearanceParams.put(CURRENT_TRACK_WIDTH_ATTR, item.getValue());
} else if (item.getAttrName() == CURRENT_TRACK_COLOR_ATTR) {
gpxAppearanceParams.put(CURRENT_TRACK_COLOR_ATTR, item.getValue());
}
}
popup.dismiss();
updateAppearanceTitle(activity, app, trackWidthProp, renderer, apprTitleView, gpxAppearanceParams.containsKey(CURRENT_TRACK_WIDTH_ATTR) ? gpxAppearanceParams.get(CURRENT_TRACK_WIDTH_ATTR) : prefWidth.get(), gpxAppearanceParams.containsKey(CURRENT_TRACK_COLOR_ATTR) ? gpxAppearanceParams.get(CURRENT_TRACK_COLOR_ATTR) : prefColor.get());
}
});
popup.show();
}
});
builder.setCustomTitle(apprTitleView);
}
} else {
builder.setTitle(R.string.show_gpx);
}
builder.setPositiveButton(R.string.shared_string_ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (gpxAppearanceParams.size() > 0) {
for (Map.Entry<String, String> entry : gpxAppearanceParams.entrySet()) {
final OsmandSettings.CommonPreference<String> pref = app.getSettings().getCustomRenderProperty(entry.getKey());
pref.set(entry.getValue());
}
if (activity instanceof MapActivity) {
ConfigureMapMenu.refreshMapComplete((MapActivity) activity);
}
}
GPXFile currentGPX = null;
// clear all previously selected files before adding new one
OsmandApplication app = (OsmandApplication) activity.getApplication();
if (app != null && app.getSelectedGpxHelper() != null) {
app.getSelectedGpxHelper().clearAllGpxFileToShow();
}
if (app != null && showCurrentGpx && adapter.getItem(0).getSelected()) {
currentGPX = app.getSavingTrackHelper().getCurrentGpx();
}
List<String> s = new ArrayList<>();
for (int i = (showCurrentGpx ? 1 : 0); i < adapter.length(); i++) {
if (adapter.getItem(i).getSelected()) {
s.add(list.get(i).getFileName());
}
}
dialog.dismiss();
loadGPXFileInDifferentThread(activity, callbackWithObject, dir, currentGPX, s.toArray(new String[s.size()]));
}
});
builder.setNegativeButton(R.string.shared_string_cancel, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && list.size() > 1 || !showCurrentGpx && list.size() > 0) {
builder.setNeutralButton(R.string.gpx_add_track, null);
}
}
final AlertDialog dlg = builder.create();
dlg.setCanceledOnTouchOutside(true);
if (list.size() == 0 || showCurrentGpx && list.size() == 1) {
final View footerView = activity.getLayoutInflater().inflate(R.layout.no_gpx_files_list_footer, null);
TextView descTextView = (TextView) footerView.findViewById(R.id.descFolder);
String descPrefix = app.getString(R.string.gpx_no_tracks_title_folder);
SpannableString spannableDesc = new SpannableString(descPrefix + ": " + dir.getAbsolutePath());
spannableDesc.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), descPrefix.length() + 1, spannableDesc.length(), 0);
descTextView.setText(spannableDesc);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
footerView.findViewById(R.id.button).setVisibility(View.GONE);
} else {
footerView.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addTrack(activity, dlg);
}
});
}
dlg.getListView().addFooterView(footerView, null, false);
}
dlg.getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (multipleChoice) {
ContextMenuItem item = adapter.getItem(position);
item.setSelected(!item.getSelected());
listAdapter.notifyDataSetInvalidated();
if (position == 0 && showCurrentGpx && item.getSelected()) {
OsmandMonitoringPlugin monitoringPlugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class);
if (monitoringPlugin == null) {
AlertDialog.Builder confirm = new AlertDialog.Builder(activity);
confirm.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(activity, PluginActivity.class);
intent.putExtra(PluginActivity.EXTRA_PLUGIN_ID, OsmandMonitoringPlugin.ID);
activity.startActivity(intent);
}
});
confirm.setNegativeButton(R.string.shared_string_cancel, null);
confirm.setMessage(activity.getString(R.string.enable_plugin_monitoring_services));
confirm.show();
} else if (!app.getSettings().SAVE_GLOBAL_TRACK_TO_GPX.get()) {
monitoringPlugin.controlDialog(activity, false);
}
}
} else {
dlg.dismiss();
if (showCurrentGpx && position == 0) {
callbackWithObject.processResult(null);
} else {
String fileName = list.get(position).getFileName();
SelectedGpxFile selectedGpxFile = app.getSelectedGpxHelper().getSelectedFileByName(fileName);
if (selectedGpxFile != null) {
callbackWithObject.processResult(new GPXFile[] { selectedGpxFile.getGpxFile() });
} else {
loadGPXFileInDifferentThread(activity, callbackWithObject, dir, null, fileName);
}
}
}
}
});
dlg.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button addTrackButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEUTRAL);
if (addTrackButton != null) {
addTrackButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addTrack(activity, dlg);
}
});
}
}
});
dlg.show();
try {
dlg.getListView().setFastScrollEnabled(true);
} catch (Exception e) {
// java.lang.ClassCastException: com.android.internal.widget.RoundCornerListAdapter
// Unknown reason but on some devices fail
}
return dlg;
}
use of net.osmand.plus.dialogs.ConfigureMapMenu.AppearanceListItem in project Osmand by osmandapp.
the class TrackSegmentFragment method updateHeader.
private void updateHeader() {
imageView = (ImageView) headerView.findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
GpxDataItem gpxDataItem = getGpxDataItem();
GPXFile gpx = getGpx();
WptPt pointToShow = gpx != null ? gpx.findPointToShow() : null;
if (pointToShow != null) {
LatLon location = new LatLon(pointToShow.getLatitude(), pointToShow.getLongitude());
final OsmandSettings settings = app.getSettings();
String trackName = "";
if (gpx.showCurrentTrack) {
trackName = getString(R.string.shared_string_currently_recording_track);
} else if (gpxDataItem != null) {
trackName = gpxDataItem.getFile().getName();
} else {
trackName = gpx.path;
}
settings.setMapLocationToShow(location.getLatitude(), location.getLongitude(), settings.getLastKnownMapZoom(), new PointDescription(PointDescription.POINT_TYPE_WPT, trackName), false, getRect());
MapActivity.launchMapActivityMoveToTop(getActivity());
}
}
});
final View splitColorView = headerView.findViewById(R.id.split_color_view);
final View divider = headerView.findViewById(R.id.divider);
final View splitIntervalView = headerView.findViewById(R.id.split_interval_view);
final View colorView = headerView.findViewById(R.id.color_view);
vis = (SwitchCompat) headerView.findViewById(R.id.showOnMapToggle);
final ProgressBar progressBar = (ProgressBar) headerView.findViewById(R.id.mapLoadProgress);
final boolean selected = getGpx() != null && ((getGpx().showCurrentTrack && app.getSelectedGpxHelper().getSelectedCurrentRecordingTrack() != null) || (getGpx().path != null && app.getSelectedGpxHelper().getSelectedFileByPath(getGpx().path) != null));
vis.setChecked(selected);
vis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
selectedSplitInterval = 0;
}
SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(getGpx(), vis.isChecked(), false);
final List<GpxDisplayGroup> groups = getDisplayGroups();
if (groups.size() > 0) {
updateSplit(groups, vis.isChecked() ? sf : null);
if (getGpxDataItem() != null) {
updateSplitInDatabase();
}
}
updateSplitIntervalView(splitIntervalView);
updateColorView(colorView);
}
});
updateColorView(colorView);
colorView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
colorListPopupWindow = new ListPopupWindow(getActivity());
colorListPopupWindow.setAnchorView(colorView);
colorListPopupWindow.setContentWidth(AndroidUtils.dpToPx(app, 200f));
colorListPopupWindow.setModal(true);
colorListPopupWindow.setDropDownGravity(Gravity.RIGHT | Gravity.TOP);
colorListPopupWindow.setVerticalOffset(AndroidUtils.dpToPx(app, -48f));
colorListPopupWindow.setHorizontalOffset(AndroidUtils.dpToPx(app, -6f));
final GpxAppearanceAdapter gpxApprAdapter = new GpxAppearanceAdapter(getActivity(), getGpx().getColor(0), GpxAppearanceAdapterType.TRACK_COLOR);
colorListPopupWindow.setAdapter(gpxApprAdapter);
colorListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AppearanceListItem item = gpxApprAdapter.getItem(position);
if (item != null) {
if (item.getAttrName() == CURRENT_TRACK_COLOR_ATTR) {
int clr = item.getColor();
if (vis.isChecked()) {
SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(getGpx(), vis.isChecked(), false);
if (clr != 0 && sf.getModifiableGpxFile() != null) {
sf.getModifiableGpxFile().setColor(clr);
if (getGpxDataItem() != null) {
app.getGpxDatabase().updateColor(getGpxDataItem(), clr);
}
}
} else if (getGpxDataItem() != null) {
app.getGpxDatabase().updateColor(getGpxDataItem(), clr);
}
if (getGpx().showCurrentTrack) {
app.getSettings().CURRENT_TRACK_COLOR.set(clr);
}
refreshTrackBitmap();
}
}
colorListPopupWindow.dismiss();
updateColorView(colorView);
}
});
colorListPopupWindow.show();
}
});
boolean hasPath = getGpx() != null && (getGpx().tracks.size() > 0 || getGpx().routes.size() > 0);
if (rotatedTileBox == null || mapBitmap == null || mapTrackBitmap == null) {
QuadRect rect = getRect();
if (rect.left != 0 && rect.top != 0) {
progressBar.setVisibility(View.VISIBLE);
double clat = rect.bottom / 2 + rect.top / 2;
double clon = rect.left / 2 + rect.right / 2;
WindowManager mgr = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
mgr.getDefaultDisplay().getMetrics(dm);
RotatedTileBoxBuilder boxBuilder = new RotatedTileBoxBuilder().setLocation(clat, clon).setZoom(15).density(dm.density).setPixelDimensions(dm.widthPixels, AndroidUtils.dpToPx(app, 152f), 0.5f, 0.5f);
rotatedTileBox = boxBuilder.build();
while (rotatedTileBox.getZoom() < 17 && rotatedTileBox.containsLatLon(rect.top, rect.left) && rotatedTileBox.containsLatLon(rect.bottom, rect.right)) {
rotatedTileBox.setZoom(rotatedTileBox.getZoom() + 1);
}
while (rotatedTileBox.getZoom() >= 7 && (!rotatedTileBox.containsLatLon(rect.top, rect.left) || !rotatedTileBox.containsLatLon(rect.bottom, rect.right))) {
rotatedTileBox.setZoom(rotatedTileBox.getZoom() - 1);
}
final DrawSettings drawSettings = new DrawSettings(!app.getSettings().isLightContent(), true);
final ResourceManager resourceManager = app.getResourceManager();
final MapRenderRepositories renderer = resourceManager.getRenderer();
if (resourceManager.updateRenderedMapNeeded(rotatedTileBox, drawSettings)) {
resourceManager.updateRendererMap(rotatedTileBox, new AsyncLoadingThread.OnMapLoadedListener() {
@Override
public void onMapLoaded(boolean interrupted) {
app.runInUIThread(new Runnable() {
@Override
public void run() {
if (updateEnable) {
mapBitmap = renderer.getBitmap();
if (mapBitmap != null) {
progressBar.setVisibility(View.GONE);
refreshTrackBitmap();
}
}
}
});
}
});
}
imageView.setVisibility(View.VISIBLE);
} else {
imageView.setVisibility(View.GONE);
}
} else {
refreshTrackBitmap();
}
if (hasPath) {
if (getGpx() != null && !getGpx().showCurrentTrack && adapter.getCount() > 0) {
prepareSplitIntervalAdapterData();
setupSplitIntervalView(splitIntervalView);
updateSplitIntervalView(splitIntervalView);
splitIntervalView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
splitListPopupWindow = new ListPopupWindow(getActivity());
splitListPopupWindow.setAnchorView(splitIntervalView);
splitListPopupWindow.setContentWidth(AndroidUtils.dpToPx(app, 200f));
splitListPopupWindow.setModal(true);
splitListPopupWindow.setDropDownGravity(Gravity.RIGHT | Gravity.TOP);
splitListPopupWindow.setVerticalOffset(AndroidUtils.dpToPx(app, -48f));
splitListPopupWindow.setHorizontalOffset(AndroidUtils.dpToPx(app, -6f));
splitListPopupWindow.setAdapter(new ArrayAdapter<>(getTrackActivity(), R.layout.popup_list_text_item, options));
splitListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedSplitInterval = position;
SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(getGpx(), vis.isChecked(), false);
final List<GpxDisplayGroup> groups = getDisplayGroups();
if (groups.size() > 0) {
updateSplit(groups, vis.isChecked() ? sf : null);
if (getGpxDataItem() != null) {
updateSplitInDatabase();
}
}
splitListPopupWindow.dismiss();
updateSplitIntervalView(splitIntervalView);
}
});
splitListPopupWindow.show();
}
});
splitIntervalView.setVisibility(View.VISIBLE);
} else {
splitIntervalView.setVisibility(View.GONE);
}
splitColorView.setVisibility(View.VISIBLE);
divider.setVisibility(View.VISIBLE);
} else {
splitColorView.setVisibility(View.GONE);
divider.setVisibility(View.GONE);
}
}
Aggregations