/
TextMessageAdapter.java
230 lines (187 loc) · 7.85 KB
/
TextMessageAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Copyright (c) 2005-2017, BearWare.dk
*
* Contact Information:
*
* Bjoern D. Rasmussen
* Kirketoften 5
* DK-8260 Viby J
* Denmark
* Email: contact@bearware.dk
* Phone: +45 20 20 54 59
* Web: http://www.bearware.dk
*
* This source code is part of the TeamTalk 5 SDK owned by
* BearWare.dk. All copyright statements may not be removed
* or altered from any source distribution. If you use this
* software in a product, an acknowledgment in the product
* documentation is required.
*
*/
package dk.bearware.data;
import java.util.Vector;
import dk.bearware.ServerProperties;
import dk.bearware.TextMsgType;
import dk.bearware.gui.AccessibilityAssistant;
import dk.bearware.gui.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class TextMessageAdapter extends BaseAdapter {
private Vector<MyTextMessage> messages;
private LayoutInflater inflater;
private AccessibilityAssistant accessibilityAssistant;
private int myuserid;
private boolean show_logs = true;
int def_bg_color, def_text_color;
// color for text messages from other users
int user_bg_color = 0xff4c9fff, user_text_color = Color.WHITE;
// color for text message from oneself
int self_bg_color = 0xff659f5d, self_text_color = Color.WHITE;
// color for log-message type MSGTYPE_LOG_INFO
int loginfo_bg_color, loginfo_text_color; //uses default color scheme
// color for log-message type MSGTYPE_LOG_ERROR
int logerr_bg_color = 0xffcd0028, logerr_text_color = Color.WHITE;
// color for log-message type MSGTYPE_SERVERPROP
int srvinfo_bg_color = Color.DKGRAY, srvinfo_text_color = Color.WHITE;
public TextMessageAdapter(Context context, AccessibilityAssistant accessibilityAssistant,
Vector<MyTextMessage> msgs, int myuserid) {
this(context, accessibilityAssistant);
setMyUserID(myuserid);
setTextMessages(msgs);
}
@SuppressWarnings("ResourceType")
public TextMessageAdapter(Context context, AccessibilityAssistant accessibilityAssistant) {
inflater = LayoutInflater.from(context);
this.accessibilityAssistant = accessibilityAssistant;
setTextMessages(new Vector<MyTextMessage>());
TypedArray array = context.getTheme().obtainStyledAttributes(new int[] {
android.R.attr.colorBackground,
android.R.attr.textColorPrimary,
});
def_bg_color = array.getColor(0, 0xFF00FF);
def_text_color = array.getColor(1, 0xFF00FF);
array.recycle();
loginfo_bg_color = def_bg_color;
loginfo_text_color = def_text_color;
}
public void setTextMessages(Vector<MyTextMessage> msgs) {
messages = msgs;
}
Vector<MyTextMessage> getMessages() {
if(show_logs)
return messages;
Vector<MyTextMessage> result = new Vector<MyTextMessage>();
for(MyTextMessage m : messages) {
switch(m.nMsgType) {
case MyTextMessage.MSGTYPE_LOG_ERROR :
case MyTextMessage.MSGTYPE_LOG_INFO :
break;
default :
result.add(m);
break;
}
}
return result;
}
public void setMyUserID(int userid) {
myuserid = userid;
}
public void showLogMessages(boolean enable) {
show_logs = enable;
}
@Override
public int getCount() {
return getMessages().size();
}
@Override
public Object getItem(int position) {
return getMessages().get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyTextMessage txtmsg = getMessages().get(position);
int bg_color = Color.BLACK, text_color = Color.WHITE;
switch(txtmsg.nMsgType) {
case TextMsgType.MSGTYPE_CHANNEL :
case TextMsgType.MSGTYPE_USER : {
if(convertView == null ||
convertView.findViewById(R.id.item_textmsg) == null)
convertView = inflater.inflate(R.layout.item_textmsg, parent, false);
if(txtmsg.nFromUserID == myuserid) {
bg_color = self_bg_color;
text_color = self_text_color;
}
else {
bg_color = user_bg_color;
text_color = user_text_color;
}
TextView name = (TextView) convertView.findViewById(R.id.name_text);
TextView msgtext = (TextView) convertView.findViewById(R.id.msg_text);
TextView msgdate = (TextView) convertView.findViewById(R.id.time_text);
name.setText(txtmsg.szNickName);
msgdate.setText(txtmsg.time.toString());
msgtext.setText(txtmsg.szMessage);
name.setTextColor(text_color);
msgdate.setTextColor(text_color);
msgtext.setTextColor(text_color);
break;
}
case MyTextMessage.MSGTYPE_SERVERPROP : {
if(convertView == null ||
convertView.findViewById(R.id.item_textmsg_srvinfo) == null) {
convertView = inflater.inflate(R.layout.item_textmsg_srvinfo, parent, false);
}
bg_color = srvinfo_bg_color;
text_color = srvinfo_text_color;
TextView logmsg = (TextView) convertView.findViewById(R.id.srvname_text);
TextView logmotd = (TextView) convertView.findViewById(R.id.srvmotd_text);
TextView logtm = (TextView) convertView.findViewById(R.id.logtime_text);
ServerProperties p = (ServerProperties)txtmsg.userData;
logmsg.setText(p.szServerName);
logmotd.setText(p.szMOTD);
logtm.setText(txtmsg.time.toString());
logmsg.setTextColor(text_color);
logtm.setTextColor(text_color);
break;
}
case MyTextMessage.MSGTYPE_LOG_ERROR :
case MyTextMessage.MSGTYPE_LOG_INFO :
default : {
if(convertView == null ||
convertView.findViewById(R.id.item_textmsg_logmsg) == null) {
convertView = inflater.inflate(R.layout.item_textmsg_logmsg, parent, false);
}
switch(txtmsg.nMsgType) {
case MyTextMessage.MSGTYPE_LOG_ERROR :
bg_color = logerr_bg_color;
text_color = logerr_text_color;
break;
case MyTextMessage.MSGTYPE_LOG_INFO :
bg_color = loginfo_bg_color;
text_color = loginfo_text_color;
break;
}
TextView logmsg = (TextView) convertView.findViewById(R.id.logmsg_text);
TextView logtm = (TextView) convertView.findViewById(R.id.logtime_text);
logmsg.setText(txtmsg.szMessage);
logtm.setText(txtmsg.time.toString());
logmsg.setTextColor(text_color);
logtm.setTextColor(text_color);
break;
}
}
convertView.setBackgroundColor(bg_color);
convertView.setAccessibilityDelegate(accessibilityAssistant);
return convertView;
}
}