Merge "1.fix Dispose 2.move proximity to GeofenceManager 3.seperate FenceStatus class...
[platform/core/csapi/geofence.git] / Tizen.Location.Geofence / Tizen.Location.Geofence / Fence.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
19 namespace Tizen.Location.Geofence
20 {
21     /// <summary>
22     /// Geo-fence defines a virtual perimeter for a real-world geographic area.
23     /// If you create a geofence, you can trigger some activities when a device enters(or exits) the geofences defined by you.
24     /// You can create a geofence with the information of Geopoint, Wi-Fi, or BT.
25     /// <list >
26     /// <item>Geopoint: Geofence is specified by coordinates (Latitude and Longitude) and Radius</item>
27     /// <item>WIFI: Geofence is specified by BSSID of Wi-Fi access point</item>
28     /// <item>BT: Geofence is specified by Bluetooth address</item>
29     /// </list>
30     /// Basic service set identification(BSSID) The BSSID is the MAC address of the wireless access point(WAP) generated by combining the 24 bit Organization Unique Identifier(the manufacturer's identity)
31     /// and the manufacturer's assigned 24-bit identifier for the radio chipset in the WAP.
32     /// </summary>
33     public class Fence : IDisposable
34     {
35         private bool _disposed = false;
36
37         internal IntPtr Handle
38         {
39             get;
40             set;
41         }
42
43         internal Fence(IntPtr handle)
44         {
45             Handle = handle;
46         }
47
48         ~Fence()
49         {
50             Dispose(false);
51         }
52         /// <summary>
53         /// Gets the type of geofence
54         /// </summary>
55         public FenceType Type
56         {
57             get
58             {
59                 FenceType val;
60                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceType(Handle, out val);
61                 if (ret != GeofenceError.None)
62                 {
63                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get GeofenceType");
64                 }
65
66                 return val;
67             }
68         }
69
70         /// <summary>
71         /// Gets the id of place.
72         /// </summary>
73         public int PlaceId
74         {
75             get
76             {
77                 int result = -1;
78                 GeofenceError ret = (GeofenceError)Interop.Geofence.FencePlaceID(Handle, out result);
79                 if (ret != GeofenceError.None)
80                 {
81                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get PlaceId");
82                 }
83
84                 return result;
85             }
86         }
87
88         /// <summary>
89         ///Gets the longitude of geofence.
90         /// </summary>
91         public double Longitude
92         {
93             get
94             {
95                 double result = -1;
96                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLongitude(Handle, out result);
97                 if (ret != GeofenceError.None)
98                 {
99                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Longitude");
100                 }
101
102                 return result;
103
104             }
105         }
106
107         /// <summary>
108         ///Gets the latitude of geofence.
109         /// </summary>
110         public double Latitude
111         {
112             get
113             {
114                 double result = -1;
115                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLatitude(Handle, out result);
116                 if (ret != GeofenceError.None)
117                 {
118                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Latitude");
119                 }
120
121                 return result;
122             }
123         }
124
125         /// <summary>
126         /// Gets the radius of geofence.
127         /// </summary>
128         public int Radius
129         {
130             get
131             {
132                 int result = -1;
133                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceRadius(Handle, out result);
134                 if (ret != GeofenceError.None)
135                 {
136                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Radius");
137                 }
138
139                 return result;
140             }
141         }
142
143         /// <summary>
144         /// Gets the address of geofence.
145         /// </summary>
146         public string Address
147         {
148             get
149             {
150                 string result = "";
151                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceAddress(Handle, out result);
152                 if (ret != GeofenceError.None)
153                 {
154                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Adress");
155                 }
156
157                 return result;
158             }
159         }
160
161         /// <summary>
162         /// Gets the bssid of geofence.
163         /// </summary>
164         public string Bssid
165         {
166             get
167             {
168                 string result = "";
169                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceBSSID(Handle, out result);
170                 if (ret != GeofenceError.None)
171                 {
172                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Bssid");
173                 }
174
175                 return result;
176             }
177         }
178
179         /// <summary>
180         ///Gets the ssid of geofence.
181         /// </summary>
182         public string Ssid
183         {
184             get
185             {
186                 string result = "";
187                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceSSID(Handle, out result);
188                 if (ret != GeofenceError.None)
189                 {
190                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Ssid");
191                 }
192
193                 return result;
194             }
195         }
196
197         /// <summary>
198         /// Creates a geopoint type of new geofence.
199         /// </summary>
200         /// <param name="placeId">The current place id</param>
201         /// <param name="latitude">Specifies the value of latitude of geofence [-90.0 ~ 90.0] (degrees) </param>
202         /// <param name="longitude">Specifies the value of longitude of geofence [-180.0 ~ 180.0] (degrees) </param>
203         /// <param name="radius">Specifies the value of radius of geofence [100 ~ 500](meter) </param>
204         /// <param name="adsress">Specifies the value of address</param>
205         /// <returns>Newly created geofence instance </returns>
206         /// <exception cref="ArgumentException">Incase of Invalid parameter</exception>
207         /// <exception cref="InvalidOperationException">Incase of any System error</exception>
208         /// <exception cref="UnauthorizedAccessException">Incase of Pvivileges are not defined</exception>
209         /// <exception cref="NotSupportedException">Incase of Geofence is not supported</exception>
210         public static Fence CreateGPSFence(int placeId, int latitude, int longitude, int radius, string address)
211         {
212             IntPtr handle = IntPtr.Zero;
213             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateGPSFence(placeId, latitude, longitude, radius,address, out handle);
214             if (ret != GeofenceError.None)
215             {
216                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from GPS Data for " + placeId);
217             }
218
219             return new Fence(handle);
220         }
221
222         /// <summary>
223         /// Creates a Wi-Fi type of new geofence.
224         /// </summary>
225         /// <param name="placeId">The current place id </param>
226         /// <param name="bssid">Specifies the value of BSSID of Wi-Fi MAC address</param>
227         /// <param name="ssid"> Specifies the value of SSID of Wi-Fi Device </param>
228         /// <returns>Newly created geofence instance </returns>
229         /// <exception cref="ArgumentException">Incase of Invalid parameter</exception>
230         /// <exception cref="InvalidOperationException">Incase of any System error</exception>
231         /// <exception cref="UnauthorizedAccessException">Incase of Pvivileges are not defined</exception>
232         /// <exception cref="NotSupportedException">Incase of Geofence is not supported</exception>
233         public static Fence CreateWifiFence(int placeId, string bssid, string ssid)
234         {
235             IntPtr handle = IntPtr.Zero;
236             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateWiFiFence(placeId, bssid, ssid, out handle);
237             if (ret != GeofenceError.None)
238             {
239                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Wifi Data for " + placeId);
240             }
241
242             return new Fence(handle);
243         }
244
245         /// <summary>
246         /// Creates a bluetooth type of new geofence.
247         /// </summary>
248         /// <param name="placeId">The current place id </param>
249         /// <param name="bssid">Specifies the value of BSSID of BT MAC address</param>
250         /// <param name="ssid"> Specifies the value of SSID of BT Device </param>
251         /// <returns>Newly created geofence instance </returns>
252         /// <exception cref="ArgumentException">Incase of Invalid parameter</exception>
253         /// <exception cref="InvalidOperationException">Incase of any System error</exception>
254         /// <exception cref="UnauthorizedAccessException">Incase of Pvivileges are not defined</exception>
255         /// <exception cref="NotSupportedException">Incase of Geofence is not supported</exception>
256         public static Fence CreateBTFence(int placeId, string bssid, string ssid)
257         {
258             IntPtr handle = IntPtr.Zero;
259             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateBTFence(placeId, bssid, ssid, out handle);
260             if (ret != GeofenceError.None)
261             {
262                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Bluetooth Data for " + placeId);
263             }
264
265             return new Fence(handle);
266         }
267
268         /// <summary>
269         /// Overloaded Dispose API for destroying the fence Handle.
270         /// </summary>
271         public void Dispose()
272         {
273             Dispose(true);
274             GC.SuppressFinalize(this);
275         }
276
277         private void Dispose(bool disposing)
278         {
279             if (_disposed)
280                 return;
281
282             if (Handle != IntPtr.Zero)
283             {
284                 Interop.Geofence.Destroy(Handle);
285                 Handle = IntPtr.Zero;
286             }
287
288             _disposed = true;
289         }
290     }
291 }