Modify documentation of APIs
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / MapService.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.Threading.Tasks;
20
21 namespace Tizen.Maps
22 {
23     /// <summary>
24     /// Map service class for service request
25     /// </summary>
26     public partial class MapService : IDisposable
27     {
28         internal Interop.ServiceHandle handle;
29
30         private PlaceFilter _filter;
31         private SearchPreference _searchPreference;
32
33         private static List<string> s_providers;
34         private string _serviceProvider;
35
36
37         /// <summary>
38         /// Creates a new Maps Service object for given service provider.
39         /// </summary>
40         /// <param name="serviceProvider">A string which representing name of map service provider</param>
41         /// <param name="serviceProviderKey">A string which representing certificate key to use the map service provider</param>
42         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
43         /// <privilege>http://tizen.org/privilege/network.get</privilege>
44         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
45         /// <exception cref="System.ArgumentException">Thrown when parameters are invalid.</exception>
46         /// <exception cref="System.InvalidOperationException">Thrown when native operation failed to allocate memory, connect to service.</exception>
47         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
48         public MapService(string serviceProvider, string serviceProviderKey)
49         {
50             _serviceProvider = serviceProvider;
51             handle = new Interop.ServiceHandle(serviceProvider);
52             ProviderKey = serviceProviderKey;
53             PlaceSearchFilter = new PlaceFilter();
54             Preferences = new SearchPreference();
55         }
56
57         /// <summary>
58         /// Gets list of available map service providers.
59         /// </summary>
60         /// <value>The list of map service providers.</value>
61         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
62         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
63         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have privileges to access this property.</exception>
64         public static IEnumerable<string> Providers
65         {
66             get
67             {
68                 if (s_providers != null) return s_providers;
69
70                 s_providers = new List<string>();
71                 Interop.ServiceHandle.ForeachProvider(provider => s_providers.Add(provider));
72                 return s_providers;
73             }
74         }
75
76         /// <summary>
77         /// Gets name of map service provider.
78         /// </summary>
79         public string Provider { get { return _serviceProvider; } }
80
81
82         /// <summary>
83         /// Gets and sets a string representing keys for map service provider
84         /// </summary>
85         /// <remark>Regaularly, the provider key is issued by each maps provider, after signing up for a plan in the web site.
86         /// Depending on the plan and its provider which you have signed, you might pay for the network traffic.</remark>
87         public string ProviderKey
88         {
89             get
90             {
91                 return handle.ProviderKey;
92             }
93             set
94             {
95                 handle.ProviderKey = value;
96             }
97         }
98
99         /// <summary>
100         /// Gets and sets a filter used for place search result.
101         /// </summary>
102         public PlaceFilter PlaceSearchFilter
103         {
104             get
105             {
106                 return _filter;
107             }
108             set
109             {
110                 if (value != null)
111                 {
112                     _filter = value;
113                 }
114             }
115         }
116
117         /// <summary>
118         /// Gets search preferences used for <see cref="Geocode"/> or <see cref="ReverseGeocode"/> request.
119         /// </summary>
120         public IGeocodePreference GeocodePreferences
121         {
122             get
123             {
124                 return Preferences as IGeocodePreference;
125             }
126         }
127
128         /// <summary>
129         /// Gets search preferences used for <see cref="Place"/> search request.
130         /// </summary>
131         public IPlaceSearchPreference PlaceSearchPreferences
132         {
133             get
134             {
135                 return Preferences as IPlaceSearchPreference;
136             }
137         }
138
139         /// <summary>
140         /// Gets search preferences used for <see cref="Route"/> search request.
141         /// </summary>
142         public IRouteSearchPreference RouteSearchPreferences
143         {
144             get
145             {
146                 return Preferences as IRouteSearchPreference;
147             }
148         }
149
150         /// <summary>
151         /// Gets and sets search preferences.
152         /// </summary>
153         public SearchPreference Preferences
154         {
155             get
156             {
157                 if (_searchPreference == null)
158                 {
159                     _searchPreference = new SearchPreference(handle.Preferences);
160                 }
161                 return _searchPreference;
162             }
163             set
164             {
165                 if (value != null)
166                 {
167                     handle.Preferences = value.handle;
168                     _searchPreference = value;
169                 }
170             }
171         }
172
173         /// <summary>
174         /// Gets the user's consent to use maps data.
175         /// </summary>
176         /// <param name="provider">A string which representing the name of maps provider</param>
177         /// <returns>Returns true if user agreed that the application can use maps data, otherwise false.</returns>
178         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
179         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
180         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
181         public static async Task<bool> RequestUserConsent(string provider)
182         {
183             TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
184             Interop.ServiceHandle.RequestUserConsentCallback cb = (consented, serviceProvider, userData) =>
185             {
186                 tcs.TrySetResult(consented);
187             };
188
189             var err = Interop.ServiceHandle.RequestUserConsent(provider, cb, IntPtr.Zero);
190             if (err.IsFailed())
191             {
192                 tcs.TrySetException(err.GetException("Failed to get user consent"));
193             }
194             return await tcs.Task.ConfigureAwait(false);
195         }
196
197         /// <summary>
198         /// Checks if the Maps Service supports given request.
199         /// </summary>
200         /// <param name="type">Request type to check</param>
201         /// <returns>Returns true if the Maps Service supports a request, otherwise false.</returns>
202         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
203         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
204         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
205         public bool IsSupported(ServiceRequestType type)
206         {
207             bool result;
208             var err = handle.IsServiceSupported((Interop.ServiceType)type, out result);
209             err.WarnIfFailed($"Failed to get if {type} is supported");
210             return (err.IsSuccess()) ? result : false;
211         }
212
213         /// <summary>
214         /// Checks if the Maps Service supports given data feature.
215         /// </summary>
216         /// <param name="data">Data feature to check</param>
217         /// <returns>Returns true if the Maps Service supports a data feature, otherwise false.</returns>
218         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
219         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
220         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
221         public bool IsSupported(ServiceData data)
222         {
223             bool result;
224             var err = handle.IsDataSupported((Interop.ServiceData)data, out result);
225             err.WarnIfFailed($"Failed to get if {data} data is supported");
226             return (err.IsSuccess()) ? result : false;
227         }
228
229         /// <summary>
230         /// Creates geocode search request for given free-formed address string.
231         /// </summary>
232         /// <param name="address">A string which representing free-formed address</param>
233         /// <returns>GeocodeRequest object created with address string</returns>
234         public GeocodeRequest CreateGeocodeRequest(string address)
235         {
236             return new GeocodeRequest(this, address);
237         }
238
239         /// <summary>
240         /// Creates geocode search request for given free-formed address string, within the specified boundary.
241         /// </summary>
242         /// <param name="address">A string which representing free-formed address</param>
243         /// <param name="boundary">An instance of Area object which representing interested area</param>
244         /// <seealso cref="Area">
245         /// <returns>GeocodeRequest object created with address string and specified boundary</returns>
246         public GeocodeRequest CreateGeocodeRequest(string address, Area boundary)
247         {
248             return new GeocodeRequest(this, address, boundary);
249         }
250
251         /// <summary>
252         /// Creates geocode search request for given structured address.
253         /// </summary>
254         /// <param name="address">A string which representing address of interest</param>
255         /// <returns>Returns GeocodeRequest object created with structured address</returns>
256         public GeocodeRequest CreateGeocodeRequest(PlaceAddress address)
257         {
258             return new GeocodeRequest(this, address);
259         }
260
261         /// <summary>
262         /// Creates a reverse geocode search request for given latitude and longitude.
263         /// </summary>
264         /// <param name="latitude">Latitude of interested place</param>
265         /// <param name="longitute">Longitude of interested place</param>
266         /// <returns>Returns ReverseGeocodeRequest object created with location coordinates</returns>
267         public ReverseGeocodeRequest CreateReverseGeocodeRequest(double latitude, double longitute)
268         {
269             return new ReverseGeocodeRequest(this, latitude, longitute);
270         }
271
272         /// <summary>
273         /// Creates a reverse geocode search request for given position coordinates list.
274         /// </summary>
275         /// <param name="coordinates">Coordinates list with [2 ~ 100] coordinates</param>
276         /// <returns>Returns MultiReverseGeocodeRequest object created with list of location coordinates</returns>
277         public MultiReverseGeocodeRequest CreateMultiReverseGeocodeRequest(IEnumerable<Geocoordinates> coordinates)
278         {
279             return new MultiReverseGeocodeRequest(this, coordinates);
280         }
281
282         /// <summary>
283         /// Creates a route search request for origin and destination points.
284         /// </summary>
285         /// <param name="from">Starting point</param>
286         /// <param name="to">Destination</param>
287         /// <returns>Returns RouteSearchRequest object created with origin and destination coordinates</returns>
288         public RouteSearchRequest CreateRouteSearchRequest(Geocoordinates from, Geocoordinates to)
289         {
290             return new RouteSearchRequest(this, from, to);
291         }
292
293         /// <summary>
294         /// Creates a place search request for specified search radius around a given coordinates position.
295         /// </summary>
296         /// <param name="coordinates">A geographical coordinates of center</param>
297         /// <param name="distance">A double value which representing radius of area to search places</param>
298         /// <returns>Returns PlaceSearchRequest object created with location coordinates and search radius</returns>
299         public PlaceSearchRequest CreatePlaceSearchRequest(Geocoordinates coordinates, int distance)
300         {
301             return new PlaceSearchRequest(this, coordinates, distance);
302         }
303
304         /// <summary>
305         /// Creates a place search request for places within specified boundary.
306         /// </summary>
307         /// <param name="boundary">An instance of Area object which representing area to search interested places</param>
308         /// <returns>Returns PlaceSearchRequest object created with specified boundary</returns>
309         public PlaceSearchRequest CreatePlaceSearchRequest(Area boundary)
310         {
311             return new PlaceSearchRequest(this, boundary);
312         }
313
314         /// <summary>
315         /// Creates a place search request for free-formed address within boundary.
316         /// </summary>
317         /// <param name="address">A string which representing free-formed address</param>
318         /// <param name="boundary">An instance of Area object which representing area to search interested places</param>
319         /// <returns>Returns PlaceSearchRequest object created with address string and specified boundary</returns>
320         public PlaceSearchRequest CreatePlaceSearchRequest(string address, Area boundary)
321         {
322             return new PlaceSearchRequest(this, address, boundary);
323         }
324
325         #region IDisposable Support
326         private bool _disposedValue = false;
327         protected virtual void Dispose(bool disposing)
328         {
329             if (!_disposedValue)
330             {
331                 if (disposing)
332                 {
333                     _filter.Dispose();
334                     _searchPreference.Dispose();
335                 }
336                 handle.Dispose();
337                 _disposedValue = true;
338             }
339         }
340
341         /// <summary>
342         /// Releases all resources used by this object.
343         /// </summary>
344         public void Dispose()
345         {
346             Dispose(true);
347             GC.SuppressFinalize(this);
348         }
349         #endregion
350     }
351 }