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 /// ProximitySensor Class. Used for registering callbacks for proximity sensor and getting proximity data
24 public sealed class ProximitySensor : Sensor
26 private static string ProximitySensorKey = "http://tizen.org/feature/sensor.proximity";
29 /// Gets the proximity state.
31 /// <since_tizen> 3 </since_tizen>
32 /// <value> Proximity state </value>
33 public ProximitySensorState Proximity { get; private set; } = ProximitySensorState.Unknown;
36 /// Returns true or false based on whether proximity sensor 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 ProximitySensor is supported");
45 return CheckIfSupported(SensorType.ProximitySensor, ProximitySensorKey);
50 /// Returns the number of proximity sensors available on the device.
52 /// <since_tizen> 3 </since_tizen>
53 /// <value> The count of proximity sensors </value>
54 public static int Count
58 Log.Info(Globals.LogTag, "Getting the count of proximity sensors");
64 /// Initializes a new instance of the <see cref="Tizen.Sensor.ProximitySensor"/> class.
66 /// <since_tizen> 3 </since_tizen>
67 /// <feature>http://tizen.org/feature/sensor.proximity</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 proximity sensor in case of multiple sensors
74 public ProximitySensor(uint index = 0) : base(index)
76 Log.Info(Globals.LogTag, "Creating ProximitySensor object");
79 internal override SensorType GetSensorType()
81 return SensorType.ProximitySensor;
85 /// Event Handler for storing the callback functions for event corresponding to change in proximity sensor data.
87 /// <since_tizen> 3 </since_tizen>
89 public event EventHandler<ProximitySensorDataUpdatedEventArgs> DataUpdated;
91 private static int GetCount()
95 int error = Interop.SensorManager.GetSensorList(SensorType.ProximitySensor, out list, out count);
96 if (error != (int)SensorError.None)
98 Log.Error(Globals.LogTag, "Error getting sensor list for proximity");
102 Interop.Libc.Free(list);
106 private static Interop.SensorListener.SensorEventCallback _callback;
108 internal override void EventListenStart()
110 _callback = (IntPtr sensorHandle, IntPtr eventPtr, IntPtr data) => {
111 Interop.SensorEventStruct sensorData = Interop.IntPtrToEventStruct(eventPtr);
113 TimeSpan = new TimeSpan((Int64)sensorData.timestamp);
114 Proximity = (ProximitySensorState) sensorData.values[0];
116 DataUpdated?.Invoke(this, new ProximitySensorDataUpdatedEventArgs(sensorData.values[0]));
119 int error = Interop.SensorListener.SetEventCallback(ListenerHandle, Interval, _callback, IntPtr.Zero);
120 if (error != (int)SensorError.None)
122 Log.Error(Globals.LogTag, "Error setting event callback for proximity sensor");
123 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to set event callback for proximity");
127 internal override void EventListenStop()
129 int error = Interop.SensorListener.UnsetEventCallback(ListenerHandle);
130 if (error != (int)SensorError.None)
132 Log.Error(Globals.LogTag, "Error unsetting event callback for proximity sensor");
133 throw SensorErrorFactory.CheckAndThrowException(error, "Unable to unset event callback for proximity");