project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/TickHandler.java
author Medo <smaxein@googlemail.com>
Tue, 24 Jul 2012 16:57:48 +0200
changeset 7355 5673e95ef647
child 7476 2fb781bbdd51
permissions -rw-r--r--
Hedgeroid: Misguided attempts at getting the connection lifecycle right. Committing this because it basically runs this way, so i might want to revert to it later :)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7355
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
package org.hedgewars.hedgeroid.netplay;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
import android.os.Handler;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
import android.os.Looper;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
import android.os.Message;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
/**
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
 * This class handles regularly calling a specified runnable
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
 * on the looper provided in the constructor. The first call
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
 * occurs without delay (though still via the looper), all
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
 * following calls are delayed by (approximately) the interval.
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
 * The interval can be changed at any time, which will cause
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
 * an immediate execution of the runnable again.
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
 */
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
class TickHandler extends Handler {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
	private final Runnable callback;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
	private int messageId;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
	private long interval;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
	private boolean running;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
	
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
	public TickHandler(Looper looper, long interval, Runnable callback) {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
		super(looper);
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
		this.callback = callback;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
		this.interval = interval;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
	}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
	
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
	public synchronized void stop() {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
		messageId++;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
		running = false;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
	}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
	
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
	public synchronized void start() {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
		messageId++;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
		sendMessage(obtainMessage(messageId));
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
		running = true;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
	}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	public synchronized void setInterval(long interval) {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		this.interval = interval;
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
		if(running) {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
			start();
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
		}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
	}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
	@Override
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
	public synchronized void handleMessage(Message msg) {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
		if(msg.what == messageId) {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
			callback.run();
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
		}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
		if(msg.what == messageId) {
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
			sendMessageDelayed(obtainMessage(messageId), interval);
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
		}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
	}
5673e95ef647 Hedgeroid: Misguided attempts at getting the connection lifecycle right.
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
}