--- a/src/com/beem/project/beem/ui/SendIM.java Thu Jun 18 20:51:12 2009 +0200
+++ b/src/com/beem/project/beem/ui/SendIM.java Thu Jun 18 21:14:43 2009 +0200
@@ -3,11 +3,14 @@
import java.util.List;
import android.app.Activity;
+import android.content.ComponentName;
import android.content.Intent;
+import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
+import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;
@@ -22,7 +25,7 @@
import android.widget.ScrollView;
import android.widget.TextView;
-import com.beem.project.beem.BeemApplication;
+import com.beem.project.beem.BeemService;
import com.beem.project.beem.R;
import com.beem.project.beem.service.Contact;
import com.beem.project.beem.service.Message;
@@ -34,30 +37,28 @@
/**
* This activity class provides the view for instant messaging after selecting a correspondant.
- *
* @author barbu
*/
public class SendIM extends Activity implements OnClickListener, OnKeyListener {
- private static final String TAG = "SEND_IM";
- private EditText mToSend;
- private SendIMDialogSmiley mSmyDialog;
- private SharedPreferences mSet;
- private BeemApplication mBeemApplication;
- private Handler mHandler;
- private IXmppFacade mService;
- private Contact mContact;
- private IChatManager mChatManager;
+ private static final String TAG = "SEND_IM";
+ private EditText mToSend;
+ private SendIMDialogSmiley mSmyDialog;
+ private SharedPreferences mSet;
+ private Handler mHandler;
+ private Contact mContact;
+ private IChatManager mChatManager;
private IChatManagerListener mChatManagerListener;
- private IMessageListener mMessageListener;
- private IChat mChat;
- private TextView mText;
- private TextView mLogin;
+ private IMessageListener mMessageListener;
+ private IChat mChat;
+ private TextView mText;
+ private TextView mLogin;
+ private ScrollView mScrolling;
+ private char mSpeak;
- private ScrollView mScrolling;
-
- private char mSpeak;
+ private final ServiceConnection mServConn = new BeemServiceConnection();
+ private IXmppFacade xmppFacade;
/**
* Constructor.
@@ -83,7 +84,6 @@
mHandler = new Handler();
mChatManagerListener = new OnChatListener();
mMessageListener = new OnMessageListener();
- mBeemApplication = BeemApplication.getApplication(this);
setContentView(R.layout.sendim);
mToSend = (EditText) findViewById(R.id.userText);
mSet = getSharedPreferences("lol", MODE_PRIVATE);
@@ -91,7 +91,6 @@
mToSend.setOnClickListener(this);
mToSend.setOnKeyListener(this);
mLogin = (TextView) findViewById(R.id.sendimlogin);
- // mContact = getIntent().getParcelableExtra("contact");
mContact = new Contact(getIntent().getData());
setViewHeader();
mText = (TextView) findViewById(R.id.sendimlist);
@@ -133,12 +132,12 @@
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
- case KeyEvent.KEYCODE_DPAD_CENTER:
- case KeyEvent.KEYCODE_ENTER:
- sendText();
- return true;
- default:
- return false;
+ case KeyEvent.KEYCODE_DPAD_CENTER:
+ case KeyEvent.KEYCODE_ENTER:
+ sendText();
+ return true;
+ default:
+ return false;
}
}
return false;
@@ -160,11 +159,11 @@
@Override
public final boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
- case R.id.sendim_smiley:
- mSmyDialog.show();
- return true;
- default:
- return false;
+ case R.id.sendim_smiley:
+ mSmyDialog.show();
+ return true;
+ default:
+ return false;
}
}
@@ -179,6 +178,7 @@
} catch (RemoteException e) {
Log.d(TAG, "Error while closing chat", e);
}
+ unbindService(mServConn);
}
/**
@@ -187,26 +187,7 @@
@Override
protected void onResume() {
super.onResume();
- mBeemApplication = BeemApplication.getApplication(this);
- if (!mBeemApplication.isConnected())
- mBeemApplication.startBeemService();
- mBeemApplication.callWhenConnectedToServer(mHandler, new Runnable() {
-
- @Override
- public void run() {
- mService = mBeemApplication.getXmppFacade();
- try {
- if (mChatManager == null) {
- mChatManager = mService.getChatManager();
- mChatManager.addChatCreationListener(mChatManagerListener);
- }
- switchChat(mContact);
- } catch (RemoteException e) {
-
- Log.e(TAG, "Error during chat manager creation", e);
- }
- }
- });
+ bindService(new Intent(this, BeemService.class), mServConn, BIND_AUTO_CREATE);
mScrolling.fullScroll(ScrollView.FOCUS_DOWN);
}
@@ -219,28 +200,35 @@
// TODO cancel the notification if any
if (mContact == null)
mContact = getIntent().getParcelableExtra("contact");
- mService = mBeemApplication.getXmppFacade();
setViewHeader();
}
- /**
- * {@inheritDoc}
- */
- @Override
- protected void onStop() {
- super.onStop();
- try {
- mChat.setOpen(false);
- } catch (RemoteException e) {
- Log.d(TAG, "mchat open false", e);
+ private class BeemServiceConnection implements ServiceConnection {
+
+ @Override
+ public void onServiceConnected(ComponentName name, IBinder service) {
+ xmppFacade = IXmppFacade.Stub.asInterface(service);
+ try {
+ if (mChatManager == null) {
+ mChatManager = xmppFacade.getChatManager();
+ mChatManager.addChatCreationListener(mChatManagerListener);
+ }
+ switchChat(mContact);
+ } catch (RemoteException e) {
+ Log.e(TAG, "Error during chat manager creation", e);
+ }
}
- mBeemApplication.unbindBeemService();
+
+ @Override
+ public void onServiceDisconnected(ComponentName name) {
+ xmppFacade = null;
+ }
}
/**
- * Send a message to the contact over the XMPP connection. Also display it on activity view.
- * TODO : Gerer l'exception si la connexion se coupe pendant la conversation
+ * Send a message to the contact over the XMPP connection. Also display it on activity view. TODO : Gerer
+ * l'exception si la connexion se coupe pendant la conversation
*/
private void sendText() {
String text = mToSend.getText().toString();
@@ -248,7 +236,7 @@
Message msg = new Message(mContact.getJID(), Message.MSG_TYPE_CHAT);
msg.setBody(text);
try {
- //TODO: PAs connecter au serveur BING grosse Error ! INTERDICTION !
+ // TODO: PAs connecter au serveur BING grosse Error ! INTERDICTION !
mChat.sendMessage(msg);
if (mSpeak != 1)
mText.append(getString(R.string.SendIMYouSay) + text + '\n');
@@ -284,9 +272,7 @@
/**
* Show the message history.
- *
- * @param messages
- * list of message to display
+ * @param messages list of message to display
*/
private void showMessageList(List<Message> messages) {
mText.setText("");
@@ -311,11 +297,8 @@
/**
* Change the correspondant of the chat.
- *
- * @param newContact
- * New contact to chat with
- * @throws RemoteException
- * if an errors occurs in the connection with the service
+ * @param newContact New contact to chat with
+ * @throws RemoteException if an errors occurs in the connection with the service
*/
private void switchChat(Contact newContact) throws RemoteException {
if (mChat != null)
@@ -326,15 +309,12 @@
mContact = newContact;
mToSend.requestFocus();
}
-
/**
* Listener for chat creation. (maybe not necessary)
- *
* @author darisk
*/
private class OnChatListener extends IChatManagerListener.Stub {
-
/**
* {@inheritDoc}
*/
@@ -347,7 +327,6 @@
/**
* Listener for new chat messages.
- *
* @author darisk
*/
private class OnMessageListener extends IMessageListener.Stub {