csapi-sensor: add descriptions for activity/gesture sensor class
[platform/core/csapi/sensor.git] / Tizen.System.Sensor / Tizen.System.Sensor / Plugins / WristUpGestureDetector.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10
11 namespace Tizen.System.Sensor
12 {
13     /// <summary>
14     /// WristUpGestureDetector Class. Used for registering callbacks for wrist up gesture detector and getting the wrist up state
15     /// </summary>
16     public class WristUpGestureDetector : Sensor
17     {
18         /// <summary>
19         /// Gets the state of the wrist up gesture.
20         /// </summary>
21         public DetectorState WristUp { get; private set; }
22
23         /// <summary>
24         /// Returns true or false based on whether wrist up gesture detector is supported by device.
25         /// </summary>
26         public static bool IsSupported
27         {
28             get
29             {
30                 Log.Info(Globals.LogTag, "Checking if the wrist up gesture detector is supported");
31                 return CheckIfSupported();
32             }
33         }
34
35         /// <summary>
36         /// Returns the number of wrist up gesture detector available on the device.
37         /// </summary>
38         public static int Count
39         {
40             get
41             {
42                 Log.Info(Globals.LogTag, "Getting the count of wrist up gesture detectors");
43                 return GetCount();
44             }
45         }
46
47         /// <summary>
48         /// Initializes a new instance of the <see cref="Tizen.System.Sensor.WristUpGestureDetector"/> class.
49         /// </summary>
50         /// <param name='index'>
51         /// Index. Default value for this is 0. Index refers to a particular wrist up gesture detector in case of multiple sensors.
52         /// </param>
53         public WristUpGestureDetector(int index = 0) : base(index)
54         {
55             Log.Info(Globals.LogTag, "Creating wrist up gesture detector object");
56         }
57
58         internal override SensorType GetSensorType()
59         {
60             return SensorType.WristUpGestureDetector;
61         }
62
63         private static bool CheckIfSupported()
64         {
65             bool isSupported;
66             int error = Interop.SensorManager.SensorIsSupported(SensorType.WristUpGestureDetector, out isSupported);
67             if (error != (int)SensorError.None)
68             {
69                 Log.Error(Globals.LogTag, "Error checking if wrist up gesture detector is supported");
70                 isSupported = false;
71             }
72             return isSupported;
73         }
74
75         private static int GetCount()
76         {
77             IntPtr list;
78             int count;
79             int error = Interop.SensorManager.GetSensorList(SensorType.WristUpGestureDetector, out list, out count);
80             if (error != (int)SensorError.None)
81             {
82                 Log.Error(Globals.LogTag, "Error getting sensor list for wrist up gesture detector");
83                 count = 0;
84             }
85             else
86                 Interop.Libc.Free(list);
87             return count;
88         }
89
90         /// <summary>
91         /// Event Handler for storing the callback functions for event corresponding to change in wrist up gesture detector data.
92         /// </summary>
93         public event EventHandler<WristUpGestureDetectorDataUpdatedEventArgs> DataUpdated;
94
95         protected override void EventListenStart()
96         {
97             int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, SensorEventCallback, IntPtr.Zero);
98             if (error != (int)SensorError.None)
99             {
100                 Log.Error(Globals.LogTag, "Error setting event callback for wrist up gesture detector");
101                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for wrist up gesture detector");
102             }
103         }
104
105         protected override void EventListenStop()
106         {
107             int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
108             if (error != (int)SensorError.None)
109             {
110                 Log.Error(Globals.LogTag, "Error unsetting event callback for wrist up gesture detector");
111                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for wrist up gesture detector");
112             }
113         }
114
115         private void SensorEventCallback(IntPtr sensorHandle, IntPtr sensorPtr, IntPtr data)
116         {
117             Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(sensorPtr);
118             TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
119             WristUp = (DetectorState) sensorData.values[0];
120
121             DataUpdated?.Invoke(this, new WristUpGestureDetectorDataUpdatedEventArgs(sensorData.values[0]));
122         }
123     }
124 }