From e72d419e9aa32eade54aabcc73c3ef69615aaf18 Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Thu, 28 Sep 2017 15:35:36 +0300 Subject: [PATCH] Add support for different coordinate formats --- .../fragment_coordinate_input_dialog.xml | 2 - ...r_coordinate_input_bottom_sheet_dialog.xml | 365 ++++++++---------- OsmAnd/res/values/strings.xml | 2 +- ...rdinateInputBottomSheetDialogFragment.java | 88 ++++- .../CoordinateInputDialogFragment.java | 122 ++++-- 5 files changed, 332 insertions(+), 247 deletions(-) diff --git a/OsmAnd/res/layout/fragment_coordinate_input_dialog.xml b/OsmAnd/res/layout/fragment_coordinate_input_dialog.xml index e5c17996b0..f993663744 100644 --- a/OsmAnd/res/layout/fragment_coordinate_input_dialog.xml +++ b/OsmAnd/res/layout/fragment_coordinate_input_dialog.xml @@ -77,7 +77,6 @@ @@ -97,7 +96,6 @@ diff --git a/OsmAnd/res/layout/fragment_marker_coordinate_input_bottom_sheet_dialog.xml b/OsmAnd/res/layout/fragment_marker_coordinate_input_bottom_sheet_dialog.xml index e44479208b..f294958fc2 100644 --- a/OsmAnd/res/layout/fragment_marker_coordinate_input_bottom_sheet_dialog.xml +++ b/OsmAnd/res/layout/fragment_marker_coordinate_input_bottom_sheet_dialog.xml @@ -42,230 +42,197 @@ android:textColor="?android:textColorSecondary" android:textSize="@dimen/default_desc_text_size"/> - + android:layout_height="@dimen/bottom_sheet_list_item_height" + android:background="?attr/selectableItemBackground" + android:gravity="center_vertical" + android:minHeight="@dimen/bottom_sheet_list_item_height" + android:paddingEnd="@dimen/bottom_sheet_content_padding" + android:paddingLeft="@dimen/bottom_sheet_content_padding" + android:paddingRight="@dimen/bottom_sheet_content_padding" + android:paddingStart="@dimen/bottom_sheet_content_padding"> - + - + - + + - - - - - + android:layout_height="@dimen/bottom_sheet_list_item_height" + android:background="?attr/selectableItemBackground" + android:gravity="center_vertical" + android:minHeight="@dimen/bottom_sheet_list_item_height" + android:paddingEnd="@dimen/bottom_sheet_content_padding" + android:paddingLeft="@dimen/bottom_sheet_content_padding" + android:paddingRight="@dimen/bottom_sheet_content_padding" + android:paddingStart="@dimen/bottom_sheet_content_padding"> - + - + - + + - - - - - - + android:layout_height="@dimen/bottom_sheet_list_item_height" + android:background="?attr/selectableItemBackground" + android:gravity="center_vertical" + android:minHeight="@dimen/bottom_sheet_list_item_height" + android:paddingEnd="@dimen/bottom_sheet_content_padding" + android:paddingLeft="@dimen/bottom_sheet_content_padding" + android:paddingRight="@dimen/bottom_sheet_content_padding" + android:paddingStart="@dimen/bottom_sheet_content_padding"> - + - + - + + - - - - - - + android:layout_height="@dimen/bottom_sheet_list_item_height" + android:background="?attr/selectableItemBackground" + android:gravity="center_vertical" + android:minHeight="@dimen/bottom_sheet_list_item_height" + android:paddingEnd="@dimen/bottom_sheet_content_padding" + android:paddingLeft="@dimen/bottom_sheet_content_padding" + android:paddingRight="@dimen/bottom_sheet_content_padding" + android:paddingStart="@dimen/bottom_sheet_content_padding"> - + - + - + + - - - - - - + android:layout_height="@dimen/bottom_sheet_list_item_height" + android:background="?attr/selectableItemBackground" + android:gravity="center_vertical" + android:minHeight="@dimen/bottom_sheet_list_item_height" + android:paddingEnd="@dimen/bottom_sheet_content_padding" + android:paddingLeft="@dimen/bottom_sheet_content_padding" + android:paddingRight="@dimen/bottom_sheet_content_padding" + android:paddingStart="@dimen/bottom_sheet_content_padding"> - + - + - - - - - - + + diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 82b48f1fb2..241d599842 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -9,7 +9,7 @@ 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy --> - Choose oordinate format before start. YOu can always change it by tapping Options. + Choose coordinate format before start. You can always change it by tapping Options. Fast Coordinates input Use location Add your location as first point to plan perfect route. diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputBottomSheetDialogFragment.java index 069e3dbc37..08bbba07f8 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputBottomSheetDialogFragment.java @@ -5,6 +5,7 @@ import android.os.Build; import android.os.Bundle; import android.support.annotation.DrawableRes; import android.support.annotation.Nullable; +import android.support.v4.content.ContextCompat; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.View; @@ -22,8 +23,6 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.BottomSheetDialogFragment; import net.osmand.plus.helpers.AndroidUiHelper; -import org.w3c.dom.Text; - public class CoordinateInputBottomSheetDialogFragment extends BottomSheetDialogFragment { public final static String TAG = "CoordinateInputBottomSheetDialogFragment"; @@ -31,15 +30,20 @@ public class CoordinateInputBottomSheetDialogFragment extends BottomSheetDialogF private boolean portrait; private View mainView; private boolean night; + private int coordinateFormat = -1; + private CoordinateInputFormatChangeListener listener; - private Format currentFormat = Format.DEGREES; + public void setListener(CoordinateInputFormatChangeListener listener) { + this.listener = listener; + } - private enum Format { - DEGREES, - MINUTES, - SECONDS, - UTM, - OLC + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Bundle args = getArguments(); + if (args != null) { + coordinateFormat = getArguments().getInt(CoordinateInputDialogFragment.COORDINATE_FORMAT, -1); + } } @Nullable @@ -61,49 +65,89 @@ public class CoordinateInputBottomSheetDialogFragment extends BottomSheetDialogF ImageView degreesIcon = (ImageView) mainView.findViewById(R.id.degrees_icon); TextView degreesText = (TextView) mainView.findViewById(R.id.degrees_text); - if (currentFormat == Format.DEGREES) { + if (coordinateFormat == PointDescription.FORMAT_DEGREES) { degreesIcon.setImageDrawable(getIcon(R.drawable.ic_action_coordinates_latitude, R.color.dashboard_blue)); + degreesText.setTextColor(ContextCompat.getColor(mapActivity, R.color.dashboard_blue)); } else { degreesIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_coordinates_latitude)); } + degreesText.setText(PointDescription.formatToHumanString(getContext(), PointDescription.FORMAT_DEGREES)); ImageView minutesIcon = (ImageView) mainView.findViewById(R.id.minutes_icon); TextView minutesText = (TextView) mainView.findViewById(R.id.minutes_text); - if (currentFormat == Format.MINUTES) { + if (coordinateFormat == PointDescription.FORMAT_MINUTES) { minutesIcon.setImageDrawable(getIcon(R.drawable.ic_action_coordinates_latitude, R.color.dashboard_blue)); + minutesText.setTextColor(ContextCompat.getColor(mapActivity, R.color.dashboard_blue)); } else { minutesIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_coordinates_latitude)); } + minutesText.setText(PointDescription.formatToHumanString(getContext(), PointDescription.FORMAT_MINUTES)); ImageView secondsIcon = (ImageView) mainView.findViewById(R.id.seconds_icon); TextView secondsText = (TextView) mainView.findViewById(R.id.seconds_text); - if (currentFormat == Format.SECONDS) { + if (coordinateFormat == PointDescription.FORMAT_SECONDS) { secondsIcon.setImageDrawable(getIcon(R.drawable.ic_action_coordinates_latitude, R.color.dashboard_blue)); + secondsText.setTextColor(ContextCompat.getColor(mapActivity, R.color.dashboard_blue)); } else { secondsIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_coordinates_latitude)); } + secondsText.setText(PointDescription.formatToHumanString(getContext(), PointDescription.FORMAT_SECONDS)); ImageView utmIcon = (ImageView) mainView.findViewById(R.id.utm_icon); TextView utmText = (TextView) mainView.findViewById(R.id.utm_text); - if (currentFormat == Format.UTM) { + if (coordinateFormat == PointDescription.UTM_FORMAT) { utmIcon.setImageDrawable(getIcon(R.drawable.ic_action_coordinates_latitude, R.color.dashboard_blue)); + utmText.setTextColor(ContextCompat.getColor(mapActivity, R.color.dashboard_blue)); } else { utmIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_coordinates_latitude)); } + utmText.setText(PointDescription.formatToHumanString(getContext(), PointDescription.UTM_FORMAT)); ImageView olcIcon = (ImageView) mainView.findViewById(R.id.olc_icon); TextView olcText = (TextView) mainView.findViewById(R.id.olc_text); - if (currentFormat == Format.OLC) { + if (coordinateFormat == PointDescription.OLC_FORMAT) { olcIcon.setImageDrawable(getIcon(R.drawable.ic_action_coordinates_latitude, R.color.dashboard_blue)); + olcText.setTextColor(ContextCompat.getColor(mapActivity, R.color.dashboard_blue)); } else { olcIcon.setImageDrawable(getContentIcon(R.drawable.ic_action_coordinates_latitude)); } + olcText.setText(PointDescription.formatToHumanString(getContext(), PointDescription.OLC_FORMAT)); - ((TextView) mainView.findViewById(R.id.degrees_text)).setText(PointDescription.formatToHumanString(getContext(), PointDescription.FORMAT_DEGREES)); - ((TextView) mainView.findViewById(R.id.minutes_text)).setText(PointDescription.formatToHumanString(getContext(), PointDescription.FORMAT_MINUTES)); - ((TextView) mainView.findViewById(R.id.seconds_text)).setText(PointDescription.formatToHumanString(getContext(), PointDescription.FORMAT_SECONDS)); - ((TextView) mainView.findViewById(R.id.utm_text)).setText(PointDescription.formatToHumanString(getContext(), PointDescription.UTM_FORMAT)); - ((TextView) mainView.findViewById(R.id.olc_text)).setText(PointDescription.formatToHumanString(getContext(), PointDescription.OLC_FORMAT)); + View.OnClickListener formatChangeListener = new View.OnClickListener() { + @Override + public void onClick(View view) { + int format; + switch (view.getId()) { + case R.id.degrees_row: + format = PointDescription.FORMAT_DEGREES; + break; + case R.id.minutes_row: + format = PointDescription.FORMAT_MINUTES; + break; + case R.id.seconds_row: + format = PointDescription.FORMAT_SECONDS; + break; + case R.id.utm_row: + format = PointDescription.UTM_FORMAT; + break; + case R.id.olc_row: + format = PointDescription.OLC_FORMAT; + break; + default: + throw new IllegalArgumentException("Unsupported format"); + } + if (listener != null) { + listener.onCoordinateFormatChanged(format); + } + dismiss(); + } + }; + + mainView.findViewById(R.id.degrees_row).setOnClickListener(formatChangeListener); + mainView.findViewById(R.id.minutes_row).setOnClickListener(formatChangeListener); + mainView.findViewById(R.id.seconds_row).setOnClickListener(formatChangeListener); + mainView.findViewById(R.id.utm_row).setOnClickListener(formatChangeListener); + mainView.findViewById(R.id.olc_row).setOnClickListener(formatChangeListener); mainView.findViewById(R.id.cancel_row).setOnClickListener(new View.OnClickListener() { @Override @@ -119,7 +163,7 @@ public class CoordinateInputBottomSheetDialogFragment extends BottomSheetDialogF mainView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { - final View scrollView = mainView.findViewById(R.id.marker_show_direction_scroll_view); + final View scrollView = mainView.findViewById(R.id.marker_coordinate_input_scroll_view); int scrollViewHeight = scrollView.getHeight(); int dividerHeight = AndroidUtils.dpToPx(getContext(), 1); int cancelButtonHeight = getContext().getResources().getDimensionPixelSize(R.dimen.bottom_sheet_cancel_button_height); @@ -167,4 +211,8 @@ public class CoordinateInputBottomSheetDialogFragment extends BottomSheetDialogF protected Drawable getContentIcon(@DrawableRes int id) { return getIcon(id, night ? R.color.ctx_menu_info_text_dark : R.color.on_map_icon_color); } + + interface CoordinateInputFormatChangeListener { + void onCoordinateFormatChanged(int format); + } } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java index 188677f5a3..af036556c0 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java @@ -2,33 +2,29 @@ package net.osmand.plus.mapmarkers; import android.content.Context; import android.os.Bundle; -import android.support.annotation.LayoutRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; import android.support.v7.widget.Toolbar; import android.text.Editable; +import android.text.InputFilter; import android.text.InputType; import android.text.TextWatcher; -import android.view.Gravity; import android.view.LayoutInflater; -import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; -import android.view.WindowManager; -import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.GridView; -import android.widget.TextView; +import net.osmand.data.PointDescription; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandTextFieldBoxes; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.helpers.FontCache; import net.osmand.plus.widgets.TextViewEx; import java.util.ArrayList; @@ -38,16 +34,23 @@ import studio.carbonylgroup.textfieldboxes.ExtendedEditText; public class CoordinateInputDialogFragment extends DialogFragment { + public static final String TAG = "CoordinateInputDialogFragment"; + + public static final String COORDINATE_FORMAT = "coordinate_format"; + private static final int DELETE_BUTTON_POSITION = 9; private static final int CLEAR_BUTTON_POSITION = 11; - - public static final String TAG = "CoordinateInputDialogFragment"; + private static final int DEGREES_MAX_LENGTH = 8; + private static final int MINUTES_MAX_LENGTH = 10; + private static final int SECONDS_MAX_LENGTH = 13; private boolean lightTheme; private EditText focusedEditText; private boolean useOsmandKeyboard = true; - private List textFieldBoxes = new ArrayList<>(); - private List extendedEditTexts = new ArrayList<>(); + private List textFieldBoxes; + private ExtendedEditText nameEditText; + private List extendedLatLonEditTexts; + private int coordinateFormat = -1; @Override public void onCreate(Bundle savedInstanceState) { @@ -57,8 +60,11 @@ public class CoordinateInputDialogFragment extends DialogFragment { int themeId = lightTheme ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme; setStyle(STYLE_NO_FRAME, themeId); - CoordinateInputBottomSheetDialogFragment fragment = new CoordinateInputBottomSheetDialogFragment(); - fragment.show(getMapActivity().getSupportFragmentManager(), CoordinateInputBottomSheetDialogFragment.TAG); + if (coordinateFormat == -1) { + CoordinateInputBottomSheetDialogFragment fragment = new CoordinateInputBottomSheetDialogFragment(); + fragment.setListener(createCoordinateInputFormatChangeListener()); + fragment.show(getMapActivity().getSupportFragmentManager(), CoordinateInputBottomSheetDialogFragment.TAG); + } } @Nullable @@ -67,6 +73,13 @@ public class CoordinateInputDialogFragment extends DialogFragment { final View mainView = inflater.inflate(R.layout.fragment_coordinate_input_dialog, container); final MapActivity mapActivity = getMapActivity(); + if (coordinateFormat == -1) { + Fragment coordinateInputBottomSheetDialogFragment = mapActivity.getSupportFragmentManager().findFragmentByTag(CoordinateInputBottomSheetDialogFragment.TAG); + if (coordinateInputBottomSheetDialogFragment != null) { + ((CoordinateInputBottomSheetDialogFragment) coordinateInputBottomSheetDialogFragment).setListener(createCoordinateInputFormatChangeListener()); + } + } + Toolbar toolbar = (Toolbar) mainView.findViewById(R.id.coordinate_input_toolbar); toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.ic_arrow_back)); @@ -80,10 +93,16 @@ public class CoordinateInputDialogFragment extends DialogFragment { optionsButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - + CoordinateInputBottomSheetDialogFragment fragment = new CoordinateInputBottomSheetDialogFragment(); + Bundle args = new Bundle(); + args.putInt(COORDINATE_FORMAT, coordinateFormat); + fragment.setArguments(args); + fragment.setListener(createCoordinateInputFormatChangeListener()); + fragment.show(getMapActivity().getSupportFragmentManager(), CoordinateInputBottomSheetDialogFragment.TAG); } }); + textFieldBoxes = new ArrayList<>(); final OsmandTextFieldBoxes latitudeBox = (OsmandTextFieldBoxes) mainView.findViewById(R.id.latitude_box); textFieldBoxes.add(latitudeBox); final OsmandTextFieldBoxes longitudeBox = (OsmandTextFieldBoxes) mainView.findViewById(R.id.longitude_box); @@ -91,12 +110,12 @@ public class CoordinateInputDialogFragment extends DialogFragment { final OsmandTextFieldBoxes nameBox = (OsmandTextFieldBoxes) mainView.findViewById(R.id.name_box); textFieldBoxes.add(nameBox); + extendedLatLonEditTexts = new ArrayList<>(); final ExtendedEditText latitudeEditText = (ExtendedEditText) mainView.findViewById(R.id.latitude_edit_text); - extendedEditTexts.add(latitudeEditText); + extendedLatLonEditTexts.add(latitudeEditText); final ExtendedEditText longitudeEditText = (ExtendedEditText) mainView.findViewById(R.id.longitude_edit_text); - extendedEditTexts.add(longitudeEditText); - final ExtendedEditText nameEditText = (ExtendedEditText) mainView.findViewById(R.id.name_edit_text); - extendedEditTexts.add(nameEditText); + extendedLatLonEditTexts.add(longitudeEditText); + nameEditText = (ExtendedEditText) mainView.findViewById(R.id.name_edit_text); final View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() { @Override @@ -147,16 +166,33 @@ public class CoordinateInputDialogFragment extends DialogFragment { @Override public void afterTextChanged(Editable editable) { - if (focusedEditText != null) { + if (focusedEditText != null && focusedEditText != nameEditText) { String str = focusedEditText.getText().toString(); - if(str.length() == 2 && len < str.length()){ - focusedEditText.append(":"); - } else if (str.length() == 5 && len < str.length()) { + int strLength = str.length(); + if (strLength == 2 && len < strLength) { + String strToAppend; + if (coordinateFormat == PointDescription.FORMAT_DEGREES) { + strToAppend = "."; + } else { + strToAppend = ":"; + } + focusedEditText.append(strToAppend); + } else if (strLength == 5 && coordinateFormat != PointDescription.FORMAT_DEGREES && len < strLength) { + String strToAppend; + if (coordinateFormat == PointDescription.FORMAT_MINUTES) { + strToAppend = "."; + } else { + strToAppend = ":"; + } + focusedEditText.append(strToAppend); + } else if (strLength == 8 && coordinateFormat == PointDescription.FORMAT_SECONDS && len < strLength) { focusedEditText.append("."); - } else if (str.length() == 10) { + } else if ((strLength == DEGREES_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_DEGREES) + || (strLength == MINUTES_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_MINUTES) + || (strLength == SECONDS_MAX_LENGTH && coordinateFormat == PointDescription.FORMAT_SECONDS)) { if (focusedEditText == latitudeEditText) { longitudeBox.select(); - } else if (focusedEditText == longitudeEditText) { + } else { nameBox.select(); } } @@ -207,6 +243,40 @@ public class CoordinateInputDialogFragment extends DialogFragment { return mainView; } + @Override + public void onDestroyView() { + focusedEditText = null; + if (getDialog() != null && getRetainInstance()) { + getDialog().setDismissMessage(null); + } + super.onDestroyView(); + } + + private CoordinateInputBottomSheetDialogFragment.CoordinateInputFormatChangeListener createCoordinateInputFormatChangeListener() { + return new CoordinateInputBottomSheetDialogFragment.CoordinateInputFormatChangeListener() { + @Override + public void onCoordinateFormatChanged(int format) { + coordinateFormat = format; + changeEditTextLengths(); + } + }; + } + + private void changeEditTextLengths() { + int maxLength; + if (coordinateFormat == PointDescription.FORMAT_DEGREES) { + maxLength = DEGREES_MAX_LENGTH; + } else if (coordinateFormat == PointDescription.FORMAT_MINUTES) { + maxLength = MINUTES_MAX_LENGTH; + } else { + maxLength = SECONDS_MAX_LENGTH; + } + InputFilter[] filtersArray = new InputFilter[] {new InputFilter.LengthFilter(maxLength)}; + for (ExtendedEditText extendedEditText : extendedLatLonEditTexts) { + extendedEditText.setFilters(filtersArray); + } + } + public void changeKeyboardInBoxes(boolean useOsmandKeyboard) { for (OsmandTextFieldBoxes textFieldBox : textFieldBoxes) { textFieldBox.setUseOsmandKeyboard(useOsmandKeyboard); @@ -214,9 +284,10 @@ public class CoordinateInputDialogFragment extends DialogFragment { } public void changeKeyboardInEditTexts(boolean useOsmandKeyboard) { - for (ExtendedEditText extendedEditText : extendedEditTexts) { + for (ExtendedEditText extendedEditText : extendedLatLonEditTexts) { extendedEditText.setInputType(useOsmandKeyboard ? InputType.TYPE_NULL : InputType.TYPE_CLASS_TEXT); } + nameEditText.setInputType(useOsmandKeyboard ? InputType.TYPE_NULL : InputType.TYPE_CLASS_TEXT); } private MapActivity getMapActivity() { @@ -233,6 +304,7 @@ public class CoordinateInputDialogFragment extends DialogFragment { return false; } CoordinateInputDialogFragment fragment = new CoordinateInputDialogFragment(); + fragment.setRetainInstance(true); fragment.show(mapActivity.getSupportFragmentManager(), TAG); return true; } catch (RuntimeException e) {