aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils/PeriodicHandler.java
blob: 288bbb09d2f221a8576472ad84b45fb6ac8a8a9c (plain)
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
package cgeo.geocaching.utils;

import android.os.Handler;
import android.os.Message;

import java.lang.ref.WeakReference;

/**
 * A PeriodicHandler class helps with the implementation of a periodic
 * action embedded in a thread with a looper such as the UI thread.
 * The onPeriodic() method of the listener will be called periodically.
 * The clock may drift as the implementation does not target real-time
 * actions.
 *
 * The handler will be interrupted if the device goes to sleep.
 *
 * The handler only keeps a weak reference to the listener. If the listener
 * is garbage-collected without having stopped the timer, the handler will
 * stop itself.
 */
final public class PeriodicHandler extends Handler {

    public static interface PeriodicHandlerListener {
        public void onPeriodic();
    }

    final static private int START = 0;
    final static private int STOP = 1;
    final static private int ACT = 2;

    final private long period;

    final private WeakReference<PeriodicHandlerListener> listenerRef;

    /**
     * Create a new PeriodicHandler object.
     *
     * @param period
     *            The period in milliseconds.
     */
    public PeriodicHandler(final long period, final PeriodicHandlerListener listener) {
        this.period = period;
        listenerRef = new WeakReference<PeriodicHandlerListener>(listener);
    }

    @Override
    public void handleMessage(final Message msg) {
        switch (msg.what) {
            case START:
                removeMessages(ACT);
                sendEmptyMessage(ACT);
                break;
            case STOP:
                removeMessages(ACT);
                break;
            case ACT:
                final PeriodicHandlerListener listener = listenerRef.get();
                if (listener != null) {
                    sendEmptyMessageDelayed(ACT, period);
                    listener.onPeriodic();
                }
                break;
            default:
                throw new UnsupportedOperationException();
        }
    }

    /**
     * Start the periodic handler.
     *
     * Restarting a handler that is already started will only
     * reset its clock.
     */
    public void start() {
        sendEmptyMessage(START);
    }

    /**
     * Stop the periodic handler.
     *
     * If this method is called from the looper thread, this call is
     * guaranteed to be synchronous.
     */
    public void stop() {
        sendMessageAtFrontOfQueue(obtainMessage(STOP));
    }

}