author | unc0rr |
Sat, 14 Nov 2015 22:19:05 +0300 | |
changeset 11381 | 437a60995fe1 |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7586
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
1 |
/* |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
* |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
9 |
* |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
* GNU General Public License for more details. |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
* |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
*/ |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
|
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
package org.hedgewars.hedgeroid.util; |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
|
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
import android.os.Handler; |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
import android.os.Looper; |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
import android.os.Message; |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
|
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
/** |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
* This class allows you to define a runnable that is called when there has been |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
* no activity for a set amount of time, where activity is determined by calls |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
* to the activity() method of the handler. It is used to update the map preview |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
* when there have been no updates to the relevant map information for a time, |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
* to prevent triggering several updates at once when different parts of the |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
* information change. |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
*/ |
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
public final class CalmDownHandler extends Handler { |
10017 | 35 |
int runningMessagesCounter = 0; |
36 |
final Runnable inactivityRunnable; |
|
37 |
final long inactivityMs; |
|
38 |
boolean stopped; |
|
39 |
||
40 |
public CalmDownHandler(Looper looper, Runnable runnable, long inactivityMs) { |
|
41 |
super(looper); |
|
42 |
this.inactivityRunnable = runnable; |
|
43 |
this.inactivityMs = inactivityMs; |
|
44 |
} |
|
7586
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
|
10017 | 46 |
public void activity() { |
47 |
runningMessagesCounter++; |
|
48 |
sendMessageDelayed(obtainMessage(), inactivityMs); |
|
49 |
} |
|
50 |
||
51 |
@Override |
|
52 |
public void handleMessage(Message msg) { |
|
53 |
runningMessagesCounter--; |
|
54 |
if(runningMessagesCounter==0 && !stopped) { |
|
55 |
inactivityRunnable.run(); |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
public void stop() { |
|
60 |
stopped = true; |
|
61 |
} |
|
7586
33924ff4af50
Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
62 |
} |