2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 namespace Tizen.Applications
20 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using Tizen.Applications.Notifications;
25 /// Enumeration for alarm week flag, the days of the week.
27 /// <since_tizen> 3 </since_tizen>
29 public enum AlarmWeekFlag
32 /// An identifier for Sunday.
37 /// An identifier for Monday.
42 /// An identifier for Tuesday.
47 /// An identifier for Wednesday.
52 /// An identifier for Thursday.
57 /// An identifier for Friday.
62 /// An identifier for Saturday.
67 /// All days of the week.
69 AllDays = Sunday |Monday|Tuesday|Wednesday|Thursday|Friday|Saturday,
74 WeekDays = Monday | Tuesday | Wednesday | Thursday | Friday
78 /// Mobile devices typically give constant access to information from various sources. Some of this information is best delivered through alarms.
79 /// The most obvious case is a calendar scheduling application, which lets you know when a meeting is about to start. Alarms are certainly better than actively waiting in a loop.
80 /// They are also better than putting an interface to sleep because they do not block your main UI thread.
81 /// Use of alarms helps build smooth user experiences and implements unattended data synchronization tasks.
82 /// If an application is installed after setting the alarm, your alarm is canceled automatically.
86 /// public class AlarmManagerExample
89 /// Alarm alarm = AlarmManager.CreateAlarm(24000,1000,null);
90 /// AlarmManager.CancelAll();
94 /// <since_tizen> 3 </since_tizen>
96 public static class AlarmManager
98 private const string LogTag = "Tizen.Applications.Alarm";
100 private static Interop.Alarm.DateTime ConvertDateTimeToStruct(DateTime value)
102 Interop.Alarm.DateTime time = new Interop.Alarm.DateTime();
103 time.sec = value.Second;
104 time.min = value.Minute;
105 time.hour = value.Hour;
106 time.mday = value.Day;
107 time.mon = value.Month - 1;
108 time.year = value.Year - 1900;
109 time.wday = (int)value.DayOfWeek;
110 time.yday = value.DayOfYear;
115 internal static DateTime ConvertIntPtrToDateTime(Interop.Alarm.DateTime time)
117 DateTime value = new DateTime(1900 + time.year, 1 + time.mon, time.mday, time.hour, time.min, time.sec, DateTimeKind.Utc);
122 /// Sets an alarm to be triggered after a specific time.
123 /// The alarm will first go off delay seconds later and then will go off every certain amount of time defined using period seconds.
125 /// <param name="delay">The amount of time before the first execution (in seconds).</param>
126 /// <param name="period"> The amount of time between subsequent alarms (in seconds). This value does not guarantee the accuracy.
127 /// The actual interval is calculated by the OS. The minimum value is 600sec.</param>
128 /// <param name="appControl"> The destination AppControl is used to perform a specific task when the alarm is triggered. </param>
129 /// <returns> An alarm instance is created with the set param values.</returns>
130 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
131 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
132 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
133 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
134 /// <since_tizen> 3 </since_tizen>
135 public static Alarm CreateAlarm(int delay, int period, AppControl appControl)
139 SafeAppControlHandle handle = (appControl == null) ? null : appControl.SafeAppControlHandle;
140 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmAfterDelay(handle, delay, period, out alarmId);
141 alarm = new Alarm(alarmId);
142 if (ret != AlarmError.None)
144 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
151 /// Sets an alarm to be triggered after a specific time.
152 /// The alarm will go off delay seconds later.
154 /// <param name="delay"> The amount of time before the execution (in seconds). </param>
155 /// <param name="appControl"> The destination AppControl to perform a specific task when the alarm is triggered. </param>
156 /// <returns> An alarm instance is created with the set param values.</returns>
157 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
158 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
159 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
160 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
161 /// <since_tizen> 3 </since_tizen>
162 public static Alarm CreateAlarm(int delay, AppControl appControl)
166 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmOnceAfterDelay(appControl.SafeAppControlHandle, delay, out alarmId);
167 alarm = new Alarm(alarmId);
168 if (ret != AlarmError.None)
170 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
177 /// Sets an alarm to be triggered at a specific time.
178 /// The date describes the time of the first occurrence.
180 /// <param name="value"> The first active alarm time. </param>
181 /// <param name="appControl"> The destination AppControl to perform specific work when the alarm is triggered. </param>
182 /// <returns> An alarm instance is created with the set param values.</returns>
183 /// <remarks>This operation is permitted with the UI application appcontrol only.</remarks>
184 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
185 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
186 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
187 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
188 /// <since_tizen> 3 </since_tizen>
189 public static Alarm CreateAlarm(DateTime value, AppControl appControl)
193 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(value);
194 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmOnceAtDate(appControl.SafeAppControlHandle, ref time, out alarmId);
195 alarm = new Alarm(alarmId);
196 if (ret != AlarmError.None)
198 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
205 /// Sets an alarm to be triggered periodically, starting at a specific time.
206 /// The date describes the time of the first occurrence.
207 /// The weekFlag is the repeat value of the days of the week.
208 /// If the weekFlag is AlarmWeekFlag.Tuesday, the alarm will repeat every Tuesday at a specific time.
210 /// <remarks>This operation is permitted with UI application appcontrol only.</remarks>
211 /// <param name="value"> The first active alarm time. </param>
212 /// <param name="weekFlag"> The day of the week, AlarmWeekFlag may be a combination of days, like AlarmWeekFlag.Sunday | AlarmWeekFlag.Monday.</param>
213 /// <param name="appControl"> The destination AppControl to perform specific work when the alarm is triggered. </param>
214 /// <returns> An alarm instance is created with the set param values.</returns>
215 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
216 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
217 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
218 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
219 /// <since_tizen> 3 </since_tizen>
220 public static Alarm CreateAlarm(DateTime value, AlarmWeekFlag weekFlag, AppControl appControl)
224 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(value);
225 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmRecurWeek(appControl.SafeAppControlHandle, ref time, (int)weekFlag, out alarmId);
226 alarm = new Alarm(alarmId);
227 if (ret != AlarmError.None)
229 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
236 /// Sets a notification alarm to be triggered at a specific time.
237 /// The date describes the time of the first occurrence.
239 /// <param name="dateTime"> The first active alarm time. </param>
240 /// <param name="notification"> The notification to be posted when the alarm is triggered. </param>
241 /// <returns> An alarm instance is created with the set param values.</returns>
242 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
243 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
244 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
245 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
246 /// <privilege>http://tizen.org/privilege/notification</privilege>
247 /// <since_tizen> 3 </since_tizen>
248 public static Alarm CreateAlarm(DateTime dateTime, Notification notification)
252 NotificationSafeHandle safeHandle = NotificationManager.MakeNotificationSafeHandle(notification);
253 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(dateTime);
254 AlarmError ret = Interop.Alarm.CreateAlarmNotiOnceAtDate(safeHandle, ref time, out alarmId);
255 if (ret != AlarmError.None)
257 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
260 alarm = new Alarm(alarmId);
266 /// Sets a notification alarm to be triggered after a specific time.
267 /// The alarm will first go off delay seconds later and then will go off every certain amount of time defined using period seconds.
269 /// <param name="delay">The amount of time before the first execution (in seconds). </param>
270 /// <param name="period"> The amount of time between subsequent alarms (in seconds). This value does not guarantee the accuracy. </param>
271 /// <param name="notification"> The notification to be posted when the alarm is triggered. </param>
272 /// <returns> An alarm instance is created with the set param values.</returns>
273 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
274 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
275 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
276 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
277 /// <privilege>http://tizen.org/privilege/notification</privilege>
278 /// <since_tizen> 3 </since_tizen>
279 public static Alarm CreateAlarm(int delay, int period, Notification notification)
283 NotificationSafeHandle safeHandle = NotificationManager.MakeNotificationSafeHandle(notification);
284 AlarmError ret = Interop.Alarm.CreateAlarmNotiAfterDelay(safeHandle, delay, period, out alarmId);
285 if (ret != AlarmError.None)
287 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
290 alarm = new Alarm(alarmId);
296 /// Sets a notification alarm to be triggered periodically, starting at a specific time.
297 /// The date describes the time of the first occurrence.
298 /// The weekFlag is the repeat value of the days of the week.
299 /// If the weekFlag is AlarmWeekFlag.Tuesday, the alarm will repeat every Tuesday at a specific time.
301 /// <param name="dateTime"> The first active alarm time. </param>
302 /// <param name="weekFlag"> The day of the week, AlarmWeekFlag may be a combination of days,
303 /// like AlarmWeekFlag.Sunday | AlarmWeekFlag.Monday.</param>
304 /// <param name="notification"> The notification to be posted when the alarm is triggered. </param>
305 /// <returns> An alarm instance is created with the set param values.</returns>
306 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
307 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
308 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
309 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
310 /// <privilege>http://tizen.org/privilege/notification</privilege>
311 /// <since_tizen> 3 </since_tizen>
312 public static Alarm CreateAlarm(DateTime dateTime, AlarmWeekFlag weekFlag, Notification notification)
316 NotificationSafeHandle safeHandle = NotificationManager.MakeNotificationSafeHandle(notification);
317 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(dateTime);
318 AlarmError ret = Interop.Alarm.CreateAlarmNotiRecurWeek(safeHandle, ref time, (int)weekFlag, out alarmId);
319 if (ret != AlarmError.None)
321 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
324 alarm = new Alarm(alarmId);
330 /// Sets a notification alarm to be triggered after a specific time.
331 /// The alarm will go off delay seconds later.
333 /// <param name="delay">The amount of time before the first execution (in seconds).</param>
334 /// <param name="notification"> The notification to be posted when the alarm is triggered. </param>
335 /// <returns> An alarm instance is created with the set param values.</returns>
336 /// <exception cref="ArgumentException">Thrown in case of an invalid parameter.</exception>
337 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
338 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
339 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
340 /// <privilege>http://tizen.org/privilege/notification</privilege>
341 /// <since_tizen> 3 </since_tizen>
342 public static Alarm CreateAlarm(int delay, Notification notification)
346 NotificationSafeHandle safeHandle = NotificationManager.MakeNotificationSafeHandle(notification);
347 AlarmError ret = Interop.Alarm.CreateAlarmNotiOnceAfterDelay(safeHandle, delay, out alarmId);
348 if (ret != AlarmError.None)
350 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
353 alarm = new Alarm(alarmId);
359 /// Cancels all scheduled alarms that are registered by the application that calls this API.
361 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
362 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
363 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
364 /// <since_tizen> 3 </since_tizen>
365 public static void CancelAll()
367 AlarmError ret = (AlarmError)Interop.Alarm.CancelAllAlarms();
368 if (ret != AlarmError.None)
370 throw AlarmErrorFactory.GetException(ret, "Failed to cancel Alarms");
375 /// Retrieves all registered alarms.
377 /// <returns>List of all alarm instances.</returns>
378 /// <exception cref="UnauthorizedAccessException">Thrown in case of a permission denied.</exception>
379 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
380 /// <privilege>http://tizen.org/privilege/alarm.get</privilege>
381 /// <since_tizen> 4 </since_tizen>
382 public static IEnumerable<Alarm> GetAllScheduledAlarms()
384 List<Alarm> alarms = new List<Alarm>();
385 Interop.Alarm.RegisteredAlarmCallback callback = (int alarmId, IntPtr userData) =>
387 alarms.Add(new Alarm(alarmId));
391 AlarmError ret = (AlarmError)Interop.Alarm.GetAllRegisteredAlarms(callback, IntPtr.Zero);
392 if (ret != AlarmError.None)
394 throw AlarmErrorFactory.GetException(ret, "Failed to get Alarms");
401 /// Gets the current system time.
403 /// <returns>The current system time.</returns>
404 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
405 /// <since_tizen> 3 </since_tizen>
406 public static DateTime GetCurrentTime()
409 Interop.Alarm.DateTime value;
410 AlarmError ret = (AlarmError)Interop.Alarm.GetCurrentTime(out value);
411 if (ret != AlarmError.None)
413 throw AlarmErrorFactory.GetException(ret, "Failed to get Currenttime");
418 time = ConvertIntPtrToDateTime(value);