30 * when there have been no updates to the relevant map information for a time, |
30 * when there have been no updates to the relevant map information for a time, |
31 * to prevent triggering several updates at once when different parts of the |
31 * to prevent triggering several updates at once when different parts of the |
32 * information change. |
32 * information change. |
33 */ |
33 */ |
34 public final class CalmDownHandler extends Handler { |
34 public final class CalmDownHandler extends Handler { |
35 int runningMessagesCounter = 0; |
35 int runningMessagesCounter = 0; |
36 final Runnable inactivityRunnable; |
36 final Runnable inactivityRunnable; |
37 final long inactivityMs; |
37 final long inactivityMs; |
38 boolean stopped; |
38 boolean stopped; |
39 |
39 |
40 public CalmDownHandler(Looper looper, Runnable runnable, long inactivityMs) { |
40 public CalmDownHandler(Looper looper, Runnable runnable, long inactivityMs) { |
41 super(looper); |
41 super(looper); |
42 this.inactivityRunnable = runnable; |
42 this.inactivityRunnable = runnable; |
43 this.inactivityMs = inactivityMs; |
43 this.inactivityMs = inactivityMs; |
44 } |
44 } |
45 |
45 |
46 public void activity() { |
46 public void activity() { |
47 runningMessagesCounter++; |
47 runningMessagesCounter++; |
48 sendMessageDelayed(obtainMessage(), inactivityMs); |
48 sendMessageDelayed(obtainMessage(), inactivityMs); |
49 } |
49 } |
50 |
50 |
51 @Override |
51 @Override |
52 public void handleMessage(Message msg) { |
52 public void handleMessage(Message msg) { |
53 runningMessagesCounter--; |
53 runningMessagesCounter--; |
54 if(runningMessagesCounter==0 && !stopped) { |
54 if(runningMessagesCounter==0 && !stopped) { |
55 inactivityRunnable.run(); |
55 inactivityRunnable.run(); |
56 } |
56 } |
57 } |
57 } |
58 |
58 |
59 public void stop() { |
59 public void stop() { |
60 stopped = true; |
60 stopped = true; |
61 } |
61 } |
62 } |
62 } |