beemservice - addcontact
authorVincent V.<marseille@beem-project.com>
Tue, 15 Nov 2011 02:29:48 +0100
changeset 920 68372d462902
parent 919 b1815efd3cf6
child 921 aa25c96c0ce7
beemservice - addcontact
AndroidManifest.xml
res/values/strings.xml
src/com/beem/project/beem/BeemIntent.java
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/service/XmppConnectionAdapter.java
src/com/beem/project/beem/ui/AddContact.java
src/com/beem/project/beem/ui/ContactList.java
--- a/AndroidManifest.xml	Mon Nov 14 00:47:02 2011 +0100
+++ b/AndroidManifest.xml	Tue Nov 15 02:29:48 2011 +0100
@@ -189,6 +189,7 @@
                 <action android:name="com.beem.project.beem.intent.action.CONNECT" />
                 <action android:name="com.beem.project.beem.intent.action.DISCONNECT" />
                 <action android:name="com.beem.project.beem.intent.action.SEND_MESSAGE" />
+                <action android:name="com.beem.project.beem.intent.action.ADD_CONTACT" />
             </intent-filter>
         </service>
     </application>
--- a/res/values/strings.xml	Mon Nov 14 00:47:02 2011 +0100
+++ b/res/values/strings.xml	Tue Nov 15 02:29:48 2011 +0100
@@ -24,6 +24,7 @@
 	<string name="BeemServiceDescription">Use Beem Service</string>
 	<string name="BeemServiceCreated">Beem Service Created</string>
 	<string name="BeemServiceDestroyed">Beem Service Destroyed</string>
+	<string name="BeemServiceNotConnected">Problem : Account %s not connected</string>
 
 	<!--  ContactDialog class -->
 	<string name="CDChat">Chat</string>
--- a/src/com/beem/project/beem/BeemIntent.java	Mon Nov 14 00:47:02 2011 +0100
+++ b/src/com/beem/project/beem/BeemIntent.java	Tue Nov 15 02:29:48 2011 +0100
@@ -53,6 +53,8 @@
     public static final String ACTION_SEND_MESSAGE = "com.beem.project.beem.intent.action.SEND_MESSAGE";
     
     public static final String ACTION_SYNC = "com.beem.project.beem.intent.action.SYNC";
+    
+    public static final String ACTION_ADD_CONTACT = "com.beem.project.beem.intent.action.ADD_CONTACT";
 
     /*Broadcast Receiver's action */
     public static final String ACTION_CONNECTED = "com.beem.project.beem.intent.action.CONNECTED";
--- a/src/com/beem/project/beem/BeemService.java	Mon Nov 14 00:47:02 2011 +0100
+++ b/src/com/beem/project/beem/BeemService.java	Tue Nov 15 02:29:48 2011 +0100
@@ -49,6 +49,7 @@
 import org.jivesoftware.smack.Roster;
 import org.jivesoftware.smack.Roster.SubscriptionMode;
 import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
 import org.jivesoftware.smack.provider.ProviderManager;
 import org.jivesoftware.smackx.packet.ChatStateExtension;
 import org.jivesoftware.smackx.provider.DelayInfoProvider;
@@ -62,14 +63,11 @@
 import android.app.Notification;
 import android.app.Service;
 import android.content.BroadcastReceiver;
-import android.content.ContentValues;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
-import android.database.ContentObserver;
-import android.database.Cursor;
 import android.net.ConnectivityManager;
 import android.os.Bundle;
 import android.os.Handler;
@@ -78,9 +76,8 @@
 import android.os.Looper;
 import android.os.Message;
 import android.os.RemoteException;
-import android.provider.ContactsContract;
-import android.provider.ContactsContract.RawContacts;
 import android.util.Log;
+import android.widget.Toast;
 
 import com.beem.project.beem.service.XmppConnectionAdapter;
 import com.beem.project.beem.smack.avatar.AvatarMetadataProvider;
@@ -106,6 +103,7 @@
     private static final int MESSAGE_DISCONNECT = 0x2;
     private static final int MESSAGE_SEND_MSG = 0x3;
     private static final int MESSAGE_SYNC = 0x4;
+    private static final int MESSAGE_ADD_CONTACT = 0x5;
 
     private Map<String, XmppConnectionAdapter> mConnection = new HashMap<String, XmppConnectionAdapter>();
     private Map<String, BeemConnection> mBeemConnection = new HashMap<String, BeemConnection>();
@@ -397,6 +395,8 @@
 	    msg = mHandler.obtainMessage(MESSAGE_SEND_MSG, intent.getExtras());
 	} else if (BeemIntent.ACTION_SYNC.equals(action)) {
 	    msg = mHandler.obtainMessage(MESSAGE_SYNC, intent.getExtras());
+	} else if (BeemIntent.ACTION_ADD_CONTACT.equals(action)) {
+	    msg = mHandler.obtainMessage(MESSAGE_ADD_CONTACT, intent.getExtras());
 	} else {
 	    Log.w(TAG, "Unknown intent " + intent);
 	}
@@ -412,34 +412,58 @@
 
 	@Override
 	public void handleMessage(Message msg) {
+	    String accountName = null;
+	    XmppConnectionAdapter connection = null;
 	    Bundle b = (Bundle) msg.obj;
+	    if (b.containsKey(BeemIntent.EXTRA_ACCOUNT)) {
+		accountName = b.getString(BeemIntent.EXTRA_ACCOUNT);
+		connection = mConnection.get(accountName);
+	    }
+	    if (connection == null && msg.what != MESSAGE_CONNECT && msg.what != MESSAGE_SYNC) {
+		Toast.makeText(BeemService.this, getString(R.string.BeemServiceNotConnected, accountName), Toast.LENGTH_LONG).show();
+		return;
+	    }
 	    switch (msg.what) {
 		case MESSAGE_CONNECT:
-		    handleConnect(b.getString(BeemIntent.EXTRA_ACCOUNT));
+		    handleConnect(accountName);
 		    break;
 		case MESSAGE_DISCONNECT:
-		    handleDisconnect(b);
+		    handleDisconnect(accountName);
 		    break;
 		case MESSAGE_SEND_MSG:
-		    String account = b.getString(BeemIntent.EXTRA_ACCOUNT);
-		    XmppConnectionAdapter con = mConnection.get(account);
-		    if (con != null) {
-			con.handleMessage(msg);
-		    }
+		    connection.handleMessage(accountName, msg);
 		    break;
 		case MESSAGE_SYNC:
 		    BeemNotification.StartSyncNotification(getBaseContext());
-		    String accountName = b.getString(BeemIntent.EXTRA_ACCOUNT);
 		    //TODO: Connect with option to not show status
 		    handleConnect(accountName);
-		    XmppConnectionAdapter co = mConnection.get(accountName);
-		    if (co != null) {
+		    if (!mConnection.containsKey(accountName))
+			connection = mConnection.get(accountName);
+		    if (connection != null) {
 			BeemSync sync = new BeemSync(getBaseContext());
-			if (co.getAdaptee() != null)
-			    sync.manageRoster(co.getAdaptee().getRoster(), accountName);
+			XMPPConnection xmppCo = connection.getAdaptee();
+			if (xmppCo != null)
+			    sync.manageRoster(xmppCo.getRoster(), accountName);
 		    }
 		    BeemNotification.StopSyncNotification(getBaseContext());
 		    break;
+		case MESSAGE_ADD_CONTACT:
+		    String jid = b.getString(BeemIntent.EXTRA_JID);
+
+		    XMPPConnection xmppCo = connection.getAdaptee();
+		    if (xmppCo != null) {
+			Roster r = xmppCo.getRoster();
+			if (r == null) {
+			    Log.e(TAG, "Has not logged in yet : " + accountName);
+			    return;
+			}
+			try {
+			    r.createEntry(jid, jid, null);
+			} catch (XMPPException e) {
+			    Log.e(TAG, "AddContact", e);
+			}
+		    }
+		    break;
 		default:
 		    Log.w(TAG, "Unknown message " + msg);
 	    }
@@ -488,10 +512,9 @@
 	sendBroadcast(res);
     }
 
-    private void handleDisconnect(Bundle b) {
+    private void handleDisconnect(String accountName) {
 	Intent res = new Intent(BeemIntent.ACTION_DISCONNECTED);
-	String account = b.getString(BeemIntent.EXTRA_ACCOUNT);
-	mConnection.remove(account);
+	mConnection.remove(accountName);
 	sendBroadcast(res);
     }
 }
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Mon Nov 14 00:47:02 2011 +0100
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Tue Nov 15 02:29:48 2011 +0100
@@ -44,6 +44,7 @@
 package com.beem.project.beem.service;
 
 import java.text.SimpleDateFormat;
+import java.util.Collection;
 import java.util.Date;
 import java.util.Iterator;
 
@@ -51,6 +52,7 @@
 import org.jivesoftware.smack.ConnectionListener;
 import org.jivesoftware.smack.PacketListener;
 import org.jivesoftware.smack.PrivacyListManager;
+import org.jivesoftware.smack.RosterListener;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
 import org.jivesoftware.smack.filter.PacketFilter;
@@ -245,6 +247,8 @@
 	    mService.initJingle(mAdaptee);
 	    discoverServerFeatures();
 
+	    mAdaptee.getRoster().addRosterListener(new SubscribeRosterListener());
+
 	    mApplication.setConnected(true);
 	    int mode = mPref.getInt(BeemApplication.STATUS_KEY, 0);
 	    String status = mPref.getString(BeemApplication.STATUS_TEXT_KEY, "");
@@ -377,7 +381,6 @@
 	return mAdaptee;
     }
 
-
     /**
      * Returns true if currently authenticated by successfully calling the login method.
      * @return true when successfully authenticated
@@ -403,16 +406,15 @@
 	return mErrorMsg;
     }
 
-    public void handleMessage(android.os.Message msg) {
+    public void handleMessage(String accountName, android.os.Message msg) {
 	Log.e(TAG, "HANDLEMESSAGE");
 
 	Bundle b = (Bundle) msg.obj;
-	String from = b.getString(BeemIntent.EXTRA_ACCOUNT);
 	String to = b.getString(BeemIntent.EXTRA_JID);
 	String body = b.getString(BeemIntent.EXTRA_MESSAGE);
 
 	Message send = new Message();
-	send.setFrom(from);
+	send.setFrom(accountName);
 	send.setThread(""); //TODO: set ThreadID
 	send.setTo(to);
 	send.setBody(body);
@@ -750,6 +752,30 @@
 	}
     }
 
+    private class SubscribeRosterListener implements RosterListener {
+
+	@Override
+	public void entriesAdded(Collection<String> addresses) {
+	    Log.e(TAG, "entriesAdded");
+	}
+
+	@Override
+	public void entriesUpdated(Collection<String> addresses) {
+	    Log.e(TAG, "entriesUpdated");
+	}
+
+	@Override
+	public void entriesDeleted(Collection<String> addresses) {
+	    Log.e(TAG, "entriesDeleted");
+	}
+
+	@Override
+	public void presenceChanged(Presence presence) {
+	    Log.e(TAG, "presenceChanged");
+	}
+
+    }
+
     /**
      * Make an xmpp uri for a spcific jid.
      * @param jid the jid to represent as an uri
--- a/src/com/beem/project/beem/ui/AddContact.java	Mon Nov 14 00:47:02 2011 +0100
+++ b/src/com/beem/project/beem/ui/AddContact.java	Tue Nov 15 02:29:48 2011 +0100
@@ -43,8 +43,6 @@
  */
 package com.beem.project.beem.ui;
 
-import java.util.ArrayList;
-import java.util.List;
 import java.util.regex.Pattern;
 
 import android.accounts.Account;
@@ -61,6 +59,7 @@
 
 import com.beem.project.beem.BeemApplication;
 import com.beem.project.beem.BeemIntent;
+import com.beem.project.beem.BeemService;
 import com.beem.project.beem.R;
 import com.beem.project.beem.ui.wizard.AccountConfigure;
 
@@ -97,6 +96,7 @@
     @Override
     protected void onResume() {
 	super.onResume();
+
 	Bundle b = getIntent().getExtras();
 	if (b != null && b.containsKey(BeemIntent.EXTRA_ACCOUNT))
 	    mAccount = b.getString(BeemIntent.EXTRA_ACCOUNT);
@@ -139,14 +139,18 @@
 	@Override
 	public void onClick(View v) {
 	    Log.e(TAG, "Account : " + mAccount);
-	    String login = getWidgetText(R.id.addc_login);
-	    boolean isEmail = Pattern.matches("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+[a-zA-Z]{2,4}", login);
+	    String jid = getWidgetText(R.id.addc_login);
+	    boolean isEmail = Pattern.matches("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+[a-zA-Z]{2,4}", jid);
 	    if (!isEmail) {
 		Toast.makeText(AddContact.this, getString(R.string.AddCContactAddedLoginError), Toast.LENGTH_SHORT)
 		    .show();
 		return;
 	    }
-	    //TODO: Intent -> add contact
+
+	    Intent intent = new Intent(BeemIntent.ACTION_ADD_CONTACT);
+	    intent.putExtra(BeemIntent.EXTRA_ACCOUNT, mAccount);
+	    intent.putExtra(BeemIntent.EXTRA_JID, jid);
+	    startService(intent);
 
 	}
     };
--- a/src/com/beem/project/beem/ui/ContactList.java	Mon Nov 14 00:47:02 2011 +0100
+++ b/src/com/beem/project/beem/ui/ContactList.java	Tue Nov 15 02:29:48 2011 +0100
@@ -328,7 +328,6 @@
 		    if (!cursorContact.isNull(1)) {
 			String mimeType = cursorContact.getString(2);
 			String data = cursorContact.getString(3);
-			Log.e(TAG, "SOURCE ID :" + data + " - " + mimeType);
 			if (mimeType.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
 			    TextView nameText = (TextView) view.findViewById(R.id.contactlistpseudo);
 			    nameText.setText(data);