project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/util/CalmDownHandler.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7586 33924ff4af50
permissions -rw-r--r--
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    35
    int runningMessagesCounter = 0;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    36
    final Runnable inactivityRunnable;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    37
    final long inactivityMs;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    38
    boolean stopped;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    39
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    40
    public CalmDownHandler(Looper looper, Runnable runnable, long inactivityMs) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    41
        super(looper);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    42
        this.inactivityRunnable = runnable;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    43
        this.inactivityMs = inactivityMs;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    44
    }
7586
33924ff4af50 Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    46
    public void activity() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    47
        runningMessagesCounter++;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    48
        sendMessageDelayed(obtainMessage(), inactivityMs);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    49
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    50
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    51
    @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    52
    public void handleMessage(Message msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    53
        runningMessagesCounter--;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    54
        if(runningMessagesCounter==0 && !stopped) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    55
            inactivityRunnable.run();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    56
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    57
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    58
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    59
    public void stop() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    60
        stopped = true;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7586
diff changeset
    61
    }
7586
33924ff4af50 Hedgeroid: Some code cleanup, adding a forgotten file
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
}