Deprecating Tizen.Maps APIs (#5108)
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / MapServiceRequest.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     /// Base class for a map service request.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     /// <typeparam name="T"></typeparam>
28     [Obsolete("Deprecated since API11. Might be removed in API13.")]
29     public abstract class MapServiceRequest<T> : IDisposable
30     {
31         internal TaskCompletionSource<IEnumerable<T>> _requestTask;
32         internal string errMessage;
33         internal int? _requestID;
34         internal ServiceRequestType _type;
35
36         internal Action startExecutionAction;
37         internal Interop.ErrorCode errorCode;
38
39         internal MapService _service;
40
41         /// <summary>
42         /// Creates a map service request.
43         /// </summary>
44         /// <param name="service">Map service object.</param>
45         /// <param name="type">Request type.</param>
46         internal MapServiceRequest(MapService service, ServiceRequestType type)
47         {
48             _service = service;
49             _type = type;
50         }
51
52         /// <summary>
53         /// Destroy the MapServiceRequest object.
54         /// </summary>
55         ~MapServiceRequest()
56         {
57             Dispose(false);
58         }
59
60         /// <summary>
61         /// Sends a request to the map service provider.
62         /// </summary>
63         /// <since_tizen> 3 </since_tizen>
64         /// <returns>Response from the map service provider.</returns>
65         /// <privilege>http://tizen.org/privilege/mapservice</privilege>
66         /// <privilege>http://tizen.org/privilege/internet</privilege>
67         /// <privilege>http://tizen.org/privilege/network.get</privilege>
68         /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
69         /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
70         /// <exception cref="System.InvalidOperationException">Thrown when the result is invalid.</exception>
71         /// <exception cref="System.ArgumentException">Thrown when arguments are invalid.</exception>
72         [Obsolete("Deprecated since API11. Might be removed in API13.")]
73         public async Task<IEnumerable<T>> GetResponseAsync()
74         {
75             IEnumerable<T> task = null;
76             if (_requestTask == null || _requestTask.Task.IsCanceled)
77             {
78                 _requestTask = new TaskCompletionSource<IEnumerable<T>>();
79                 startExecutionAction();
80                 task = await _requestTask.Task.ConfigureAwait(false);
81             }
82             errorCode.WarnIfFailed(errMessage);
83             return task;
84         }
85
86         internal void Cancel()
87         {
88             if (_requestTask?.Task.IsCompleted == false)
89             {
90                 _requestTask?.SetCanceled();
91                 if (_requestID != null)
92                 {
93                     var err = Interop.CancelRequest(_service.handle, (int)_requestID);
94                     err.ThrowIfFailed($"Unable to cancel service request, Type: {_type}, ID: {_requestID}");
95                 }
96
97                 errorCode = Interop.ErrorCode.Canceled;
98             }
99         }
100
101         #region IDisposable Support
102         private bool _disposedValue = false;
103
104         /// <summary>
105         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
106         /// </summary>
107         /// <param name="disposing">If true, managed and unmanaged resources can be disposed, otherwise only unmanaged resources can be disposed.</param>
108         /// <since_tizen> 3 </since_tizen>
109         [Obsolete("Deprecated since API11. Might be removed in API13.")]
110         protected virtual void Dispose(bool disposing)
111         {
112             if (!_disposedValue)
113             {
114                 if (disposing)
115                 {
116                     Cancel();
117                 }
118                 _disposedValue = true;
119             }
120         }
121
122         /// <summary>
123         /// Releases all the resources used by this object.
124         /// </summary>
125         /// <since_tizen> 3 </since_tizen>
126         [Obsolete("Deprecated since API11. Might be removed in API13.")]
127         public void Dispose()
128         {
129             Dispose(true);
130             GC.SuppressFinalize(this);
131         }
132         #endregion
133     }
134 }