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.
18 using System.Collections.Generic;
19 namespace Tizen.System
22 /// The Vibrator class provides the properties and methods to control a vibrator.
25 /// The Vibrator API provides the way to access the vibrators in the device.
26 /// It allows the management of the device's vibrator parameters, such as the vibration count and level.
27 /// It provides the methods to vibrate and stop the vibration.
30 /// http://tizen.org/privilege/haptic
33 /// Console.WriteLine("Total number of Vibrators are: {0}", Tizen.System.Vibrator.NumberOfVibrators);
35 public class Vibrator : IDisposable
37 private readonly int _vibratorId;
38 private IntPtr _hapticHandle = IntPtr.Zero;
39 private bool _disposedValue = false;
40 private static Lazy<IReadOnlyList<Vibrator>> _lazyVibrators;
41 private static int _maxcount = 0;
42 private Vibrator(int id)
45 DeviceError res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
46 if (res != DeviceError.None)
48 throw DeviceExceptionFactory.CreateException(res, "unable to create Vibrator for given Id");
50 res = (DeviceError)Interop.Device.DeviceHapticGetCount(out _maxcount);
51 if (res != DeviceError.None)
53 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
63 /// Gets the number of the available vibrators.
65 /// <since_tizen> 3 </since_tizen>
66 public static int NumberOfVibrators
71 DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
72 if (res != DeviceError.None)
74 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
80 /// Gets all the available vibrators.
82 /// <since_tizen> 3 </since_tizen>
83 public static IReadOnlyList<Vibrator> Vibrators
87 _lazyVibrators = new Lazy<IReadOnlyList<Vibrator>>(() => GetAllVibrators());
88 return _lazyVibrators.Value;
91 private static IReadOnlyList<Vibrator> GetAllVibrators()
94 List<Vibrator> vibrators = new List<Vibrator>();
95 DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
96 if (res != DeviceError.None)
98 throw DeviceExceptionFactory.CreateException(res, "unable to get Vibrators count.");
100 for (int i = 0; i < NumberOfVibrators; i++)
102 vibrators.Add(new Vibrator(i));
108 /// Vibrates during the specified time with a constant intensity.
109 /// This function can be used to start monotonous vibration for the specified time.
111 /// <since_tizen> 3 </since_tizen>
112 /// <param name="duration">The play duration in milliseconds.</param>
113 /// <param name="feedback">The amount of the intensity variation (0 ~ 100).</param>
114 /// <exception cref="ArgumentException"> When an invalid parameter value is set.</exception>
115 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
116 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
117 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
119 /// Vibrator vibrator = Vibrator.Vibrators[0];
122 /// vibrator.Vibrate(2000, 70);
124 /// Catch(Exception e)
129 public void Vibrate(int duration, int feedback)
132 DeviceError res = DeviceError.None;
133 if (_hapticHandle == IntPtr.Zero)
135 res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
136 if (res != DeviceError.None)
138 throw DeviceExceptionFactory.CreateException(res, "unable to get vibrator.");
142 res = (DeviceError)Interop.Device.DeviceHapticVibrate(_hapticHandle, duration, feedback, out effect);
143 if (res != DeviceError.None)
145 throw DeviceExceptionFactory.CreateException(res, "unable to vibrate the current vibrator.");
149 /// Stops all the vibration effects which are being played.
150 /// This function can be used to stop all the effects started by Vibrate().
152 /// <since_tizen> 3 </since_tizen>
153 /// <exception cref="ArgumentException"> In case an invalid vibrator instance is used.</exception>
154 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
155 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
156 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
158 /// Vibrator vibrator = Vibrator.Vibrators[0];
163 /// Catch(Exception e)
169 if (_hapticHandle != IntPtr.Zero)
171 DeviceError res = (DeviceError)Interop.Device.DeviceHapticStop(_hapticHandle, IntPtr.Zero);
172 if (res != DeviceError.None)
174 throw DeviceExceptionFactory.CreateException(res, "unable to stop the current vibrator.");
179 /// Dispose API for closing the internal resources.
180 /// This function can be used to stop all the effects started by Vibrate().
182 /// <since_tizen> 3 </since_tizen>
183 public void Dispose()
186 GC.SuppressFinalize(this);
189 protected virtual void Dispose(bool disposing)
193 if (_hapticHandle != IntPtr.Zero)
195 Interop.Device.DeviceHapticClose(_hapticHandle);
196 _hapticHandle = IntPtr.Zero;
198 _disposedValue = true;