From 246877cf10386138176efc21bec26227593fc94f Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Thu, 7 Dec 2017 18:00:54 +0200 Subject: [PATCH] Add will be closed at --- .../net/osmand/util/OpeningHoursParser.java | 115 ++++++++++++++++-- OsmAnd/res/values/strings.xml | 1 + .../MapContextMenuFragment.java | 2 + .../plus/mapcontextmenu/MenuController.java | 4 + .../mapcontextmenu/MenuTitleController.java | 8 +- .../controllers/AmenityMenuController.java | 15 +++ 6 files changed, 135 insertions(+), 10 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java b/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java index 7a2b31cc81..666ca30ec0 100644 --- a/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java +++ b/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java @@ -222,6 +222,34 @@ public class OpeningHoursParser { return openFrom; } + public String getClosedAtStr(Calendar cal) { + String closedAt = getClosedAtDayStr(cal); + if (Algorithms.isEmpty(closedAt)) { + closedAt = getClosedAtNextDayStr(cal); + } + return closedAt; + } + + private String getClosedAtDayStr(Calendar cal) { + String closedAt = ""; + for (OpeningHoursRule r : rules) { + if (r.containsDay(cal) && r.containsMonth(cal)) { + closedAt = r.getClosedAtStr(cal, false); + } + } + return closedAt; + } + + private String getClosedAtNextDayStr(Calendar cal) { + String closedAt = ""; + for (OpeningHoursRule r : rules) { + if (r.containsNextDay(cal) && r.containsMonth(cal)) { + closedAt = r.getClosedAtStr(cal, true); + } + } + return closedAt; + } + public String getCurrentRuleTime(Calendar cal) { // make exception for overlapping times i.e. // (1) Mo 14:00-16:00; Tu off @@ -354,6 +382,14 @@ public class OpeningHoursParser { */ public boolean containsDay(Calendar cal); + /** + * Check if the next day after "cal" is part of this rule + * + * @param cal the time to check + * @return true if the next day is part of the rule + */ + boolean containsNextDay(Calendar cal); + /** * Check if the month of "cal" is part of this rule * @@ -381,6 +417,8 @@ public class OpeningHoursParser { boolean isOpen24_7(); String getOpenedFromStr(Calendar cal, boolean checkPrevious); + + String getClosedAtStr(Calendar cal, boolean checkNext); } /** @@ -598,6 +636,16 @@ public class OpeningHoursParser { return false; } + @Override + public boolean containsNextDay(Calendar cal) { + int i = cal.get(Calendar.DAY_OF_WEEK); + int p = (i + 6) % 7; + if (days[p]) { + return true; + } + return false; + } + /** * Check if the previous weekday of time "cal" is part of this rule * @@ -677,6 +725,14 @@ public class OpeningHoursParser { return p; } + private int getNextDay(int currentDay) { + int n = currentDay + 1; + if (n > 6) { + n -= 7; + } + return n; + } + private int getCurrentTimeInMinutes(Calendar cal) { return cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE); } @@ -804,23 +860,49 @@ public class OpeningHoursParser { if (startTime < endTime || endTime == -1) { if (days[d] && !checkPrevious) { if (time - startTime <= 600 && (endTime == -1 || time <= endTime)) { - int stHour = startTime / 60; - int stTime = startTime - stHour * 60; - formatTime(stHour, stTime, sb); + formatTime(startTime, sb); break; } } } else { if (time >= startTime && days[d] && !checkPrevious) { - int stHour = startTime / 60; - int stTime = startTime - stHour * 60; - formatTime(stHour, stTime, sb); + formatTime(startTime, sb); break; } else if (time < endTime && days[p] && checkPrevious) { if (24 * 60 - endTime + time <= 600) { - int stHour = startTime / 60; - int stTime = startTime - stHour * 60; - formatTime(stHour, stTime, sb); + formatTime(startTime, sb); + break; + } + } + } + } + return sb.toString(); + } + + @Override + public String getClosedAtStr(Calendar cal, boolean checkNext) { + int limit = 300; + StringBuilder sb = new StringBuilder(); + int d = getCurrentDay(cal); + int n = getNextDay(d); + int time = getCurrentTimeInMinutes(cal); + for (int i = 0; i < startTimes.size(); i++) { + int startTime = startTimes.get(i); + int endTime = endTimes.get(i); + if (startTime < endTime && endTime != -1) { + if (days[d] && !checkNext) { + if (time <= endTime && endTime - time <= limit ) { + formatTime(endTime, sb); + break; + } + } + } else { + if (time <= endTime && days[d] && !checkNext) { + formatTime(endTime, sb); + break; + } else if (time < endTime && days[n] && checkNext) { + if (endTime - time <= limit) { + formatTime(endTime, sb); break; } } @@ -1004,6 +1086,11 @@ public class OpeningHoursParser { return false; } + @Override + public boolean containsNextDay(Calendar cal) { + return false; + } + @Override public boolean containsMonth(Calendar cal) { return false; @@ -1029,6 +1116,11 @@ public class OpeningHoursParser { return ""; } + @Override + public String getClosedAtStr(Calendar cal, boolean checkNext) { + return ""; + } + @Override public String toString() { return toRuleString(); @@ -1374,6 +1466,11 @@ public class OpeningHoursParser { b.append(t); } + private static void formatTime(int minutes, StringBuilder sb) { + int hour = minutes / 60; + int time = minutes - hour * 60; + formatTime(hour, time, sb); + } /** * test if the calculated opening hours are what you expect diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 0f819d4c24..69e904c7c3 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -11,6 +11,7 @@ --> Opened Opened from + Will be closed at Additional actions \u2022 Detection of stop signs now considers driving direction\n\n diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 662da8c670..c0c6c1643d 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -1112,6 +1112,8 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo openingHoursStr = getString(R.string.shared_string_is_open_24_7); } else if (!Algorithms.isEmpty(menu.getOpenFromStr())) { openingHoursStr = getString(R.string.opened_from) + " " + menu.getOpenFromStr(); + } else if (!Algorithms.isEmpty(menu.getClosedAtStr())) { + openingHoursStr = getString(R.string.will_be_closed_at) + " " + menu.getClosedAtStr(); } openingHoursTextView.setText(openingHoursStr); openingHoursView.setVisibility(View.VISIBLE); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java index 24e2c3d922..6d1d6c0a0d 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java @@ -432,6 +432,10 @@ public abstract class MenuController extends BaseMenuController { return ""; } + public String getClosedAtStr() { + return ""; + } + public String getCommonTypeStr() { return ""; } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuTitleController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuTitleController.java index d12e2433ec..78365380b3 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuTitleController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuTitleController.java @@ -20,6 +20,7 @@ public abstract class MenuTitleController { protected String streetStr = ""; protected boolean open24_7; protected String openFromStr = ""; + protected String closedAtStr = ""; private AddressLookupRequest addressLookupRequest; @@ -108,8 +109,12 @@ public abstract class MenuTitleController { return openFromStr; } + public String getClosedAtStr() { + return closedAtStr; + } + public boolean isOpened() { - if (isOpen24_7() || !Algorithms.isEmpty(getOpenFromStr())) { + if (isOpen24_7() || !Algorithms.isEmpty(getOpenFromStr()) || !Algorithms.isEmpty(getClosedAtStr())) { return true; } else { return false; @@ -211,6 +216,7 @@ public abstract class MenuTitleController { if (menuController != null) { open24_7 = menuController.isOpen24_7(); openFromStr = menuController.getOpenFromStr(); + closedAtStr = menuController.getClosedAtStr(); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java index 3cb5bdc9e8..53b9bbbcde 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java @@ -128,6 +128,11 @@ public class AmenityMenuController extends MenuController { return getOpenFromStr(amenity); } + @Override + public String getClosedAtStr() { + return getClosedAtStr(amenity); + } + public static String getTypeStr(Amenity amenity) { PoiCategory pc = amenity.getType(); PoiType pt = pc.getPoiTypeByKeyName(amenity.getSubType()); @@ -166,6 +171,16 @@ public class AmenityMenuController extends MenuController { } } + public static String getClosedAtStr(Amenity amenity) { + OpeningHoursParser.OpeningHours openingHours = OpeningHoursParser.parseOpenedHours(amenity.getOpeningHours()); + if (openingHours == null) { + return ""; + } else { + Calendar cal = Calendar.getInstance(); + return openingHours.getClosedAtStr(cal); + } + } + @Override public String getCommonTypeStr() { PoiCategory pc = amenity.getType();