project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/MessageLog.java
author Medo <smaxein@googlemail.com>
Thu, 19 Jul 2012 22:55:36 +0200
changeset 7346 b0f67c5b4215
parent 7332 3f2e130f9715
child 7352 641f11cdd319
permissions -rw-r--r--
Hedgeroid: Lobby activity improvements

package org.hedgewars.hedgeroid.netplay;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.hedgewars.hedgeroid.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;

public class MessageLog {
	private static final int BACKLOG_LINES = 200;
	
	private static final int INFO_COLOR = Color.GRAY;
	private static final int CHAT_COLOR = Color.GREEN;
	private static final int MECHAT_COLOR = Color.CYAN;
	private static final int WARN_COLOR = Color.RED;
	private static final int ERROR_COLOR = Color.RED;
	
	private final Context context;
	private List<Observer> observers = new LinkedList<Observer>();
	private List<CharSequence> log = new LinkedList<CharSequence>();
	
	public MessageLog(Context context) {
		this.context = context;
	}
	
	private Spanned makeLogTime() {
		String time = DateFormat.getTimeFormat(context).format(new Date());
		return withColor("[" + time + "] ", INFO_COLOR);
	}
	
	private static Spanned span(CharSequence s, Object o) {
		Spannable spannable = new SpannableString(s);
		spannable.setSpan(o, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		return spannable;
	}
	
	private static Spanned withColor(CharSequence s, int color) {
		return span(s, new ForegroundColorSpan(color));
	}
	
	private static Spanned bold(CharSequence s) {
		return span(s, new StyleSpan(Typeface.BOLD));
	}
	
	private void append(CharSequence msg) {
		SpannableStringBuilder ssb = new SpannableStringBuilder();
		ssb.append(makeLogTime()).append(msg);
		appendRaw(ssb);
	}
	
	private void appendRaw(CharSequence msg) {
		if(log.size() > BACKLOG_LINES) {
			log.remove(0);
			for(Observer o : observers) {
				o.lineRemoved();
			}
		}
		log.add(msg);
		for(Observer o : observers) {
			o.lineAdded(msg);
		}
	}
	
	void appendPlayerJoin(String playername) {
		append(withColor("***" + context.getResources().getString(R.string.log_player_join, playername), INFO_COLOR));
	}
	
	void appendPlayerLeave(String playername, String partMsg) {
		String msg = "***";
		if(partMsg != null) {
			msg += context.getResources().getString(R.string.log_player_leave_with_msg, playername, partMsg);
		} else {
			msg += context.getResources().getString(R.string.log_player_leave, playername);
		}
		append(withColor(msg, INFO_COLOR));
	}
	
	void appendChat(String playerName, String msg) {
		if(msg.startsWith("/me ")) {
			append(withColor("*"+playerName+" "+msg, MECHAT_COLOR));
		} else {
			Spanned name = bold(playerName+": ");
			Spanned fullMessage = withColor(TextUtils.concat(name, msg), CHAT_COLOR);
			append(fullMessage);			
		}
	}
	
	void appendMessage(int type, String msg) {
		switch(type) {
		case JnaFrontlib.NETCONN_MSG_TYPE_ERROR:
			append(withColor("***"+msg, ERROR_COLOR));
			break;
		case JnaFrontlib.NETCONN_MSG_TYPE_WARNING:
			append(withColor("***"+msg, WARN_COLOR));
			break;
		case JnaFrontlib.NETCONN_MSG_TYPE_PLAYERINFO:
			// TODO better formatting or different way to display
			append(msg);
			break;
		case JnaFrontlib.NETCONN_MSG_TYPE_SERVERMESSAGE:
			appendRaw(span(TextUtils.concat("\n", Html.fromHtml(msg), "\n"), new RelativeSizeSpan(1.5f)));
			break;
		default:
			Log.e("MessageLog", "Unknown messagetype "+type);
		}
	}
	
	void clear() {
		for(Observer o : observers) {
			o.clear();
		}
		log.clear();
	}
	
	public void registerObserver(Observer o) {
		observers.add(o);
	}
	
	public void unregisterObserver(Observer o) {
		observers.remove(o);
	}
	
	public static interface Observer {
		void lineAdded(CharSequence text);
		void lineRemoved();
		void clear();
	}

	public Collection<CharSequence> getLog() {
		return Collections.unmodifiableList(log);
	}
}