project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/util/CalmDownHandler.java
changeset 7586 33924ff4af50
child 10017 de822cd3df3a
equal deleted inserted replaced
7584:7831c84cc644 7586:33924ff4af50
       
     1 /*
       
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
       
     3  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License
       
     7  * as published by the Free Software Foundation; either version 2
       
     8  * of the License, or (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program; if not, write to the Free Software
       
    17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    18  */
       
    19 
       
    20 package org.hedgewars.hedgeroid.util;
       
    21 
       
    22 import android.os.Handler;
       
    23 import android.os.Looper;
       
    24 import android.os.Message;
       
    25 
       
    26 /**
       
    27  * This class allows you to define a runnable that is called when there has been
       
    28  * no activity for a set amount of time, where activity is determined by calls
       
    29  * to the activity() method of the handler. It is used to update the map preview
       
    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
       
    32  * information change.
       
    33  */
       
    34 public final class CalmDownHandler extends Handler {
       
    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 	}
       
    45 	
       
    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 	}
       
    62 }