1 /*
2 * Copyright 2016 The Android Open Source Project
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 package com.example.android.directboot.alarms;
18 
19 import android.app.AlarmManager;
20 import android.app.PendingIntent;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.util.Log;
24 
25 import java.util.Calendar;
26 
27 /**
28  * Utility class for alarms.
29  */
30 public class AlarmUtil {
31 
32     private static final String TAG = "AlarmUtil";
33     private final Context mContext;
34     private final AlarmManager mAlarmManager;
35 
AlarmUtil(Context context)36     public AlarmUtil(Context context) {
37         mContext = context;
38         mAlarmManager = mContext.getSystemService(AlarmManager.class);
39     }
40 
41     /**
42      * Schedules an alarm using {@link AlarmManager}.
43      *
44      * @param alarm the alarm to be scheduled
45      */
scheduleAlarm(Alarm alarm)46     public void scheduleAlarm(Alarm alarm) {
47         Intent intent = new Intent(mContext, AlarmIntentService.class);
48         intent.putExtra(AlarmIntentService.ALARM_KEY, alarm);
49         PendingIntent pendingIntent = PendingIntent
50                 .getService(mContext, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
51         Calendar alarmTime = Calendar.getInstance();
52         alarmTime.set(Calendar.MONTH, alarm.month);
53         alarmTime.set(Calendar.DATE, alarm.date);
54         alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour);
55         alarmTime.set(Calendar.MINUTE, alarm.minute);
56 
57         AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(
58                 alarmTime.getTimeInMillis(),
59                 pendingIntent);
60         mAlarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
61         Log.i(TAG,
62                 String.format("Alarm scheduled at (%2d:%02d) Date: %d, Month: %d",
63                         alarm.hour, alarm.minute,
64                         alarm.month, alarm.date));
65     }
66 
67     /**
68      * Cancels the scheduled alarm.
69      *
70      * @param alarm the alarm to be canceled.
71      */
cancelAlarm(Alarm alarm)72     public void cancelAlarm(Alarm alarm) {
73         Intent intent = new Intent(mContext, AlarmIntentService.class);
74         intent.putExtra(AlarmIntentService.ALARM_KEY, alarm);
75         PendingIntent pendingIntent = PendingIntent
76                 .getService(mContext, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
77         mAlarmManager.cancel(pendingIntent);
78     }
79 
80     /**
81      * Returns a next alarm time (nearest day) Calendar instance with the hour and the minute.
82      *
83      * @param hour the integer of the hour an alarm should go off
84      * @param minute the integer of the minute an alarm should go off
85      * @return a {@link Calendar} instance an alarm should go off given the passed hour and the
86      *         minute
87      */
getNextAlarmTime(int hour, int minute)88     public Calendar getNextAlarmTime(int hour, int minute) {
89         Calendar alarmTime = Calendar.getInstance();
90         alarmTime.set(Calendar.HOUR_OF_DAY, hour);
91         alarmTime.set(Calendar.MINUTE, minute);
92         if ((alarmTime.getTimeInMillis() - System.currentTimeMillis()) < 0) {
93             alarmTime.add(Calendar.DATE, 1);
94         }
95         return alarmTime;
96     }
97 }
98