Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.Alarm / Tizen.Applications / Alarm.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.Applications
20 {
21     /// <summary>
22     /// The Alarm API allows setting an "alarm clock" for the delivery of a notification at some point in the future.
23     /// </summary>
24     /// <example>
25     /// <code>
26     /// public class AlarmExample
27     /// {
28     ///     /// ...
29     ///     IEnumerable &lt; Alarm &gt; alarms = AlarmManager.GetAllScheduledAlarms();
30     ///     alarms[0].Cancel();
31     /// }
32     /// </code>
33     /// </example>
34     public class Alarm
35     {
36         private const string _logTag = "Tizen.Applications.Alarm";
37
38         /// <summary>
39         /// Constructor created with new AlarmId.
40         /// </summary>
41         /// <param name="id"></param>
42         internal Alarm(int id)
43         {
44             AlarmId = id;
45         }
46
47         /// <summary>
48         /// The alarm ID uniquely identifies an alarm.
49         /// </summary>
50         public int AlarmId
51         {
52             get; private set;
53         }
54
55         /// <summary>
56         /// Gets the recurrence days of the week.
57         /// </summary>
58         /// <privilege>http://tizen.org/privilege/alarm.get</privilege>
59         /// <remarks>
60         /// week_flag may be a combination of days, like Tuesday | Friday
61         /// </remarks>
62         public AlarmWeekFlag WeekFlag
63         {
64             get
65             {
66                 int week;
67                 AlarmError ret = (AlarmError)Interop.Alarm.GetAlarmWeekFlag(AlarmId, out week);
68                 if (ret != AlarmError.None)
69                 {
70                     Log.Error(_logTag, "Failed to get WeekFlag");
71                 }
72
73                 return (AlarmWeekFlag)week;
74             }
75         }
76
77         /// <summary>
78         /// Gets the scheduled time.
79         /// </summary>
80         /// <privilege>http://tizen.org/privilege/alarm.get</privilege>
81         public DateTime ScheduledDate
82         {
83             get
84             {
85                 Interop.Alarm.DateTime value;
86                 AlarmError ret = (AlarmError)Interop.Alarm.GetAlarmScheduledDate(AlarmId, out value);
87                 if (ret != AlarmError.None)
88                 {
89                     Log.Error(_logTag, "Failed to get WeekFlag");
90                 }
91
92                 DateTime time = AlarmManager.ConvertIntPtrToDateTime(value);
93                 return time;
94             }
95         }
96
97         /// <summary>
98         /// Gets the period of time between the recurrent alarms.
99         /// </summary>
100         /// <privilege>http://tizen.org/privilege/alarm.get</privilege>
101         public int Period
102         {
103             get
104             {
105                 int period;
106                 AlarmError ret = (AlarmError)Interop.Alarm.GetAlarmScheduledPeriod(AlarmId, out period);
107                 if (ret != AlarmError.None)
108                 {
109                     Log.Error(_logTag, "Failed to get WeekFlag");
110                 }
111
112                 return period;
113             }
114         }
115
116         /// <summary>
117         /// Gets the AppControl to be invoked when the the alarm is triggered.
118         /// </summary>
119         /// <privilege>http://tizen.org/privilege/alarm.get</privilege>
120         public AppControl AlarmAppControl
121         {
122             get
123             {
124                 SafeAppControlHandle handle;
125                 AlarmError ret = (AlarmError)Interop.Alarm.GetAlarmAppControl(AlarmId, out handle);
126
127                 if (ret != AlarmError.None)
128                 {
129                     Log.Error(_logTag, "Failed to get WeekFlag");
130                 }
131
132                 return new AppControl(handle);
133             }
134         }
135
136         /// <summary>
137         /// Gets whether the alarm will launch global application or not.
138         /// </summary>
139         /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
140         /// <privilege>http://tizen.org/privilege/alarm.get</privilege>
141         public bool Global
142         {
143             get
144             {
145                 bool global;
146                 AlarmError ret = (AlarmError)Interop.Alarm.GetAlarmGlobalFlag(AlarmId, out global);
147                 if (ret != AlarmError.None)
148                 {
149                     Log.Error(_logTag, "Failed to get WeekFlag");
150                 }
151
152                 return global;
153             }
154
155             set
156             {
157                 AlarmError ret = (AlarmError)Interop.Alarm.SetAlarmGlobalFlag(AlarmId, value);
158                 if (ret != AlarmError.None)
159                 {
160                     Log.Error(_logTag, "Failed to get WeekFlag");
161                 }
162             }
163         }
164
165         /// <summary>
166         /// Cancels the the specific alarm.
167         /// </summary>
168         /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</exception>
169         /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied due to insufficient previlleges.</exception>
170         /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
171         /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
172         public void Cancel()
173         {
174             AlarmError ret = (AlarmError)Interop.Alarm.CancelAlarm(AlarmId);
175             if (ret != AlarmError.None)
176             {
177                 throw AlarmErrorFactory.GetException(ret, "Failed to Cancel alarm");
178             }
179         }
180     }
181 }