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