The service now send message by intents.
--- a/AndroidManifest.xml Tue May 17 00:53:28 2011 +0200
+++ b/AndroidManifest.xml Thu Jun 02 01:28:40 2011 +0200
@@ -49,6 +49,7 @@
<action android:name="com.beem.project.beem.BeemService"></action>
<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_MSG" />
</intent-filter>
</service>
</application>
--- a/src/com/beem/project/beem/BeemService.java Tue May 17 00:53:28 2011 +0200
+++ b/src/com/beem/project/beem/BeemService.java Thu Jun 02 01:28:40 2011 +0200
@@ -113,6 +113,7 @@
//private static final String COMMAND_NAMESPACE = "http://jabber.org/protocol/commands";
private static final int MESSAGE_CONNECT = 0x1;
+ private static final int MESSAGE_SEND_MSG = 0x2;
private NotificationManager mNotificationManager;
private XmppConnectionAdapter mConnection;
private SharedPreferences mSettings;
@@ -446,8 +447,11 @@
private void handleIntent(Intent intent) {
Message msg = null;
- if (com.beem.project.beem.Intent.ACTION_CONNECT.equals(intent.getAction())) {
+ String action = intent.getAction();
+ if (com.beem.project.beem.Intent.ACTION_CONNECT.equals(action)) {
msg = mHandler.obtainMessage(MESSAGE_CONNECT, intent.getExtras());
+ } else if (com.beem.project.beem.Intent.ACTION_SEND_MSG.equals(action)) {
+ msg = mHandler.obtainMessage(MESSAGE_SEND_MSG, intent.getExtras());
} else {
Log.w(TAG, "Unknown intent " + intent);
}
@@ -524,6 +528,13 @@
case MESSAGE_CONNECT:
handleConnect(b);
break;
+ case MESSAGE_SEND_MSG:
+ String account = b.getString(com.beem.project.beem.Intent.EXTRA_ACCOUNT);
+ XmppConnectionAdapter con = mConnections.get(account);
+ if (con != null) {
+ con.handleMessage(msg);
+ }
+ break;
default:
Log.w(TAG, "Unknown message " + msg);
}
--- a/src/com/beem/project/beem/Intent.java Tue May 17 00:53:28 2011 +0200
+++ b/src/com/beem/project/beem/Intent.java Thu Jun 02 01:28:40 2011 +0200
@@ -57,5 +57,9 @@
public static final String ACTION_DISCONNECTED = "com.beem.project.beem.intent.action.DISCONNECTED";
public static final String EXTRA_ACCOUNT = "com.beem.project.beem.intent.extra.ACCOUNT";
+
+ public static final String EXTRA_JID = "com.beem.project.beem.intent.extra.JID";
+
+
}
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java Tue May 17 00:53:28 2011 +0200
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java Thu Jun 02 01:28:40 2011 +0200
@@ -43,8 +43,11 @@
*/
package com.beem.project.beem.service;
+import org.jivesoftware.smack.Chat;
+import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
+import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.PrivacyListManager;
import org.jivesoftware.smack.Roster;
@@ -63,6 +66,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.os.Bundle;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.util.Log;
@@ -96,13 +100,17 @@
* Beem connection closed Intent name.
*/
+ private static final int MESSAGE_CONNECT = 0x1;
+ private static final int MESSAGE_SEND_MSG = 0x2;
+
private static final int SMACK_PRIORITY_MIN = -128;
private static final int SMACK_PRIORITY_MAX = 128;
private static final String TAG = "XMPPConnectionAdapter";
private final XMPPConnection mAdaptee;
- private IChatManager mChatManager;
+ private ChatManager mChatManager;
private final String mLogin;
private final String mPassword;
+ private final OnMessageListener mMsgListener = new OnMessageListener();
private String mResource;
private String mErrorMsg;
private RosterAdapter mRoster;
@@ -242,7 +250,8 @@
mAdaptee.login(mLogin, mPassword, mResource);
mUserInfo = new UserInfo(mAdaptee.getUser());
- mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService);
+// mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService);
+ mChatManager = mAdaptee.getChatManager();
//nikita: I commented this line because of the logs provided in http://www.beem-project.com/issues/321
//Also, since the privacylistmanager isn't finished and used, it will be safer to not initialize it
//mPrivacyListManager = new PrivacyListManagerAdapter(PrivacyListManager.getInstanceFor(mAdaptee));
@@ -397,7 +406,8 @@
*/
@Override
public IChatManager getChatManager() throws RemoteException {
- return mChatManager;
+ return null;
+// return mChatManager;
}
/**
@@ -464,6 +474,30 @@
return mErrorMsg;
}
+ public void handleMessage(android.os.Message msg) {
+ Bundle b = (Bundle) msg.obj;
+ switch (msg.what) {
+ case MESSAGE_SEND_MSG:
+ sendMessage(b);
+ break;
+ default:
+ Log.w(TAG, "Unknown message " + msg);
+ }
+ }
+
+ private void sendMessage(Bundle b) {
+ String jid = b.getString(com.beem.project.beem.Intent.EXTRA_JID);
+ CharSequence msg = b.getCharSequence(Intent.EXTRA_TEXT);
+ if (jid == null || msg == null)
+ return;
+ Chat chat = mChatManager.createChat(jid, mMsgListener);
+ try {
+ chat.sendMessage(msg.toString());
+ } catch (XMPPException e) {
+ Log.e(TAG, "Error while sending message", e);
+ }
+ }
+
/**
* Initialize the features provided by beem.
*/
@@ -751,4 +785,16 @@
}
}
+ private class OnMessageListener implements MessageListener {
+
+ @Override
+ public void processMessage(Chat chat, org.jivesoftware.smack.packet.Message message) {
+ // TODO
+// Intent intent = new Intent(ACTION_MSG_RECEIVED);
+// intent.putExtra(EXTRA_MSG, message);
+// Log.d(TAG, "Notify " + intent.toString());
+// mService.sendOrderedBroadcast(intent, null);
+ }
+ }
+
}
--- a/src/com/beem/project/beem/ui/Chat.java Tue May 17 00:53:28 2011 +0200
+++ b/src/com/beem/project/beem/ui/Chat.java Thu Jun 02 01:28:40 2011 +0200
@@ -883,19 +883,24 @@
final String inputContent = mInputField.getText().toString();
if (!"".equals(inputContent)) {
- Message msgToSend = new Message(mContact.getJIDWithRes(), Message.MSG_TYPE_CHAT);
- msgToSend.setBody(inputContent);
+// Message msgToSend = new Message(mContact.getJIDWithRes(), Message.MSG_TYPE_CHAT);
+// msgToSend.setBody(inputContent);
- try {
- if (mChat == null) {
- mChat = mChatManager.createChat(mContact, mMessageListener);
- mChat.setOpen(true);
- }
- mChat.sendMessage(msgToSend);
- } catch (RemoteException e) {
- Log.e(TAG, e.getMessage());
- }
+// try {
+// if (mChat == null) {
+// mChat = mChatManager.createChat(mContact, mMessageListener);
+// mChat.setOpen(true);
+// }
+// mChat.sendMessage(msgToSend);
+// } catch (RemoteException e) {
+// Log.e(TAG, e.getMessage());
+// }
+ Intent i = new Intent(com.beem.project.beem.Intent.ACTION_SEND_MSG);
+ i.putExtra(com.beem.project.beem.Intent.EXTRA_JID, "");
+ i.putExtra(com.beem.project.beem.Intent.EXTRA_ACCOUNT, "");
+ i.putExtra(Intent.EXTRA_TEXT, inputContent);
+ startService(i);
final String self = getString(R.string.chat_self);
MessageText lastMessage = null;
if (mListMessages.size() != 0)
@@ -945,4 +950,6 @@
}
}
}
+
}
+