FileDocCategorySizeDatePackage
AndroidHeartBeatService.javaAPI DocAndroid 1.5 API5146Wed May 06 22:42:46 BST 2009com.android.im.service

AndroidHeartBeatService

public class AndroidHeartBeatService extends android.content.BroadcastReceiver implements com.android.im.engine.HeartbeatService

Fields Summary
private static final String
WAKELOCK_TAG
private static final String
HEARTBEAT_INTENT_ACTION
private static final android.net.Uri
HEARTBEAT_CONTENT_URI
private static final String
HEARTBEAT_CONTENT_TYPE
private android.content.Context
mContext
private android.app.AlarmManager
mAlarmManager
private PowerManager.WakeLock
mWakeLock
private android.util.SparseArray
mAlarms
private static int
sNextId
Constructors Summary
public AndroidHeartBeatService(android.content.Context context)


       
        mContext = context;
        mAlarmManager = (AlarmManager)context.getSystemService(
                Context.ALARM_SERVICE);
        PowerManager powerManager = (PowerManager)context.getSystemService(
                Context.POWER_SERVICE);
        mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                WAKELOCK_TAG);
        mAlarms = new SparseArray<Alarm>();
    
Methods Summary
private voidcancelAlarm(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$AlarmfindAlarm(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 intnextId()

         
        return sNextId++;
    
public voidonReceive(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 voidsetAlarm(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 voidstartHeartbeat(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 voidstopAll()

        for (int i = 0; i < mAlarms.size(); i++) {
            Alarm alarm = mAlarms.valueAt(i);
            cancelAlarm(alarm);
        }
    
public synchronized voidstopHeartbeat(Callback callback)

        Alarm alarm = findAlarm(callback);
        if (alarm != null) {
            cancelAlarm(alarm);
        }