Methods Summary |
---|
private void | cancelAlarm(com.android.im.service.AndroidHeartBeatService$Alarm alarm)
mAlarmManager.cancel(alarm.mAlaramSender);
int index = mAlarms.indexOfValue(alarm);
if (index >= 0) {
mAlarms.delete(mAlarms.keyAt(index));
}
// Unregister the BroadcastReceiver if there isn't a alarm anymore.
if (mAlarms.size() == 0) {
mContext.unregisterReceiver(this);
}
|
private com.android.im.service.AndroidHeartBeatService$Alarm | findAlarm(Callback callback)
for (int i = 0; i < mAlarms.size(); i++) {
Alarm alarm = mAlarms.valueAt(i);
if (alarm.mCallback == callback) {
return alarm;
}
}
return null;
|
private static synchronized int | nextId()
return sNextId++;
|
public void | onReceive(android.content.Context context, android.content.Intent intent)
mWakeLock.acquire();
try {
int id = (int)ContentUris.parseId(intent.getData());
Alarm alarm = mAlarms.get(id);
if (alarm == null) {
return;
}
Callback callback = alarm.mCallback;
long nextSchedule = callback.sendHeartbeat();
if (nextSchedule <= 0) {
cancelAlarm(alarm);
} else {
setAlarm(alarm, nextSchedule);
}
} finally {
mWakeLock.release();
}
|
private void | setAlarm(com.android.im.service.AndroidHeartBeatService$Alarm alarm, long offset)
long triggerAtTime = SystemClock.elapsedRealtime() + offset;
mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime,
alarm.mAlaramSender);
|
public synchronized void | startHeartbeat(Callback callback, long triggerTime)
Alarm alarm = findAlarm(callback);
if (alarm == null) {
alarm = new Alarm();
int id = nextId();
alarm.mCallback = callback;
Uri data = ContentUris.withAppendedId(HEARTBEAT_CONTENT_URI, id);
Intent i = new Intent().setDataAndType(data, HEARTBEAT_CONTENT_TYPE);
alarm.mAlaramSender = PendingIntent.getBroadcast(mContext, 0, i, 0);
if (mAlarms.size() == 0) {
mContext.registerReceiver(this, IntentFilter.create(
HEARTBEAT_INTENT_ACTION, HEARTBEAT_CONTENT_TYPE));
}
mAlarms.append(id, alarm);
}
setAlarm(alarm, triggerTime);
|
public synchronized void | stopAll()
for (int i = 0; i < mAlarms.size(); i++) {
Alarm alarm = mAlarms.valueAt(i);
cancelAlarm(alarm);
}
|
public synchronized void | stopHeartbeat(Callback callback)
Alarm alarm = findAlarm(callback);
if (alarm != null) {
cancelAlarm(alarm);
}
|