de14cd41f03577a59b0d2ba11b3b8f0cb23c3701
[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
20
21 namespace Tizen.System
22 {
23     /// <summary>
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:
27     /// Tap
28     /// SoftInputPanel
29     /// Key0
30     /// Key1
31     /// Key2
32     /// Key3
33     /// Key4
34     /// Key5
35     /// Key6
36     /// Key7
37     /// Key8
38     /// Key9
39     /// KeyStar
40     /// KeySharp
41     /// KeyBack
42     /// Hold
43     /// HardwareKeyPressed
44     /// HardwareKeyHold
45     /// Message
46     /// Email
47     /// WakeUp
48     /// Schedule
49     /// Timer
50     /// General
51     /// PowerOn
52     /// PowerOff
53     /// ChargerConnected
54     /// ChargingError
55     /// FullyCharged
56     /// LowBattery
57     /// Lock
58     /// UnLock
59     /// VibrationModeAbled
60     /// SilentModeDisabled
61     /// BluetoothDeviceConnected
62     /// BluetoothDeviceDisconnected
63     /// ListReorder
64     /// ListSlider
65     /// VolumeKeyPressed
66     /// </summary>
67     /// <privilege>
68     /// For controlling the haptic device:
69     /// http://tizen.org/privilege/haptic
70     /// For controlling the sound, privilege is not needed.
71     /// </privilege>
72     /// <example>
73     /// <code>
74     /// Feedback feedback = new Feedback();
75     /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
76     /// </code>
77     /// </example>
78     /// <since_tizen> 3 </since_tizen>
79     public class Feedback
80     {
81         private const string LogTag = "Tizen.System.Feedback";
82
83         private readonly Dictionary<string, int> Pattern = new Dictionary<string, int>
84         {
85             {"Tap", 0},
86             {"SoftInputPanel", 1},
87             {"Key0", 6},
88             {"Key1", 7},
89             {"Key2", 8},
90             {"Key3", 9},
91             {"Key4", 10},
92             {"Key5", 11},
93             {"Key6", 12},
94             {"Key7", 13},
95             {"Key8", 14},
96             {"Key9", 15},
97             {"KeyStar", 16},
98             {"KeySharp", 17},
99             {"KeyBack", 18},
100             {"Hold", 19},
101             {"HardwareKeyPressed", 21},
102             {"HardwareKeyHold", 22},
103             {"Message", 23},
104             {"Email", 25},
105             {"WakeUp", 27},
106             {"Schedule", 29},
107             {"Timer", 31},
108             {"General", 33},
109             {"PowerOn", 36},
110             {"PowerOff", 37},
111             {"ChargerConnected", 38},
112             {"ChargingError", 40},
113             {"FullyCharged", 42},
114             {"LowBattery", 44},
115             {"Lock", 46},
116             {"UnLock", 47},
117             {"VibrationModeAbled", 55},
118             {"SilentModeDisabled", 56},
119             {"BluetoothDeviceConnected", 57},
120             {"BluetoothDeviceDisconnected", 58},
121             {"ListReorder", 62},
122             {"ListSlider", 63},
123             {"VolumeKeyPressed", 64},
124         };
125
126         /// <summary>
127         /// Constructor of Feedback class
128         /// </summary>
129         /// <since_tizen> 3 </since_tizen>
130         /// <feature>
131         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
132         /// </feature>
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>
136         /// <example>
137         /// <code>
138         /// Feedback feedback = new Feedback();
139         /// </code>
140         /// </example>
141         public Feedback()
142         {
143             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Initialize();
144             if (res != Interop.Feedback.FeedbackError.None)
145             {
146                 Log.Warn(LogTag, string.Format("Failed to initialize feedback. err = {0}", res));
147                 switch (res)
148                 {
149                     case Interop.Feedback.FeedbackError.NotSupported:
150                         throw new NotSupportedException("Device is not supported");
151                     default:
152                         throw new InvalidOperationException("Failed to initialize");
153                 }
154             }
155         }
156
157         /// <summary>
158         /// Finalizes an instance of the Feedback class.
159         /// </summary>
160         ~Feedback()
161         {
162             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Deinitialize();
163             if (res != Interop.Feedback.FeedbackError.None)
164             {
165                 Log.Warn(LogTag, string.Format("Failed to deinitialize feedback. err = {0}", res));
166                 switch (res)
167                 {
168                     case Interop.Feedback.FeedbackError.NotInitialized:
169                         throw new Exception("Not initialized");
170                     default:
171                         throw new InvalidOperationException("Failed to initialize");
172                 }
173             }
174         }
175
176         /// <summary>
177         /// Gets the supported information about a specific type and pattern.
178         /// </summary>
179         /// <remarks>
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.
184         /// </remarks>
185         /// <since_tizen> 3 </since_tizen>
186         /// <param name="type">The feedback type.</param>
187         /// <param name="pattern">The feedback pattern string.</param>
188         /// <feature>
189         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
190         /// </feature>
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>
198         /// <example>
199         /// <code>
200             /// Feedback feedback = new Feedback();
201         /// bool res = feedback.IsSupportedPattern(FeedbackType.Vibration, "Tap");
202         /// </code>
203         /// </example>
204         public bool IsSupportedPattern(FeedbackType type, String pattern)
205         {
206             bool supported = false;
207             int number;
208             Interop.Feedback.FeedbackError res;
209
210             if (!Pattern.TryGetValue(pattern, out number))
211                 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
212
213             res = (Interop.Feedback.FeedbackError)Interop.Feedback.IsSupportedPattern((Interop.Feedback.FeedbackType)type, number, out supported);
214
215             if (res != Interop.Feedback.FeedbackError.None)
216             {
217                 Log.Warn(LogTag, string.Format("Failed to get supported information. err = {0}", res));
218                 switch (res)
219                 {
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:
229                     default:
230                         throw new InvalidOperationException("Failed to get supported information");
231                 }
232             }
233             return supported;
234         }
235
236         /// <summary>
237         /// Plays a specific feedback pattern.
238         /// </summary>
239         /// <remarks>
240         /// To play Vibration type, app should have http://tizen.org/privilege/haptic privilege.
241         /// </remarks>
242         /// <since_tizen> 3 </since_tizen>
243         /// <param name="type">The feedback type.</param>
244         /// <param name="pattern">The feedback pattern string.</param>
245         /// <feature>
246         /// http://tizen.org/feature/feedback.vibration for FeedbackType.Vibration
247         /// </feature>
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>
254         /// <example>
255         /// <code>
256         /// Feedback feedback = new Feedback();
257         /// feedback.Play(FeedbackType.All, "Tap");
258         /// </code>
259         /// </example>
260         public void Play(FeedbackType type, String pattern)
261         {
262             int number;
263             Interop.Feedback.FeedbackError res;
264
265             if (!Pattern.TryGetValue(pattern, out number))
266                 throw new ArgumentException($"Not supported pattern string : {pattern}", nameof(pattern));
267
268             if (type == FeedbackType.All)
269                 res = (Interop.Feedback.FeedbackError)Interop.Feedback.Play(number);
270             else
271                 res = (Interop.Feedback.FeedbackError)Interop.Feedback.PlayType((Interop.Feedback.FeedbackType)type, number);
272
273             if (res != Interop.Feedback.FeedbackError.None)
274             {
275                 Log.Warn(LogTag, string.Format("Failed to play feedback. err = {0}", res));
276                 switch (res)
277                 {
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:
287                     default:
288                         throw new InvalidOperationException("Failed to play pattern");
289                 }
290             }
291         }
292
293         /// <summary>
294         /// Stops to play the feedback.
295         /// </summary>
296         /// <remarks>
297         /// To stop vibration, the application should have http://tizen.org/privilege/haptic privilege.
298         /// </remarks>
299         /// <since_tizen> 3 </since_tizen>
300         /// <feature>
301         /// http://tizen.org/feature/feedback.vibration
302         /// </feature>
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>
309         /// <example>
310         /// <code>
311         /// Feedback Feedback1 = new Feedback();
312         /// Feedback1.Stop();
313         /// </code>
314         /// </example>
315         public void Stop()
316         {
317             Interop.Feedback.FeedbackError res = (Interop.Feedback.FeedbackError)Interop.Feedback.Stop();
318
319             if (res != Interop.Feedback.FeedbackError.None)
320             {
321                 Log.Warn(LogTag, string.Format("Failed to stop feedback. err = {0}", res));
322                 switch (res)
323                 {
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:
333                     default:
334                         throw new InvalidOperationException("Failed to stop pattern");
335                 }
336             }
337         }
338     }
339 }