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;
19 using System.ComponentModel;
22 namespace Tizen.System
25 /// The Feedback API provides functions to control haptic and sound.
26 /// The Feedback API provides the way to play and stop feedback, and get the information whether a specific pattern is supported.
27 /// Below is the supported pattern string:
44 /// HardwareKeyPressed
60 /// VibrationModeAbled
61 /// SilentModeDisabled
62 /// BluetoothDeviceConnected
63 /// BluetoothDeviceDisconnected
69 /// For controlling the haptic device:
70 /// http://tizen.org/privilege/haptic
71 /// For controlling the sound, privilege is not needed.
75 /// Feedback feedback = new Feedback();
76 /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
79 /// <since_tizen> 3 </since_tizen>
82 private const string LogTag = "Tizen.System.Feedback";
84 private readonly Dictionary<string, int> Pattern = new Dictionary<string, int>
87 {"SoftInputPanel", 1},
102 {"HardwareKeyPressed", 21},
103 {"HardwareKeyHold", 22},
112 {"ChargerConnected", 38},
113 {"ChargingError", 40},
114 {"FullyCharged", 42},
118 {"VibrationModeAbled", 55},
119 {"SilentModeDisabled", 56},
120 {"BluetoothDeviceConnected", 57},
121 {"BluetoothDeviceDisconnected", 58},
124 {"VolumeKeyPressed", 64},
128 /// Constructor of Feedback class
130 /// <since_tizen> 3 </since_tizen>
132 /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
134 /// <exception cref="NotSupportedException">Thrown when failed because the devices (vibration and sound) are not supported.</exception>
135 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
136 /// <privilege>http://tizen.org/privilege/haptic</privilege>
139 /// Feedback feedback = new Feedback();
144 Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Initialize();
145 if (res != Interop.Feedback.FeedbackError.None)
147 Log.Warn(LogTag, string.Format("Failed to initialize feedback. err = {0}", res));
150 case Interop.Feedback.FeedbackError.NotSupported:
151 throw new NotSupportedException("Device is not supported");
153 throw new InvalidOperationException("Failed to initialize");
159 /// Finalizes an instance of the Feedback class.
163 Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Deinitialize();
164 if (res != Interop.Feedback.FeedbackError.None)
166 Log.Warn(LogTag, string.Format("Failed to deinitialize feedback. err = {0}", res));
171 /// Gets the supported information about a specific type and pattern.
174 /// Now, IsSupportedPattern is not working for FeedbackType.All.
175 /// This API is working for FeedbackType.Sound and FeedbackType.Vibration only.
176 /// If you use FeedbackType.All for type parameter, this API will throw ArgumentException.
177 /// To get the supported information for Vibration type, the application should have http://tizen.org/privilege/haptic privilege.
179 /// <since_tizen> 3 </since_tizen>
180 /// <param name="type">The feedback type.</param>
181 /// <param name="pattern">The feedback pattern string.</param>
183 /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
185 /// <returns>Information whether a pattern is supported.</returns>
186 /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
187 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
188 /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
189 /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted (No privilege).</exception>
190 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
191 /// <privilege>http://tizen.org/privilege/haptic</privilege>
194 /// Feedback feedback = new Feedback();
195 /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
198 public bool IsSupportedPattern(FeedbackType type, String pattern)
200 bool supported = false;
202 Interop.Feedback.FeedbackError res;
204 if (!Pattern.TryGetValue(pattern, out number))
205 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
207 res = (Interop.Feedback.FeedbackError)Interop.Feedback.IsSupportedPattern((Interop.Feedback.FeedbackType)type, number, out supported);
209 if (res != Interop.Feedback.FeedbackError.None)
211 Log.Warn(LogTag, string.Format("Failed to get supported information. err = {0}", res));
214 case Interop.Feedback.FeedbackError.NotInitialized:
215 throw new Exception("Not initialized");
216 case Interop.Feedback.FeedbackError.InvalidParameter:
217 throw new ArgumentException("Invalid Arguments");
218 case Interop.Feedback.FeedbackError.NotSupported:
219 throw new NotSupportedException("Device is not supported");
220 case Interop.Feedback.FeedbackError.PermissionDenied:
221 throw new UnauthorizedAccessException("Access is not granted");
222 case Interop.Feedback.FeedbackError.OperationFailed:
224 throw new InvalidOperationException("Failed to get supported information");
231 /// Plays a specific feedback pattern.
234 /// To play Vibration type, app should have http://tizen.org/privilege/haptic privilege.
236 /// <since_tizen> 3 </since_tizen>
237 /// <param name="type">The feedback type.</param>
238 /// <param name="pattern">The feedback pattern string.</param>
240 /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
242 /// <exception cref="Exception">Thrown when failed because feedback is not initialized.</exception>
243 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
244 /// <exception cref="NotSupportedException">Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.</exception>
245 /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted(No privilege)</exception>
246 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
247 /// <privilege>http://tizen.org/privilege/haptic</privilege>
250 /// Feedback feedback = new Feedback();
251 /// feedback.Play(FeedbackType.All, "Tap");
254 public void Play(FeedbackType type, String pattern)
257 Interop.Feedback.FeedbackError res;
259 if (!Pattern.TryGetValue(pattern, out number))
260 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
262 if (type == FeedbackType.All)
263 res = (Interop.Feedback.FeedbackError)Interop.Feedback.Play(number);
265 res = (Interop.Feedback.FeedbackError)Interop.Feedback.PlayType((Interop.Feedback.FeedbackType)type, number);
267 if (res != Interop.Feedback.FeedbackError.None)
269 Log.Warn(LogTag, string.Format("Failed to play feedback. err = {0}", res));
272 case Interop.Feedback.FeedbackError.NotInitialized:
273 throw new Exception("Not initialized");
274 case Interop.Feedback.FeedbackError.InvalidParameter:
275 throw new ArgumentException("Invalid Arguments");
276 case Interop.Feedback.FeedbackError.NotSupported:
277 throw new NotSupportedException("Not supported");
278 case Interop.Feedback.FeedbackError.PermissionDenied:
279 throw new UnauthorizedAccessException("Access is not granted");
280 case Interop.Feedback.FeedbackError.OperationFailed:
282 throw new InvalidOperationException("Failed to play pattern");
288 /// Stops to play the feedback.
291 /// To stop vibration, the application should have http://tizen.org/privilege/haptic privilege.
293 /// <since_tizen> 3 </since_tizen>
295 /// http://tizen.org/feature/feedback.vibration
297 /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
298 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament</exception>
299 /// <exception cref="NotSupportedException">Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.</exception>
300 /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted (No privilege).</exception>
301 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
302 /// <privilege>http://tizen.org/privilege/haptic</privilege>
305 /// Feedback Feedback1 = new Feedback();
306 /// Feedback1.Stop();
311 Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Stop();
313 if (res != Interop.Feedback.FeedbackError.None)
315 Log.Warn(LogTag, string.Format("Failed to stop feedback. err = {0}", res));
318 case Interop.Feedback.FeedbackError.NotInitialized:
319 throw new Exception("Not initialized");
320 case Interop.Feedback.FeedbackError.InvalidParameter:
321 throw new ArgumentException("Invalid Arguments");
322 case Interop.Feedback.FeedbackError.NotSupported:
323 throw new NotSupportedException("Not supported");
324 case Interop.Feedback.FeedbackError.PermissionDenied:
325 throw new UnauthorizedAccessException("Access is not granted");
326 case Interop.Feedback.FeedbackError.OperationFailed:
328 throw new InvalidOperationException("Failed to stop pattern");
334 /// Gets the count of theme can be used according to feedback type.
337 /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
339 /// <since_tizen> 10 </since_tizen>
340 /// <param name="type">The feedback type.</param>
341 /// <returns>The counf of theme can be used according to feedback type.</returns>
342 /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
343 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
344 /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
345 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
348 /// Feedback feedback = new Feedback();
349 /// uint coundOfTheme = feedback.GetCountOfThemeInternal(FeedbackType.Sound);
352 [EditorBrowsable(EditorBrowsableState.Never)]
353 public uint GetCountOfThemeInternal(FeedbackType type)
355 uint countOfTheme = 0;
356 Interop.Feedback.FeedbackError res;
358 res = (Interop.Feedback.FeedbackError)Interop.Feedback.GetCountOfThemeInternal((Interop.Feedback.FeedbackType)type, out countOfTheme);
360 if (res != Interop.Feedback.FeedbackError.None)
362 Log.Warn(LogTag, string.Format("Failed to get count of theme internal. err = {0}", res));
365 case Interop.Feedback.FeedbackError.NotInitialized:
366 throw new Exception("Not initialized");
367 case Interop.Feedback.FeedbackError.InvalidParameter:
368 throw new ArgumentException("Invalid Arguments");
369 case Interop.Feedback.FeedbackError.NotSupported:
370 throw new NotSupportedException("Device is not supported");
371 case Interop.Feedback.FeedbackError.OperationFailed:
373 throw new InvalidOperationException("Failed to get count of theme internal");
380 /// Gets the index of theme selected.
383 /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
385 /// <since_tizen> 10 </since_tizen>
386 /// <param name="type">The feedback type.</param>
387 /// <returns>The index of theme selected as default theme according to feedback type.</returns>
388 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
389 /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
390 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
393 /// Feedback feedback = new Feedback();
394 /// uint indexOfTheme = feedback. GetThemeIndexInternal(FeedbackType.Sound);
397 [EditorBrowsable(EditorBrowsableState.Never)]
398 public uint GetThemeIndexInternal(FeedbackType type)
400 uint countOfTheme = 0;
401 Interop.Feedback.FeedbackError res;
403 res = (Interop.Feedback.FeedbackError)Interop.Feedback.GetThemeIndexInternal((Interop.Feedback.FeedbackType)type, out countOfTheme);
405 if (res != Interop.Feedback.FeedbackError.None)
407 Log.Warn(LogTag, string.Format("Failed to get index of theme internal. err = {0}", res));
410 case Interop.Feedback.FeedbackError.InvalidParameter:
411 throw new ArgumentException("Invalid Arguments");
412 case Interop.Feedback.FeedbackError.NotSupported:
413 throw new NotSupportedException("Device is not supported");
414 case Interop.Feedback.FeedbackError.OperationFailed:
416 throw new InvalidOperationException("Failed to get index of theme internal");
423 /// Sets the index of theme according to feedback type.
426 /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
428 /// <since_tizen> 10 </since_tizen>
429 /// <param name="type">The feedback type.</param>
430 /// <param name="indexOfTheme">The index of theme will be set.</param>
431 /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
432 /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
433 /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
436 /// Feedback feedback = new Feedback();
437 /// uint indexOfTheme = 0;
438 /// feedback.SetThemeIndexInternal(FeedbackType.Sound, indexOfTheme);
441 [EditorBrowsable(EditorBrowsableState.Never)]
442 public void SetThemeIndexInternal(FeedbackType type, uint indexOfTheme)
444 Interop.Feedback.FeedbackError res;
446 res = (Interop.Feedback.FeedbackError)Interop.Feedback.SetThemeIndexInternal((Interop.Feedback.FeedbackType)type, indexOfTheme);
448 if (res != Interop.Feedback.FeedbackError.None)
450 Log.Warn(LogTag, string.Format("Failed to set index of theme internal. err = {0}", res));
453 case Interop.Feedback.FeedbackError.InvalidParameter:
454 throw new ArgumentException("Invalid Arguments");
455 case Interop.Feedback.FeedbackError.NotSupported:
456 throw new NotSupportedException("Device is not supported");
457 case Interop.Feedback.FeedbackError.OperationFailed:
459 throw new InvalidOperationException("Failed to set index of theme internal");