1 /* 2 * Copyright (C) 2014 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.android.systemui.statusbar.policy; 18 19 import android.app.AlarmManager; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.os.UserHandle; 25 26 import java.io.FileDescriptor; 27 import java.io.PrintWriter; 28 import java.util.ArrayList; 29 30 public class NextAlarmController extends BroadcastReceiver { 31 32 private final ArrayList<NextAlarmChangeCallback> mChangeCallbacks = new ArrayList<>(); 33 34 private AlarmManager mAlarmManager; 35 private AlarmManager.AlarmClockInfo mNextAlarm; 36 NextAlarmController(Context context)37 public NextAlarmController(Context context) { 38 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 39 IntentFilter filter = new IntentFilter(); 40 filter.addAction(Intent.ACTION_USER_SWITCHED); 41 filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED); 42 context.registerReceiverAsUser(this, UserHandle.ALL, filter, null, null); 43 updateNextAlarm(); 44 } 45 dump(FileDescriptor fd, PrintWriter pw, String[] args)46 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 47 pw.println("NextAlarmController state:"); 48 pw.print(" mNextAlarm="); pw.println(mNextAlarm); 49 } 50 addStateChangedCallback(NextAlarmChangeCallback cb)51 public void addStateChangedCallback(NextAlarmChangeCallback cb) { 52 mChangeCallbacks.add(cb); 53 cb.onNextAlarmChanged(mNextAlarm); 54 } 55 removeStateChangedCallback(NextAlarmChangeCallback cb)56 public void removeStateChangedCallback(NextAlarmChangeCallback cb) { 57 mChangeCallbacks.remove(cb); 58 } 59 onReceive(Context context, Intent intent)60 public void onReceive(Context context, Intent intent) { 61 final String action = intent.getAction(); 62 if (action.equals(Intent.ACTION_USER_SWITCHED) 63 || action.equals(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED)) { 64 updateNextAlarm(); 65 } 66 } 67 updateNextAlarm()68 private void updateNextAlarm() { 69 mNextAlarm = mAlarmManager.getNextAlarmClock(UserHandle.USER_CURRENT); 70 fireNextAlarmChanged(); 71 } 72 fireNextAlarmChanged()73 private void fireNextAlarmChanged() { 74 int n = mChangeCallbacks.size(); 75 for (int i = 0; i < n; i++) { 76 mChangeCallbacks.get(i).onNextAlarmChanged(mNextAlarm); 77 } 78 } 79 80 public interface NextAlarmChangeCallback { onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm)81 void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm); 82 } 83 } 84