Les conversations sont retenues dans les chats du service.
authorDa Risk <darisk972@gmail.com>
Fri, 22 May 2009 22:29:14 +0200
changeset 219 79ccbe331695
parent 218 5a7653411251
child 220 2b8bebb95bbd
Les conversations sont retenues dans les chats du service. On peut donc les réafficher dans l'activité SendIm. J'en ai profité pour rendre cette activité SingleTop ainsi une seule instance set utilisé. Il reste un probleme avec les notifications qui ne peuvent pas etre réutilisé. Si on a 2 notifications la 2e ne marchera pas.
AndroidManifest.xml
res/values/strings.xml
src/com/beem/project/beem/service/BeemChatManager.java
src/com/beem/project/beem/service/ChatAdapter.java
src/com/beem/project/beem/service/Message.java
src/com/beem/project/beem/service/aidl/IChat.aidl
src/com/beem/project/beem/ui/SendIM.java
--- a/AndroidManifest.xml	Tue May 19 22:45:03 2009 +0200
+++ b/AndroidManifest.xml	Fri May 22 22:29:14 2009 +0200
@@ -13,7 +13,7 @@
 		</activity>
 		<activity android:name=".ui.ContactListSettings"
 			android:label="@string/app_name" />
-		<activity android:name=".ui.SendIM" android:label="@string/app_name" />
+		<activity android:name=".ui.SendIM" android:label="@string/app_name" android:launchMode="singleTop" />
 		<activity android:name=".ui.ChangeStatus" android:label="@string/app_name" />
 		<activity android:name=".ui.AccountCreation" android:label="@string/app_name" />
 		<activity android:name=".ui.AddContact" android:label="@string/app_name" />
--- a/res/values/strings.xml	Tue May 19 22:45:03 2009 +0200
+++ b/res/values/strings.xml	Fri May 22 22:29:14 2009 +0200
@@ -71,7 +71,7 @@
 	
 	<!--  SendIM class -->
 
-	<string name="SendIMSays"> says :\n</string>
+	<string name="SendIMSays"> %s says :\n</string>
 	<string name="SendIMYouSay">You say :\n</string>
 	<string name="SendIMToSendHint">Tip text here</string>
 	<string name="SendIMState">Is : </string>
--- a/src/com/beem/project/beem/service/BeemChatManager.java	Tue May 19 22:45:03 2009 +0200
+++ b/src/com/beem/project/beem/service/BeemChatManager.java	Fri May 22 22:29:14 2009 +0200
@@ -113,7 +113,7 @@
 	    Log.w(TAG, "CA devrait pas 1!!" + chat.getParticipant().getJID());
     }
 
-    private IChat getChat(Chat chat) {
+    private ChatAdapter getChat(Chat chat) {
 	String key = StringUtils.parseBareAddress(chat.getParticipant());
 	if (mChats.containsKey(key)) {
 	    return mChats.get(key);
@@ -172,6 +172,7 @@
 		notif.defaults = Notification.DEFAULT_ALL;
 		notif.flags = Notification.FLAG_AUTO_CANCEL;
 		Intent intent = new Intent(mService, SendIM.class);
+		intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT| Intent.FLAG_ACTIVITY_SINGLE_TOP);
 		// TODO use prefix for name
 		intent.putExtra("contact", chat.getParticipant());
 		notif.setLatestEventInfo(mService, text, mService.getString(R.string.BeemChatManagerNewMessage),
@@ -185,18 +186,18 @@
 
 	@Override
 	public void processMessage(Chat chat, Message message) {
-	    IChat newchat = getChat(chat);
+	    ChatAdapter newchat = getChat(chat);
 	    try {
 
 		if (message.getBody() != null)
-		    newchat.addToLastMessages(message.getBody());
+		    newchat.addMessage(new com.beem.project.beem.service.Message(message));
 		final int n = mRemoteMessageListeners.beginBroadcast();
 		for (int i = 0; i < n; i++) {
 		    IMessageListener listener = mRemoteMessageListeners.getBroadcastItem(i);
 		    listener.processMessage(newchat, new com.beem.project.beem.service.Message(message));
 		}
 		mRemoteMessageListeners.finishBroadcast();
-		if (newchat.isOpen() == false) {
+		if (! newchat.isOpen()) {
 		    notifyNewChat(newchat);
 		}
 	    } catch (RemoteException e) {
--- a/src/com/beem/project/beem/service/ChatAdapter.java	Tue May 19 22:45:03 2009 +0200
+++ b/src/com/beem/project/beem/service/ChatAdapter.java	Fri May 22 22:29:14 2009 +0200
@@ -3,6 +3,10 @@
  */
 package com.beem.project.beem.service;
 
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
 import org.jivesoftware.smack.Chat;
 import org.jivesoftware.smack.XMPPException;
 
@@ -20,7 +24,8 @@
     private String mState;
     private StringBuffer mLastMessages;
     private boolean isOpen;
- 
+    private List<Message> mMessages;
+
     /**
      * Constructor.
      * @param chat The chat to adapt
@@ -29,6 +34,7 @@
 	mLastMessages = new StringBuffer();
 	mAdaptee = chat;
 	mParticipant = new Contact(chat.getParticipant());
+	mMessages = new LinkedList<Message>();
     }
 
     /**
@@ -54,6 +60,7 @@
 	// send.set
 	try {
 	    mAdaptee.sendMessage(send);
+	    mMessages.add(message);
 	} catch (XMPPException e) {
 	    // TODO Auto-generated catch block
 	    e.printStackTrace();
@@ -64,7 +71,7 @@
     public String getState() throws RemoteException {
 	return mState;
     }
-    
+
     @Override
     public void setState(String state) throws RemoteException {
 	mState = state;
@@ -74,24 +81,10 @@
 	return mAdaptee;
     }
 
-    @Override
-    public String getLastMessages() throws RemoteException {
-	return mLastMessages.toString();
-    }
-
-    @Override
-    public void addToLastMessages(String msg) throws RemoteException {
-	mLastMessages.append(msg).append('\n');
-    }
-
-    @Override
-    public void clearLastMessages() throws RemoteException {
-	mLastMessages.delete(0, mLastMessages.length());
-    }
-
     /**
      * @param isOpen the isOpen to set
      */
+    @Override
     public void setOpen(boolean isOpen) {
 	this.isOpen = isOpen;
     }
@@ -99,8 +92,20 @@
     /**
      * @return the isOpen
      */
+    @Override
     public boolean isOpen() {
 	return isOpen;
     }
 
+    @Override
+    public List<Message> getMessages() throws RemoteException {
+	return Collections.unmodifiableList(mMessages);
+    }
+
+    void addMessage(Message msg) {
+	if (mMessages.size() == 50)
+	    mMessages.remove(0);
+	mMessages.add(msg);
+    }
+
 }
--- a/src/com/beem/project/beem/service/Message.java	Tue May 19 22:45:03 2009 +0200
+++ b/src/com/beem/project/beem/service/Message.java	Fri May 22 22:29:14 2009 +0200
@@ -39,6 +39,7 @@
     private String mBody;
     private String mSubject;
     private String mTo;
+    private String mFrom;
     private String mThread;
     // TODO ajouter l'erreur
 
@@ -69,6 +70,7 @@
 	mBody = "";
 	mSubject = "";
 	mThread = "";
+	mFrom = null;
     }
 
     /**
@@ -100,6 +102,7 @@
 		Log.w("BEEM_MESSAGE", "type de message non gerer" + smackMsg.getType());
 		break;
 	}
+	this.mFrom = smackMsg.getFrom();
 	if (mType == MSG_TYPE_ERROR) {
 	    XMPPError er = smackMsg.getError();
 	    String msg = er.getMessage();
@@ -124,6 +127,7 @@
 	mBody = in.readString();
 	mSubject = in.readString();
 	mThread = in.readString();
+	mFrom = in.readString();
     }
 
     /**
@@ -137,6 +141,7 @@
 	dest.writeString(mBody);
 	dest.writeString(mSubject);
 	dest.writeString(mThread);
+	dest.writeString(mFrom);
     }
 
     /**
@@ -204,6 +209,20 @@
     }
 
     /**
+     * @param mFrom the mFrom to set
+     */
+    public void setFrom(String mFrom) {
+	this.mFrom = mFrom;
+    }
+
+    /**
+     * @return the mFrom
+     */
+    public String getFrom() {
+	return mFrom;
+    }
+
+    /**
      * Get the thread of the message.
      * @return the thread
      */
--- a/src/com/beem/project/beem/service/aidl/IChat.aidl	Tue May 19 22:45:03 2009 +0200
+++ b/src/com/beem/project/beem/service/aidl/IChat.aidl	Fri May 22 22:29:14 2009 +0200
@@ -29,14 +29,6 @@
 	
 	void setState(in String state);
 	
-	String getLastMessages();
-	
-	void addToLastMessages(in String msg);
-	
-	void clearLastMessages();
-/*	
-	void addMessageListener(in IMessageListener listener);
-	
-	void removeMessageListener(in IMessageListener listener);
-*/	
+	List<Message> getMessages();
+
 }
\ No newline at end of file
--- a/src/com/beem/project/beem/ui/SendIM.java	Tue May 19 22:45:03 2009 +0200
+++ b/src/com/beem/project/beem/ui/SendIM.java	Fri May 22 22:29:14 2009 +0200
@@ -1,6 +1,9 @@
 package com.beem.project.beem.ui;
 
+import java.util.List;
+
 import android.app.Activity;
+import android.content.Intent;
 import android.content.SharedPreferences;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
@@ -50,7 +53,7 @@
     private TextView mText;
     private TextView mLogin;
     private ScrollView mScrolling;
-    private boolean mSpeak;
+    private char mSpeak;
 
     /**
      * Constructor.
@@ -82,31 +85,29 @@
 	mScrolling = (ScrollView) findViewById(R.id.sendimscroll);
     }
 
-    private void setViewHeader() {
-	Drawable avatar = (Drawable) getResources().getDrawable(R.drawable.avatar);
-	ImageView imgV = (ImageView) findViewById(R.id.sendimavatar);
-	imgV.setImageDrawable(avatar);
-	mLogin = (TextView) findViewById(R.id.sendimlogin);
-	mLogin.setText(mContact.getJID());
-	TextView status = (TextView) findViewById(R.id.sendimstatus);
-	status.setTextSize(12);
-	mLogin.setTextColor(getResources().getColor(R.color.white));
-	String statmsg = mContact.getMsgState();
-	if (statmsg != null)
-	    status.setText(statmsg);;
-    }
-
     @Override
-    public void onStart() {
+    protected void onStart() {
 	super.onStart();
 	// TODO cancel the notification if any
 	if (mContact == null)
-	    mContact = getIntent().getParcelableExtra("contact");	
+	    mContact = getIntent().getParcelableExtra("contact");
 	mService = mBeemApplication.getXmppFacade();
 	setViewHeader();
 
     }
 
+    @Override
+    protected void onNewIntent(Intent intent) {
+	// TODO Auto-generated method stub
+	super.onNewIntent(intent);
+	Contact c = intent.getParcelableExtra("contact");
+	try {
+	    switchChat(c);
+	} catch (RemoteException e) {
+	    Log.e(TAG, "Error durring switch chat", e);
+	}
+	setViewHeader();
+    }
 
     @Override
     protected void onResume() {
@@ -122,21 +123,14 @@
 		try {
 		    mChatManager = mService.getChatManager();
 		    mChatManager.addChatCreationListener(mChatManagerListener);
-		    mChat = mChatManager.createChat(mContact, mMessageListener);
-		    String text = mChat.getLastMessages();
-		    if (!"".equals(text)) {
-			mText.append(mContact.getJID() + " " + getString(R.string.SendIMSays));
-			mText.append(text);
-			//mChat.clearLastMessages();
-		    }
-		    mChat.setOpen(true);
+		    switchChat(mContact);
 		} catch (RemoteException e) {
 		    Log.e(TAG, "Error during chat manager creation", e);
 		}
 	    }
 	});
+    }
 
-    }
     @Override
     protected void onPause() {
 	Log.d(TAG, "onPause");
@@ -166,7 +160,8 @@
 	if (mChatManager != null) {
 	    try {
 		mChatManager.removeChatCreationListener(mChatManagerListener);
-		mChatManager.destroyChat(mChat);
+		// TODO trouver quand detruire le chat
+		// mChatManager.destroyChat(mChat);
 	    } catch (RemoteException e) {
 		Log.e(TAG, "mchat manager and SendIM destroy", e);
 	    }
@@ -181,34 +176,6 @@
     }
 
     /**
-     * This method send a message to the server over the XMPP connection and display it on activity view TODO :
-     * Exception si la connexion se coupe pendant la conversation
-     */
-    private void sendText() {
-	if (mSpeak)
-	    mSpeak = false;
-	String text = mToSend.getText().toString();
-	if (!text.equals("")) {
-	    Message msg = new Message(mContact.getJID(), Message.MSG_TYPE_CHAT);
-	    msg.setBody(text);
-	    try {
-		mChat.sendMessage(msg);
-	    } catch (RemoteException e) {
-		// TODO Auto-generated catch block
-		e.printStackTrace();
-	    }
-	    if (!mSpeak)
-		mText.append(getString(R.string.SendIMYouSay) + text + "\n");
-	    else
-		mText.append(text + "\n");
-	    mToSend.setText(null);
-	    mScrolling.fullScroll(ScrollView.FOCUS_DOWN);
-	    mToSend.requestFocus();
-	    mSpeak = true;
-	}
-    }
-
-    /**
      * Abstract method inherited from OnKeyListener
      */
     public boolean onKey(View v, int keyCode, KeyEvent event) {
@@ -247,6 +214,76 @@
 	}
     }
 
+    /**
+     * This method send a message to the server over the XMPP connection and display it on activity view TODO :
+     * Exception si la connexion se coupe pendant la conversation
+     */
+    private void sendText() {
+	String text = mToSend.getText().toString();
+	if (!text.equals("")) {
+	    Message msg = new Message(mContact.getJID(), Message.MSG_TYPE_CHAT);
+	    msg.setBody(text);
+	    try {
+		mChat.sendMessage(msg);
+	    } catch (RemoteException e) {
+		// TODO Auto-generated catch block
+		e.printStackTrace();
+	    }
+	    if (mSpeak != 1)
+		mText.append(getString(R.string.SendIMYouSay) + text + "\n");
+	    else
+		mText.append(text + "\n");
+	    mToSend.setText(null);
+	    mScrolling.fullScroll(ScrollView.FOCUS_DOWN);
+	    mToSend.requestFocus();
+	    mSpeak = 1;
+	}
+    }
+
+    private void switchChat(Contact newContact) throws RemoteException {
+	if (mChat != null)
+	    mChat.setOpen(false);
+	mChat = mChatManager.createChat(newContact, mMessageListener);
+	showMessageList(mChat.getMessages());
+	mChat.setOpen(true);
+	mContact = newContact;
+    }
+
+    private void setViewHeader() {
+	Drawable avatar = (Drawable) getResources().getDrawable(R.drawable.avatar);
+	ImageView imgV = (ImageView) findViewById(R.id.sendimavatar);
+	imgV.setImageDrawable(avatar);
+	mLogin = (TextView) findViewById(R.id.sendimlogin);
+	mLogin.setText(mContact.getJID());
+	TextView status = (TextView) findViewById(R.id.sendimstatus);
+	status.setTextSize(12);
+	mLogin.setTextColor(getResources().getColor(R.color.white));
+	String statmsg = mContact.getMsgState();
+	if (statmsg != null)
+	    status.setText(statmsg);
+    }
+
+    private void showMessageList(List<Message> messages) {
+	mText.setText("");
+	mSpeak = 0;
+	for (Message message : messages) {
+	    String from = message.getFrom();
+	    if (from == null) {
+		if (mSpeak != 1)
+		    mText.append(getString(R.string.SendIMYouSay));
+		mSpeak = 1;
+	    } else {
+		if (mSpeak != 2) {
+		    String str = String.format(getString(R.string.SendIMSays), from);
+		    mText.append(str);
+		}
+		mSpeak = 2;
+	    }
+	    mText.append(message.getBody() + '\n');
+	}
+	mScrolling.fullScroll(ScrollView.FOCUS_DOWN);
+    }
+
     private class OnChatListener extends IChatManagerListener.Stub {
 
 	@Override
@@ -269,22 +306,26 @@
 		@Override
 		public void run() {
 		    if (m.getBody() != null) {
-			if (!mSpeak)
+			if (mSpeak == 2)
 			    mText.append(m.getBody() + "\n");
-			else
-			    mText.append(mContact.getJID() + " " + getString(R.string.SendIMSays) + m.getBody() + "\n");
-			mSpeak = false;
+			else {
+			    String str = String.format(getString(R.string.SendIMSays), m.getFrom());
+			    mText.append(str);
+			    mText.append(m.getBody() + "\n");
+			}
 			mScrolling.fullScroll(ScrollView.FOCUS_DOWN);
 			mToSend.requestFocus();
+			mSpeak = 2;
 		    }
+
 		}
 	    });
 	}
 
 	@Override
 	public void stateChanged(IChat chat) throws RemoteException {
-	    //TODO: a integrer dans l'ui
-	    //Log.d(TAG, "state changed");
+	    // TODO: a integrer dans l'ui
+	    // Log.d(TAG, "state changed");
 	}
     }
 }