Fix bug when creating contact.
authorDa Risk <da_risk@beem-project.com>
Tue, 21 Feb 2012 21:07:42 +0100
changeset 984 f27ce0166608
parent 983 d7df77602216
child 985 24474027eb45
Fix bug when creating contact. The contact is created asynchronously so we cannot try to get it immediatly. As a consequence the method IRoster.createContact now return a boolean.
res/values-fr/strings.xml
src/com/beem/project/beem/service/RosterAdapter.java
src/com/beem/project/beem/service/aidl/IRoster.aidl
src/com/beem/project/beem/ui/AddContact.java
--- a/res/values-fr/strings.xml	Tue Feb 21 12:11:58 2012 +0100
+++ b/res/values-fr/strings.xml	Tue Feb 21 21:07:42 2012 +0100
@@ -39,7 +39,7 @@
 	<string name="AddCOkButton">Ajouter</string>
 	<string name="AddCContactAdded">Contact ajouté</string>
 	<string name="AddCContactAddedError">Erreur Contact non ajouté</string>
-	<string name="AddCContactAddedLoginError">Error Nom d\'utilisateur</string>
+	<string name="AddCContactAddedLoginError">Mauvais nom d\'utilisateur</string>
 	<string name="AddCBadForm">Mauvais formulaire</string>
 	<string name="AddCContactAlready">Contact déjà ajouté</string>
 
--- a/src/com/beem/project/beem/service/RosterAdapter.java	Tue Feb 21 12:11:58 2012 +0100
+++ b/src/com/beem/project/beem/service/RosterAdapter.java	Tue Feb 21 21:07:42 2012 +0100
@@ -50,6 +50,18 @@
 import java.util.List;
 import java.util.Map;
 
+import android.content.Context;
+import android.os.RemoteCallbackList;
+import android.os.RemoteException;
+import android.util.Log;
+
+import com.beem.project.beem.R;
+import com.beem.project.beem.service.aidl.IBeemRosterListener;
+import com.beem.project.beem.smack.avatar.AvatarListener;
+import com.beem.project.beem.smack.avatar.AvatarManager;
+import com.beem.project.beem.smack.avatar.AvatarMetadataExtension.Info;
+import com.beem.project.beem.utils.Status;
+
 import org.jivesoftware.smack.Roster;
 import org.jivesoftware.smack.RosterEntry;
 import org.jivesoftware.smack.RosterGroup;
@@ -58,17 +70,6 @@
 import org.jivesoftware.smack.packet.Presence;
 import org.jivesoftware.smack.util.StringUtils;
 
-import android.content.Context;
-import android.os.RemoteCallbackList;
-import android.os.RemoteException;
-import android.util.Log;
-
-import com.beem.project.beem.R;
-import com.beem.project.beem.service.aidl.IBeemRosterListener;
-import com.beem.project.beem.utils.Status;
-import com.beem.project.beem.smack.avatar.AvatarMetadataExtension.Info;
-import com.beem.project.beem.smack.avatar.AvatarManager;
-import com.beem.project.beem.smack.avatar.AvatarListener;
 
 /**
  * This class implement a Roster adapter for BEEM.
@@ -124,16 +125,16 @@
      * {@inheritDoc}
      */
     @Override
-    public Contact addContact(String user, String name, String[] groups) throws RemoteException {
+    public boolean addContact(String user, String name, String[] groups) throws RemoteException {
 	RosterEntry contact = mAdaptee.getEntry(user);
 	try {
 	    mAdaptee.createEntry(user, name, groups);
 	    contact = mAdaptee.getEntry(user);
 	} catch (XMPPException e) {
 	    Log.e(TAG, "Error while adding new contact", e);
-	    return null;
+	    return false;
 	}
-	return getContactFromRosterEntry(contact);
+	return true;
     }
 
     /**
--- a/src/com/beem/project/beem/service/aidl/IRoster.aidl	Tue Feb 21 12:11:58 2012 +0100
+++ b/src/com/beem/project/beem/service/aidl/IRoster.aidl	Tue Feb 21 21:07:42 2012 +0100
@@ -49,7 +49,7 @@
 
 interface IRoster {
 
-    Contact addContact(in String user, in String name, in String[] groups);
+    boolean addContact(in String user, in String name, in String[] groups);
 
     void deleteContact(in Contact contact);
 
--- a/src/com/beem/project/beem/ui/AddContact.java	Tue Feb 21 12:11:58 2012 +0100
+++ b/src/com/beem/project/beem/ui/AddContact.java	Tue Feb 21 21:07:42 2012 +0100
@@ -64,6 +64,7 @@
 
 import com.beem.project.beem.BeemService;
 import com.beem.project.beem.R;
+import com.beem.project.beem.service.aidl.IRoster;
 import com.beem.project.beem.service.aidl.IXmppFacade;
 import com.beem.project.beem.utils.BeemBroadcastReceiver;
 
@@ -139,6 +140,16 @@
     }
 
     /**
+     * Get the text of a widget.
+     * @param id the id of the widget.
+     * @return the text of the widget.
+     */
+    private String getWidgetText(int id) {
+	EditText widget = (EditText) this.findViewById(id);
+	return widget.getText().toString();
+    }
+
+    /**
      * The ServiceConnection used to connect to the Beem service.
      */
     private class BeemServiceConnection implements ServiceConnection {
@@ -161,16 +172,6 @@
     }
 
     /**
-     * Get the text of a widget.
-     * @param id the id of the widget.
-     * @return the text of the widget.
-     */
-    private String getWidgetText(int id) {
-	EditText widget = (EditText) this.findViewById(id);
-	return widget.getText().toString();
-    }
-
-    /**
      * Listener.
      */
     private class OkListener implements OnClickListener {
@@ -184,10 +185,6 @@
 	public void onClick(View v) {
 	    String login;
 	    login = getWidgetText(R.id.addc_login);
-	    if (login.length() == 0) {
-		Toast.makeText(AddContact.this, getString(R.string.AddCBadForm), Toast.LENGTH_SHORT).show();
-		return;
-	    }
 	    boolean isEmail = Pattern.matches("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+[a-zA-Z]{2,4}", login);
 	    if (!isEmail) {
 		Toast.makeText(AddContact.this, getString(R.string.AddCContactAddedLoginError), Toast.LENGTH_SHORT)
@@ -200,13 +197,14 @@
 		mGroup.add(getWidgetText(R.id.addc_group));
 	    try {
 		if (mXmppFacade != null) {
-		    if (mXmppFacade.getRoster().getContact(login) != null) {
-			mGroup.addAll(mXmppFacade.getRoster().getContact(login).getGroups());
+		    IRoster roster = mXmppFacade.getRoster();
+		    if (roster.getContact(login) != null) {
+			mGroup.addAll(roster.getContact(login).getGroups());
 			Toast.makeText(AddContact.this, getString(R.string.AddCContactAlready), Toast.LENGTH_SHORT)
 			    .show();
 			return;
 		    }
-		    if (mXmppFacade.getRoster().addContact(login, alias, mGroup.toArray(new String[mGroup.size()])) == null) {
+		    if (!roster.addContact(login, alias, mGroup.toArray(new String[mGroup.size()]))) {
 			Toast.makeText(AddContact.this, getString(R.string.AddCContactAddedError), Toast.LENGTH_SHORT)
 			    .show();
 			return;