From 885836d923baf52269fc1a0c7179c5f65fcad31e Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Thu, 27 Aug 2015 17:03:32 +0300 Subject: [PATCH] Opening hours editing is working --- .../net/osmand/util/OpeningHoursParser.java | 11 +- OsmAnd/res/layout/open_time_list_item.xml | 63 ++-- .../plus/osmedit/BasicDataFragment.java | 321 ++++++++++++++++-- .../OpeningHoursDaysDialogFragment.java | 17 +- .../OpeningHoursHoursDialogFragment.java | 33 +- 5 files changed, 357 insertions(+), 88 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java b/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java index 993de124f8..bde16fbdc0 100644 --- a/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java +++ b/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java @@ -2,6 +2,10 @@ package net.osmand.util; /* Can be commented out in order to run the main function separately */ +import net.osmand.PlatformUtil; + +import org.apache.commons.logging.Log; + import java.io.Serializable; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -17,6 +21,7 @@ import java.util.Calendar; * if the OSM feature is open at a certain time. */ public class OpeningHoursParser { + private static final Log LOG = PlatformUtil.getLog(OpeningHoursParser.class); private static final String[] daysStr = new String[] {"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ private static final String[] monthsStr = new String[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; @@ -612,7 +617,8 @@ public class OpeningHoursParser { String timeSubstr = r.substring(k); String[] times = timeSubstr.split(","); boolean timesExist = true; - for (String time : times) { + for (int i = 0; i < times.length; i++) { + String time = times[i]; time = time.trim(); if(time.length() == 0){ continue; @@ -627,6 +633,9 @@ public class OpeningHoursParser { } String[] stEnd = time.split("-"); //$NON-NLS-1$ if (stEnd.length != 2) { + if (i == times.length - 1 && basic.getStartTime() == 0 && basic.getEndTime() == 0) { + return false; + } continue; } timesExist = true; diff --git a/OsmAnd/res/layout/open_time_list_item.xml b/OsmAnd/res/layout/open_time_list_item.xml index 72c736ef80..ec08cfac52 100644 --- a/OsmAnd/res/layout/open_time_list_item.xml +++ b/OsmAnd/res/layout/open_time_list_item.xml @@ -19,8 +19,8 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="24dp" - android:orientation="vertical" - android:layout_weight="1"> + android:layout_weight="1" + android:orientation="vertical"> + android:gravity="center_vertical"> - + android:layout_marginLeft="16dp" + android:layout_marginRight="16dp" + android:orientation="vertical"> + + + + + - - - - - - + android:layout_marginLeft="16dp" + android:orientation="vertical"> - + + + + i){// overflow handling, e.g. Su-We + for (int j = startDay; j <= 6; j++) { + days[j] = true; + } + for (int j = 0; j <= i; j++){ + days[j] = true; + } + } + startDay = -1; + } else { + days[i] = true; + } + previousDay = i; + } else { + // Read Month + int m = 0; + for (String s : monthsStr) { + if (s.charAt(0) == ch && s.charAt(1) == r.charAt(k + 1) + && s.charAt(2) == r.charAt(k + 2)) { + break; + } + m++; + } + if (m < monthsStr.length) { + if (startMonth != -1) { + for (int j = startMonth; j <= m; j++) { + months[j] = true; + } + if (startMonth > m) {// overflow handling, e.g. Oct-Mar + for (int j = startMonth; j <= 11; j++) { + months[j] = true; + } + for (int j = 0; j <= m; j++) { + months[j] = true; + } + } + startMonth = -1; + } else { + months[m] = true; + } + previousMonth = m; + } + } + } else { + return new UnparseableRule(r); + } + } + if(previousDay == -1){ + // no days given => take all days. + for (int i = 0; i<7; i++){ + days[i] = true; + } + } + if (previousMonth == -1) { + // no month given => take all months. + for (int i = 0; i < 12; i++) { + months[i] = true; + } + } + String timeSubstr = localRuleString.substring(k); + String[] times = timeSubstr.split(","); + boolean timesExist = true; + for (int i = 0; i < times.length; i++) { + String time = times[i]; + time = time.trim(); + if(time.length() == 0){ + continue; + } + if(time.equals("off")){ + break; // add no time values + } + if(time.equals("24/7")){ + // for some reason, this is used. See tagwatch. + basic.addTimeRange(0, 24*60); + break; + } + String[] stEnd = time.split("-"); //$NON-NLS-1$ + if (stEnd.length != 2) { + if (i == times.length - 1 && basic.getStartTime() == 0 && basic.getEndTime() == 0) { + return new UnparseableRule(r); + } + continue; + } + timesExist = true; + int st; + int end; + try { + int i1 = stEnd[0].indexOf(':'); + int i2 = stEnd[1].indexOf(':'); + int startHour, startMin, endHour, endMin; + if(i1 == -1) { + // if no minutes are given, try complete value as hour + startHour = Integer.parseInt(stEnd[0].trim()); + startMin = 0; + } else { + startHour = Integer.parseInt(stEnd[0].substring(0, i1).trim()); + startMin = Integer.parseInt(stEnd[0].substring(i1 + 1).trim()); + } + if(i2 == -1) { + // if no minutes are given, try complete value as hour + endHour = Integer.parseInt(stEnd[1].trim()); + endMin = 0; + } else { + endHour = Integer.parseInt(stEnd[1].substring(0, i2).trim()); + endMin = Integer.parseInt(stEnd[1].substring(i2 + 1).trim()); + } + st = startHour * 60 + startMin; + end = endHour * 60 + endMin; + } catch (NumberFormatException e) { + return new UnparseableRule(r); + } + basic.addTimeRange(st, end); + } + if(!timesExist){ + return new UnparseableRule(r); + } + return basic; + } + private class MyOnFocusChangeListener implements TextWatcher { private final EditPoiData data; private final String tagName; @@ -279,8 +496,12 @@ public class BasicDataFragment extends Fragment { this.deleteDrawable = deleteDrawable; } - public void addOpeningHoursRule(OpeningHoursParser.BasicOpeningHourRule rule) { - openingHours.addRule(rule); + public void setOpeningHoursRule(OpeningHoursParser.BasicOpeningHourRule rule, int position) { + if (position == -1) { + openingHours.addRule(rule); + } else { + openingHours.getRules().set(position, rule); + } updateViews(); } @@ -303,28 +524,46 @@ public class BasicDataFragment extends Fragment { } private View getView(final int position) { - OpeningHoursParser.BasicOpeningHourRule rule = - (BasicOpeningHourRule) openingHours.getRules().get(position); - final View view = LayoutInflater.from(linearLayout.getContext()) .inflate(R.layout.open_time_list_item, null, false); + ImageView clockIconImageView = (ImageView) view.findViewById(R.id.clockIconImageView); clockIconImageView.setImageDrawable(clockDrawable); TextView daysTextView = (TextView) view.findViewById(R.id.daysTextView); - StringBuilder stringBuilder = new StringBuilder(); - rule.appendDaysString(stringBuilder); - daysTextView.setText(stringBuilder.toString()); + View timeContainer = view.findViewById(R.id.timeContainer); - TextView openingTextView = (TextView) view.findViewById(R.id.openingTextView); - final int openingHour = rule.getStartTime() / 60; - int openingMinute = rule.getStartTime() - openingHour * 60; - openingTextView.setText(formatTime(openingHour, openingMinute)); + if (openingHours.getRules().get(position) instanceof BasicOpeningHourRule) { + final OpeningHoursParser.BasicOpeningHourRule rule = + (BasicOpeningHourRule) openingHours.getRules().get(position); + StringBuilder stringBuilder = new StringBuilder(); + rule.appendDaysString(stringBuilder); - TextView closingTextView = (TextView) view.findViewById(R.id.closingTextView); - int enHour = rule.getEndTime() / 60; - int enTime = rule.getEndTime() - enHour * 60; - closingTextView.setText(formatTime(enHour, enTime)); + daysTextView.setText(stringBuilder.toString()); + + TextView openingTextView = (TextView) view.findViewById(R.id.openingTextView); + final int openingHour = rule.getStartTime() / 60; + int openingMinute = rule.getStartTime() - openingHour * 60; + openingTextView.setText(formatTime(openingHour, openingMinute)); + + TextView closingTextView = (TextView) view.findViewById(R.id.closingTextView); + int enHour = rule.getEndTime() / 60; + int enTime = rule.getEndTime() - enHour * 60; + closingTextView.setText(formatTime(enHour, enTime)); + timeContainer.setVisibility(View.VISIBLE); + + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + OpeningHoursDaysDialogFragment fragment = + OpeningHoursDaysDialogFragment.createInstance(rule, position); + fragment.show(getChildFragmentManager(), "OpenTimeDialogFragment"); + } + }); + } else if (openingHours.getRules().get(position) instanceof UnparseableRule) { + daysTextView.setText(openingHours.getRules().get(position).toRuleString(false)); + timeContainer.setVisibility(View.GONE); + } ImageButton deleteItemImageButton = (ImageButton) view.findViewById(R.id.deleteItemImageButton); deleteItemImageButton.setImageDrawable(deleteDrawable); @@ -338,16 +577,38 @@ public class BasicDataFragment extends Fragment { return view; } } - private static String formatTime(int h, int t) { - StringBuilder b = new StringBuilder(); - if (h < 10) { - b.append("0"); //$NON-NLS-1$ + + // TODO: 8/27/15 Consider moving to OpeningHoursParser + private static class UnparseableRule implements OpeningHoursParser.OpeningHoursRule { + private String ruleString; + + public UnparseableRule(String ruleString) { + this.ruleString = ruleString; } - b.append(h).append(":"); //$NON-NLS-1$ - if (t < 10) { - b.append("0"); //$NON-NLS-1$ + + @Override + public boolean isOpenedForTime(Calendar cal, boolean checkPrevious) { + return false; + } + + @Override + public boolean containsPreviousDay(Calendar cal) { + return false; + } + + @Override + public boolean containsDay(Calendar cal) { + return false; + } + + @Override + public boolean containsMonth(Calendar cal) { + return false; + } + + @Override + public String toRuleString(boolean avoidMonths) { + return ruleString; } - b.append(t); - return b.toString(); } } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/OpeningHoursDaysDialogFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/OpeningHoursDaysDialogFragment.java index cd3f2e6f6a..db134d2056 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/OpeningHoursDaysDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/OpeningHoursDaysDialogFragment.java @@ -63,17 +63,9 @@ public class OpeningHoursDaysDialogFragment extends DialogFragment { for (int i = 0; i < 7; i++) { days[(first + 5 + i) % 7] = dayToShow[i]; } - OpeningHoursHoursDialogFragment.createInstance(item, null, true) + + OpeningHoursHoursDialogFragment.createInstance(item, positionToAdd, true) .show(getFragmentManager(), "TimePickerDialogFragment"); - if (positionToAdd != -1) { - -// time.insert(item, positionToAdd); -// selectedRule = positionToAdd; - } else { -// time.notifyDataSetChanged(); - } -// updateTimePickers(); - } }); @@ -82,8 +74,9 @@ public class OpeningHoursDaysDialogFragment extends DialogFragment { return builder.create(); } - public static OpeningHoursDaysDialogFragment createInstance(final OpeningHoursParser.BasicOpeningHourRule item, - final int positionToAdd) { + public static OpeningHoursDaysDialogFragment createInstance( + @NonNull final OpeningHoursParser.BasicOpeningHourRule item, + final int positionToAdd) { LOG.debug("createInstance(" + "item=" + item + ", positionToAdd=" + positionToAdd + ")"); OpeningHoursDaysDialogFragment daysDialogFragment = new OpeningHoursDaysDialogFragment(); Bundle bundle = new Bundle(); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/OpeningHoursHoursDialogFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/OpeningHoursHoursDialogFragment.java index 7877b20dcb..df0b416e96 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/dialogs/OpeningHoursHoursDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/dialogs/OpeningHoursHoursDialogFragment.java @@ -17,34 +17,30 @@ import net.osmand.plus.R; import net.osmand.plus.osmedit.BasicDataFragment; import net.osmand.util.OpeningHoursParser; -import java.util.Calendar; - public class OpeningHoursHoursDialogFragment extends DialogFragment { - public static final String INITIAL_TIME = "initial_time"; public static final String IS_START = "is_start"; public static final String BASIC_OPENING_HOUR_RULE = "basic_opening_hour_rule"; + public static final String POSITION_TO_ADD = "position_to_add"; @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bundle args = getArguments(); final boolean isStart = args.getBoolean(IS_START); + final int positionToAdd = args.getInt(POSITION_TO_ADD); final OpeningHoursParser.BasicOpeningHourRule item = (OpeningHoursParser.BasicOpeningHourRule) args.getSerializable(BASIC_OPENING_HOUR_RULE); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - Calendar initialState = (Calendar) args.getSerializable(INITIAL_TIME); - if (initialState == null) { - initialState = Calendar.getInstance(); - initialState.set(Calendar.HOUR_OF_DAY, isStart? 8 : 20); - initialState.set(Calendar.MINUTE, 0); - } + int time = isStart ? item.getStartTime() : item.getEndTime(); + int hour = time / 60; + int minute = time - hour * 60; final TimePicker timePicker = new TimePicker(getActivity()); timePicker.setIs24HourView(DateFormat.is24HourFormat(getActivity())); - timePicker.setCurrentHour(initialState.get(Calendar.HOUR_OF_DAY)); - timePicker.setCurrentMinute(initialState.get(Calendar.MINUTE)); + timePicker.setCurrentHour(hour); + timePicker.setCurrentMinute(minute); builder.setView(timePicker) .setPositiveButton(R.string.next_proceed, new DialogInterface.OnClickListener() { @@ -55,11 +51,13 @@ public class OpeningHoursHoursDialogFragment extends DialogFragment { int time = minute + hourOfDay * 60; if (isStart) { item.setStartTime(time); - OpeningHoursHoursDialogFragment.createInstance(item, null, false) + OpeningHoursHoursDialogFragment + .createInstance(item, positionToAdd, false) .show(getFragmentManager(), "TimePickerDialogFragment"); } else { item.setEndTime(time); - ((BasicDataFragment) getParentFragment()).addBasicOpeningHoursRule(item); + ((BasicDataFragment) getParentFragment()) + .setBasicOpeningHoursRule(item, positionToAdd); } } }) @@ -82,14 +80,15 @@ public class OpeningHoursHoursDialogFragment extends DialogFragment { return builder.create(); } - public static OpeningHoursHoursDialogFragment createInstance(OpeningHoursParser.BasicOpeningHourRule item, - Calendar initialTime, - boolean isStart) { + public static OpeningHoursHoursDialogFragment createInstance( + @NonNull OpeningHoursParser.BasicOpeningHourRule item, + int positionToAdd, + boolean isStart) { OpeningHoursHoursDialogFragment fragment = new OpeningHoursHoursDialogFragment(); Bundle bundle = new Bundle(); bundle.putSerializable(BASIC_OPENING_HOUR_RULE, item); + bundle.putSerializable(POSITION_TO_ADD, positionToAdd); bundle.putBoolean(IS_START, isStart); - bundle.putSerializable(INITIAL_TIME, initialTime); fragment.setArguments(bundle); return fragment; }