2 * Copyright (c) 2018 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;
21 namespace Tizen.System
24 /// The Feedback API provides functions to control haptic and sound.
25 /// The Feedback API provides the way to play and stop feedback, and get the information whether a specific pattern is supported.
26 /// Below is the supported pattern string:
43 /// HardwareKeyPressed
59 /// VibrationModeAbled
60 /// SilentModeDisabled
61 /// BluetoothDeviceConnected
62 /// BluetoothDeviceDisconnected
68 /// For controlling the haptic device:
69 /// http://tizen.org/privilege/haptic
70 /// For controlling the sound, privilege is not needed.
74 /// Feedback feedback = new Feedback();
75 /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
78 /// <since_tizen> 3 </since_tizen>
81 private const string LogTag = "Tizen.System.Feedback";
83 private readonly Dictionary<string, int> Pattern = new Dictionary<string, int>
86 {"SoftInputPanel", 1},
101 {"HardwareKeyPressed", 21},
102 {"HardwareKeyHold", 22},
111 {"ChargerConnected", 38},
112 {"ChargingError", 40},
113 {"FullyCharged", 42},
117 {"VibrationModeAbled", 55},
118 {"SilentModeDisabled", 56},
119 {"BluetoothDeviceConnected", 57},
120 {"BluetoothDeviceDisconnected", 58},
123 {"VolumeKeyPressed", 64},
127 /// Constructor of Feedback class
129 /// <since_tizen> 3 </since_tizen>
131 /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
133 /// <exception cref="NotSupportedException">Thrown when failed because the devices (vibration and sound) are not supported.</exception>
134 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
135 /// <privilege>http://tizen.org/privilege/haptic</privilege>
138 /// Feedback feedback = new Feedback();
143 Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Initialize();
144 if (res != Interop.Feedback.FeedbackError.None)
146 Log.Warn(LogTag, string.Format("Failed to initialize feedback. err = {0}", res));
149 case Interop.Feedback.FeedbackError.NotSupported:
150 throw new NotSupportedException("Device is not supported");
152 throw new InvalidOperationException("Failed to initialize");
158 /// Finalizes an instance of the Feedback class.
162 Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Deinitialize();
163 if (res != Interop.Feedback.FeedbackError.None)
165 Log.Warn(LogTag, string.Format("Failed to deinitialize feedback. err = {0}", res));
168 case Interop.Feedback.FeedbackError.NotInitialized:
169 throw new Exception("Not initialized");
171 throw new InvalidOperationException("Failed to initialize");
177 /// Gets the supported information about a specific type and pattern.
180 /// Now, IsSupportedPattern is not working for FeedbackType.All.
181 /// This API is working for FeedbackType.Sound and FeedbackType.Vibration only.
182 /// If you use FeedbackType.All for type parameter, this API will throw ArgumentException.
183 /// To get the supported information for Vibration type, the application should have http://tizen.org/privilege/haptic privilege.
185 /// <since_tizen> 3 </since_tizen>
186 /// <param name="type">The feedback type.</param>
187 /// <param name="pattern">The feedback pattern string.</param>
189 /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
191 /// <returns>Information whether a pattern is supported.</returns>
192 /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
193 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
194 /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
195 /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted (No privilege).</exception>
196 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
197 /// <privilege>http://tizen.org/privilege/haptic</privilege>
200 /// Feedback feedback = new Feedback();
201 /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
204 public bool IsSupportedPattern(FeedbackType type, String pattern)
206 bool supported = false;
208 Interop.Feedback.FeedbackError res;
210 if (!Pattern.TryGetValue(pattern, out number))
211 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
213 res = (Interop.Feedback.FeedbackError)Interop.Feedback.IsSupportedPattern((Interop.Feedback.FeedbackType)type, number, out supported);
215 if (res != Interop.Feedback.FeedbackError.None)
217 Log.Warn(LogTag, string.Format("Failed to get supported information. err = {0}", res));
220 case Interop.Feedback.FeedbackError.NotInitialized:
221 throw new Exception("Not initialized");
222 case Interop.Feedback.FeedbackError.InvalidParameter:
223 throw new ArgumentException("Invalid Arguments");
224 case Interop.Feedback.FeedbackError.NotSupported:
225 throw new NotSupportedException("Device is not supported");
226 case Interop.Feedback.FeedbackError.PermissionDenied:
227 throw new UnauthorizedAccessException("Access is not granted");
228 case Interop.Feedback.FeedbackError.OperationFailed:
230 throw new InvalidOperationException("Failed to get supported information");
237 /// Plays a specific feedback pattern.
240 /// To play Vibration type, app should have http://tizen.org/privilege/haptic privilege.
242 /// <since_tizen> 3 </since_tizen>
243 /// <param name="type">The feedback type.</param>
244 /// <param name="pattern">The feedback pattern string.</param>
246 /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
248 /// <exception cref="Exception">Thrown when failed because feedback is not initialized.</exception>
249 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
250 /// <exception cref="NotSupportedException">Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.</exception>
251 /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted(No privilege)</exception>
252 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
253 /// <privilege>http://tizen.org/privilege/haptic</privilege>
256 /// Feedback feedback = new Feedback();
257 /// feedback.Play(FeedbackType.All, "Tap");
260 public void Play(FeedbackType type, String pattern)
263 Interop.Feedback.FeedbackError res;
265 if (!Pattern.TryGetValue(pattern, out number))
266 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
268 if (type == FeedbackType.All)
269 res = (Interop.Feedback.FeedbackError)Interop.Feedback.Play(number);
271 res = (Interop.Feedback.FeedbackError)Interop.Feedback.PlayType((Interop.Feedback.FeedbackType)type, number);
273 if (res != Interop.Feedback.FeedbackError.None)
275 Log.Warn(LogTag, string.Format("Failed to play feedback. err = {0}", res));
278 case Interop.Feedback.FeedbackError.NotInitialized:
279 throw new Exception("Not initialized");
280 case Interop.Feedback.FeedbackError.InvalidParameter:
281 throw new ArgumentException("Invalid Arguments");
282 case Interop.Feedback.FeedbackError.NotSupported:
283 throw new NotSupportedException("Not supported");
284 case Interop.Feedback.FeedbackError.PermissionDenied:
285 throw new UnauthorizedAccessException("Access is not granted");
286 case Interop.Feedback.FeedbackError.OperationFailed:
288 throw new InvalidOperationException("Failed to play pattern");
294 /// Stops to play the feedback.
297 /// To stop vibration, the application should have http://tizen.org/privilege/haptic privilege.
299 /// <since_tizen> 3 </since_tizen>
301 /// http://tizen.org/feature/feedback.vibration
303 /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
304 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament</exception>
305 /// <exception cref="NotSupportedException">Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.</exception>
306 /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted (No privilege).</exception>
307 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
308 /// <privilege>http://tizen.org/privilege/haptic</privilege>
311 /// Feedback Feedback1 = new Feedback();
312 /// Feedback1.Stop();
317 Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Stop();
319 if (res != Interop.Feedback.FeedbackError.None)
321 Log.Warn(LogTag, string.Format("Failed to stop feedback. err = {0}", res));
324 case Interop.Feedback.FeedbackError.NotInitialized:
325 throw new Exception("Not initialized");
326 case Interop.Feedback.FeedbackError.InvalidParameter:
327 throw new ArgumentException("Invalid Arguments");
328 case Interop.Feedback.FeedbackError.NotSupported:
329 throw new NotSupportedException("Not supported");
330 case Interop.Feedback.FeedbackError.PermissionDenied:
331 throw new UnauthorizedAccessException("Access is not granted");
332 case Interop.Feedback.FeedbackError.OperationFailed:
334 throw new InvalidOperationException("Failed to stop pattern");