merge
authorNikita Kozlov <nikita@beem-project.com>
Sat, 13 Feb 2010 15:46:21 +0100
changeset 656 b78913fc1a08
parent 655 a122632f8622 (current diff)
parent 654 6f4814eb7951 (diff)
child 657 f0ee4f04b3ac
merge
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/layout/chat_msg_row.xml	Sat Feb 13 15:46:21 2010 +0100
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_width="fill_parent" 
+    android:layout_height="wrap_content"
+    >
+
+    <TextView android:id="@+id/chatmessagename"
+	android:layout_width="fill_parent"
+	android:layout_height="wrap_content"
+	android:textSize="16sp"
+	android:textColor="#FFFFFF"
+	android:textStyle="bold"
+	android:paddingBottom="1dp"
+	/>
+    <TextView android:id="@+id/chatmessagetext"
+	android:layout_width="fill_parent"
+	android:layout_height="wrap_content"
+	android:autoLink="all"
+	/>
+</LinearLayout>
+
--- a/src/com/beem/project/beem/service/Message.java	Sat Feb 13 15:44:51 2010 +0100
+++ b/src/com/beem/project/beem/service/Message.java	Sat Feb 13 15:46:21 2010 +0100
@@ -47,7 +47,6 @@
 
 import android.os.Parcel;
 import android.os.Parcelable;
-import android.util.Log;
 
 /**
  * This class represents a instant message.
@@ -135,7 +134,7 @@
 		mType = MSG_TYPE_ERROR;
 		break;
 	    default:
-		Log.w("BEEM_MESSAGE", "type de message non gerer" + smackMsg.getType());
+		mType = MSG_TYPE_NORMAL;
 		break;
 	}
 	this.mFrom = smackMsg.getFrom();
--- a/src/com/beem/project/beem/ui/Chat.java	Sat Feb 13 15:44:51 2010 +0100
+++ b/src/com/beem/project/beem/ui/Chat.java	Sat Feb 13 15:46:21 2010 +0100
@@ -53,7 +53,6 @@
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.ComponentName;
-import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -61,7 +60,6 @@
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Color;
-import android.graphics.Typeface;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -69,6 +67,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;
@@ -80,7 +79,6 @@
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageView;
-import android.widget.LinearLayout;
 import android.widget.ListView;
 import android.widget.TextView;
 
@@ -344,30 +342,45 @@
      * @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();
+    }
+
+    /**
+     * Convert a list of Mesage coming from the service to a list of MessageText that can be displayed in UI.
+     * @param chatMessages the list of Message
+     * @return a list of message that can be displayed.
+     */
+    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;
     }
 
     /**
@@ -387,7 +400,7 @@
 	public void onServiceConnected(ComponentName name, IBinder service) {
 	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
 	    try {
-		if ((mRoster = mXmppFacade.getRoster()) != null )
+		if ((mRoster = mXmppFacade.getRoster()) != null)
 		    mRoster.addRosterListener(mBeemRosterListener);
 		mContact = new Contact(getIntent().getData());
 		if ((mChatManager = mXmppFacade.getChatManager()) != null)
@@ -496,6 +509,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 +537,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 +582,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 +592,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 +602,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 +615,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 +647,7 @@
 	private String mBareJid;
 	private String mName;
 	private String mMessage;
+	private boolean mIsError;
 
 	/**
 	 * Constructor.
@@ -641,6 +659,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, final boolean isError) {
+	    mBareJid = bareJid;
+	    mName = name;
+	    mMessage = message;
+	    mIsError = isError;
 	}
 
 	/**
@@ -692,54 +725,13 @@
 	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.
+	 * Get the message type.
+	 * @return true if the message is an error 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,16 +773,10 @@
 
 	    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();
 	    mInputField.setText(null);
 	}
     }
-
-    @Override
-    protected void finalize() {
-	Log.e("CHATFIN", "FINALIZE");
-    }
 }