FileDocCategorySizeDatePackage
ResettableTimeout.javaAPI DocAndroid 1.5 API3899Wed May 06 22:41:56 BST 2009com.android.server

ResettableTimeout

public abstract class ResettableTimeout extends Object
Utility class that you can call on with a timeout, and get called back after a given time, dealing correctly with restarting the timeout.

For example, this class is used by the android.os.Vibrator class.

Fields Summary
private android.os.ConditionVariable
mLock
private volatile long
mOffAt
private volatile boolean
mOffCalled
private Thread
mThread
Constructors Summary
Methods Summary
public voidcancel()
Cancel the timeout and call off now.

        synchronized (this) {
            mOffAt = 0;
            if (mThread != null) {
                mThread.interrupt();
                mThread = null;
            }
            if (!mOffCalled) {
                mOffCalled = true;
                off();
            }
        }
    
public voidgo(long milliseconds)
Does the following steps.

1. Call on()

2. Start the timer.

3. At the timeout, calls off()

If you call this again, the timeout is reset to the new one

        synchronized (this) {
            mOffAt = SystemClock.uptimeMillis() + milliseconds;

            boolean alreadyOn;

            // By starting the thread first and waiting, we ensure that if the
            // thread to stop it can't start, we don't turn the vibrator on
            // forever.  This still isn't really sufficient, because we don't
            // have another processor watching us.  We really should have a
            // service for this in case our process crashes.
            if (mThread == null) {
                alreadyOn = false;
                mLock.close();
                mThread = new T();
                mThread.start();
                mLock.block();
                mOffCalled = false;
            } else {
                alreadyOn = true;
                // poke the thread so it gets the new timeout.
                mThread.interrupt();
            }
            on(alreadyOn);
        }
    
public abstract voidoff()
Override this to do what you need to do when it's stopping. This is called with the monitor on this method held, so be careful.

public abstract voidon(boolean alreadyOn)
Override this do what you need to do when it's starting This is called with the monitor on this method held, so be careful.

param
alreadyOn is true if it's currently running