[Sensor] Add new batched type sensor (#1522)
[platform/core/csapi/tizenfx.git] / src / Tizen.Sensor / Tizen.Sensor / Plugins / PickUpGestureDetector.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.Sensor
20 {
21     /// <summary>
22     /// The PickUpGestureDetector class is used for registering callbacks for the pick up activity detector and getting the pick up state.
23     /// </summary>
24     /// <since_tizen> 3 </since_tizen>
25     public sealed class PickUpGestureDetector : Sensor
26     {
27         private static string GestureDetectorKey = "http://tizen.org/feature/sensor.gesture_recognition";
28
29         /// <summary>
30         /// Gets the state of the pick up gesture.
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         /// <value> The pick up state. </value>
34         public DetectorState PickUp { get; private set; } = DetectorState.Unknown;
35
36         /// <summary>
37         /// Returns true or false based on whether the pick up gesture detector is supported by the device.
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         /// <value><c>true</c> if supported; otherwise <c>false</c>.</value>
41         public static bool IsSupported
42         {
43             get
44             {
45                 Log.Info(Globals.LogTag, "Checking if the pick up gesture detector is supported");
46                 return CheckIfSupported(SensorType.PickUpGestureDetector, GestureDetectorKey);
47             }
48         }
49
50         /// <summary>
51         /// Returns the number of pick up gesture detectors available on the device.
52         /// </summary>
53         /// <since_tizen> 3 </since_tizen>
54         /// <value> The count of pick up gesture detectors. </value>
55         public static int Count
56         {
57             get
58             {
59                 Log.Info(Globals.LogTag, "Getting the count of pick up gesture detectors");
60                 return GetCount();
61             }
62         }
63
64         /// <summary>
65         /// Initializes a new instance of the <see cref="Tizen.Sensor.PickUpGestureDetector"/> class.
66         /// </summary>
67         /// <since_tizen> 3 </since_tizen>
68         /// <feature>http://tizen.org/feature/sensor.gesture_recognition</feature>
69         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
70         /// <exception cref="NotSupportedException">Thrown when the sensor is not supported.</exception>
71         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
72         /// <param name='index'>
73         /// Index. Default value for this is 0. Index refers to a particular pick up gesture detector in case of multiple sensors.
74         /// </param>
75         public PickUpGestureDetector(uint index = 0) : base(index)
76         {
77             Log.Info(Globals.LogTag, "Creating pick up gesture detector object");
78         }
79
80         internal override SensorType GetSensorType()
81         {
82             return SensorType.PickUpGestureDetector;
83         }
84
85         private static bool CheckIfSupported()
86         {
87             bool isSupported;
88             int error = Interop.SensorManager.SensorIsSupported(SensorType.PickUpGestureDetector, out isSupported);
89             if (error != (int)SensorError.None)
90             {
91                 Log.Error(Globals.LogTag, "Error checking if pick up gesture detector is supported");
92                 isSupported = false;
93             }
94             return isSupported;
95         }
96
97         private static int GetCount()
98         {
99             IntPtr list;
100             int count;
101             int error = Interop.SensorManager.GetSensorList(SensorType.PickUpGestureDetector, out list, out count);
102             if (error != (int)SensorError.None)
103             {
104                 Log.Error(Globals.LogTag, "Error getting sensor list for pick up gesture detector");
105                 count = 0;
106             }
107             else
108                 Interop.Libc.Free(list);
109             return count;
110         }
111
112         /// <summary>
113         /// Read pick up gesture detector data synchronously.
114         /// </summary>
115         internal override void ReadData()
116         {
117             Interop.SensorEventStruct sensorData;
118             int error = Interop.SensorListener.ReadData(ListenerHandle, out sensorData);
119             if (error != (int)SensorError.None)
120             {
121                 Log.Error(Globals.LogTag, "Error reading pick up gesture detector data");
122                 throw SensorErrorFactory.CheckAndThrowException(error, "Reading pick up gesture detector data failed");
123             }
124
125             Timestamp = sensorData.timestamp;
126             PickUp = (DetectorState)sensorData.values[0];
127         }
128
129         /// <summary>
130         /// An event handler for storing the callback functions for the event corresponding to the change in the pick up gesture detector data. 
131         /// </summary>
132         /// <since_tizen> 3 </since_tizen>
133         public event EventHandler<PickUpGestureDetectorDataUpdatedEventArgs> DataUpdated;
134
135         private static Interop.SensorListener.SensorEventsCallback _callback;
136
137         internal override void EventListenStart()
138         {
139             _callback = (IntPtr sensorHandle, IntPtr eventPtr, uint events_count, IntPtr data) => {
140                 updateBatchEvents(eventPtr, events_count);
141                 Interop.SensorEventStruct sensorData = latestEvent();
142
143                 Timestamp = sensorData.timestamp;
144                 PickUp = (DetectorState) sensorData.values[0];
145
146                 DataUpdated?.Invoke(this, new PickUpGestureDetectorDataUpdatedEventArgs(sensorData.values[0]));
147             };
148
149             int error = Interop.SensorListener.SetEventsCallback(ListenerHandle, _callback, IntPtr.Zero);
150             if (error != (int)SensorError.None)
151             {
152                 Log.Error(Globals.LogTag, "Error setting event callback for pick up gesture detector");
153                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for pick up gesture detector");
154             }
155         }
156
157         internal override void EventListenStop()
158         {
159             int error = Interop.SensorListener.UnsetEventsCallback(ListenerHandle);
160             if (error != (int)SensorError.None)
161             {
162                 Log.Error(Globals.LogTag, "Error unsetting event callback for pick up gesture detector");
163                 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for pick up gesture detector");
164             }
165         }
166     }
167 }