[System.Feedback] Add internal APIs to support multi-theme sound conf usage
[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         };
126
127         /// <summary>
128         /// Constructor of Feedback class
129         /// </summary>
130         /// <since_tizen> 3 </since_tizen>
131         /// <feature>
132         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
133         /// </feature>
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>
137         /// <example>
138         /// <code>
139         /// Feedback feedback = new Feedback();
140         /// </code>
141         /// </example>
142         public Feedback()
143         {
144             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Initialize();
145             if (res != Interop.Feedback.FeedbackError.None)
146             {
147                 Log.Warn(LogTag, string.Format("Failed to initialize feedback. err = {0}", res));
148                 switch (res)
149                 {
150                     case Interop.Feedback.FeedbackError.NotSupported:
151                         throw new NotSupportedException("Device is not supported");
152                     default:
153                         throw new InvalidOperationException("Failed to initialize");
154                 }
155             }
156         }
157
158         /// <summary>
159         /// Finalizes an instance of the Feedback class.
160         /// </summary>
161         ~Feedback()
162         {
163             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Deinitialize();
164             if (res != Interop.Feedback.FeedbackError.None)
165             {
166                 Log.Warn(LogTag, string.Format("Failed to deinitialize feedback. err = {0}", res));
167             }
168         }
169
170         /// <summary>
171         /// Gets the supported information about a specific type and pattern.
172         /// </summary>
173         /// <remarks>
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.
178         /// </remarks>
179         /// <since_tizen> 3 </since_tizen>
180         /// <param name="type">The feedback type.</param>
181         /// <param name="pattern">The feedback pattern string.</param>
182         /// <feature>
183         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
184         /// </feature>
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>
192         /// <example>
193         /// <code>
194             /// Feedback feedback = new Feedback();
195         /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
196         /// </code>
197         /// </example>
198         public bool IsSupportedPattern(FeedbackType type, String pattern)
199         {
200             bool supported = false;
201             int number;
202             Interop.Feedback.FeedbackError res;
203
204             if (!Pattern.TryGetValue(pattern, out number))
205                 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
206
207             res = (Interop.Feedback.FeedbackError)Interop.Feedback.IsSupportedPattern((Interop.Feedback.FeedbackType)type, number, out supported);
208
209             if (res != Interop.Feedback.FeedbackError.None)
210             {
211                 Log.Warn(LogTag, string.Format("Failed to get supported information. err = {0}", res));
212                 switch (res)
213                 {
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:
223                     default:
224                         throw new InvalidOperationException("Failed to get supported information");
225                 }
226             }
227             return supported;
228         }
229
230         /// <summary>
231         /// Plays a specific feedback pattern.
232         /// </summary>
233         /// <remarks>
234         /// To play Vibration type, app should have http://tizen.org/privilege/haptic privilege.
235         /// </remarks>
236         /// <since_tizen> 3 </since_tizen>
237         /// <param name="type">The feedback type.</param>
238         /// <param name="pattern">The feedback pattern string.</param>
239         /// <feature>
240         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
241         /// </feature>
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>
248         /// <example>
249         /// <code>
250         /// Feedback feedback = new Feedback();
251         /// feedback.Play(FeedbackType.All, "Tap");
252         /// </code>
253         /// </example>
254         public void Play(FeedbackType type, String pattern)
255         {
256             int number;
257             Interop.Feedback.FeedbackError res;
258
259             if (!Pattern.TryGetValue(pattern, out number))
260                 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
261
262             if (type == FeedbackType.All)
263                 res = (Interop.Feedback.FeedbackError)Interop.Feedback.Play(number);
264             else
265                 res = (Interop.Feedback.FeedbackError)Interop.Feedback.PlayType((Interop.Feedback.FeedbackType)type, number);
266
267             if (res != Interop.Feedback.FeedbackError.None)
268             {
269                 Log.Warn(LogTag, string.Format("Failed to play feedback. err = {0}", res));
270                 switch (res)
271                 {
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:
281                     default:
282                         throw new InvalidOperationException("Failed to play pattern");
283                 }
284             }
285         }
286
287         /// <summary>
288         /// Stops to play the feedback.
289         /// </summary>
290         /// <remarks>
291         /// To stop vibration, the application should have http://tizen.org/privilege/haptic privilege.
292         /// </remarks>
293         /// <since_tizen> 3 </since_tizen>
294         /// <feature>
295         /// http://tizen.org/feature/feedback.vibration
296         /// </feature>
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>
303         /// <example>
304         /// <code>
305         /// Feedback Feedback1 = new Feedback();
306         /// Feedback1.Stop();
307         /// </code>
308         /// </example>
309         public void Stop()
310         {
311             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Stop();
312
313             if (res != Interop.Feedback.FeedbackError.None)
314             {
315                 Log.Warn(LogTag, string.Format("Failed to stop feedback. err = {0}", res));
316                 switch (res)
317                 {
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:
327                     default:
328                         throw new InvalidOperationException("Failed to stop pattern");
329                 }
330             }
331         }
332
333         /// <summary>
334         /// Gets the count of theme can be used according to feedback type.
335         /// </summary>
336         /// <remarks>
337         /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
338         /// </remarks>
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>
346         /// <example>
347         /// <code>
348         /// Feedback feedback = new Feedback();
349         /// uint coundOfTheme = feedback.GetCountOfThemeInternal(FeedbackType.Sound);
350         /// </code>
351         /// </example>
352         [EditorBrowsable(EditorBrowsableState.Never)]
353         public uint GetCountOfThemeInternal(FeedbackType type)
354         {
355             uint countOfTheme = 0;
356             Interop.Feedback.FeedbackError res;
357
358             res = (Interop.Feedback.FeedbackError)Interop.Feedback.GetCountOfThemeInternal((Interop.Feedback.FeedbackType)type, out countOfTheme);
359
360             if (res != Interop.Feedback.FeedbackError.None)
361             {
362                 Log.Warn(LogTag, string.Format("Failed to get count of theme internal. err = {0}", res));
363                 switch (res)
364                 {
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:
372                     default:
373                         throw new InvalidOperationException("Failed to get count of theme internal");
374                 }
375             }
376             return countOfTheme;
377         }
378
379         /// <summary>
380         /// Gets the index of theme selected.
381         /// </summary>
382         /// <remarks>
383         /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
384         /// </remarks>
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>
391         /// <example>
392         /// <code>
393         /// Feedback feedback = new Feedback();
394         /// uint indexOfTheme = feedback. GetThemeIndexInternal(FeedbackType.Sound);
395         /// </code>
396         /// </example>
397         [EditorBrowsable(EditorBrowsableState.Never)]
398         public uint GetThemeIndexInternal(FeedbackType type)
399         {
400             uint countOfTheme = 0;
401             Interop.Feedback.FeedbackError res;
402
403             res = (Interop.Feedback.FeedbackError)Interop.Feedback.GetThemeIndexInternal((Interop.Feedback.FeedbackType)type, out countOfTheme);
404
405             if (res != Interop.Feedback.FeedbackError.None)
406             {
407                 Log.Warn(LogTag, string.Format("Failed to get index of theme internal. err = {0}", res));
408                 switch (res)
409                 {
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:
415                     default:
416                         throw new InvalidOperationException("Failed to get index of theme internal");
417                 }
418             }
419             return countOfTheme;
420         }
421
422         /// <summary>
423         /// Sets the index of theme according to feedback type.
424         /// </summary>
425         /// <remarks>
426         /// Now this internal API works for FeedbackType.Sound only, FeedbackType.Vibration is not supported.
427         /// </remarks>
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>
434         /// <example>
435         /// <code>
436         /// Feedback feedback = new Feedback();
437         /// uint indexOfTheme = 0;
438         /// feedback.SetThemeIndexInternal(FeedbackType.Sound, indexOfTheme);
439         /// </code>
440         /// </example>
441         [EditorBrowsable(EditorBrowsableState.Never)]
442         public void SetThemeIndexInternal(FeedbackType type, uint indexOfTheme)
443         {
444             Interop.Feedback.FeedbackError res;
445
446             res = (Interop.Feedback.FeedbackError)Interop.Feedback.SetThemeIndexInternal((Interop.Feedback.FeedbackType)type, indexOfTheme);
447
448             if (res != Interop.Feedback.FeedbackError.None)
449             {
450                 Log.Warn(LogTag, string.Format("Failed to set index of theme internal. err = {0}", res));
451                 switch (res)
452                 {
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:
458                     default:
459                         throw new InvalidOperationException("Failed to set index of theme internal");
460                 }
461             }
462         }
463     }
464 }