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.os.Bundle; 24 import android.util.Log; 25 26 import java.util.Calendar; 27 28 /** 29 * Utility class for alarms. 30 */ 31 public class AlarmUtil { 32 33 private static final String TAG = "AlarmUtil"; 34 private final Context mContext; 35 private final AlarmManager mAlarmManager; 36 AlarmUtil(Context context)37 public AlarmUtil(Context context) { 38 mContext = context; 39 mAlarmManager = mContext.getSystemService(AlarmManager.class); 40 } 41 42 /** 43 * Schedules an alarm using {@link AlarmManager}. 44 * 45 * @param alarm the alarm to be scheduled 46 */ scheduleAlarm(Alarm alarm)47 public void scheduleAlarm(Alarm alarm) { 48 Intent intent = new Intent(mContext, AlarmIntentService.class); 49 Bundle extras = writeAlarm(alarm); 50 intent.putExtras(extras); 51 52 PendingIntent pendingIntent = PendingIntent 53 .getService(mContext, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 54 Calendar alarmTime = Calendar.getInstance(); 55 alarmTime.set(Calendar.MONTH, alarm.month); 56 alarmTime.set(Calendar.DATE, alarm.date); 57 alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour); 58 alarmTime.set(Calendar.MINUTE, alarm.minute); 59 60 AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo( 61 alarmTime.getTimeInMillis(), 62 pendingIntent); 63 mAlarmManager.setAlarmClock(alarmClockInfo, pendingIntent); 64 Log.i(TAG, 65 String.format("Alarm scheduled at (%2d:%02d) Date: %d, Month: %d", 66 alarm.hour, alarm.minute, 67 alarm.month, alarm.date)); 68 } 69 70 /** 71 * Cancels the scheduled alarm. 72 * 73 * @param alarm the alarm to be canceled. 74 */ cancelAlarm(Alarm alarm)75 public void cancelAlarm(Alarm alarm) { 76 Intent intent = new Intent(mContext, AlarmIntentService.class); 77 PendingIntent pendingIntent = PendingIntent 78 .getService(mContext, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 79 mAlarmManager.cancel(pendingIntent); 80 } 81 82 /** 83 * Returns a next alarm time (nearest day) Calendar instance with the hour and the minute. 84 * 85 * @param hour the integer of the hour an alarm should go off 86 * @param minute the integer of the minute an alarm should go off 87 * @return a {@link Calendar} instance an alarm should go off given the passed hour and the 88 * minute 89 */ getNextAlarmTime(int hour, int minute)90 public Calendar getNextAlarmTime(int hour, int minute) { 91 Calendar alarmTime = Calendar.getInstance(); 92 alarmTime.set(Calendar.HOUR_OF_DAY, hour); 93 alarmTime.set(Calendar.MINUTE, minute); 94 if ((alarmTime.getTimeInMillis() - System.currentTimeMillis()) < 0) { 95 alarmTime.add(Calendar.DATE, 1); 96 } 97 return alarmTime; 98 } 99 readAlarm(Bundle extras)100 public static Alarm readAlarm(Bundle extras) { 101 int id = extras.getInt(AlarmIntentService.KEY_ALARM_ID); 102 int month = extras.getInt(AlarmIntentService.KEY_ALARM_MONTH); 103 int date = extras.getInt(AlarmIntentService.KEY_ALARM_DATE); 104 int hour = extras.getInt(AlarmIntentService.KEY_ALARM_HOUR); 105 int minute = extras.getInt(AlarmIntentService.KEY_ALARM_MINUTE); 106 107 return new Alarm(id, month, date, hour, minute); 108 } 109 writeAlarm(Alarm alarm)110 public static Bundle writeAlarm(Alarm alarm){ 111 Bundle extras = new Bundle(); 112 extras.putInt(AlarmIntentService.KEY_ALARM_ID, alarm.id); 113 extras.putInt(AlarmIntentService.KEY_ALARM_MONTH, alarm.month); 114 extras.putInt(AlarmIntentService.KEY_ALARM_DATE, alarm.date); 115 extras.putInt(AlarmIntentService.KEY_ALARM_HOUR, alarm.hour); 116 extras.putInt(AlarmIntentService.KEY_ALARM_MINUTE, alarm.minute); 117 118 return extras; 119 } 120 } 121