# HG changeset patch # User Da Risk # Date 1237586054 -3600 # Node ID 85faac00d92c2b73bc53e1e7954d1cceafc31a8e # Parent 7057a9d3657adfc6d735b1bb86e67fa7ca3e31d2 Asynchronous service almost perfect. Still need some improvments diff -r 7057a9d3657a -r 85faac00d92c AndroidManifest.xml --- a/AndroidManifest.xml Thu Mar 19 22:23:18 2009 +0100 +++ b/AndroidManifest.xml Fri Mar 20 22:54:14 2009 +0100 @@ -1,22 +1,25 @@ - - - - - - - - - - - - - - - + package="com.beem.project.beem" android:versionCode="1" + android:versionName="1.0"> + + + + + + + + + + + + + + + + + diff -r 7057a9d3657a -r 85faac00d92c src/com/beem/project/beem/Beem.java --- a/src/com/beem/project/beem/Beem.java Thu Mar 19 22:23:18 2009 +0100 +++ b/src/com/beem/project/beem/Beem.java Fri Mar 20 22:54:14 2009 +0100 @@ -1,9 +1,20 @@ package com.beem.project.beem; +import com.beem.project.beem.service.aidl.IBeemConnectionListener; +import com.beem.project.beem.service.aidl.IXMPPConnection; +import com.beem.project.beem.service.aidl.IXMPPFacade; + import android.app.Activity; +import android.content.ComponentName; import android.content.Intent; +import android.content.ServiceConnection; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.os.RemoteException; +import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -11,27 +22,104 @@ import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; +import android.widget.Toast; public class Beem extends Activity { private SharedPreferences mSettings; + private IXMPPFacade facade; + + private Handler mHandler; + /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + mHandler = new Handler(); setContentView(R.layout.main); mSettings = getSharedPreferences(getString(R.string.PreferenceFileName), MODE_PRIVATE); showJID(); Button btConnection = (Button) findViewById(R.id.connection); btConnection.setOnClickListener(new OnClickListener() { - + @Override public void onClick(View v) { // TODO Auto-generated method stub - startService(new Intent(Beem.this,BeemService.class)); + // startService(new Intent(Beem.this,BeemService.class)); + boolean ok = bindService(new Intent(Beem.this,BeemService.class), new ServiceConnection() { + + @Override + public void onServiceDisconnected(ComponentName name) { + // TODO Auto-generated method stub + + } + + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + // TODO Auto-generated method stub + try { + facade = (IXMPPFacade) service; + IXMPPConnection con = facade.getXMPPConnection(); + con.addConnectionListener(new IBeemConnectionListener.Stub() { + + @Override + public void reconnectionSuccessful() throws RemoteException { + // TODO Auto-generated method stub + + } + + @Override + public void reconnectionFailed() throws RemoteException { + // TODO Auto-generated method stub + + } + + @Override + public void reconnectingIn(int seconds) throws RemoteException { + // TODO Auto-generated method stub + + } + + @Override + public void onConnect() throws RemoteException { + // TODO Auto-generated method stub + mHandler.post(new Runnable() { + + @Override + public void run() { + // TODO Auto-generated method stub + Log.w("BEEM", "ON s'est COnnecter !!!"); + Toast toast = Toast.makeText(Beem.this, "Connection estabished", Toast.LENGTH_LONG); + toast.show(); + } + }); + + } + + @Override + public void connectionClosedOnError() throws RemoteException { + // TODO Auto-generated method stub + + } + + @Override + public void connectionClosed() throws RemoteException { + // TODO Auto-generated method stub + + } + }); + facade.connectAsync(); + } catch (RemoteException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }, BIND_AUTO_CREATE); + if (! ok) + Log.w("BEEM", "Connection au service echoue"); } }); } @@ -67,4 +155,13 @@ } + private class TestHandler extends Handler{ + + @Override + public void handleMessage(Message msg) { + // TODO Auto-generated method stub + super.handleMessage(msg); + } + + } } diff -r 7057a9d3657a -r 85faac00d92c src/com/beem/project/beem/BeemService.java --- a/src/com/beem/project/beem/BeemService.java Thu Mar 19 22:23:18 2009 +0100 +++ b/src/com/beem/project/beem/BeemService.java Fri Mar 20 22:54:14 2009 +0100 @@ -36,19 +36,33 @@ private IXMPPConnection connection; private SharedPreferences settings; - private String login; - private String password; - private String host; - private Set onlineContactList = new HashSet(); - private Roster roster; - + private String mLogin; + private String mPassword; + private String mHost; private IXMPPFacade.Stub bind = new IXMPPFacade.Stub() { @Override public IXMPPConnection getXMPPConnection() throws RemoteException { - // TODO Auto-generated method stub return connection; } + + + @Override + public void disconnect() throws RemoteException { + connection.disconnect(); + } + + + @Override + public void connectAsync() throws RemoteException { + connection.connectAsync(mLogin, mPassword, "BEEM"); + } + + + @Override + public void connectSync() throws RemoteException { + connection.connectSync(mLogin, mPassword, "BEEM"); + } }; /* @@ -58,11 +72,10 @@ */ @Override public IBinder onBind(Intent intent) { - // TODO Auto-generated method stub - new Thread(connectingThread).start(); - if (IXMPPConnection.class.getName().equals(intent.getAction())) - return bind; - return null; + showBasicNotification(R.string.BeemServiceCreated); + return bind; + // to forbid a client to bind + // return null; } @Override @@ -70,16 +83,22 @@ super.onCreate(); settings = getSharedPreferences(getString(R.string.PreferenceFileName), MODE_PRIVATE); - login = settings.getString(getString(R.string.PreferenceLoginKey), ""); - password = settings.getString( + mLogin = settings.getString(getString(R.string.PreferenceLoginKey), ""); + mPassword = settings.getString( getString(R.string.PreferencePasswordKey), ""); - host = settings.getString(getString(R.string.PreferenceHostKey), ""); + mHost = settings.getString(getString(R.string.PreferenceHostKey), ""); notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + connection = new XMPPConnectionAdapter("10.0.2.2"); // address } @Override public void onStart(Intent intent, int startId) { - new Thread(connectingThread).start(); + try { + connection.connectSync(mLogin, mPassword, "BEEM"); + } catch (RemoteException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } @Override @@ -108,61 +127,4 @@ } } - private RosterListener rosterListener = new RosterListener() { - - @Override - public void presenceChanged(Presence presence) { - // TODO Auto-generated method stub - String user = presence.getFrom(); - Presence bestPresence = roster.getPresence(user); - if (bestPresence.getType().equals(Presence.Type.available)) - onlineContactList.add(user); - } - - @Override - public void entriesUpdated(Collection arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void entriesDeleted(Collection arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void entriesAdded(Collection arg0) { - // TODO Auto-generated method stub - - } - }; - - private Runnable connectingThread = new Runnable() { - - @Override - public void run() { - // TODO Auto-generated method stub - try { - // TODO effectuer la connexion dans un thread - connection = new XMPPConnectionAdapter("10.0.2.2"); // address - // du pc - // host de - // l'emulateur - connection.connect(); - connection.login(login, password, "BEEM"); - /* - * roster = connection.getRoster(); - * roster.addRosterListener(rosterListener); - */ - showBasicNotification(R.string.BeemServiceCreated); - } catch (RemoteException e) { - // TODO Auto-generated catch block - Toast toast = Toast.makeText(BeemService.this, "ERREUR " + e.getMessage(), - Toast.LENGTH_LONG); - toast.show(); - e.printStackTrace(); - } - } - }; } diff -r 7057a9d3657a -r 85faac00d92c src/com/beem/project/beem/service/XMPPConnectionAdapter.java --- a/src/com/beem/project/beem/service/XMPPConnectionAdapter.java Thu Mar 19 22:23:18 2009 +0100 +++ b/src/com/beem/project/beem/service/XMPPConnectionAdapter.java Fri Mar 20 22:54:14 2009 +0100 @@ -4,57 +4,51 @@ package com.beem.project.beem.service; import org.jivesoftware.smack.ConnectionConfiguration; +import org.jivesoftware.smack.ConnectionListener; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPException; +import android.os.RemoteCallbackList; import android.os.RemoteException; import com.beem.project.beem.BeemException; +import com.beem.project.beem.service.aidl.IBeemConnectionListener; import com.beem.project.beem.service.aidl.IRoster; import com.beem.project.beem.service.aidl.IXMPPConnection; /** * @author darisk - * + * */ public class XMPPConnectionAdapter extends IXMPPConnection.Stub { - + private XMPPConnection adaptee; private BeemException lastException; + + private RemoteCallbackList mRemoteConnListeners = new RemoteCallbackList(); + private ConnexionListenerAdapter mConListener = new ConnexionListenerAdapter(); public XMPPConnectionAdapter(XMPPConnection con) { adaptee = con; } public XMPPConnectionAdapter(String serviceName) { - adaptee = new XMPPConnection(serviceName); + this(new XMPPConnection(serviceName)); } - - public XMPPConnectionAdapter(ConnectionConfiguration config) { - adaptee = new XMPPConnection(config); + + public XMPPConnectionAdapter(ConnectionConfiguration config) { + this(new XMPPConnection(config)); } - public boolean connect() { - try { + @Override + public boolean connectSync(String username, String password, String resource) + throws RemoteException { + try { adaptee.connect(); - lastException = null; - return true; - } catch (XMPPException e) { - lastException = new BeemException(e); - } - return false; - } - - public boolean disconnect() { - adaptee.disconnect(); - lastException = null; - return true; - } - - public boolean login(String username, String password, String resource) { - try { + adaptee.addConnectionListener(mConListener); adaptee.login(username, password, resource); lastException = null; + triggerAsynchronousConnectEvent(); return true; } catch (XMPPException e) { lastException = new BeemException(e); @@ -62,8 +56,149 @@ return false; } + public boolean disconnect() { + adaptee.disconnect(); + lastException = null; + return true; + } + @Override public IRoster getRoster() throws RemoteException { return new RosterAdapter(adaptee.getRoster()); } + + @Override + public void connectAsync(final String username, final String password, final String resource) + throws RemoteException { + Thread t = new Thread(new Runnable() { + + @Override + public void run() { + try { + connectSync(username,password,resource); + } catch (RemoteException e) { + + } + } + }); + t.start(); + } + + @Override + public void addConnectionListener(IBeemConnectionListener listen) + throws RemoteException { + if (listen != null) + mRemoteConnListeners.register(listen); + } + + @Override + public void removeConnectionListener(IBeemConnectionListener listen) + throws RemoteException { + if (listen != null) + mRemoteConnListeners.unregister(listen); + } + + private void triggerAsynchronousConnectEvent() { + mConListener.onConnect(); + } + + private class ConnexionListenerAdapter implements ConnectionListener { + + public void onConnect() { + final int N = mRemoteConnListeners.beginBroadcast(); + + for (int i = 0; i < N; i++) { + IBeemConnectionListener listener = mRemoteConnListeners.getBroadcastItem(i); + try { + listener.onConnect(); + } catch (RemoteException e) { + // The RemoteCallbackList will take care of removing the + // dead listeners. + } + } + mRemoteConnListeners.finishBroadcast(); + } + + @Override + public void connectionClosed() { + final int N = mRemoteConnListeners.beginBroadcast(); + + for (int i = 0; i < N; i++) { + IBeemConnectionListener listener = mRemoteConnListeners.getBroadcastItem(i); + try { + listener.connectionClosed(); + } catch (RemoteException e) { + // The RemoteCallbackList will take care of removing the + // dead listeners. + } + } + mRemoteConnListeners.finishBroadcast(); + } + + @Override + public void connectionClosedOnError(Exception arg0) { + final int N = mRemoteConnListeners.beginBroadcast(); + + for (int i = 0; i < N; i++) { + IBeemConnectionListener listener = mRemoteConnListeners.getBroadcastItem(i); + try { + listener.connectionClosedOnError(); + } catch (RemoteException e) { + // The RemoteCallbackList will take care of removing the + // dead listeners. + } + } + mRemoteConnListeners.finishBroadcast(); + } + + @Override + public void reconnectingIn(int arg0) { + final int N = mRemoteConnListeners.beginBroadcast(); + + for (int i = 0; i < N; i++) { + IBeemConnectionListener listener = mRemoteConnListeners.getBroadcastItem(i); + try { + listener.reconnectingIn(arg0); + } catch (RemoteException e) { + // The RemoteCallbackList will take care of removing the + // dead listeners. + } + } + mRemoteConnListeners.finishBroadcast(); + } + + @Override + public void reconnectionFailed(Exception arg0) { + final int N = mRemoteConnListeners.beginBroadcast(); + + for (int i = 0; i < N; i++) { + IBeemConnectionListener listener = mRemoteConnListeners.getBroadcastItem(i); + try { + listener.reconnectionFailed(); + } catch (RemoteException e) { + // The RemoteCallbackList will take care of removing the + // dead listeners. + } + } + mRemoteConnListeners.finishBroadcast(); + } + + @Override + public void reconnectionSuccessful() { + final int N = mRemoteConnListeners.beginBroadcast(); + + for (int i = 0; i < N; i++) { + IBeemConnectionListener listener = mRemoteConnListeners.getBroadcastItem(i); + try { + listener.reconnectionSuccessful(); + } catch (RemoteException e) { + // The RemoteCallbackList will take care of removing the + // dead listeners. + } + } + mRemoteConnListeners.finishBroadcast(); + } + } + + } diff -r 7057a9d3657a -r 85faac00d92c src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl --- a/src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl Thu Mar 19 22:23:18 2009 +0100 +++ b/src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl Fri Mar 20 22:54:14 2009 +0100 @@ -4,6 +4,8 @@ void connectionClosed(); + void onConnect(); + //void connectionClosedOnError(in Exception e); void connectionClosedOnError(); diff -r 7057a9d3657a -r 85faac00d92c src/com/beem/project/beem/service/aidl/IXMPPConnection.aidl --- a/src/com/beem/project/beem/service/aidl/IXMPPConnection.aidl Thu Mar 19 22:23:18 2009 +0100 +++ b/src/com/beem/project/beem/service/aidl/IXMPPConnection.aidl Fri Mar 20 22:54:14 2009 +0100 @@ -1,16 +1,20 @@ package com.beem.project.beem.service.aidl; import com.beem.project.beem.service.aidl.IRoster; +import com.beem.project.beem.service.aidl.IBeemConnectionListener; interface IXMPPConnection { - boolean connect(); + boolean connectSync(in String username, in String password, in String resource); + + void connectAsync(in String username, in String password, in String resource); boolean disconnect(); - boolean login(in String username, in String password, in String resource); // void login(String username, String password, String resource); IRoster getRoster(); + void addConnectionListener(in IBeemConnectionListener listen); + void removeConnectionListener(in IBeemConnectionListener listen); } \ No newline at end of file diff -r 7057a9d3657a -r 85faac00d92c src/com/beem/project/beem/service/aidl/IXMPPFacade.aidl --- a/src/com/beem/project/beem/service/aidl/IXMPPFacade.aidl Thu Mar 19 22:23:18 2009 +0100 +++ b/src/com/beem/project/beem/service/aidl/IXMPPFacade.aidl Fri Mar 20 22:54:14 2009 +0100 @@ -5,5 +5,11 @@ interface IXMPPFacade { IXMPPConnection getXMPPConnection(); + + void connectSync(); + + void connectAsync(); + + void disconnect(); }