From d9b4a213c9a679e2d4b0a6a399c05ce81f696d73 Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Fri, 8 Dec 2017 18:32:52 +0200 Subject: [PATCH] Add opening day str --- .../net/osmand/util/OpeningHoursParser.java | 60 ++++++++++++------- OsmAnd/res/values/strings.xml | 1 + .../MapContextMenuFragment.java | 2 + .../plus/mapcontextmenu/OpeningHoursInfo.java | 12 +++- .../controllers/AmenityMenuController.java | 17 ++++-- 5 files changed, 65 insertions(+), 27 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java b/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java index a45dcc017e..d6f159c637 100644 --- a/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java +++ b/OsmAnd-java/src/net/osmand/util/OpeningHoursParser.java @@ -28,6 +28,9 @@ public class OpeningHoursParser { private static final String[] monthsStr; private static final String[] localMothsStr; + private static final int LOW_TIME_LIMIT = 120; + private static final int HIGH_TIME_LIMIT = 300; + static { DateFormatSymbols dateFormatSymbols = DateFormatSymbols.getInstance(Locale.US); monthsStr = dateFormatSymbols.getShortMonths(); @@ -195,44 +198,63 @@ public class OpeningHoursParser { } public String getNearToOpeningTime(Calendar cal) { - return getTime(cal, true, true); + return getTime(cal, LOW_TIME_LIMIT, true); } public String getOpeningTime(Calendar cal) { - return getTime(cal, false, true); + return getTime(cal, HIGH_TIME_LIMIT, true); } public String getNearToClosingTime(Calendar cal) { - return getTime(cal, true, false); + return getTime(cal, LOW_TIME_LIMIT, false); } public String getClosingTime(Calendar cal) { - return getTime(cal, false, false); + return getTime(cal, HIGH_TIME_LIMIT, false); } - private String getTime(Calendar cal, boolean nearToEvent, boolean opening) { - String time = getTimeDay(cal, nearToEvent, opening); + public String getOpeningDay(Calendar calendar) { + Calendar cal = (Calendar) calendar.clone(); + String openingTime = ""; + for (int i = 0; i < 7; i++) { + for (OpeningHoursRule r : rules) { + if (r.containsDay(cal) && r.containsMonth(cal)) { + openingTime = r.getTime(cal, false, -1, true); + } + } + if (!Algorithms.isEmpty(openingTime)) { + openingTime += " " + localDaysStr[cal.get(Calendar.DAY_OF_WEEK)]; + break; + } else { + cal.add(Calendar.DAY_OF_MONTH, 1); + } + } + return openingTime; + } + + private String getTime(Calendar cal, int limit, boolean opening) { + String time = getTimeDay(cal, limit, opening); if (Algorithms.isEmpty(time)) { - time = getTimeAnotherDay(cal, nearToEvent, opening); + time = getTimeAnotherDay(cal, limit, opening); } return time; } - private String getTimeDay(Calendar cal, boolean nearToEvent, boolean opening) { + private String getTimeDay(Calendar cal, int limit, boolean opening) { String atTime = ""; for (OpeningHoursRule r : rules) { if (r.containsDay(cal) && r.containsMonth(cal)) { - atTime = r.getTime(cal, false, nearToEvent, opening); + atTime = r.getTime(cal, false, limit, opening); } } return atTime; } - private String getTimeAnotherDay(Calendar cal, boolean nearToEvent, boolean opening) { + private String getTimeAnotherDay(Calendar cal, int limit, boolean opening) { String atTime = ""; for (OpeningHoursRule r : rules) { if (((opening && r.containsPreviousDay(cal)) || (!opening && r.containsNextDay(cal))) && r.containsMonth(cal)) { - atTime = r.getTime(cal, true, nearToEvent, opening); + atTime = r.getTime(cal, true, limit, opening); } } return atTime; @@ -404,7 +426,7 @@ public class OpeningHoursParser { boolean isOpened24_7(); - String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening); + String getTime(Calendar cal, boolean checkAnotherDay, int limit, boolean opening); } /** @@ -835,9 +857,7 @@ public class OpeningHoursParser { } @Override - public String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening) { - int nearToEventLimit = 120; - int freeLimit = 300; + public String getTime(Calendar cal, boolean checkAnotherDay, int limit, boolean opening) { StringBuilder sb = new StringBuilder(); int d = getCurrentDay(cal); int ad = opening ? getNextDay(d) : getPreviousDay(d); @@ -849,7 +869,7 @@ public class OpeningHoursParser { if (startTime < endTime || endTime == -1) { if (days[d] && !checkAnotherDay) { int diff = startTime - time; - if ((time <= startTime) && ((!nearToEvent && diff <= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) { + if (limit == -1 || ((time <= startTime) && (diff <= limit))) { formatTime(startTime, sb); break; } @@ -861,7 +881,7 @@ public class OpeningHoursParser { } else if (time > endTime && days[ad] && checkAnotherDay) { diff = 24 * 60 - endTime + time; } - if ((diff != -1) && ((!nearToEvent && diff <= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) { + if (limit == -1 || ((diff != -1) && (diff <= limit))) { formatTime(startTime, sb); break; } @@ -870,7 +890,7 @@ public class OpeningHoursParser { if (startTime < endTime && endTime != -1) { if (days[d] && !checkAnotherDay) { int diff = endTime - time; - if ((time <= endTime) && ((!nearToEvent && diff <= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) { + if (limit == -1 || ((time <= endTime) && (diff <= limit))) { formatTime(endTime, sb); break; } @@ -882,7 +902,7 @@ public class OpeningHoursParser { } else if (time < endTime && days[ad] && checkAnotherDay) { diff = startTime - time; } - if ((diff != -1) && ((!nearToEvent && diff >= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) { + if (limit == -1 || ((diff != -1) && (diff <= limit))) { formatTime(endTime, sb); break; } @@ -1093,7 +1113,7 @@ public class OpeningHoursParser { } @Override - public String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening) { + public String getTime(Calendar cal, boolean checkAnotherDay, int limit, boolean opening) { return ""; } diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index c439d6fe54..022d4c9b03 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -13,6 +13,7 @@ Opened till Will be closed at Will be opened at + Will be opened on 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 bc1fa459ee..49d4e4e03f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -1118,6 +1118,8 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo openingHoursStr = getString(R.string.opened_till) + " " + openingHoursInfo.getClosingTime(); } else if (!Algorithms.isEmpty(openingHoursInfo.getNearToClosingTime())) { openingHoursStr = getString(R.string.will_be_closed_at) + " " + openingHoursInfo.getNearToClosingTime(); + } else if (!Algorithms.isEmpty(openingHoursInfo.getOpeningDay())) { + openingHoursStr = getString(R.string.will_be_opened_on) + " " + openingHoursInfo.getOpeningDay() + "."; } openingHoursTextView.setText(openingHoursStr); openingHoursTextView.setVisibility(View.VISIBLE); diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/OpeningHoursInfo.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/OpeningHoursInfo.java index c8305ca85e..3802a21c52 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/OpeningHoursInfo.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/OpeningHoursInfo.java @@ -10,6 +10,7 @@ public class OpeningHoursInfo { private String nearToOpeningTime = ""; private String closingTime = ""; private String nearToClosingTime = ""; + private String openingDay = ""; public boolean isOpened() { return opened; @@ -59,11 +60,20 @@ public class OpeningHoursInfo { this.nearToClosingTime = nearToClosingTime; } + public String getOpeningDay() { + return openingDay; + } + + public void setOpeningDay(String openingDay) { + this.openingDay = openingDay; + } + public boolean containsInfo() { return opened24_7 || !Algorithms.isEmpty(openingTime) || !Algorithms.isEmpty(nearToOpeningTime) || !Algorithms.isEmpty(closingTime) - || !Algorithms.isEmpty(nearToClosingTime); + || !Algorithms.isEmpty(nearToClosingTime) + || !Algorithms.isEmpty(openingDay); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java index 700cc025c4..fcaad04410 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java @@ -143,12 +143,17 @@ public class AmenityMenuController extends MenuController { } else { OpeningHoursInfo info = new OpeningHoursInfo(); Calendar cal = Calendar.getInstance(); - info.setOpened(openingHours.isOpenedForTime(cal)); - info.setOpened24_7(openingHours.isOpened24_7()); - info.setOpeningTime(openingHours.getOpeningTime(cal)); - info.setNearToOpeningTime(openingHours.getNearToOpeningTime(cal)); - info.setClosingTime(openingHours.getClosingTime(cal)); - info.setNearToClosingTime(openingHours.getNearToClosingTime(cal)); + boolean opened = openingHours.isOpenedForTime(cal); + info.setOpened(opened); + if (opened) { + info.setOpened24_7(openingHours.isOpened24_7()); + info.setClosingTime(openingHours.getClosingTime(cal)); + info.setNearToClosingTime(openingHours.getNearToClosingTime(cal)); + } else { + info.setOpeningTime(openingHours.getOpeningTime(cal)); + info.setNearToOpeningTime(openingHours.getNearToOpeningTime(cal)); + info.setOpeningDay(openingHours.getOpeningDay(cal)); + } return info; } }