Deprecating Tizen.Maps APIs (#5108)
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / RouteSearchRequest.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20
21 namespace Tizen.Maps
22 {
23     /// <summary>
24     /// Routes the search request for Tizen map service requests.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     [Obsolete("Deprecated since API11. Might be removed in API13.")]
28     public class RouteSearchRequest : MapServiceRequest<Route>
29     {
30         private Interop.SearchRouteCallback _routeCallback;
31         private List<Route> _routeList = new List<Route>();
32
33         private Geocoordinates _to;
34         private Geocoordinates _from;
35         private List<Geocoordinates> _waypoints = new List<Geocoordinates>();
36
37         internal RouteSearchRequest(MapService service, Geocoordinates from, Geocoordinates to) : this(service, ServiceRequestType.SearchRoute)
38         {
39             _to = to;
40             _from = from;
41             startExecutionAction = new Action(() =>
42             {
43                 int requestID;
44                 errMessage = $"Failed to get route list for given origin {_from} and destination {_to}";
45                 if (_waypoints?.Count == 0)
46                 {
47                     _type = ServiceRequestType.SearchRoute;
48                     errorCode = _service.handle.SearchRoute(_from.handle, _to.handle, _service.Preferences.handle, _routeCallback, IntPtr.Zero, out requestID);
49                     if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
50                     {
51                         _requestTask?.TrySetException(errorCode.GetException(errMessage));
52                     }
53                 }
54                 else
55                 {
56                     _type = ServiceRequestType.SearchRouteWithWaypoints;
57
58                     var waypoints = GetCoordinateListForWaypoints();
59                     errorCode = _service.handle.SearchRouteWaypoints(waypoints, waypoints.Length, _service.Preferences.handle, _routeCallback, IntPtr.Zero, out requestID);
60                     if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
61                     {
62                         _requestTask?.TrySetException(errorCode.GetException(errMessage));
63                     }
64                 }
65                 _requestID = requestID;
66             });
67         }
68
69
70         private RouteSearchRequest(MapService service, ServiceRequestType type) : base(service, type)
71         {
72             // The Maps Service invokes this callback while iterating through the set of obtained Routes.
73             _routeCallback = (result, id, index, total, route, userData) =>
74             {
75                 errorCode = result;
76                 if (result.IsSuccess())
77                 {
78                     // The parameter route must be released using maps_route_destroy().
79                     var routeHandle = new Interop.RouteHandle(route, needToRelease: true);
80                     _routeList.Add(new Route(routeHandle));
81                     if (_routeList.Count == total)
82                     {
83                         _requestTask?.TrySetResult(_routeList);
84                     }
85                     return true;
86                 }
87                 else
88                 {
89                     // If search is failed, the value of total is 0 and route is NULL.
90                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
91                     return false;
92                 }
93             };
94         }
95
96         /// <summary>
97         /// Gets or sets a list of way-points to cover between the origin and destination.
98         /// </summary>
99         /// <since_tizen> 3 </since_tizen>
100         [Obsolete("Deprecated since API11. Might be removed in API13.")]
101         public IEnumerable<Geocoordinates> Waypoints
102         {
103             get
104             {
105                 return _waypoints;
106             }
107             set
108             {
109                 _waypoints = value.ToList();
110             }
111         }
112
113         private IntPtr[] GetCoordinateListForWaypoints()
114         {
115             var waypoints = new IntPtr[_waypoints.Count + 2];
116             waypoints[0] = _from.handle;
117             for (int i = 0; i < _waypoints.Count; ++i)
118             {
119                 waypoints[i + 1] = _waypoints[i].handle;
120             }
121             waypoints[waypoints.Length - 1] = _to.handle;
122             return waypoints;
123         }
124     }
125 }