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