47cb15531401a83aa0efb965752f5de3b5ae0440
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / AudioManager / VolumeLevel.cs
1 /*
2  * Copyright (c) 2016 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
19 namespace Tizen.Multimedia
20 {
21
22     /// <summary>
23     /// Provides a means to control volume levels.
24     /// </summary>
25     public class VolumeLevel
26     {
27         internal VolumeLevel()
28         {
29         }
30
31         /// <summary>
32         /// Gets or sets the volume level of the specified <see cref="AudioVolumeType"/>
33         /// </summary>
34         /// <param name="type">The <see cref="AudioVolumeType"/> to control.</param>
35         /// <value>The current volume level.</value>
36         /// <remarks>To set volumes, the specified privilege is required.</remarks>
37         /// <privilege>http://tizen.org/privilege/volume.set</privilege>
38         /// <exception cref="ArgumentException"><paramref name="type"/> is invalid.</exception>
39         /// <exception cref="ArgumentOutOfRangeException">
40         ///     <paramref name="type"/> is <see cref="AudioVolumeType.None"/>.
41         ///     -or-
42         ///     <paramref name="value"/> is less than zero.
43         ///     -or-
44         ///     <paramref name="value"/> is greater than <see cref="MaxVolumeLevel.this[AudioVolumeType]"/>.
45         /// </exception>
46         /// <exception cref="UnauthorizedAccessException">Caller does not have required privilege to set volume.</exception>
47         public int this[AudioVolumeType type]
48         {
49             get
50             {
51                 ValidationUtil.ValidateEnum(typeof(AudioVolumeType), type, nameof(type));
52
53                 if (type == AudioVolumeType.None)
54                 {
55                     throw new ArgumentOutOfRangeException(nameof(type),
56                         "Cannot get volume level for AudioVolumeType.None");
57                 }
58
59                 Interop.AudioVolume.GetVolume(type, out var volume).Validate("Failed to get the volume level");
60
61                 return volume;
62             }
63             set
64             {
65                 ValidationUtil.ValidateEnum(typeof(AudioVolumeType), type, nameof(value));
66
67                 if (type == AudioVolumeType.None)
68                 {
69                     throw new ArgumentOutOfRangeException(nameof(type),
70                         "Cannot set volume level for AudioVolumeType.None");
71                 }
72
73                 var ret = Interop.AudioVolume.SetVolume(type, value);
74
75                 if (ret == AudioManagerError.InvalidParameter)
76                 {
77                     throw new ArgumentOutOfRangeException(nameof(value), value,
78                         $"valid volume level range is 0 <= x <= {nameof(MaxVolumeLevel)}[{nameof(AudioVolumeType)}]");
79                 }
80
81                 ret.Validate("Failed to set the volume level");
82             }
83         }
84     }
85 }