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.
28 public enum AlarmWeekFlag
31 /// Identifier for Sunday.
36 /// Identifier for Monday.
41 /// Identifier for Tuesday.
46 /// Identifier for Wednesday.
51 /// Identifier for Thursday.
56 /// Identifier for Friday.
61 /// Identifier for Saturday.
66 /// All Days of the Week.
68 AllDays = Sunday |Monday|Tuesday|Wednesday|Thursday|Friday|Saturday,
73 WeekDays = Monday | Tuesday | Wednesday | Thursday | Friday
77 /// Mobile devices typically give constant access to information from various sources.Some of this information is best delivered through alarms -
78 /// 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.
79 /// They are also better than putting an interface to sleep because they do not block your main UI thread.
80 /// Use of alarms helps build smooth user experiences and implements unattended data synchronization tasks.
81 /// If an application is installed after setting the alarm, your alarm is cancelled automatically.
85 /// public class AlarmManagerExample
88 /// Alarm alarm = AlarmManager.CreateAlarm(24000,1000,null);
89 /// AlarmManager.CancelAll();
94 public static class AlarmManager
96 private const string LogTag = "Tizen.Applications.Alarm";
98 private static Interop.Alarm.DateTime ConvertDateTimeToStruct(DateTime value)
100 Interop.Alarm.DateTime time = new Interop.Alarm.DateTime();
101 time.sec = value.Second;
102 time.min = value.Minute;
103 time.hour = value.Hour;
104 time.mday = value.Day;
105 time.mon = value.Month - 1;
106 time.year = value.Year - 1900;
107 time.wday = (int)value.DayOfWeek;
108 time.yday = value.DayOfYear;
113 internal static DateTime ConvertIntPtrToDateTime(Interop.Alarm.DateTime time)
115 DateTime value = new DateTime(1900 + time.year, 1 + time.mon, time.mday, time.hour, time.min, time.sec, DateTimeKind.Utc);
120 /// Sets an alarm to be triggered after a specific time.
121 /// The alarm will first go off delay seconds later and then will go off every certain amount of time defined using period seconds.
123 /// <param name="delay">The amount of time before the first execution (in seconds).</param>
124 /// <param name="period"> The amount of time between subsequent alarms (in seconds). This value does not guarantee the accuracy.
125 /// The actual interval is calculated by the OS. The minimum value is 600sec</param>
126 /// <param name="appControl"> The destination AppControl to perform a specific task when the alarm is triggered </param>
127 /// <returns>Alarm Instance created with the set param values.</returns>
128 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
129 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
130 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
131 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
132 public static Alarm CreateAlarm(int delay, int period, AppControl appControl)
136 SafeAppControlHandle handle = (appControl == null) ? null : appControl.SafeAppControlHandle;
137 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmAfterDelay(handle, delay, period, out alarmId);
138 alarm = new Alarm(alarmId);
139 if (ret != AlarmError.None)
141 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
148 /// Sets an alarm to be triggered after a specific time.
149 /// The alarm will go off delay seconds later.
151 /// <param name="delay"> The amount of time before the execution (in seconds) </param>
152 /// <param name="appControl"> The destination AppControl to perform a specific task when the alarm is triggered </param>
153 /// <returns> Alarm Instance created with the set param values.</returns>
154 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
155 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
156 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
157 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
158 public static Alarm CreateAlarm(int delay, AppControl appControl)
162 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmOnceAfterDelay(appControl.SafeAppControlHandle, delay, out alarmId);
163 alarm = new Alarm(alarmId);
164 if (ret != AlarmError.None)
166 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
173 /// Sets an alarm to be triggered at a specific time.
174 /// The date describes the time of the first occurrence.
176 /// <param name="value"> The first active alarm time </param>
177 /// <param name="appControl"> The destination AppControl to perform specific work when the alarm is triggered </param>
178 /// <returns> Alarm Instance created with the set param values.</returns>
179 /// <remarks>This operation is permitted wit UI application appcontrol only.</remarks>
180 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
181 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
182 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
183 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
184 public static Alarm CreateAlarm(DateTime value, AppControl appControl)
188 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(value);
189 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmOnceAtDate(appControl.SafeAppControlHandle, ref time, out alarmId);
190 alarm = new Alarm(alarmId);
191 if (ret != AlarmError.None)
193 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
200 /// Sets an alarm to be triggered periodically, starting at a specific time.
201 /// The date describes the time of the first occurrence.
202 /// weekFlag is the repeat value of the days of the week.
203 /// If weekFlag is AlarmWeekFlag.Tuesday, the alarm will repeat every Tuesday at a specific time.
205 /// <remarks>This operation is permitted wit UI application appcontrol only.</remarks>
206 /// <param name="value"> The first active alarm time </param>
207 /// <param name="weekFlag"> The day of the week, AlarmWeekFlag may be a combination of days, like AlarmWeekFlag.Sunday | AlarmWeekFlag.Monday</param>
208 /// <param name="appControl"> The destination AppControl to perform specific work when the alarm is triggered </param>
209 /// <returns> Alarm Instance created with the set param values.</returns>
210 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
211 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
212 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
213 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
214 public static Alarm CreateAlarm(DateTime value, AlarmWeekFlag weekFlag, AppControl appControl)
218 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(value);
219 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmRecurWeek(appControl.SafeAppControlHandle, ref time, (int)weekFlag, out alarmId);
220 alarm = new Alarm(alarmId);
221 if (ret != AlarmError.None)
223 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
230 /// Sets a notification alarm to be triggered at a specific time.
231 /// The date describes the time of the first occurrence.
233 /// <param name="dateTime"> The first active alarm time </param>
234 /// <param name="notification"> The notification to be posted when the alarm is triggered </param>
235 /// <returns> Alarm Instance created with the set param values.</returns>
236 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
237 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
238 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
239 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
240 /// <privilege>http://tizen.org/privilege/notification</privilege>
241 public static Alarm CreateAlarm(DateTime dateTime, Notification notification)
245 NotificationSafeHandle safeHandle = NotificationManager.MakeNotificationSafeHandle(notification);
246 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(dateTime);
247 AlarmError ret = Interop.Alarm.CreateAlarmNotiOnceAtDate(safeHandle, ref time, out alarmId);
248 if (ret != AlarmError.None)
250 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
253 alarm = new Alarm(alarmId);
259 /// Sets a notification alarm to be triggered after a specific time.
260 /// The alarm will first go off delay seconds later and then will go off every certain amount of time defined using period seconds.
262 /// <param name="delay">The amount of time before the first execution (in seconds). </param>
263 /// <param name="period"> The amount of time between subsequent alarms (in seconds). This value does not guarantee the accuracy. </param>
264 /// <param name="notification"> The notification to be posted when the alarm is triggered </param>
265 /// <returns> Alarm Instance created with the set param values.</returns>
266 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
267 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
268 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
269 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
270 /// <privilege>http://tizen.org/privilege/notification</privilege>
271 public static Alarm CreateAlarm(int delay, int period, Notification notification)
275 NotificationSafeHandle safeHandle = NotificationManager.MakeNotificationSafeHandle(notification);
276 AlarmError ret = Interop.Alarm.CreateAlarmNotiAfterDelay(safeHandle, delay, period, out alarmId);
277 if (ret != AlarmError.None)
279 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
282 alarm = new Alarm(alarmId);
288 /// Sets a notification alarm to be triggered periodically, starting at a specific time.
289 /// The date describes the time of the first occurrence.
290 /// weekFlag is the repeat value of the days of the week.
291 /// If weekFlag is AlarmWeekFlag.Tuesday, the alarm will repeat every Tuesday at a specific time.
293 /// <param name="dateTime"> The first active alarm time </param>
294 /// <param name="weekFlag"> The day of the week, AlarmWeekFlag may be a combination of days,
295 /// like AlarmWeekFlag.Sunday | AlarmWeekFlag.Monday</param>
296 /// <param name="notification"> The notification to be posted when the alarm is triggered </param>
297 /// <returns> Alarm Instance created with the set param values.</returns>
298 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
299 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
300 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
301 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
302 /// <privilege>http://tizen.org/privilege/notification</privilege>
303 public static Alarm CreateAlarm(DateTime dateTime, AlarmWeekFlag weekFlag, Notification notification)
307 NotificationSafeHandle safeHandle = NotificationManager.MakeNotificationSafeHandle(notification);
308 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(dateTime);
309 AlarmError ret = Interop.Alarm.CreateAlarmNotiRecurWeek(safeHandle, ref time, (int)weekFlag, out alarmId);
310 if (ret != AlarmError.None)
312 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
315 alarm = new Alarm(alarmId);
321 /// Sets a notification alarm to be triggered after a specific time.
322 /// The alarm will go off delay seconds later.
324 /// <param name="delay">The amount of time before the first execution (in seconds).</param>
325 /// <param name="notification"> The notification to be posted when the alarm is triggered </param>
326 /// <returns> Alarm Instance created with the set param values.</returns>
327 /// <exception cref="ArgumentException">Thrown in case of Invalid parameter.</exception>
328 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
329 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
330 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
331 /// <privilege>http://tizen.org/privilege/notification</privilege>
332 public static Alarm CreateAlarm(int delay, Notification notification)
336 NotificationSafeHandle safeHandle = NotificationManager.MakeNotificationSafeHandle(notification);
337 AlarmError ret = Interop.Alarm.CreateAlarmNotiOnceAfterDelay(safeHandle, delay, out alarmId);
338 if (ret != AlarmError.None)
340 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
343 alarm = new Alarm(alarmId);
349 /// Cancels all scheduled alarms that are registered by the application that calls this API.
351 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
352 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
353 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
354 public static void CancelAll()
356 AlarmError ret = (AlarmError)Interop.Alarm.CancelAllAlarms();
357 if (ret != AlarmError.None)
359 throw AlarmErrorFactory.GetException(ret, "Failed to cancel Alarms");
364 /// Retrieves all registered alarms.
366 /// <returns>List of all Alarm instances.</returns>
367 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
368 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
369 /// <privilege>http://tizen.org/privilege/alarm.get</privilege>
370 public static IEnumerable<Alarm> GetAllScheduledAlarms()
372 List<Alarm> alarms = new List<Alarm>();
373 Interop.Alarm.RegisteredAlarmCallback callback = (int alarmId, IntPtr userData) =>
375 alarms.Add(new Alarm(alarmId));
379 AlarmError ret = (AlarmError)Interop.Alarm.GetAllRegisteredAlarms(callback, IntPtr.Zero);
380 if (ret != AlarmError.None)
382 throw AlarmErrorFactory.GetException(ret, "Failed to get Alarms");
389 /// Gets the current system time.
391 /// <returns>The current system time</returns>
392 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
393 public static DateTime GetCurrentTime()
396 Interop.Alarm.DateTime value;
397 AlarmError ret = (AlarmError)Interop.Alarm.GetCurrentTime(out value);
398 if (ret != AlarmError.None)
400 throw AlarmErrorFactory.GetException(ret, "Failed to get Currenttime");
405 time = ConvertIntPtrToDateTime(value);