Search in sources :

Example 1 with GpxAppearanceAdapter

use of net.osmand.plus.track.GpxAppearanceAdapter 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> gpxInfoList, final ContextMenuAdapter contextMenuAdapter, final int themeRes, final boolean nightMode) {
    final OsmandApplication app = (OsmandApplication) activity.getApplication();
    final File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR);
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, themeRes));
    final int layout = R.layout.gpx_track_item;
    final Map<String, String> gpxAppearanceParams = new HashMap<>();
    final DialogGpxDataItemCallback gpxDataItemCallback = new DialogGpxDataItemCallback(app);
    ArrayList<String> modifiableGpxFileNames = new ArrayList<>(Arrays.asList(contextMenuAdapter.getItemNames()));
    final ArrayAdapter<String> alertDialogAdapter = new ArrayAdapter<String>(activity, layout, R.id.title, modifiableGpxFileNames) {

        @Override
        public int getItemViewType(int position) {
            return showCurrentGpx && position == 0 ? 1 : 0;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }

        private GpxDataItem getDataItem(GPXInfo info) {
            return app.getGpxDbHelper().getItem(new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), info.getFileName()), gpxDataItemCallback);
        }

        @Override
        @NonNull
        public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
            // User super class to create the View
            View v = convertView;
            boolean checkLayout = getItemViewType(position) == 0;
            if (v == null) {
                v = View.inflate(new ContextThemeWrapper(activity, themeRes), layout, null);
            }
            final ContextMenuItem item = contextMenuAdapter.getItem(position);
            GPXInfo info = gpxInfoList.get(position);
            boolean currentlyRecordingTrack = showCurrentGpx && position == 0;
            GPXTrackAnalysis analysis = null;
            if (currentlyRecordingTrack) {
                analysis = app.getSavingTrackHelper().getCurrentTrack().getTrackAnalysis(app);
            } else {
                GpxDataItem dataItem = getDataItem(info);
                if (dataItem != null) {
                    analysis = dataItem.getAnalysis();
                }
            }
            updateGpxInfoView(v, item.getTitle(), info, analysis, 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 = 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);
                        }
                    });
                    UiUtilities.setupCompoundButton(ch, nightMode, PROFILE_DEPENDENT);
                } else {
                    final SwitchCompat ch = 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);
                        }
                    });
                    UiUtilities.setupCompoundButton(ch, nightMode, PROFILE_DEPENDENT);
                }
                v.findViewById(R.id.check_item).setVisibility(View.VISIBLE);
            }
            return v;
        }
    };
    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int position) {
        }
    };
    gpxDataItemCallback.setListAdapter(alertDialogAdapter);
    builder.setAdapter(alertDialogAdapter, 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 = View.inflate(new ContextThemeWrapper(activity, themeRes), R.layout.select_gpx_appearance_title, null);
                final CommonPreference<String> prefWidth = app.getSettings().getCustomRenderProperty(CURRENT_TRACK_WIDTH_ATTR);
                final 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(new ContextThemeWrapper(activity, themeRes));
                        popup.setAnchorView(apprTitleView);
                        popup.setContentWidth(AndroidUtils.dpToPx(activity, 200f));
                        popup.setModal(true);
                        popup.setDropDownGravity(Gravity.END | Gravity.TOP);
                        popup.setVerticalOffset(AndroidUtils.dpToPx(activity, -48f));
                        popup.setHorizontalOffset(AndroidUtils.dpToPx(activity, -6f));
                        final GpxAppearanceAdapter gpxApprAdapter = new GpxAppearanceAdapter(new ContextThemeWrapper(activity, themeRes), gpxAppearanceParams.containsKey(CURRENT_TRACK_COLOR_ATTR) ? gpxAppearanceParams.get(CURRENT_TRACK_COLOR_ATTR) : prefColor.get(), GpxAppearanceAdapter.GpxAppearanceAdapterType.TRACK_WIDTH_COLOR, gpxAppearanceParams.containsKey(SHOW_START_FINISH_ATTR) ? "true".equals(gpxAppearanceParams.get(SHOW_START_FINISH_ATTR)) : app.getSettings().SHOW_START_FINISH_ICONS.get(), nightMode);
                        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 (CURRENT_TRACK_WIDTH_ATTR.equals(item.getAttrName())) {
                                        gpxAppearanceParams.put(CURRENT_TRACK_WIDTH_ATTR, item.getValue());
                                    } else if (CURRENT_TRACK_COLOR_ATTR.equals(item.getAttrName())) {
                                        gpxAppearanceParams.put(CURRENT_TRACK_COLOR_ATTR, item.getValue());
                                    } else if (SHOW_START_FINISH_ATTR.equals(item.getAttrName())) {
                                        gpxAppearanceParams.put(SHOW_START_FINISH_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()) {
                        if (SHOW_START_FINISH_ATTR.equals(entry.getKey())) {
                            app.getSettings().SHOW_START_FINISH_ICONS.set("true".equals(entry.getValue()));
                        } else {
                            final CommonPreference<String> pref = app.getSettings().getCustomRenderProperty(entry.getKey());
                            pref.set(entry.getValue());
                        }
                    }
                    if (activity instanceof MapActivity) {
                        ((MapActivity) activity).refreshMapComplete();
                    }
                }
                GPXFile currentGPX = null;
                // clear all previously selected files before adding new one
                OsmandApplication app = (OsmandApplication) activity.getApplication();
                if (app.getSelectedGpxHelper() != null) {
                    app.getSelectedGpxHelper().clearAllGpxFilesToShow(false);
                }
                if (showCurrentGpx && contextMenuAdapter.getItem(0).getSelected()) {
                    currentGPX = app.getSavingTrackHelper().getCurrentGpx();
                }
                List<String> selectedGpxNames = new ArrayList<>();
                for (int i = (showCurrentGpx ? 1 : 0); i < contextMenuAdapter.length(); i++) {
                    if (contextMenuAdapter.getItem(i).getSelected()) {
                        selectedGpxNames.add(gpxInfoList.get(i).getFileName());
                    }
                }
                dialog.dismiss();
                updateSelectedTracksAppearance(app, selectedGpxNames, gpxAppearanceParams);
                loadGPXFileInDifferentThread(activity, callbackWithObject, dir, currentGPX, selectedGpxNames.toArray(new String[0]));
            }
        });
        builder.setNegativeButton(R.string.shared_string_cancel, null);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && gpxInfoList.size() > 1 || !showCurrentGpx && gpxInfoList.size() > 0) {
            builder.setNeutralButton(R.string.gpx_add_track, null);
        }
    }
    final AlertDialog dlg = builder.create();
    dlg.setCanceledOnTouchOutside(true);
    if (gpxInfoList.size() == 0 || showCurrentGpx && gpxInfoList.size() == 1) {
        final View footerView = activity.getLayoutInflater().inflate(R.layout.no_gpx_files_list_footer, null);
        TextView descTextView = 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, alertDialogAdapter, contextMenuAdapter, gpxInfoList);
                }
            });
        }
        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 = contextMenuAdapter.getItem(position);
                item.setSelected(!item.getSelected());
                alertDialogAdapter.notifyDataSetInvalidated();
                if (position == 0 && showCurrentGpx && item.getSelected()) {
                    OsmandMonitoringPlugin monitoringPlugin = OsmandPlugin.getActivePlugin(OsmandMonitoringPlugin.class);
                    if (monitoringPlugin == null) {
                        AlertDialog.Builder confirm = new AlertDialog.Builder(new ContextThemeWrapper(activity, themeRes));
                        confirm.setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Bundle params = new Bundle();
                                params.putBoolean(PluginsFragment.OPEN_PLUGINS, true);
                                MapActivity.launchMapActivityMoveToTop(activity, null, null, params);
                            }
                        });
                        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);
                    }
                }
            } else {
                dlg.dismiss();
                if (showCurrentGpx && position == 0) {
                    callbackWithObject.processResult(null);
                } else {
                    String fileName = gpxInfoList.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, alertDialogAdapter, contextMenuAdapter, gpxInfoList);
                    }
                });
            }
        }
    });
    dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            gpxDataItemCallback.setUpdateEnable(false);
        }
    });
    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;
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) OsmandApplication(net.osmand.plus.OsmandApplication) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) OsmandMonitoringPlugin(net.osmand.plus.plugins.monitoring.OsmandMonitoringPlugin) Button(android.widget.Button) CompoundButton(android.widget.CompoundButton) NonNull(androidx.annotation.NonNull) GpxDataItem(net.osmand.plus.track.helpers.GPXDatabase.GpxDataItem) ArrayList(java.util.ArrayList) List(java.util.List) TextView(android.widget.TextView) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) ContextMenuItem(net.osmand.plus.ContextMenuItem) OnItemClickListener(android.widget.AdapterView.OnItemClickListener) GPXTrackAnalysis(net.osmand.GPXUtilities.GPXTrackAnalysis) SpannableString(android.text.SpannableString) CheckBox(android.widget.CheckBox) OnClickListener(android.content.DialogInterface.OnClickListener) AdapterView(android.widget.AdapterView) GPXFile(net.osmand.GPXUtilities.GPXFile) SelectedGpxFile(net.osmand.plus.track.helpers.GpxSelectionHelper.SelectedGpxFile) File(java.io.File) SwitchCompat(androidx.appcompat.widget.SwitchCompat) DialogInterface(android.content.DialogInterface) AppearanceListItem(net.osmand.plus.track.GpxAppearanceAdapter.AppearanceListItem) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) ListPopupWindow(androidx.appcompat.widget.ListPopupWindow) GpxAppearanceAdapter(net.osmand.plus.track.GpxAppearanceAdapter) MapActivity(net.osmand.plus.activities.MapActivity) CommonPreference(net.osmand.plus.settings.backend.preferences.CommonPreference) ViewGroup(android.view.ViewGroup) Bundle(android.os.Bundle) RenderingRuleProperty(net.osmand.render.RenderingRuleProperty) ImageView(android.widget.ImageView) GpxMarkerView(net.osmand.plus.track.GpxMarkerView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) RenderingRulesStorage(net.osmand.render.RenderingRulesStorage) ActivityNotFoundException(android.content.ActivityNotFoundException) ContextThemeWrapper(android.view.ContextThemeWrapper) SelectedGpxFile(net.osmand.plus.track.helpers.GpxSelectionHelper.SelectedGpxFile) StyleSpan(android.text.style.StyleSpan) GPXFile(net.osmand.GPXUtilities.GPXFile) ArrayAdapter(android.widget.ArrayAdapter) CompoundButton(android.widget.CompoundButton)

Aggregations

ActivityNotFoundException (android.content.ActivityNotFoundException)1 DialogInterface (android.content.DialogInterface)1 OnClickListener (android.content.DialogInterface.OnClickListener)1 Bundle (android.os.Bundle)1 SpannableString (android.text.SpannableString)1 StyleSpan (android.text.style.StyleSpan)1 ContextThemeWrapper (android.view.ContextThemeWrapper)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 AdapterView (android.widget.AdapterView)1 OnItemClickListener (android.widget.AdapterView.OnItemClickListener)1 ArrayAdapter (android.widget.ArrayAdapter)1 Button (android.widget.Button)1 CheckBox (android.widget.CheckBox)1 CompoundButton (android.widget.CompoundButton)1 OnCheckedChangeListener (android.widget.CompoundButton.OnCheckedChangeListener)1 ImageView (android.widget.ImageView)1 TextView (android.widget.TextView)1 NonNull (androidx.annotation.NonNull)1 AlertDialog (androidx.appcompat.app.AlertDialog)1