Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.internal.util; |
| 18 | |
| 19 | import android.app.AlarmManager; |
| 20 | import android.content.Context; |
| 21 | import android.os.Handler; |
| 22 | import android.os.Message; |
| 23 | |
Lorenzo Colitti | e7636e4 | 2016-02-21 01:04:13 +0900 | [diff] [blame] | 24 | import com.android.internal.annotations.VisibleForTesting; |
| 25 | |
| 26 | /** |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 27 | * An AlarmListener that sends the specified message to a Handler and keeps the system awake until |
| 28 | * the message is processed. |
| 29 | * |
| 30 | * This is useful when using the AlarmManager direct callback interface to wake up the system and |
| 31 | * request that an object whose API consists of messages (such as a StateMachine) perform some |
| 32 | * action. |
| 33 | * |
| 34 | * In this situation, using AlarmManager.onAlarmListener by itself will wake up the system to send |
| 35 | * the message, but does not guarantee that the system will be awake until the target object has |
| 36 | * processed it. This is because as soon as the onAlarmListener sends the message and returns, the |
| 37 | * AlarmManager releases its wakelock and the system is free to go to sleep again. |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 38 | */ |
| 39 | public class WakeupMessage implements AlarmManager.OnAlarmListener { |
Mitchell Wills | 634c1f9 | 2016-02-18 18:01:33 -0800 | [diff] [blame] | 40 | private final AlarmManager mAlarmManager; |
Lorenzo Colitti | e7636e4 | 2016-02-21 01:04:13 +0900 | [diff] [blame] | 41 | |
| 42 | @VisibleForTesting |
| 43 | protected final Handler mHandler; |
| 44 | @VisibleForTesting |
| 45 | protected final String mCmdName; |
| 46 | @VisibleForTesting |
| 47 | protected final int mCmd, mArg1, mArg2; |
Lorenzo Colitti | eab17da | 2016-06-07 16:27:10 +0900 | [diff] [blame] | 48 | @VisibleForTesting |
| 49 | protected final Object mObj; |
Mitchell Wills | 634c1f9 | 2016-02-18 18:01:33 -0800 | [diff] [blame] | 50 | private boolean mScheduled; |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 51 | |
| 52 | public WakeupMessage(Context context, Handler handler, |
Lorenzo Colitti | eab17da | 2016-06-07 16:27:10 +0900 | [diff] [blame] | 53 | String cmdName, int cmd, int arg1, int arg2, Object obj) { |
Mitchell Wills | 634c1f9 | 2016-02-18 18:01:33 -0800 | [diff] [blame] | 54 | mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 55 | mHandler = handler; |
| 56 | mCmdName = cmdName; |
| 57 | mCmd = cmd; |
| 58 | mArg1 = arg1; |
| 59 | mArg2 = arg2; |
Lorenzo Colitti | eab17da | 2016-06-07 16:27:10 +0900 | [diff] [blame] | 60 | mObj = obj; |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | public WakeupMessage(Context context, Handler handler, String cmdName, int cmd, int arg1) { |
Lorenzo Colitti | eab17da | 2016-06-07 16:27:10 +0900 | [diff] [blame] | 64 | this(context, handler, cmdName, cmd, arg1, 0, null); |
| 65 | } |
| 66 | |
| 67 | public WakeupMessage(Context context, Handler handler, |
| 68 | String cmdName, int cmd, int arg1, int arg2) { |
| 69 | this(context, handler, cmdName, cmd, arg1, arg2, null); |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | public WakeupMessage(Context context, Handler handler, String cmdName, int cmd) { |
Lorenzo Colitti | eab17da | 2016-06-07 16:27:10 +0900 | [diff] [blame] | 73 | this(context, handler, cmdName, cmd, 0, 0, null); |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 74 | } |
| 75 | |
Mitchell Wills | 634c1f9 | 2016-02-18 18:01:33 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Schedule the message to be delivered at the time in milliseconds of the |
| 78 | * {@link android.os.SystemClock#elapsedRealtime SystemClock.elapsedRealtime()} clock and wakeup |
| 79 | * the device when it goes off. If schedule is called multiple times without the message being |
| 80 | * dispatched then the alarm is rescheduled to the new time. |
| 81 | */ |
| 82 | public synchronized void schedule(long when) { |
| 83 | mAlarmManager.setExact( |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 84 | AlarmManager.ELAPSED_REALTIME_WAKEUP, when, mCmdName, this, mHandler); |
Mitchell Wills | 634c1f9 | 2016-02-18 18:01:33 -0800 | [diff] [blame] | 85 | mScheduled = true; |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Mitchell Wills | 634c1f9 | 2016-02-18 18:01:33 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Cancel all pending messages. This includes alarms that may have been fired, but have not been |
| 90 | * run on the handler yet. |
| 91 | */ |
| 92 | public synchronized void cancel() { |
| 93 | if (mScheduled) { |
| 94 | mAlarmManager.cancel(this); |
| 95 | mScheduled = false; |
| 96 | } |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void onAlarm() { |
Mitchell Wills | 634c1f9 | 2016-02-18 18:01:33 -0800 | [diff] [blame] | 101 | // Once this method is called the alarm has already been fired and removed from |
| 102 | // AlarmManager (it is still partially tracked, but only for statistics). The alarm can now |
| 103 | // be marked as unscheduled so that it can be rescheduled in the message handler. |
| 104 | final boolean stillScheduled; |
| 105 | synchronized (this) { |
| 106 | stillScheduled = mScheduled; |
| 107 | mScheduled = false; |
| 108 | } |
| 109 | if (stillScheduled) { |
Lorenzo Colitti | eab17da | 2016-06-07 16:27:10 +0900 | [diff] [blame] | 110 | Message msg = mHandler.obtainMessage(mCmd, mArg1, mArg2, mObj); |
Mitchell Wills | 634c1f9 | 2016-02-18 18:01:33 -0800 | [diff] [blame] | 111 | mHandler.handleMessage(msg); |
| 112 | msg.recycle(); |
| 113 | } |
Lorenzo Colitti | e9ae977 | 2015-12-02 17:51:28 +0900 | [diff] [blame] | 114 | } |
| 115 | } |