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.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Applications
24 /// Enumeration for Alarm Week Flag, the days of the week.
27 public enum AlarmWeekFlag
30 /// Identifier for Sunday.
35 /// Identifier for Monday.
40 /// Identifier for Tuesday.
45 /// Identifier for Wednesday.
50 /// Identifier for Thursday.
55 /// Identifier for Friday.
60 /// Identifier for Saturday.
65 /// All Days of the Week.
67 AllDays = Sunday |Monday|Tuesday|Wednesday|Thursday|Friday|Saturday,
72 WeekDays = Monday | Tuesday | Wednesday | Thursday | Friday
76 /// Mobile devices typically give constant access to information from various sources.Some of this information is best delivered through alarms -
77 /// 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.
78 /// They are also better than putting an interface to sleep because they do not block your main UI thread.
79 /// Use of alarms helps build smooth user experiences and implements unattended data synchronization tasks.
80 /// If an application is installed after setting the alarm, your alarm is cancelled automatically.
84 /// public class AlarmManagerExample
87 /// Alarm alarm = AlarmManager.CreateAlarm(24000,1000,null);
88 /// AlarmManager.CancelAll();
93 public static class AlarmManager
95 private const string LogTag = "Tizen.Applications.Alarm";
97 private static Interop.Alarm.DateTime ConvertDateTimeToStruct(DateTime value)
99 Interop.Alarm.DateTime time = new Interop.Alarm.DateTime();
100 time.sec = value.Second;
101 time.min = value.Minute;
102 time.hour = value.Hour;
103 time.mday = value.Day;
104 time.mon = value.Month - 1;
105 time.year = value.Year - 1900;
106 time.wday = (int)value.DayOfWeek;
107 time.yday = value.DayOfYear;
112 internal static DateTime ConvertIntPtrToDateTime(Interop.Alarm.DateTime time)
114 DateTime value = new DateTime(1900 + time.year, 1 + time.mon, time.mday, time.hour, time.min, time.sec, DateTimeKind.Utc);
119 /// Sets an alarm to be triggered after a specific time.
120 /// The alarm will first go off delay seconds later and then will go off every certain amount of time defined using period seconds.
122 /// <param name="delay">The amount of time before the first execution (in seconds).</param>
123 /// <param name="period"> The amount of time between subsequent alarms (in seconds). This value does not guarantee the accuracy.
124 /// The actual interval is calculated by the OS. The minimum value is 600sec</param>
125 /// <param name="appControl"> The destination AppControl to perform a specific task when the alarm is triggered </param>
126 /// <returns>Alarm Instance created with the set param values.</returns>
127 /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</exception>
128 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
129 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
130 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
131 public static Alarm CreateAlarm(int delay, int period, AppControl appControl)
135 SafeAppControlHandle handle = (appControl == null) ? null : appControl.SafeAppControlHandle;
136 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmAfterDelay(handle, delay, period, out alarmId);
137 alarm = new Alarm(alarmId);
138 if (ret != AlarmError.None)
140 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
147 /// Sets an alarm to be triggered after a specific time.
148 /// The alarm will go off delay seconds later.
150 /// <param name="delay"> The amount of time before the execution (in seconds) </param>
151 /// <param name="appControl"> The destination AppControl to perform a specific task when the alarm is triggered </param>
152 /// <returns> Alarm Instance created with the set param values.</returns>
153 /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</exception>
154 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
155 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
156 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
157 public static Alarm CreateAlarm(int delay, AppControl appControl)
161 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmOnceAfterDelay(appControl.SafeAppControlHandle, delay, out alarmId);
162 alarm = new Alarm(alarmId);
163 if (ret != AlarmError.None)
165 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
172 /// Sets an alarm to be triggered at a specific time.
173 /// The date describes the time of the first occurrence.
175 /// <param name="value"> The first active alarm time </param>
176 /// <param name="appControl"> The destination AppControl to perform specific work when the alarm is triggered </param>
177 /// <returns> Alarm Instance created with the set param values.</returns>
178 /// <remarks>This operation is permitted wit UI application appcontrol only.</remarks>
179 /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</exception>
180 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
181 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
182 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
183 public static Alarm CreateAlarm(DateTime value, AppControl appControl)
187 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(value);
188 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmOnceAtDate(appControl.SafeAppControlHandle, ref time, out alarmId);
189 alarm = new Alarm(alarmId);
190 if (ret != AlarmError.None)
192 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
199 /// Sets an alarm to be triggered periodically, starting at a specific time.
200 /// The date describes the time of the first occurrence.
201 /// weekFlag is the repeat value of the days of the week.
202 /// If weekFlag is AlarmWeekFlag.Tuesday, the alarm will repeat every Tuesday at a specific time.
204 /// <remarks>This operation is permitted wit UI application appcontrol only.</remarks>
205 /// <param name="value"> The first active alarm time </param>
206 /// <param name="weekFlag"> The day of the week, AlarmWeekFlag may be a combination of days, like AlarmWeekFlag.Sunday | AlarmWeekFlag.Monday</param>
207 /// <param name="appControl"> The destination AppControl to perform specific work when the alarm is triggered </param>
208 /// <returns> Alarm Instance created with the set param values.</returns>
209 /// <exception cref="ArgumentException">Thrown in case of Invalid parmaeter.</exception>
210 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
211 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
212 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
213 public static Alarm CreateAlarm(DateTime value, AlarmWeekFlag weekFlag, AppControl appControl)
217 Interop.Alarm.DateTime time = ConvertDateTimeToStruct(value);
218 AlarmError ret = (AlarmError)Interop.Alarm.CreateAlarmRecurWeek(appControl.SafeAppControlHandle, ref time, (int)weekFlag, out alarmId);
219 alarm = new Alarm(alarmId);
220 if (ret != AlarmError.None)
222 throw AlarmErrorFactory.GetException(ret, "Failed to create Alarm");
229 /// Cancels all scheduled alarms that are registered by the application that calls this API.
231 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
232 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
233 /// <privilege>http://tizen.org/privilege/alarm.set</privilege>
234 public static void CancelAll()
236 AlarmError ret = (AlarmError)Interop.Alarm.CancelAllAlarms();
237 if (ret != AlarmError.None)
239 throw AlarmErrorFactory.GetException(ret, "Failed to cancel Alarms");
244 /// Retrieves all registered alarms.
246 /// <returns>List of all Alarm instances.</returns>
247 /// <exception cref="UnauthorizedAccessException">Thrown in case of permission denied.</exception>
248 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
249 /// <privilege>http://tizen.org/privilege/alarm.get</privilege>
250 public static IEnumerable<Alarm> GetAllSceduledAlarms()
252 List<Alarm> alarms = new List<Alarm>();
253 Interop.Alarm.RegisteredAlarmCallback callback = (int alarmId, IntPtr userData) =>
255 alarms.Add(new Alarm(alarmId));
259 AlarmError ret = (AlarmError)Interop.Alarm.GetAllRegisteredAlarms(callback, IntPtr.Zero);
260 if (ret != AlarmError.None)
262 throw AlarmErrorFactory.GetException(ret, "Failed to get Alarms");
269 /// Gets the current system time.
271 /// <returns>The current system time</returns>
272 /// <exception cref="InvalidOperationException">Thrown in case of any internal error.</exception>
273 public static DateTime GetCurrentTime()
276 Interop.Alarm.DateTime value;
277 AlarmError ret = (AlarmError)Interop.Alarm.GetCurrentTime(out value);
278 if (ret != AlarmError.None)
280 throw AlarmErrorFactory.GetException(ret, "Failed to get Currenttime");
285 time = ConvertIntPtrToDateTime(value);