WakeupMessage.java revision 9d3aadb24778cc36a350a04b088d2ed7ee3790e1
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
17package com.android.internal.util;
18
19import android.app.AlarmManager;
20import android.content.Context;
21import android.os.Handler;
22import android.os.Message;
23
24 /**
25 * An AlarmListener that sends the specified message to a Handler and keeps the system awake until
26 * the message is processed.
27 *
28 * This is useful when using the AlarmManager direct callback interface to wake up the system and
29 * request that an object whose API consists of messages (such as a StateMachine) perform some
30 * action.
31 *
32 * In this situation, using AlarmManager.onAlarmListener by itself will wake up the system to send
33 * the message, but does not guarantee that the system will be awake until the target object has
34 * processed it. This is because as soon as the onAlarmListener sends the message and returns, the
35 * AlarmManager releases its wakelock and the system is free to go to sleep again.
36 *
37 */
38public class WakeupMessage implements AlarmManager.OnAlarmListener {
39    private static AlarmManager sAlarmManager;
40    private final Handler mHandler;
41    private final String mCmdName;
42    private final int mCmd, mArg1, mArg2;
43
44    public WakeupMessage(Context context, Handler handler,
45            String cmdName, int cmd, int arg1, int arg2) {
46        if (sAlarmManager == null) {
47            sAlarmManager = context.getSystemService(AlarmManager.class);
48        }
49        mHandler = handler;
50        mCmdName = cmdName;
51        mCmd = cmd;
52        mArg1 = arg1;
53        mArg2 = arg2;
54    }
55
56    public WakeupMessage(Context context, Handler handler, String cmdName, int cmd, int arg1) {
57        this(context, handler, cmdName, cmd, arg1, 0);
58    }
59
60    public WakeupMessage(Context context, Handler handler, String cmdName, int cmd) {
61        this(context, handler, cmdName, cmd, 0, 0);
62    }
63
64    public void schedule(long when) {
65        sAlarmManager.setExact(
66                AlarmManager.ELAPSED_REALTIME_WAKEUP, when, mCmdName, this, mHandler);
67    }
68
69    public void cancel() {
70        sAlarmManager.cancel(this);
71    }
72
73    @Override
74    public void onAlarm() {
75        Message msg = mHandler.obtainMessage(mCmd, mArg1, mArg2);
76        mHandler.handleMessage(msg);
77        msg.recycle();
78    }
79}
80