• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.util;
18 
19 import android.app.AlarmManager;
20 import android.os.Handler;
21 import android.os.SystemClock;
22 
23 /**
24  * Schedules a timeout through AlarmManager. Ensures that the timeout is called even when
25  * the device is asleep.
26  */
27 public class AlarmTimeout implements AlarmManager.OnAlarmListener {
28 
29     public static final int MODE_CRASH_IF_SCHEDULED = 0;
30     public static final int MODE_IGNORE_IF_SCHEDULED = 1;
31     public static final int MODE_RESCHEDULE_IF_SCHEDULED = 2;
32 
33     private final AlarmManager mAlarmManager;
34     private final AlarmManager.OnAlarmListener mListener;
35     private final String mTag;
36     private final Handler mHandler;
37     private boolean mScheduled;
38 
AlarmTimeout(AlarmManager alarmManager, AlarmManager.OnAlarmListener listener, String tag, Handler handler)39     public AlarmTimeout(AlarmManager alarmManager, AlarmManager.OnAlarmListener listener,
40             String tag, Handler handler) {
41         mAlarmManager = alarmManager;
42         mListener = listener;
43         mTag = tag;
44         mHandler = handler;
45     }
46 
47     /**
48      * Schedules an alarm in {@code timeout} milliseconds in the future.
49      *
50      * @param timeout How long to wait from now.
51      * @param mode {@link #MODE_CRASH_IF_SCHEDULED}, {@link #MODE_IGNORE_IF_SCHEDULED} or
52      *             {@link #MODE_RESCHEDULE_IF_SCHEDULED}.
53      * @return {@code true} when scheduled successfully, {@code false} otherwise.
54      */
schedule(long timeout, int mode)55     public boolean schedule(long timeout, int mode) {
56         switch (mode) {
57             case MODE_CRASH_IF_SCHEDULED:
58                 if (mScheduled) {
59                     throw new IllegalStateException(mTag + " timeout is already scheduled");
60                 }
61                 break;
62             case MODE_IGNORE_IF_SCHEDULED:
63                 if (mScheduled) {
64                     return false;
65                 }
66                 break;
67             case MODE_RESCHEDULE_IF_SCHEDULED:
68                 if (mScheduled) {
69                     cancel();
70                 }
71                 break;
72             default:
73                 throw new IllegalArgumentException("Illegal mode: " + mode);
74         }
75 
76         mAlarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
77                 SystemClock.elapsedRealtime() + timeout, mTag, this, mHandler);
78         mScheduled = true;
79         return true;
80     }
81 
isScheduled()82     public boolean isScheduled() {
83         return mScheduled;
84     }
85 
cancel()86     public void cancel() {
87         if (mScheduled) {
88             mAlarmManager.cancel(this);
89             mScheduled = false;
90         }
91     }
92 
93     @Override
onAlarm()94     public void onAlarm() {
95         if (!mScheduled) {
96             // We canceled the alarm, but it still fired. Ignore.
97             return;
98         }
99         mScheduled = false;
100         mListener.onAlarm();
101     }
102 }
103