| /* |
| * Copyright 2016 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.example.android.directboot.alarms; |
| |
| import android.app.AlarmManager; |
| import android.app.PendingIntent; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.util.Log; |
| |
| import java.util.Calendar; |
| |
| /** |
| * Utility class for alarms. |
| */ |
| public class AlarmUtil { |
| |
| private static final String TAG = "AlarmUtil"; |
| private final Context mContext; |
| private final AlarmManager mAlarmManager; |
| |
| public AlarmUtil(Context context) { |
| mContext = context; |
| mAlarmManager = mContext.getSystemService(AlarmManager.class); |
| } |
| |
| /** |
| * Schedules an alarm using {@link AlarmManager}. |
| * |
| * @param alarm the alarm to be scheduled |
| */ |
| public void scheduleAlarm(Alarm alarm) { |
| Intent intent = new Intent(mContext, AlarmIntentService.class); |
| intent.putExtra(AlarmIntentService.ALARM_KEY, alarm); |
| PendingIntent pendingIntent = PendingIntent |
| .getService(mContext, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| Calendar alarmTime = Calendar.getInstance(); |
| alarmTime.set(Calendar.MONTH, alarm.month); |
| alarmTime.set(Calendar.DATE, alarm.date); |
| alarmTime.set(Calendar.HOUR_OF_DAY, alarm.hour); |
| alarmTime.set(Calendar.MINUTE, alarm.minute); |
| |
| AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo( |
| alarmTime.getTimeInMillis(), |
| pendingIntent); |
| mAlarmManager.setAlarmClock(alarmClockInfo, pendingIntent); |
| Log.i(TAG, |
| String.format("Alarm scheduled at (%2d:%02d) Date: %d, Month: %d", |
| alarm.hour, alarm.minute, |
| alarm.month, alarm.date)); |
| } |
| |
| /** |
| * Cancels the scheduled alarm. |
| * |
| * @param alarm the alarm to be canceled. |
| */ |
| public void cancelAlarm(Alarm alarm) { |
| Intent intent = new Intent(mContext, AlarmIntentService.class); |
| intent.putExtra(AlarmIntentService.ALARM_KEY, alarm); |
| PendingIntent pendingIntent = PendingIntent |
| .getService(mContext, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| mAlarmManager.cancel(pendingIntent); |
| } |
| |
| /** |
| * Returns a next alarm time (nearest day) Calendar instance with the hour and the minute. |
| * |
| * @param hour the integer of the hour an alarm should go off |
| * @param minute the integer of the minute an alarm should go off |
| * @return a {@link Calendar} instance an alarm should go off given the passed hour and the |
| * minute |
| */ |
| public Calendar getNextAlarmTime(int hour, int minute) { |
| Calendar alarmTime = Calendar.getInstance(); |
| alarmTime.set(Calendar.HOUR_OF_DAY, hour); |
| alarmTime.set(Calendar.MINUTE, minute); |
| if ((alarmTime.getTimeInMillis() - System.currentTimeMillis()) < 0) { |
| alarmTime.add(Calendar.DATE, 1); |
| } |
| return alarmTime; |
| } |
| } |