| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 24 |
import com.android.internal.annotations.VisibleForTesting; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class WakeupMessage implements AlarmManager.OnAlarmListener { |
| 40 |
private final AlarmManager mAlarmManager; |
| 41 |
|
| 42 |
@VisibleForTesting |
| 43 |
protected final Handler mHandler; |
| 44 |
@VisibleForTesting |
| 45 |
protected final String mCmdName; |
| 46 |
@VisibleForTesting |
| 47 |
protected final int mCmd, mArg1, mArg2; |
| 48 |
@VisibleForTesting |
| 49 |
protected final Object mObj; |
| 50 |
private final Runnable mRunnable; |
| 51 |
private boolean mScheduled; |
| 52 |
|
| 53 |
public WakeupMessage(Context context, Handler handler, |
| 54 |
String cmdName, int cmd, int arg1, int arg2, Object obj) { |
| 55 |
mAlarmManager = getAlarmManager(context); |
| 56 |
mHandler = handler; |
| 57 |
mCmdName = cmdName; |
| 58 |
mCmd = cmd; |
| 59 |
mArg1 = arg1; |
| 60 |
mArg2 = arg2; |
| 61 |
mObj = obj; |
| 62 |
mRunnable = null; |
| 63 |
} |
| 64 |
|
| 65 |
public WakeupMessage(Context context, Handler handler, String cmdName, int cmd, int arg1) { |
| 66 |
this(context, handler, cmdName, cmd, arg1, 0, null); |
| 67 |
} |
| 68 |
|
| 69 |
public WakeupMessage(Context context, Handler handler, |
| 70 |
String cmdName, int cmd, int arg1, int arg2) { |
| 71 |
this(context, handler, cmdName, cmd, arg1, arg2, null); |
| 72 |
} |
| 73 |
|
| 74 |
public WakeupMessage(Context context, Handler handler, String cmdName, int cmd) { |
| 75 |
this(context, handler, cmdName, cmd, 0, 0, null); |
| 76 |
} |
| 77 |
|
| 78 |
public WakeupMessage(Context context, Handler handler, String cmdName, Runnable runnable) { |
| 79 |
mAlarmManager = getAlarmManager(context); |
| 80 |
mHandler = handler; |
| 81 |
mCmdName = cmdName; |
| 82 |
mCmd = 0; |
| 83 |
mArg1 = 0; |
| 84 |
mArg2 = 0; |
| 85 |
mObj = null; |
| 86 |
mRunnable = runnable; |
| 87 |
} |
| 88 |
|
| 89 |
private static AlarmManager getAlarmManager(Context context) { |
| 90 |
return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public synchronized void schedule(long when) { |
| 100 |
mAlarmManager.setExact( |
| 101 |
AlarmManager.ELAPSED_REALTIME_WAKEUP, when, mCmdName, this, mHandler); |
| 102 |
mScheduled = true; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public synchronized void cancel() { |
| 110 |
if (mScheduled) { |
| 111 |
mAlarmManager.cancel(this); |
| 112 |
mScheduled = false; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void onAlarm() { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
final boolean stillScheduled; |
| 122 |
synchronized (this) { |
| 123 |
stillScheduled = mScheduled; |
| 124 |
mScheduled = false; |
| 125 |
} |
| 126 |
if (stillScheduled) { |
| 127 |
Message msg; |
| 128 |
if (mRunnable == null) { |
| 129 |
msg = mHandler.obtainMessage(mCmd, mArg1, mArg2, mObj); |
| 130 |
} else { |
| 131 |
msg = Message.obtain(mHandler, mRunnable); |
| 132 |
} |
| 133 |
mHandler.dispatchMessage(msg); |
| 134 |
msg.recycle(); |
| 135 |
} |
| 136 |
} |
| 137 |
} |