2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 namespace Tizen.Sensor
22 /// PickUpGestureDetector Class. Used for registering callbacks for pick up activity detector and getting the pick up state
24 public sealed class PickUpGestureDetector : Sensor
26 private static string GestureDetectorKey = "http://tizen.org/feature/sensor.gesture_recognition";
29 /// Gets the state of the pick up gesture.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> Pick up state </value>
33 public DetectorState PickUp { get; private set; } = DetectorState.Unknown;
36 /// Returns true or false based on whether pick up gesture detector is supported by device.
38 /// <since_tizen> 3 </since_tizen>
39 /// <value><c>true</c> if supported; otherwise, <c>false</c>.</value>
40 public static bool IsSupported
44 Log.Info(Globals.LogTag, "Checking if the pick up gesture detector is supported");
45 return CheckIfSupported(SensorType.PickUpGestureDetector, GestureDetectorKey);
50 /// Returns the number of pick up gesture detectors available on the device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> The count of pick up gesture detectors </value>
54 public static int Count
58 Log.Info(Globals.LogTag, "Getting the count of pick up gesture detectors");
64 /// Initializes a new instance of the <see cref="Tizen.Sensor.PickUpGestureDetector"/> class.
66 /// <since_tizen> 3 </since_tizen>
67 /// <feature>http://tizen.org/feature/sensor.gesture_recognition</feature>
68 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
69 /// <exception cref="NotSupportedException">Thrown when the sensor is not supported</exception>
70 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
71 /// <param name='index'>
72 /// Index. Default value for this is 0. Index refers to a particular pick up gesture detector in case of multiple sensors.
74 public PickUpGestureDetector(uint index = 0) : base(index)
76 Log.Info(Globals.LogTag, "Creating pick up gesture detector object");
79 internal override SensorType GetSensorType()
81 return SensorType.PickUpGestureDetector;
84 private static bool CheckIfSupported()
87 int error = Interop.SensorManager.SensorIsSupported(SensorType.PickUpGestureDetector, out isSupported);
88 if (error != (int)SensorError.None)
90 Log.Error(Globals.LogTag, "Error checking if pick up gesture detector is supported");
96 private static int GetCount()
100 int error = Interop.SensorManager.GetSensorList(SensorType.PickUpGestureDetector, out list, out count);
101 if (error != (int)SensorError.None)
103 Log.Error(Globals.LogTag, "Error getting sensor list for pick up gesture detector");
107 Interop.Libc.Free(list);
112 /// Event Handler for storing the callback functions for event corresponding to change in pick up gesture detector data.
114 /// <since_tizen> 3 </since_tizen>
115 public event EventHandler<PickUpGestureDetectorDataUpdatedEventArgs> DataUpdated;
117 private static Interop.SensorListener.SensorEventCallback _callback;
119 internal override void EventListenStart()
121 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
122 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
124 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
125 PickUp = (DetectorState) sensorData.values[0];
127 DataUpdated?.Invoke(this, new PickUpGestureDetectorDataUpdatedEventArgs(sensorData.values[0]));
130 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
131 if (error != (int)SensorError.None)
133 Log.Error(Globals.LogTag, "Error setting event callback for pick up gesture detector");
134 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for pick up gesture detector");
138 internal override void EventListenStop()
140 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
141 if (error != (int)SensorError.None)
143 Log.Error(Globals.LogTag, "Error unsetting event callback for pick up gesture detector");
144 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for pick up gesture detector");