Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Notification / Tizen.Applications.Notifications / NotificationAccessorySetBinder.cs
1 /*
2  * Copyright (c) 2017 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 namespace Tizen.Applications.Notifications
18 {
19     using Tizen.Common;
20
21     internal static class AccessorySetBinder
22     {
23         internal static void BindObject(Notification notification)
24         {
25             BindLedToHandle(notification);
26             BindSoundToHandle(notification);
27             BindVibrationToHandle(notification);
28         }
29
30         internal static void BindSafeHandle(Notification notification)
31         {
32             Notification.AccessorySet accessory = new Notification.AccessorySet();
33             BindHandleToLed(notification, accessory);
34             BindHandleToSound(notification, accessory);
35             BindHandleToVibration(notification, accessory);
36             notification.Accessory = accessory;
37         }
38
39         private static void BindLedToHandle(Notification notification)
40         {
41             NotificationError ret = NotificationError.None;
42             Notification.AccessorySet accessory = notification.Accessory;
43
44             ret = Interop.Notification.SetLed(notification.Handle, accessory.LedOption, 0);
45             if (ret != NotificationError.None)
46             {
47                 throw NotificationErrorFactory.GetException(ret, "unable to set led");
48             }
49
50             ret = Interop.Notification.SetLedTimePeriod(notification.Handle, accessory.LedOnMillisecond, accessory.LedOffMillisecond);
51             if (ret != NotificationError.None)
52             {
53                 throw NotificationErrorFactory.GetException(ret, "unable to set led period");
54             }
55
56             if (notification.Accessory.LedOption == AccessoryOption.Custom)
57             {
58                 Color color = accessory.LedColor;
59                 ret = Interop.Notification.SetLed(notification.Handle, AccessoryOption.Custom, color.GetArgb());
60                 if (ret != NotificationError.None)
61                 {
62                     throw NotificationErrorFactory.GetException(ret, "unable to set led color");
63                 }
64             }
65         }
66
67         private static void BindVibrationToHandle(Notification notification)
68         {
69             Notification.AccessorySet accessory = notification.Accessory;
70             if (accessory.CanVibrate == false)
71             {
72                 Interop.Notification.SetVibration(notification.Handle, AccessoryOption.Off, null);
73             }
74             else
75             {
76                 Interop.Notification.SetVibration(notification.Handle, AccessoryOption.On, null);
77             }
78         }
79
80         private static void BindSoundToHandle(Notification notification)
81         {
82             Notification.AccessorySet accessory = notification.Accessory;
83
84             if (accessory.SoundOption == AccessoryOption.Custom && string.IsNullOrEmpty(accessory.SoundPath))
85             {
86                 throw NotificationErrorFactory.GetException(NotificationError.InvalidParameter, "If the option is set to Custom, the path must also be set.");
87             }
88
89             NotificationError ret = Interop.Notification.SetSound(notification.Handle, accessory.SoundOption, accessory.SoundPath);
90             if (ret != NotificationError.None)
91             {
92                 throw NotificationErrorFactory.GetException(ret, "unable to set sound");
93             }
94         }
95
96         private static void BindHandleToLed(Notification notification, Notification.AccessorySet accessory)
97         {
98             AccessoryOption type;
99             int argb;
100             Interop.Notification.GetLed(notification.Handle, out type, out argb);
101
102             accessory.LedOption = type;
103             if (type == AccessoryOption.Custom)
104             {
105                 accessory.LedColor = new Color(argb >> 16 & 255, argb >> 8 & 255, argb >> 0 & 255, argb >> 24 & 255);
106             }
107
108             int onMillisecond, offMillisecond;
109             Interop.Notification.GetLedTimePeriod(notification.Handle, out onMillisecond, out offMillisecond);
110             accessory.LedOnMillisecond = onMillisecond;
111             accessory.LedOffMillisecond = offMillisecond;
112         }
113
114         private static void BindHandleToSound(Notification notification, Notification.AccessorySet accessory)
115         {
116             AccessoryOption type;
117             string path;
118
119             Interop.Notification.GetSound(notification.Handle, out type, out path);
120
121             accessory.SoundOption = type;
122             if (type == AccessoryOption.Custom)
123             {
124                 accessory.SoundPath = path;
125             }
126         }
127
128         private static void BindHandleToVibration(Notification notification, Notification.AccessorySet accessory)
129         {
130             AccessoryOption type;
131             string path;
132
133             Interop.Notification.GetVibration(notification.Handle, out type, out path);
134             if (type == AccessoryOption.Off)
135             {
136                 accessory.CanVibrate = false;
137             }
138             else
139             {
140                 accessory.CanVibrate = true;
141             }
142         }
143     }
144 }