-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Alarm.java
223 lines (179 loc) · 7.32 KB
/
Alarm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package com.tencent.mars.comm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Process;
import android.os.SystemClock;
import com.tencent.mars.xlog.Log;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
/**
* 定时器工具类,mars会在网络组件stn中使用定时器管理任务队列、连接间隔等
* Created by caoshaokun on 16/3/7.
*/
public class Alarm extends BroadcastReceiver {
private final static String TAG = "MicroMsg.Alarm";
private final static String KEXTRA_ID = "ID";
private final static String KEXTRA_PID = "PID";
private enum TSetData {
ID,
WAITTIME,
PENDINGINTENT,
}
private static WakerLock wakerlock = null;
private static Alarm bc_alarm = null;
private native void onAlarm(final long id);
private static class ComparatorAlarm implements Comparator<Object[]> {
@Override
public int compare(Object[] lhs, Object[] rhs) {
return (int) ((Long) (lhs[TSetData.ID.ordinal()]) - (Long) (rhs[TSetData.ID.ordinal()]));
}
}
private static TreeSet<Object[]> alarm_waiting_set = new TreeSet<Object[]>(new ComparatorAlarm());
public static void resetAlarm(final Context context) {
synchronized (alarm_waiting_set) {
Iterator<Object[]> it = alarm_waiting_set.iterator();
while (it.hasNext()) {
cancelAlarmMgr(context, (PendingIntent) (it.next()[TSetData.PENDINGINTENT.ordinal()]));
}
alarm_waiting_set.clear();
if (null != bc_alarm) {
context.unregisterReceiver(bc_alarm);
bc_alarm = null;
}
}
}
public static boolean start(final long id, final int after, final Context context) {
long curtime = SystemClock.elapsedRealtime();
if (0 > after) {
Log.e(TAG, "id:%d, after:%d", id, after);
return false;
}
if (null == context) {
Log.e(TAG, "null==context, id:%d, after:%d", id, after);
return false;
}
synchronized (alarm_waiting_set) {
if (null == wakerlock) {
wakerlock = new WakerLock(context);
Log.i(TAG, "start new wakerlock");
}
if (null == bc_alarm) {
bc_alarm = new Alarm();
context.registerReceiver(bc_alarm, new IntentFilter("ALARM_ACTION(" + String.valueOf(Process.myPid()) + ")"));
}
Iterator<Object[]> it = alarm_waiting_set.iterator();
while (it.hasNext()) {
if ((Long) (it.next()[TSetData.ID.ordinal()]) == id) {
Log.e(TAG, "id exist=%d", id);
return false;
}
}
long waittime = after >= 0 ? curtime + after : curtime;
PendingIntent pendingIntent = setAlarmMgr(id, waittime, context);
if (null == pendingIntent) return false;
alarm_waiting_set.add(new Object[]{id, waittime, pendingIntent});
}
return true;
}
public static boolean stop(final long id, final Context context) {
if (null == context) {
Log.e(TAG, "context==null");
return false;
}
synchronized (alarm_waiting_set) {
if (null == wakerlock) {
wakerlock = new WakerLock(context);
Log.i(TAG, "stop new wakerlock");
}
if (null == bc_alarm) {
bc_alarm = new Alarm();
IntentFilter filter = new IntentFilter();
context.registerReceiver(bc_alarm, filter);
Log.i(TAG, "stop new Alarm");
}
Iterator<Object[]> it = alarm_waiting_set.iterator();
while (it.hasNext()) {
Object[] next = it.next();
if ((Long) (next[TSetData.ID.ordinal()]) == id) {
cancelAlarmMgr(context, (PendingIntent) (next[TSetData.PENDINGINTENT.ordinal()]));
it.remove();
return true;
}
}
}
return false;
}
private static PendingIntent setAlarmMgr(final long id, final long time, final Context context) {
final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (am == null) {
Log.e(TAG, "am == null");
return null;
}
Intent intent = new Intent();
intent.setAction("ALARM_ACTION(" + String.valueOf(Process.myPid()) + ")");
intent.putExtra(KEXTRA_ID, id);
intent.putExtra(KEXTRA_PID, Process.myPid());
PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT < 23) {
pendingIntent = PendingIntent.getBroadcast(context, (int) id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
} else {
pendingIntent = PendingIntent.getBroadcast(context, (int) id, intent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}
if (Build.VERSION.SDK_INT < 19) { //KITKAT
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pendingIntent);
} else {
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pendingIntent);
}
return pendingIntent;
}
private static boolean cancelAlarmMgr(final Context context, PendingIntent pendingIntent) {
final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (am == null) {
Log.e(TAG, "am == null");
return false;
}
if (pendingIntent == null) {
Log.e(TAG, "pendingIntent == null");
return false;
}
am.cancel(pendingIntent);
pendingIntent.cancel();
return true;
}
@Override
public void onReceive(final Context context, Intent intent) {
if (null == context || null == intent) return;
final Long id = intent.getLongExtra(KEXTRA_ID, 0);
final Integer pid = intent.getIntExtra(KEXTRA_PID, 0);
if (0 == id || 0 == pid) return;
if (pid != Process.myPid()) {
Log.w(TAG, "onReceive id:%d, pid:%d, mypid:%d", id, pid, Process.myPid());
return;
}
boolean found = false;
synchronized (alarm_waiting_set) {
Iterator<Object[]> it = alarm_waiting_set.iterator();
while (it.hasNext()) {
Object[] next = it.next();
Long curId = (Long) next[TSetData.ID.ordinal()];
Log.i(TAG, "onReceive id=%d, curId=%d", id, curId);
if (curId.equals(id)) {
Log.i(TAG, "onReceive find alarm id:%d, pid:%d, delta miss time:%d", id, pid, SystemClock.elapsedRealtime() - (Long) next[TSetData.WAITTIME.ordinal()]);
it.remove();
found = true;
break;
}
}
if (!found)
Log.e(TAG, "onReceive not found id:%d, pid:%d, alarm_waiting_set.size:%d", id, pid, alarm_waiting_set.size());
}
if (null != wakerlock) wakerlock.lock(200);
if (found) onAlarm(id);
}
}