project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/ChatlogAdapter.java
author Medo <smaxein@googlemail.com>
Sat, 21 Jul 2012 14:56:52 +0200
changeset 7349 12fdfd2038d4
parent 7346 b0f67c5b4215
permissions -rw-r--r--
Hedgeroid: More work on the lobby activity
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
package org.hedgewars.hedgeroid.netplay;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
import java.util.ArrayList;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
import java.util.Collection;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
import java.util.List;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
import org.hedgewars.hedgeroid.netplay.MessageLog.Observer;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
import android.content.Context;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
import android.text.method.LinkMovementMethod;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
import android.view.View;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
import android.view.ViewGroup;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
import android.widget.AbsListView.LayoutParams;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
import android.widget.BaseAdapter;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
import android.widget.TextView;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
/**
7349
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    18
 * Optimization: ListView is smart enough to try re-using the same view for an item
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    19
 * with the same ID, but it still calls getView for those items when the list changes.
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    20
 * Since lines with a given ID never change in our chatlog, we can avoid the effort
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    21
 * of TextView.setText in many cases by checking if the view is already set up for the
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    22
 * line with the right ID - but to do that, the view needs to remember the ID it's
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    23
 * holding the text for. That's what the LoglineView does. 
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
 */
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
class LoglineView extends TextView {
7349
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    26
	long chatlogId = -1;
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
	public LoglineView(Context context) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
		super(context);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
public class ChatlogAdapter extends BaseAdapter implements Observer {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
	long idOffset = 0;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
	private List<CharSequence> log = new ArrayList<CharSequence>();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
	private Context context;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	public ChatlogAdapter(Context context) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
		this.context = context;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	public int getCount() {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
		return log.size();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
	public Object getItem(int position) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
		return log.get(position);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
	public long getItemId(int position) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
		return position+idOffset;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
	public boolean hasStableIds() {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
		return true;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	public void clear() {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
		idOffset += log.size();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
		log.clear();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
		notifyDataSetChanged();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
	public void lineAdded(CharSequence text) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
		log.add(text);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
		notifyDataSetChanged();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
	public void lineRemoved() {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
		log.remove(0);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
		idOffset += 1;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
		notifyDataSetChanged();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
	public void setLog(Collection<CharSequence> log) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
		idOffset += log.size();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
		this.log = new ArrayList<CharSequence>(log);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
		notifyDataSetChanged();
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
	
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	public View getView(int position, View convertView, ViewGroup parent) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
		LoglineView v = (LoglineView)convertView;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
		if (v == null) {
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
			v = new LoglineView(context);
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
			v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
			v.setMovementMethod(LinkMovementMethod.getInstance());
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
		}
7349
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    88
		long id = getItemId(position);
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    89
		if(id != v.chatlogId) {
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    90
			v.setText(log.get(position));
12fdfd2038d4 Hedgeroid: More work on the lobby activity
Medo <smaxein@googlemail.com>
parents: 7346
diff changeset
    91
			v.chatlogId = id;
7346
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
		}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
		return v;
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
	}
b0f67c5b4215 Hedgeroid: Lobby activity improvements
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
}