blob: 28935bff9ec54e544f573acf7ad756e11b940425 [file] [log] [blame]
Jorim Jaggic7dea6e2014-07-26 14:36:57 +02001/*
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
17package com.android.systemui.statusbar.policy;
18
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020019import android.app.AlarmManager;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.os.UserHandle;
25
Jason Monk88529052016-11-04 13:29:58 -040026import com.android.systemui.statusbar.policy.NextAlarmController.NextAlarmChangeCallback;
27
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020028import java.io.FileDescriptor;
29import java.io.PrintWriter;
30import java.util.ArrayList;
31
Jason Monk88529052016-11-04 13:29:58 -040032public class NextAlarmController extends BroadcastReceiver
33 implements CallbackController<NextAlarmChangeCallback> {
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020034
35 private final ArrayList<NextAlarmChangeCallback> mChangeCallbacks = new ArrayList<>();
36
37 private AlarmManager mAlarmManager;
Jose Lima235510e2014-08-13 12:50:01 -070038 private AlarmManager.AlarmClockInfo mNextAlarm;
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020039
40 public NextAlarmController(Context context) {
41 mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
42 IntentFilter filter = new IntentFilter();
43 filter.addAction(Intent.ACTION_USER_SWITCHED);
44 filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
Selim Cinek9c4a7072014-11-21 17:44:34 +010045 context.registerReceiverAsUser(this, UserHandle.ALL, filter, null, null);
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020046 updateNextAlarm();
47 }
48
49 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
50 pw.println("NextAlarmController state:");
51 pw.print(" mNextAlarm="); pw.println(mNextAlarm);
52 }
53
Jason Monk88529052016-11-04 13:29:58 -040054 public void addCallback(NextAlarmChangeCallback cb) {
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020055 mChangeCallbacks.add(cb);
56 cb.onNextAlarmChanged(mNextAlarm);
57 }
58
Jason Monk88529052016-11-04 13:29:58 -040059 public void removeCallback(NextAlarmChangeCallback cb) {
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020060 mChangeCallbacks.remove(cb);
61 }
62
63 public void onReceive(Context context, Intent intent) {
64 final String action = intent.getAction();
65 if (action.equals(Intent.ACTION_USER_SWITCHED)
66 || action.equals(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED)) {
67 updateNextAlarm();
68 }
69 }
70
71 private void updateNextAlarm() {
72 mNextAlarm = mAlarmManager.getNextAlarmClock(UserHandle.USER_CURRENT);
73 fireNextAlarmChanged();
74 }
75
76 private void fireNextAlarmChanged() {
77 int n = mChangeCallbacks.size();
78 for (int i = 0; i < n; i++) {
79 mChangeCallbacks.get(i).onNextAlarmChanged(mNextAlarm);
80 }
81 }
82
83 public interface NextAlarmChangeCallback {
Jose Lima235510e2014-08-13 12:50:01 -070084 void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm);
Jorim Jaggic7dea6e2014-07-26 14:36:57 +020085 }
86}