[System.Feedback] Add new feedback_pattern for "da" profile
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Feedback / Feedback / Feedback.cs
1 /*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20
21
22 namespace Tizen.System
23 {
24     /// <summary>
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:
28     /// Tap
29     /// SoftInputPanel
30     /// Key0
31     /// Key1
32     /// Key2
33     /// Key3
34     /// Key4
35     /// Key5
36     /// Key6
37     /// Key7
38     /// Key8
39     /// Key9
40     /// KeyStar
41     /// KeySharp
42     /// KeyBack
43     /// Hold
44     /// HardwareKeyPressed
45     /// HardwareKeyHold
46     /// Message
47     /// Email
48     /// WakeUp
49     /// Schedule
50     /// Timer
51     /// General
52     /// PowerOn
53     /// PowerOff
54     /// ChargerConnected
55     /// ChargingError
56     /// FullyCharged
57     /// LowBattery
58     /// Lock
59     /// UnLock
60     /// VibrationModeAbled
61     /// SilentModeDisabled
62     /// BluetoothDeviceConnected
63     /// BluetoothDeviceDisconnected
64     /// ListReorder
65     /// ListSlider
66     /// VolumeKeyPressed
67     /// </summary>
68     /// <privilege>
69     /// For controlling the haptic device:
70     /// http://tizen.org/privilege/haptic
71     /// For controlling the sound, privilege is not needed.
72     /// </privilege>
73     /// <example>
74     /// <code>
75     /// Feedback feedback = new Feedback();
76     /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
77     /// </code>
78     /// </example>
79     /// <since_tizen> 3 </since_tizen>
80     public class Feedback
81     {
82         private const string LogTag = "Tizen.System.Feedback";
83
84         private readonly Dictionary<string, int> Pattern = new Dictionary<string, int>
85         {
86             {"Tap", 0},
87             {"SoftInputPanel", 1},
88             {"Key0", 6},
89             {"Key1", 7},
90             {"Key2", 8},
91             {"Key3", 9},
92             {"Key4", 10},
93             {"Key5", 11},
94             {"Key6", 12},
95             {"Key7", 13},
96             {"Key8", 14},
97             {"Key9", 15},
98             {"KeyStar", 16},
99             {"KeySharp", 17},
100             {"KeyBack", 18},
101             {"Hold", 19},
102             {"HardwareKeyPressed", 21},
103             {"HardwareKeyHold", 22},
104             {"Message", 23},
105             {"Email", 25},
106             {"WakeUp", 27},
107             {"Schedule", 29},
108             {"Timer", 31},
109             {"General", 33},
110             {"PowerOn", 36},
111             {"PowerOff", 37},
112             {"ChargerConnected", 38},
113             {"ChargingError", 40},
114             {"FullyCharged", 42},
115             {"LowBattery", 44},
116             {"Lock", 46},
117             {"UnLock", 47},
118             {"VibrationModeAbled", 55},
119             {"SilentModeDisabled", 56},
120             {"BluetoothDeviceConnected", 57},
121             {"BluetoothDeviceDisconnected", 58},
122             {"ListReorder", 62},
123             {"ListSlider", 63},
124             {"VolumeKeyPressed", 64},
125             {"Basic", 40001},
126             {"ToggleOn", 40002},
127             {"ToggleOff", 40003},
128             {"LongPressOn", 40004},
129             {"LongPressOff", 40005},
130             {"Invalid", 40006},
131             {"Confirm", 40007},
132             {"PopUp", 40008},
133             {"PreheatEnding", 40009},
134             {"TaskEnding", 40010},
135             {"Scroll", 40011},
136             {"PageTurn", 40012},
137             {"OpStart", 40013},
138             {"OpPause", 40014},
139             {"OpStop", 40015},
140             {"Default", 40016},
141             {"DefaultLeve1", 40017},
142             {"Level1", 40018},
143             {"Level2", 40019},
144             {"Level3", 40020},
145             {"Level4", 40021},
146             {"Level5", 40022},
147             {"Level6", 40023},
148             {"Level7", 40024},
149             {"Level8", 40025},
150             {"Level9", 40026},
151             {"Level10", 40027},
152             {"TimerEnding", 40028},
153             {"BurnerDetected", 40029},
154             {"BurnerMoved", 40030},
155             {"Connected", 40031},
156             {"Disconnected", 40032},
157         };
158
159         /// <summary>
160         /// Constructor of Feedback class
161         /// </summary>
162         /// <since_tizen> 3 </since_tizen>
163         /// <feature>
164         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
165         /// </feature>
166         /// <exception cref="NotSupportedException">Thrown when failed because the devices (vibration and sound) are not supported.</exception>
167         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
168         /// <privilege>http://tizen.org/privilege/haptic</privilege>
169         /// <example>
170         /// <code>
171         /// Feedback feedback = new Feedback();
172         /// </code>
173         /// </example>
174         public Feedback()
175         {
176             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Initialize();
177             if (res != Interop.Feedback.FeedbackError.None)
178             {
179                 Log.Warn(LogTag, string.Format("Failed to initialize feedback. err = {0}", res));
180                 switch (res)
181                 {
182                     case Interop.Feedback.FeedbackError.NotSupported:
183                         throw new NotSupportedException("Device is not supported");
184                     default:
185                         throw new InvalidOperationException("Failed to initialize");
186                 }
187             }
188         }
189
190         /// <summary>
191         /// Finalizes an instance of the Feedback class.
192         /// </summary>
193         ~Feedback()
194         {
195             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Deinitialize();
196             if (res != Interop.Feedback.FeedbackError.None)
197             {
198                 Log.Warn(LogTag, string.Format("Failed to deinitialize feedback. err = {0}", res));
199             }
200         }
201
202         /// <summary>
203         /// Gets the supported information about a specific type and pattern.
204         /// </summary>
205         /// <remarks>
206         /// Now, IsSupportedPattern is not working for FeedbackType.All.
207         /// This API is working for FeedbackType.Sound and FeedbackType.Vibration only.
208         /// If you use FeedbackType.All for type parameter, this API will throw ArgumentException.
209         /// To get the supported information for Vibration type, the application should have http://tizen.org/privilege/haptic privilege.
210         /// </remarks>
211         /// <since_tizen> 3 </since_tizen>
212         /// <param name="type">The feedback type.</param>
213         /// <param name="pattern">The feedback pattern string.</param>
214         /// <feature>
215         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
216         /// </feature>
217         /// <returns>Information whether a pattern is supported.</returns>
218         /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
219         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
220         /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
221         /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted (No privilege).</exception>
222         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
223         /// <privilege>http://tizen.org/privilege/haptic</privilege>
224         /// <example>
225         /// <code>
226             /// Feedback feedback = new Feedback();
227         /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
228         /// </code>
229         /// </example>
230         public bool IsSupportedPattern(FeedbackType type, String pattern)
231         {
232             bool supported = false;
233             int number;
234             Interop.Feedback.FeedbackError res;
235
236             if (!Pattern.TryGetValue(pattern, out number))
237                 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
238
239             res = (Interop.Feedback.FeedbackError)Interop.Feedback.IsSupportedPattern((Interop.Feedback.FeedbackType)type, number, out supported);
240
241             if (res != Interop.Feedback.FeedbackError.None)
242             {
243                 Log.Warn(LogTag, string.Format("Failed to get supported information. err = {0}", res));
244                 switch (res)
245                 {
246                     case Interop.Feedback.FeedbackError.NotInitialized:
247                         throw new Exception("Not initialized");
248                     case Interop.Feedback.FeedbackError.InvalidParameter:
249                         throw new ArgumentException("Invalid Arguments");
250                     case Interop.Feedback.FeedbackError.NotSupported:
251                         throw new NotSupportedException("Device is not supported");
252                     case Interop.Feedback.FeedbackError.PermissionDenied:
253                         throw new UnauthorizedAccessException("Access is not granted");
254                     case Interop.Feedback.FeedbackError.OperationFailed:
255                     default:
256                         throw new InvalidOperationException("Failed to get supported information");
257                 }
258             }
259             return supported;
260         }
261
262         /// <summary>
263         /// Plays a specific feedback pattern.
264         /// </summary>
265         /// <remarks>
266         /// To play Vibration type, app should have http://tizen.org/privilege/haptic privilege.
267         /// </remarks>
268         /// <since_tizen> 3 </since_tizen>
269         /// <param name="type">The feedback type.</param>
270         /// <param name="pattern">The feedback pattern string.</param>
271         /// <feature>
272         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
273         /// </feature>
274         /// <exception cref="Exception">Thrown when failed because feedback is not initialized.</exception>
275         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
276         /// <exception cref="NotSupportedException">Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.</exception>
277         /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted(No privilege)</exception>
278         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
279         /// <privilege>http://tizen.org/privilege/haptic</privilege>
280         /// <example>
281         /// <code>
282         /// Feedback feedback = new Feedback();
283         /// feedback.Play(FeedbackType.All, "Tap");
284         /// </code>
285         /// </example>
286         public void Play(FeedbackType type, String pattern)
287         {
288             int number;
289             Interop.Feedback.FeedbackError res;
290
291             if (!Pattern.TryGetValue(pattern, out number))
292                 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
293
294             if (type == FeedbackType.All)
295                 res = (Interop.Feedback.FeedbackError)Interop.Feedback.Play(number);
296             else
297                 res = (Interop.Feedback.FeedbackError)Interop.Feedback.PlayType((Interop.Feedback.FeedbackType)type, number);
298
299             if (res != Interop.Feedback.FeedbackError.None)
300             {
301                 Log.Warn(LogTag, string.Format("Failed to play feedback. err = {0}", res));
302                 switch (res)
303                 {
304                     case Interop.Feedback.FeedbackError.NotInitialized:
305                         throw new Exception("Not initialized");
306                     case Interop.Feedback.FeedbackError.InvalidParameter:
307                         throw new ArgumentException("Invalid Arguments");
308                     case Interop.Feedback.FeedbackError.NotSupported:
309                         throw new NotSupportedException("Not supported");
310                     case Interop.Feedback.FeedbackError.PermissionDenied:
311                         throw new UnauthorizedAccessException("Access is not granted");
312                     case Interop.Feedback.FeedbackError.OperationFailed:
313                     default:
314                         throw new InvalidOperationException("Failed to play pattern");
315                 }
316             }
317         }
318
319         /// <summary>
320         /// Stops to play the feedback.
321         /// </summary>
322         /// <remarks>
323         /// To stop vibration, the application should have http://tizen.org/privilege/haptic privilege.
324         /// </remarks>
325         /// <since_tizen> 3 </since_tizen>
326         /// <feature>
327         /// http://tizen.org/feature/feedback.vibration
328         /// </feature>
329         /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
330         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament</exception>
331         /// <exception cref="NotSupportedException">Thrown when failed because the device (haptic, sound) or a specific pattern is not supported.</exception>
332         /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted (No privilege).</exception>
333         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
334         /// <privilege>http://tizen.org/privilege/haptic</privilege>
335         /// <example>
336         /// <code>
337         /// Feedback Feedback1 = new Feedback();
338         /// Feedback1.Stop();
339         /// </code>
340         /// </example>
341         public void Stop()
342         {
343             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Stop();
344
345             if (res != Interop.Feedback.FeedbackError.None)
346             {
347                 Log.Warn(LogTag, string.Format("Failed to stop feedback. err = {0}", res));
348                 switch (res)
349                 {
350                     case Interop.Feedback.FeedbackError.NotInitialized:
351                         throw new Exception("Not initialized");
352                     case Interop.Feedback.FeedbackError.InvalidParameter:
353                         throw new ArgumentException("Invalid Arguments");
354                     case Interop.Feedback.FeedbackError.NotSupported:
355                         throw new NotSupportedException("Not supported");
356                     case Interop.Feedback.FeedbackError.PermissionDenied:
357                         throw new UnauthorizedAccessException("Access is not granted");
358                     case Interop.Feedback.FeedbackError.OperationFailed:
359                     default:
360                         throw new InvalidOperationException("Failed to stop pattern");
361                 }
362             }
363         }
364
365         /// <summary>
366         /// Gets the count of theme can be used according to feedback type.
367         /// </summary>
368         /// <remarks>
369         /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
370         /// </remarks>
371         /// <since_tizen> 10 </since_tizen>
372         /// <param name="type">The feedback type.</param>
373         /// <returns>The counf of theme can be used according to feedback type.</returns>
374         /// <exception cref="Exception">Thrown when failed because the feedback is not initialized.</exception>
375         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
376         /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
377         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
378         /// <example>
379         /// <code>
380         /// Feedback feedback = new Feedback();
381         /// uint coundOfTheme = feedback.GetCountOfThemeInternal(FeedbackType.Sound);
382         /// </code>
383         /// </example>
384         [EditorBrowsable(EditorBrowsableState.Never)]
385         public uint GetCountOfThemeInternal(FeedbackType type)
386         {
387             uint countOfTheme = 0;
388             Interop.Feedback.FeedbackError res;
389
390             res = (Interop.Feedback.FeedbackError)Interop.Feedback.GetCountOfThemeInternal((Interop.Feedback.FeedbackType)type, out countOfTheme);
391
392             if (res != Interop.Feedback.FeedbackError.None)
393             {
394                 Log.Warn(LogTag, string.Format("Failed to get count of theme internal. err = {0}", res));
395                 switch (res)
396                 {
397                     case Interop.Feedback.FeedbackError.NotInitialized:
398                         throw new Exception("Not initialized");
399                     case Interop.Feedback.FeedbackError.InvalidParameter:
400                         throw new ArgumentException("Invalid Arguments");
401                     case Interop.Feedback.FeedbackError.NotSupported:
402                         throw new NotSupportedException("Device is not supported");
403                     case Interop.Feedback.FeedbackError.OperationFailed:
404                     default:
405                         throw new InvalidOperationException("Failed to get count of theme internal");
406                 }
407             }
408             return countOfTheme;
409         }
410
411         /// <summary>
412         /// Gets the index of theme selected.
413         /// </summary>
414         /// <remarks>
415         /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
416         /// </remarks>
417         /// <since_tizen> 10 </since_tizen>
418         /// <param name="type">The feedback type.</param>
419         /// <returns>The index of theme selected as default theme according to feedback type.</returns>
420         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
421         /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
422         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
423         /// <example>
424         /// <code>
425         /// Feedback feedback = new Feedback();
426         /// uint indexOfTheme = feedback. GetThemeIndexInternal(FeedbackType.Sound);
427         /// </code>
428         /// </example>
429         [EditorBrowsable(EditorBrowsableState.Never)]
430         public uint GetThemeIndexInternal(FeedbackType type)
431         {
432             uint countOfTheme = 0;
433             Interop.Feedback.FeedbackError res;
434
435             res = (Interop.Feedback.FeedbackError)Interop.Feedback.GetThemeIndexInternal((Interop.Feedback.FeedbackType)type, out countOfTheme);
436
437             if (res != Interop.Feedback.FeedbackError.None)
438             {
439                 Log.Warn(LogTag, string.Format("Failed to get index of theme internal. err = {0}", res));
440                 switch (res)
441                 {
442                     case Interop.Feedback.FeedbackError.InvalidParameter:
443                         throw new ArgumentException("Invalid Arguments");
444                     case Interop.Feedback.FeedbackError.NotSupported:
445                         throw new NotSupportedException("Device is not supported");
446                     case Interop.Feedback.FeedbackError.OperationFailed:
447                     default:
448                         throw new InvalidOperationException("Failed to get index of theme internal");
449                 }
450             }
451             return countOfTheme;
452         }
453
454         /// <summary>
455         /// Sets the index of theme according to feedback type.
456         /// </summary>
457         /// <remarks>
458         /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
459         /// To set the index of theme for Sound type, the application should have http://tizen.org/privilege/systemsettings.admin privilege.
460         /// </remarks>
461         /// <since_tizen> 10 </since_tizen>
462         /// <param name="type">The feedback type.</param>
463         /// <param name="indexOfTheme">The index of theme will be set.</param>
464         /// <exception cref="ArgumentException">Thrown when failed because of an invalid arguament.</exception>
465         /// <exception cref="NotSupportedException">Thrown when failed becuase the device (haptic, sound) is not supported.</exception>
466         /// <exception cref="UnauthorizedAccessException">Thrown when failed because the access is not granted(No privilege)</exception>
467         /// <exception cref="InvalidOperationException">Thrown when failed because of a system error.</exception>
468         /// <example>
469         /// <code>
470         /// Feedback feedback = new Feedback();
471         /// uint indexOfTheme = 0;
472         /// feedback.SetThemeIndexInternal(FeedbackType.Sound, indexOfTheme);
473         /// </code>
474         /// </example>
475         [EditorBrowsable(EditorBrowsableState.Never)]
476         public void SetThemeIndexInternal(FeedbackType type, uint indexOfTheme)
477         {
478             Interop.Feedback.FeedbackError res;
479
480             res = (Interop.Feedback.FeedbackError)Interop.Feedback.SetThemeIndexInternal((Interop.Feedback.FeedbackType)type, indexOfTheme);
481
482             if (res != Interop.Feedback.FeedbackError.None)
483             {
484                 Log.Warn(LogTag, string.Format("Failed to set index of theme internal. err = {0}", res));
485                 switch (res)
486                 {
487                     case Interop.Feedback.FeedbackError.InvalidParameter:
488                         throw new ArgumentException("Invalid Arguments");
489                     case Interop.Feedback.FeedbackError.NotSupported:
490                         throw new NotSupportedException("Device is not supported");
491                     case Interop.Feedback.FeedbackError.PermissionDenied:
492                         throw new UnauthorizedAccessException("Access is not granted");
493                     case Interop.Feedback.FeedbackError.OperationFailed:
494                     default:
495                         throw new InvalidOperationException("Failed to set index of theme internal");
496                 }
497             }
498         }
499     }
500 }