--- a/src/com/beem/project/beem/ui/Chat.java Sun Jan 31 16:04:10 2010 +0100
+++ b/src/com/beem/project/beem/ui/Chat.java Thu Feb 11 17:52:41 2010 +0100
@@ -69,6 +69,7 @@
import android.text.util.Linkify;
import android.util.Log;
import android.view.KeyEvent;
+import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@@ -344,30 +345,40 @@
* @throws RemoteException If a Binder remote-invocation error occurred.
*/
private void playRegisteredTranscript() throws RemoteException {
- String fromBareJid = null;
- String fromName = null;
- List<Message> chatMessages = mChat.getMessages();
mListMessages.clear();
+ List<MessageText> msgList = convertMessagesList(mChat.getMessages());
+ mListMessages.addAll(msgList);
mMessagesListAdapter.notifyDataSetChanged();
+ }
+
+ private List<MessageText> convertMessagesList(List<Message> chatMessages) {
+ List<MessageText> result = new ArrayList<MessageText>(chatMessages.size());
+ String remoteName = mContact.getName();
+ String localName = getString(R.string.chat_self);
MessageText lastMessage = null;
for (Message m : chatMessages) {
- fromBareJid = StringUtils.parseBareAddress(m.getFrom());
- fromName = mContact.getName();
- if (fromBareJid == null) {
- fromBareJid = getString(R.string.chat_self);
- fromName = getString(R.string.chat_self);
- }
- if (m.getBody() != null) {
- if (lastMessage == null || !lastMessage.getBareJid().equals(fromBareJid)) {
- lastMessage = new MessageText(fromBareJid, fromName, m.getBody());
- mListMessages.add(lastMessage);
- } else {
- lastMessage.setMessage(lastMessage.getMessage().concat("\n" + m.getBody()));
+ String name = remoteName;
+ String fromBareJid = StringUtils.parseBareAddress(m.getFrom());
+ if (m.getType() == Message.MSG_TYPE_ERROR) {
+ lastMessage = null;
+ result.add(new MessageText(fromBareJid, name, m.getBody(), true));
+ continue;
+ } else if (m.getType() == Message.MSG_TYPE_CHAT) {
+ if (fromBareJid == null) { //nofrom or from == yours
+ name = localName;
+ }
+ if (m.getBody() != null) {
+ if (lastMessage == null || !lastMessage.getBareJid().equals(fromBareJid)) {
+ lastMessage = new MessageText(fromBareJid, name, m.getBody());
+ result.add(lastMessage);
+ } else {
+ lastMessage.setMessage(lastMessage.getMessage().concat("\n" + m.getBody()));
+ }
}
}
}
- mMessagesListAdapter.notifyDataSetChanged();
+ return result;
}
/**
@@ -496,6 +507,9 @@
} else if (msg.getBody() != null)
mListMessages.add(new MessageText(fromBareJid, mContact.getName(), msg.getBody()));
mMessagesListAdapter.notifyDataSetChanged();
+ } else if (msg.getType() == Message.MSG_TYPE_ERROR) {
+ mListMessages.add(new MessageText(fromBareJid, mContact.getName(), msg.getBody(), true));
+ mMessagesListAdapter.notifyDataSetChanged();
}
}
});
@@ -521,7 +535,6 @@
// Check for a contact status message update
if (!(mContactStatusMsgTextView.getText().toString().equals(mContact.getMsgState()))) {
- Log.d(TAG, "Setting status message - " + mContact.getMsgState());
mContactStatusMsgTextView.setText(mContact.getMsgState());
Linkify.addLinks(mContactStatusMsgTextView, Linkify.WEB_URLS);
}
@@ -567,6 +580,7 @@
* Returns the number of messages contained in the messages list.
* @return The number of messages contained in the messages list.
*/
+ @Override
public int getCount() {
return mListMessages.size();
}
@@ -576,8 +590,9 @@
* @param position The position of the requested item.
* @return The item from the messages list at the requested position.
*/
+ @Override
public Object getItem(int position) {
- return position;
+ return mListMessages.get(position);
}
/**
@@ -585,6 +600,7 @@
* @param position The position of the requested item.
* @return The id of an item from the messages list at the requested position.
*/
+ @Override
public long getItemId(int position) {
return position;
}
@@ -597,27 +613,26 @@
* @return A View corresponding to the data at the specified position.
*/
public View getView(int position, View convertView, ViewGroup parent) {
- MessageView sv;
+ View sv;
if (convertView == null) {
- sv = new MessageView(Chat.this, mListMessages.get(position).getName(), mListMessages.get(position)
- .getMessage());
+ LayoutInflater inflater = Chat.this.getLayoutInflater();
+ sv = inflater.inflate(R.layout.chat_msg_row, null);
} else {
- sv = (MessageView) convertView;
- sv.setName(mListMessages.get(position).getName());
- sv.setMessage(mListMessages.get(position).getMessage());
+ sv = convertView;
}
+ MessageText msg = mListMessages.get(position);
+ TextView msgName = (TextView) sv.findViewById(R.id.chatmessagename);
+ msgName.setText(msg.getName());
+ msgName.setTextColor(Color.WHITE);
+ msgName.setError(null);
+ TextView msgText = (TextView) sv.findViewById(R.id.chatmessagetext);
+ msgText.setText(msg.getMessage());
+ if (msg.isError()) {
+ msgName.setText("Error");
+ msgName.setTextColor(Color.RED);
+ msgName.setError("testing");
- //TODO Put this in the xml layout
- sv.setPadding(2, 2, 2, 4);
-
- sv.mName.setTextSize(16);
- sv.mName.setTextColor(Color.WHITE);
- sv.mName.setTypeface(Typeface.DEFAULT_BOLD);
-
- sv.mMessage.setLinkTextColor(Color.WHITE);
- sv.mMessage.setPadding(0, 4, 0, 4);
- Linkify.addLinks(sv.mMessage, Linkify.WEB_URLS);
-
+ }
return sv;
}
}
@@ -630,6 +645,7 @@
private String mBareJid;
private String mName;
private String mMessage;
+ private boolean mIsError;
/**
* Constructor.
@@ -641,6 +657,21 @@
mBareJid = bareJid;
mName = name;
mMessage = message;
+ mIsError = false;
+ }
+
+ /**
+ * Constructor.
+ * @param bareJid A String containing the bare JID of the message's author.
+ * @param name A String containing the name of the message's author.
+ * @param message A String containing the message.
+ * @param isError if the message is an error message.
+ */
+ public MessageText(final String bareJid, final String name, final String message, boolean isError) {
+ mBareJid = bareJid;
+ mName = name;
+ mMessage = message;
+ mIsError = isError;
}
/**
@@ -692,54 +723,9 @@
public void setMessage(String message) {
mMessage = message;
}
- }
- /**
- * We will use a MessageView to display each message.
- * TODO : put this in an xml file
- */
- private class MessageView extends LinearLayout {
- private final TextView mName;
- private final TextView mMessage;
-
- /**
- * Constructor.
- * @param context The context of the MessageView
- * @param name A String containing the message's author.
- * @param message A String containing the message.
- */
- public MessageView(final Context context, final String name, final String message) {
- super(context);
-
- this.setOrientation(VERTICAL);
-
- mName = new TextView(context);
- mName.setText(name);
- addView(mName,
- new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
- android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
-
- mMessage = new TextView(context);
- mMessage.setText(message);
- addView(mMessage,
- new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,
- android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
- }
-
- /**
- * Convenience method to set the title of a MessageView.
- * @param name A String containing the message's author.
- */
- public void setName(String name) {
- mName.setText(name);
- }
-
- /**
- * Convenience method to set the dialogue of a MessageView.
- * @param message A String containing the message.
- */
- public void setMessage(String message) {
- mMessage.setText(message);
+ public boolean isError() {
+ return mIsError;
}
}
@@ -781,7 +767,6 @@
if (lastMessage != null && lastMessage.getName().equals(self)) {
lastMessage.setMessage(lastMessage.getMessage().concat("\n" + inputContent));
- mListMessages.set(mListMessages.size() - 1, lastMessage);
} else
mListMessages.add(new MessageText(self, self, inputContent));
mMessagesListAdapter.notifyDataSetChanged();