Utilisation de setData() dans les pour passer un contact aux Intent
authorDa Risk <darisk972@gmail.com>
Sat, 23 May 2009 20:15:18 +0200
changeset 222 f72be51936d5
parent 221 d2c030543834
child 223 bb656974bab1
Utilisation de setData() dans les pour passer un contact aux Intent Fixe le bug sur les notifications.
src/com/beem/project/beem/service/BeemChatManager.java
src/com/beem/project/beem/service/Contact.java
src/com/beem/project/beem/ui/ContactDialog.java
src/com/beem/project/beem/ui/ContactList.java
src/com/beem/project/beem/ui/SendIM.java
--- a/src/com/beem/project/beem/service/BeemChatManager.java	Sat May 23 16:56:49 2009 +0200
+++ b/src/com/beem/project/beem/service/BeemChatManager.java	Sat May 23 20:15:18 2009 +0200
@@ -142,8 +142,8 @@
 	public void chatCreated(Chat chat, boolean locally) {
 	    IChat newchat = getChat(chat);
 	    if (!locally) {
-		// chat.addMessageListener(mChatListener);
-		notifyNewChat(newchat);
+		// Pas necessaire vu qu'on recoit l'event processMessage juste apres
+		// notifyNewChat(newchat);
 	    }
 	    chat.addMessageListener(mChatListener);
 	    final int n = mRemoteChatCreationListeners.beginBroadcast();
@@ -173,10 +173,9 @@
 		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());
+		intent.setData(chat.getParticipant().toUri());
 		notif.setLatestEventInfo(mService, text, mService.getString(R.string.BeemChatManagerNewMessage),
-		    PendingIntent.getActivity(mService, 0, intent, PendingIntent.FLAG_ONE_SHOT));
+		    PendingIntent.getActivity(mService, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
 		int id = chat.hashCode();
 		mService.sendNotification(id, notif);
 	    } catch (RemoteException e) {
--- a/src/com/beem/project/beem/service/Contact.java	Sat May 23 16:56:49 2009 +0200
+++ b/src/com/beem/project/beem/service/Contact.java	Sat May 23 20:15:18 2009 +0200
@@ -9,13 +9,14 @@
 
 import org.jivesoftware.smack.RosterGroup;
 import org.jivesoftware.smack.packet.Presence;
+import org.jivesoftware.smack.util.StringUtils;
+
+import android.net.Uri;
+import android.os.Parcel;
+import android.os.Parcelable;
 
 import com.beem.project.beem.utils.Status;
 
-import android.os.Parcel;
-import android.os.Parcelable;
-import android.util.Log;
-
 /**
  * This class contains informations on a jabber contact.
  * @author darisk
@@ -51,9 +52,14 @@
      * Constructor.
      */
     public Contact() {
-	// TODO Auto-generated constructor stub
     }
 
+    public Contact(Uri uri) {
+	if(! uri.getScheme().equals("xmpp"))
+	    throw new IllegalArgumentException();
+	mJID = uri.getSchemeSpecificPart();
+    }
+    
     /**
      * Constructor.
      * @param jid JID of the contact
@@ -143,11 +149,11 @@
 	mStatus = Status.getStatusFromPresence(presence);
 	mMsgState = presence.getStatus();
     }
-    
+
     public void setStatus(PresenceAdapter presence) {
 	mStatus = presence.getStatus();
 	mMsgState = presence.getStatusText();
-	
+
     }
 
     /**
@@ -176,14 +182,13 @@
 
     /**
      * Set the Jabber ID of the contact.
-     * @param mjid the jabber ID to set
+     * @param jid the jabber ID to set
      */
-    public void setJID(String mjid) {
-	mJID = mjid;
+    public void setJID(String jid) {
+	mJID = jid;
     }
 
     /**
-     * 
      * @param res
      */
     public void addRes(String res) {
@@ -192,7 +197,6 @@
     }
 
     /**
-     * 
      * @param res
      */
     public void delRes(String res) {
@@ -212,7 +216,7 @@
     public List<String> getMRes() {
 	return mRes;
     }
-    
+
     @Override
     public String toString() {
 	if (mJID != null)
@@ -225,7 +229,7 @@
 	    mGroups.add(rosterGroup.getName());
 	}
     }
-    
+
     public void addGroup(String group) {
 	mGroups.add(group);
     }
@@ -236,11 +240,22 @@
     public void setGroups(List<String> mGroups) {
 	this.mGroups = mGroups;
     }
-    
+
     /**
      * @return the mGroups
      */
     public List<String> getGroups() {
 	return mGroups;
-    }    
+    }
+
+    public Uri toUri() {
+	StringBuilder build = new StringBuilder("xmpp:");
+	build.append(StringUtils.parseName(mJID))
+		.append('@')
+		.append(StringUtils.parseServer(mJID));
+	Uri u = Uri.parse(build.toString());
+	return u;
+    }
+    
+    
 }
--- a/src/com/beem/project/beem/ui/ContactDialog.java	Sat May 23 16:56:49 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactDialog.java	Sat May 23 20:15:18 2009 +0200
@@ -41,7 +41,7 @@
 	public void onClick(View v) {
 	    Activity a = ContactDialog.this.getOwnerActivity();
 	    Intent i = new Intent(mContext, SendIM.class);
-	    i.putExtra("contact", mContact);
+	    i.setData(mContact.toUri());
 	    a.startActivity(i);
 	    dismiss();
 	}
--- a/src/com/beem/project/beem/ui/ContactList.java	Sat May 23 16:56:49 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Sat May 23 20:15:18 2009 +0200
@@ -253,7 +253,7 @@
 		for (Contact curContact : mListContact) {
 		    if (jid.equals(curContact.getJID())) {
 			Intent i = new Intent(ContactList.this, SendIM.class);
-			i.putExtra("contact", curContact);
+			i.setData(curContact.toUri());
 			startActivity(i);
 			break;
 		    }
--- a/src/com/beem/project/beem/ui/SendIM.java	Sat May 23 16:56:49 2009 +0200
+++ b/src/com/beem/project/beem/ui/SendIM.java	Sat May 23 20:15:18 2009 +0200
@@ -79,7 +79,8 @@
 	mToSend.setOnClickListener(this);
 	mToSend.setOnKeyListener(this);
 	mLogin = (TextView) findViewById(R.id.sendimlogin);
-	mContact = getIntent().getParcelableExtra("contact");
+	// mContact = getIntent().getParcelableExtra("contact");
+	mContact = new Contact(getIntent().getData());
 	setViewHeader();
 	mText = (TextView) findViewById(R.id.sendimlist);
 	mScrolling = (ScrollView) findViewById(R.id.sendimscroll);
@@ -88,7 +89,6 @@
     @Override
     protected void onStart() {
 	super.onStart();
-	Log.i(TAG, "on start");
 	// TODO cancel the notification if any
 	if (mContact == null)
 	    mContact = getIntent().getParcelableExtra("contact");
@@ -100,15 +100,13 @@
     @Override
     protected void onNewIntent(Intent intent) {
 	super.onNewIntent(intent);
-	mContact = intent.getParcelableExtra("contact");
+	mContact = new Contact(intent.getData());
 	setViewHeader();
-	Log.i(TAG, "new intent");
     }
 
     @Override
     protected void onResume() {
 	super.onResume();
-	Log.i(TAG, "resume");
 	mBeemApplication = BeemApplication.getApplication(this);
 	if (!mBeemApplication.isConnected())
 	    mBeemApplication.startBeemService();
@@ -133,7 +131,6 @@
     @Override
     protected void onPause() {
 	super.onPause();
-	Log.d(TAG, "onPause");
 	try {
 	    mChat.setOpen(false);
 	} catch (RemoteException e) {
@@ -144,7 +141,6 @@
     @Override
     protected void onStop() {
 	super.onStop();
-	Log.d(TAG, "onStop");
 	try {
 	    mChat.setOpen(false);
 	} catch (RemoteException e) {
@@ -156,7 +152,6 @@
     @Override
     protected void onDestroy() {
 	super.onDestroy();
-	Log.d(TAG, "onDestroy");
 	if (mChatManager != null) {
 	    try {
 		mChatManager.removeChatCreationListener(mChatManagerListener);
@@ -317,7 +312,6 @@
 			mToSend.requestFocus();
 			mSpeak = 2;
 		    }
-
 		}
 	    });
 	}