|
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; |
|
21 |
|
22 import java.util.ArrayList; |
|
23 import java.util.Collection; |
|
24 import java.util.List; |
|
25 |
|
26 import org.hedgewars.hedgeroid.netplay.MessageLog; |
|
27 |
|
28 import android.content.Context; |
|
29 import android.text.method.LinkMovementMethod; |
|
30 import android.view.View; |
|
31 import android.view.ViewGroup; |
|
32 import android.widget.AbsListView.LayoutParams; |
|
33 import android.widget.BaseAdapter; |
|
34 import android.widget.TextView; |
|
35 |
|
36 /** |
|
37 * Optimization: ListView is smart enough to try re-using the same view for an item |
|
38 * with the same ID, but it still calls getView for those items when the list changes. |
|
39 * Since lines with a given ID never change in our chatlog, we can avoid the effort |
|
40 * of TextView.setText in many cases by checking if the view is already set up for the |
|
41 * line with the right ID - but to do that, the view needs to remember the ID it's |
|
42 * holding the text for. That's what the LoglineView does. |
|
43 */ |
|
44 class LoglineView extends TextView { |
|
45 long chatlogId = -1; |
|
46 |
|
47 public LoglineView(Context context) { |
|
48 super(context); |
|
49 } |
|
50 } |
|
51 |
|
52 /** |
|
53 * For performance reasons, the chatlog is implemented as ListView instead of a |
|
54 * single TextView (although I later learned that TextView might also have |
|
55 * facilities for efficient appending with limited backlog... oh well, this |
|
56 * works). Every chat line is a line in the ListView, and this adapter prepares |
|
57 * the textviews from a messagelog in an efficient way. |
|
58 */ |
|
59 public class ChatlogAdapter extends BaseAdapter implements MessageLog.Listener { |
|
60 long idOffset = 0; |
|
61 private List<CharSequence> log = new ArrayList<CharSequence>(); |
|
62 private Context context; |
|
63 |
|
64 public ChatlogAdapter(Context context) { |
|
65 this.context = context; |
|
66 } |
|
67 |
|
68 public int getCount() { |
|
69 return log.size(); |
|
70 } |
|
71 |
|
72 public Object getItem(int position) { |
|
73 return log.get(position); |
|
74 } |
|
75 |
|
76 public long getItemId(int position) { |
|
77 return position+idOffset; |
|
78 } |
|
79 |
|
80 public boolean hasStableIds() { |
|
81 return true; |
|
82 } |
|
83 |
|
84 public void clear() { |
|
85 idOffset += log.size(); |
|
86 log.clear(); |
|
87 notifyDataSetChanged(); |
|
88 } |
|
89 |
|
90 public void lineAdded(CharSequence text) { |
|
91 log.add(text); |
|
92 notifyDataSetChanged(); |
|
93 } |
|
94 |
|
95 public void lineRemoved() { |
|
96 log.remove(0); |
|
97 idOffset += 1; |
|
98 notifyDataSetChanged(); |
|
99 } |
|
100 |
|
101 public void setLog(Collection<CharSequence> log) { |
|
102 idOffset += log.size(); |
|
103 this.log = new ArrayList<CharSequence>(log); |
|
104 notifyDataSetChanged(); |
|
105 } |
|
106 |
|
107 public View getView(int position, View convertView, ViewGroup parent) { |
|
108 LoglineView v = (LoglineView)convertView; |
|
109 if (v == null) { |
|
110 v = new LoglineView(context); |
|
111 v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); |
|
112 v.setMovementMethod(LinkMovementMethod.getInstance()); |
|
113 } |
|
114 long id = getItemId(position); |
|
115 if(id != v.chatlogId) { |
|
116 v.setText(log.get(position)); |
|
117 v.chatlogId = id; |
|
118 } |
|
119 return v; |
|
120 } |
|
121 } |