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.");
59 /// Finalizes an instance of the Vibrator class.
66 /// Gets the number of the available vibrators.
68 /// <since_tizen> 3 </since_tizen>
69 public static int NumberOfVibrators
74 DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
75 if (res != DeviceError.None)
77 Log.Warn(DeviceExceptionFactory.LogTag, "unable to get Vibrators count.");
83 /// Gets all the available vibrators.
85 /// <since_tizen> 3 </since_tizen>
86 public static IReadOnlyList<Vibrator> Vibrators
90 _lazyVibrators = new Lazy<IReadOnlyList<Vibrator>>(() => GetAllVibrators());
91 return _lazyVibrators.Value;
94 private static IReadOnlyList<Vibrator> GetAllVibrators()
97 List<Vibrator> vibrators = new List<Vibrator>();
98 DeviceError res = (DeviceError)Interop.Device.DeviceHapticGetCount(out count);
99 if (res != DeviceError.None)
101 throw DeviceExceptionFactory.CreateException(res, "unable to get Vibrators count.");
103 for (int i = 0; i < NumberOfVibrators; i++)
105 vibrators.Add(new Vibrator(i));
111 /// Vibrates during the specified time with a constant intensity.
112 /// This function can be used to start monotonous vibration for the specified time.
114 /// <since_tizen> 3 </since_tizen>
115 /// <param name="duration">The play duration in milliseconds.</param>
116 /// <param name="feedback">The amount of the intensity variation (0 ~ 100).</param>
117 /// <exception cref="ArgumentException"> When an invalid parameter value is set.</exception>
118 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
119 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
120 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
122 /// Vibrator vibrator = Vibrator.Vibrators[0];
125 /// vibrator.Vibrate(2000, 70);
127 /// Catch(Exception e)
132 public void Vibrate(int duration, int feedback)
135 DeviceError res = DeviceError.None;
136 if (_hapticHandle == IntPtr.Zero)
138 res = (DeviceError)Interop.Device.DeviceHapticOpen(_vibratorId, out _hapticHandle);
139 if (res != DeviceError.None)
141 throw DeviceExceptionFactory.CreateException(res, "unable to get vibrator.");
145 res = (DeviceError)Interop.Device.DeviceHapticVibrate(_hapticHandle, duration, feedback, out effect);
146 if (res != DeviceError.None)
148 throw DeviceExceptionFactory.CreateException(res, "unable to vibrate the current vibrator.");
152 /// Stops all the vibration effects which are being played.
153 /// This function can be used to stop all the effects started by Vibrate().
155 /// <since_tizen> 3 </since_tizen>
156 /// <exception cref="ArgumentException"> In case an invalid vibrator instance is used.</exception>
157 /// <exception cref="UnauthorizedAccessException">If the privilege is not set.</exception>
158 /// <exception cref="InvalidOperationException">In case of any system error.</exception>
159 /// <exception cref="NotSupportedException">In case the device does not support this behavior.</exception>
161 /// Vibrator vibrator = Vibrator.Vibrators[0];
166 /// Catch(Exception e)
172 if (_hapticHandle != IntPtr.Zero)
174 DeviceError res = (DeviceError)Interop.Device.DeviceHapticStop(_hapticHandle, IntPtr.Zero);
175 if (res != DeviceError.None)
177 throw DeviceExceptionFactory.CreateException(res, "unable to stop the current vibrator.");
182 /// Dispose API for closing the internal resources.
183 /// This function can be used to stop all the effects started by Vibrate().
185 /// <since_tizen> 3 </since_tizen>
186 public void Dispose()
189 GC.SuppressFinalize(this);
193 /// Dispose API for closing the internal resources.
194 /// This function can be used to stop all the effects started by Vibrate().
196 /// <since_tizen> 3 </since_tizen>
197 protected virtual void Dispose(bool disposing)
201 if (_hapticHandle != IntPtr.Zero)
203 Interop.Device.DeviceHapticClose(_hapticHandle);
204 _hapticHandle = IntPtr.Zero;
206 _disposedValue = true;