1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
#pragma once
#include "map/bookmark_manager.hpp"
#include "map/extrapolation/extrapolator.hpp"
#include "map/routing_mark.hpp"
#include "map/transit/transit_display.hpp"
#include "map/transit/transit_reader.hpp"
#include "routing/route.hpp"
#include "routing/routing_callbacks.hpp"
#include "routing/routing_session.hpp"
#include "routing/speed_camera_manager.hpp"
#include "storage/storage_defines.hpp"
#include "drape_frontend/drape_engine_safe_ptr.hpp"
#include "drape/pointers.hpp"
#include "tracking/reporter.hpp"
#include "geometry/point2d.hpp"
#include "base/thread_checker.hpp"
#include <chrono>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
#include <vector>
namespace storage
{
class CountryInfoGetter;
}
namespace routing
{
class NumMwmIds;
}
class DataSource;
namespace power_management
{
class PowerManager;
}
struct RoutePointInfo
{
std::string m_name;
RouteMarkType m_markType = RouteMarkType::Start;
size_t m_intermediateIndex = 0;
bool m_isPassed = false;
bool m_isMyPosition = false;
m2::PointD m_position;
};
class RoutingManager final
{
public:
class Delegate
{
public:
virtual void OnRouteFollow(routing::RouterType type) = 0;
virtual void RegisterCountryFilesOnRoute(std::shared_ptr<routing::NumMwmIds> ptr) const = 0;
virtual ~Delegate() = default;
};
struct Callbacks
{
using DataSourceGetterFn = std::function<DataSource &()>;
using CountryInfoGetterFn = std::function<storage::CountryInfoGetter &()>;
using CountryParentNameGetterFn = std::function<std::string(std::string const &)>;
using GetStringsBundleFn = std::function<StringsBundle const &()>;
using PowerManagerGetter = std::function<power_management::PowerManager const &()>;
template <typename DataSourceGetter, typename CountryInfoGetter,
typename CountryParentNameGetter, typename StringsBundleGetter,
typename PowerManagerGetter>
Callbacks(DataSourceGetter && dataSourceGetter, CountryInfoGetter && countryInfoGetter,
CountryParentNameGetter && countryParentNameGetter,
StringsBundleGetter && stringsBundleGetter, PowerManagerGetter && powerManagerGetter)
: m_dataSourceGetter(std::forward<DataSourceGetter>(dataSourceGetter))
, m_countryInfoGetter(std::forward<CountryInfoGetter>(countryInfoGetter))
, m_countryParentNameGetterFn(std::forward<CountryParentNameGetter>(countryParentNameGetter))
, m_stringsBundleGetter(std::forward<StringsBundleGetter>(stringsBundleGetter))
, m_powerManagerGetter(std::forward<PowerManagerGetter>(powerManagerGetter))
{
}
DataSourceGetterFn m_dataSourceGetter;
CountryInfoGetterFn m_countryInfoGetter;
CountryParentNameGetterFn m_countryParentNameGetterFn;
GetStringsBundleFn m_stringsBundleGetter;
PowerManagerGetter m_powerManagerGetter;
};
using RouteBuildingCallback =
std::function<void(routing::RouterResultCode, storage::CountriesSet const &)>;
using RouteStartBuildCallback = std::function<void(std::vector<RouteMarkData> const & points)>;
enum class Recommendation
{
// It can be recommended if location is found almost immediately
// after restoring route points from file. In this case we can
// rebuild route using "my position".
RebuildAfterPointsLoading = 0,
};
using RouteRecommendCallback = std::function<void(Recommendation)>;
RoutingManager(Callbacks && callbacks, Delegate & delegate);
void SetBookmarkManager(BookmarkManager * bmManager);
void SetTransitManager(TransitReadManager * transitManager);
routing::RoutingSession const & RoutingSession() const { return m_routingSession; }
routing::RoutingSession & RoutingSession() { return m_routingSession; }
void SetRouter(routing::RouterType type);
routing::RouterType GetRouter() const { return m_currentRouterType; }
bool IsRoutingActive() const { return m_routingSession.IsActive(); }
bool IsRouteBuilt() const { return m_routingSession.IsBuilt(); }
bool IsRouteBuilding() const { return m_routingSession.IsBuilding(); }
bool IsRouteRebuildingOnly() const { return m_routingSession.IsRebuildingOnly(); }
bool IsRouteNotReady() const { return m_routingSession.IsNotReady(); }
bool IsRouteFinished() const { return m_routingSession.IsFinished(); }
bool IsOnRoute() const { return m_routingSession.IsOnRoute(); }
bool IsRoutingFollowing() const { return m_routingSession.IsFollowing(); }
bool IsRouteValid() const { return m_routingSession.IsRouteValid(); }
void BuildRoute(uint32_t timeoutSec);
void SetUserCurrentPosition(m2::PointD const & position);
void ResetRoutingSession() { m_routingSession.Reset(); }
// FollowRoute has a bug where the router follows the route even if the method hads't been called.
// This method was added because we do not want to break the behaviour that is familiar to our
// users.
bool DisableFollowMode();
void SetRouteBuildingListener(RouteBuildingCallback const & buildingCallback)
{
m_routingBuildingCallback = buildingCallback;
}
void SetRouteStartBuildListener(RouteStartBuildCallback const & startBuildCallback)
{
m_routingStartBuildCallback = startBuildCallback;
}
/// See warning above.
void SetRouteProgressListener(routing::ProgressCallback const & progressCallback)
{
m_routingSession.SetProgressCallback(progressCallback);
}
void SetRouteRecommendationListener(RouteRecommendCallback const & recommendCallback)
{
m_routeRecommendCallback = recommendCallback;
}
void FollowRoute();
void CloseRouting(bool removeRoutePoints);
void GetRouteFollowingInfo(location::FollowingInfo & info) const
{
m_routingSession.GetRouteFollowingInfo(info);
}
TransitRouteInfo GetTransitRouteInfo() const;
m2::PointD GetRouteEndPoint() const { return m_routingSession.GetEndPoint(); }
/// Returns the most situable router engine type.
routing::RouterType GetBestRouter(m2::PointD const & startPoint,
m2::PointD const & finalPoint) const;
routing::RouterType GetLastUsedRouter() const;
void SetLastUsedRouter(routing::RouterType type);
// Sound notifications for turn instructions.
void EnableTurnNotifications(bool enable) { m_routingSession.EnableTurnNotifications(enable); }
bool AreTurnNotificationsEnabled() const
{
return m_routingSession.AreTurnNotificationsEnabled();
}
/// \brief Sets a locale for TTS.
/// \param locale is a string with locale code. For example "en", "ru", "zh-Hant" and so on.
/// \note See sound/tts/languages.txt for the full list of available locales.
void SetTurnNotificationsLocale(std::string const & locale)
{
m_routingSession.SetTurnNotificationsLocale(locale);
}
/// @return current TTS locale. For example "en", "ru", "zh-Hant" and so on.
/// In case of error returns an empty string.
/// \note The method returns correct locale after SetTurnNotificationsLocale has been called.
/// If not, it returns an empty string.
std::string GetTurnNotificationsLocale() const
{
return m_routingSession.GetTurnNotificationsLocale();
}
/// \brief Adds to @param notifications strings - notifications, which are ready to be
/// pronounced to end user right now.
/// Adds notifications about turns and speed camera on the road.
///
/// \note Current notifications will be deleted after call and second call
/// will not return previous data, only newer.
void GenerateNotifications(std::vector<std::string> & notifications);
void AddRoutePoint(RouteMarkData && markData);
std::vector<RouteMarkData> GetRoutePoints() const;
size_t GetRoutePointsCount() const;
void RemoveRoutePoint(RouteMarkType type, size_t intermediateIndex = 0);
void RemoveRoutePoints();
void RemoveIntermediateRoutePoints();
void MoveRoutePoint(size_t currentIndex, size_t targetIndex);
void MoveRoutePoint(RouteMarkType currentType, size_t currentIntermediateIndex,
RouteMarkType targetType, size_t targetIntermediateIndex);
void HideRoutePoint(RouteMarkType type, size_t intermediateIndex = 0);
bool CouldAddIntermediatePoint() const;
bool IsMyPosition(RouteMarkType type, size_t intermediateIndex = 0);
void SetRouterImpl(routing::RouterType type);
void RemoveRoute(bool deactivateFollowing);
void CheckLocationForRouting(location::GpsInfo const & info);
void CallRouteBuilded(routing::RouterResultCode code,
storage::CountriesSet const & absentCountries);
void OnBuildRouteReady(routing::Route const & route, routing::RouterResultCode code);
void OnRebuildRouteReady(routing::Route const & route, routing::RouterResultCode code);
void OnNeedMoreMaps(uint64_t routeId, storage::CountriesSet const & absentCountries);
void OnRemoveRoute(routing::RouterResultCode code);
void OnRoutePointPassed(RouteMarkType type, size_t intermediateIndex);
void OnLocationUpdate(location::GpsInfo const & info);
void CallRouteBuildStart(std::vector<RouteMarkData> const & points);
void SetAllowSendingPoints(bool isAllowed)
{
m_trackingReporter.SetAllowSendingPoints(isAllowed);
}
routing::SpeedCameraManager & GetSpeedCamManager() { return m_routingSession.GetSpeedCamManager(); }
bool IsSpeedLimitExceeded() const;
void SetTurnNotificationsUnits(measurement_utils::Units const units)
{
m_routingSession.SetTurnNotificationsUnits(units);
}
void SetDrapeEngine(ref_ptr<df::DrapeEngine> engine, bool is3dAllowed);
/// \returns true if altitude information along |m_route| is available and
/// false otherwise.
bool HasRouteAltitude() const;
/// \brief Fills altitude of current route points and distance in meters form the beginning
/// of the route point based on the route in RoutingSession.
bool GetRouteAltitudesAndDistancesM(std::vector<double> & routePointDistanceM,
feature::TAltitudes & altitudes) const;
/// \brief Generates 4 bytes per point image (RGBA) and put the data to |imageRGBAData|.
/// \param width is width of chart shall be generated in pixels.
/// \param height is height of chart shall be generated in pixels.
/// \param altitudes route points altitude.
/// \param routePointDistanceM distance in meters from route beginning to route points.
/// \param imageRGBAData is bits of result image in RGBA.
/// \param minRouteAltitude is min altitude along the route in altitudeUnits.
/// \param maxRouteAltitude is max altitude along the route in altitudeUnits.
/// \param altitudeUnits is units (meters or feet) which is used to pass min and max altitudes.
/// \returns If there is valid route info and the chart was generated returns true
/// and false otherwise. If the method returns true it is guaranteed that the size of
/// |imageRGBAData| is not zero.
/// \note If HasRouteAltitude() method returns true, GenerateRouteAltitudeChart(...)
/// could return false if route was deleted or rebuilt between the calls.
bool GenerateRouteAltitudeChart(uint32_t width, uint32_t height,
feature::TAltitudes const & altitudes,
std::vector<double> const & routePointDistanceM,
std::vector<uint8_t> & imageRGBAData, int32_t & minRouteAltitude,
int32_t & maxRouteAltitude,
measurement_utils::Units & altitudeUnits) const;
uint32_t OpenRoutePointsTransaction();
void ApplyRoutePointsTransaction(uint32_t transactionId);
void CancelRoutePointsTransaction(uint32_t transactionId);
static uint32_t InvalidRoutePointsTransactionId();
/// \returns true if there are route points saved in file and false otherwise.
bool HasSavedRoutePoints() const;
/// \brief It loads road points from file and delete file after loading.
/// The result of the loading will be sent via SafeCallback.
using LoadRouteHandler = platform::SafeCallback<void(bool success)>;
void LoadRoutePoints(LoadRouteHandler const & handler);
/// \brief It saves route points to file.
void SaveRoutePoints();
/// \brief It deletes file with saved route points if it exists.
void DeleteSavedRoutePoints();
void UpdatePreviewMode();
void CancelPreviewMode();
routing::RouterType GetCurrentRouterType() const { return m_currentRouterType; }
private:
/// \returns true if the route has warnings.
bool InsertRoute(routing::Route const & route);
struct RoadInfo
{
RoadInfo() = default;
explicit RoadInfo(m2::PointD const & pt, FeatureID const & featureId)
: m_startPoint(pt)
, m_featureId(featureId)
{}
m2::PointD m_startPoint;
FeatureID m_featureId;
double m_distance = 0.0;
};
using RoadWarningsCollection = std::map<routing::RoutingOptions::Road, std::vector<RoadInfo>>;
using GetMwmIdFn = std::function<MwmSet::MwmId (routing::NumMwmId numMwmId)>;
void CollectRoadWarnings(std::vector<routing::RouteSegment> const & segments, m2::PointD const & startPt,
double baseDistance, GetMwmIdFn const & getMwmIdFn, RoadWarningsCollection & roadWarnings);
void CreateRoadWarningMarks(RoadWarningsCollection && roadWarnings);
bool IsTrackingReporterEnabled() const;
void MatchLocationToRoute(location::GpsInfo & info,
location::RouteMatchingInfo & routeMatchingInfo) const;
location::RouteMatchingInfo GetRouteMatchingInfo(location::GpsInfo & info);
uint32_t GenerateRoutePointsTransactionId() const;
void SetPointsFollowingMode(bool enabled);
void ReorderIntermediatePoints();
m2::RectD ShowPreviewSegments(std::vector<RouteMarkData> const & routePoints);
void HidePreviewSegments();
void SetSubroutesVisibility(bool visible);
void CancelRecommendation(Recommendation recommendation);
std::vector<RouteMarkData> GetRoutePointsToSave() const;
void OnExtrapolatedLocationUpdate(location::GpsInfo const & info);
RouteBuildingCallback m_routingBuildingCallback;
RouteRecommendCallback m_routeRecommendCallback;
RouteStartBuildCallback m_routingStartBuildCallback;
Callbacks m_callbacks;
df::DrapeEngineSafePtr m_drapeEngine;
routing::RouterType m_currentRouterType = routing::RouterType::Count;
bool m_loadAltitudes = false;
routing::RoutingSession m_routingSession;
Delegate & m_delegate;
tracking::Reporter m_trackingReporter;
BookmarkManager * m_bmManager = nullptr;
extrapolation::Extrapolator m_extrapolator;
std::vector<dp::DrapeID> m_drapeSubroutes;
mutable std::mutex m_drapeSubroutesMutex;
std::unique_ptr<location::GpsInfo> m_gpsInfoCache;
TransitRouteInfo m_transitRouteInfo;
struct RoutePointsTransaction
{
std::vector<RouteMarkData> m_routeMarks;
};
std::map<uint32_t, RoutePointsTransaction> m_routePointsTransactions;
std::chrono::steady_clock::time_point m_loadRoutePointsTimestamp;
std::map<std::string, m2::PointF> m_transitSymbolSizes;
TransitReadManager * m_transitReadManager = nullptr;
DECLARE_THREAD_CHECKER(m_threadChecker);
};
|