# HG changeset patch # User Da Risk # Date 1237494680 -3600 # Node ID a49d1556772ca102805a8ae969f55bcf0efb36a8 # Parent 213b84d2e74315e02e15017d40c8c4c302046c7f Starting work on the Service interface. Connect to the server asynchrously diff -r 213b84d2e743 -r a49d1556772c src/com/beem/project/beem/BeemException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/BeemException.java Thu Mar 19 21:31:20 2009 +0100 @@ -0,0 +1,67 @@ +/** + * + */ +package com.beem.project.beem; + +import android.os.Parcel; +import android.os.Parcelable; + +/** + * @author darisk + * + */ +public class BeemException extends Exception implements Parcelable { + + public static final Parcelable.Creator CREATOR = new Creator() { + + @Override + public BeemException[] newArray(int size) { + // TODO Auto-generated method stub + return new BeemException[size]; + } + + @Override + public BeemException createFromParcel(Parcel source) { + // TODO Auto-generated method stub + return new BeemException(source); + } + }; + + public BeemException() { + super(); + // TODO Auto-generated constructor stub + } + + public BeemException(String detailMessage, Throwable throwable) { + super(detailMessage, throwable); + // TODO Auto-generated constructor stub + } + + public BeemException(String detailMessage) { + super(detailMessage); + // TODO Auto-generated constructor stub + } + + public BeemException(Throwable throwable) { + super(throwable); + // TODO Auto-generated constructor stub + } + + private BeemException(Parcel parcel){ + this(parcel.readString()); + } + + @Override + public int describeContents() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + // TODO Auto-generated method stub + dest.writeString(getLocalizedMessage()); + } + + +} diff -r 213b84d2e743 -r a49d1556772c src/com/beem/project/beem/BeemService.java --- a/src/com/beem/project/beem/BeemService.java Thu Mar 19 12:40:29 2009 +0100 +++ b/src/com/beem/project/beem/BeemService.java Thu Mar 19 21:31:20 2009 +0100 @@ -3,12 +3,13 @@ */ package com.beem.project.beem; -import java.util.List; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; -import org.jivesoftware.smack.XMPPConnection; -import org.jivesoftware.smack.XMPPException; - -import com.beem.project.beem.service.IXMPPFacade; +import org.jivesoftware.smack.Roster; +import org.jivesoftware.smack.RosterListener; +import org.jivesoftware.smack.packet.Presence; import android.app.Notification; import android.app.NotificationManager; @@ -16,88 +17,152 @@ import android.app.Service; import android.content.Intent; import android.content.SharedPreferences; -import android.content.res.Resources; import android.os.IBinder; +import android.os.Looper; import android.os.RemoteException; -import android.widget.TextView; import android.widget.Toast; +import com.beem.project.beem.service.XMPPConnectionAdapter; +import com.beem.project.beem.service.aidl.IXMPPConnection; +import com.beem.project.beem.service.aidl.IXMPPFacade; + /** * @author darisk - * + * */ public class BeemService extends Service { private NotificationManager notificationManager; - private XMPPConnection connection; + private IXMPPConnection connection; private SharedPreferences settings; private String login; private String password; private String host; - + private Set onlineContactList = new HashSet(); + private Roster roster; + private IXMPPFacade.Stub bind = new IXMPPFacade.Stub() { - - @Override - public List getContactList() throws RemoteException { - // TODO Auto-generated method stub - return null; - } + + @Override + public IXMPPConnection getXMPPConnection() throws RemoteException { + // TODO Auto-generated method stub + return connection; + } }; - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see android.app.Service#onBind(android.content.Intent) */ @Override - public IBinder onBind(Intent arg0) { + 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; } - - private void showBasicNotification(int stringResource) { - String text = (String) getText(stringResource); - Notification notif = new Notification(R.drawable.logo, text, System.currentTimeMillis()); - notif.defaults = Notification.DEFAULT_ALL; - notif.setLatestEventInfo(this, text, text, PendingIntent.getActivity(this, 0, new Intent(),0)); - notificationManager.notify(stringResource, notif); - Toast toast = Toast.makeText(this, R.string.BeemServiceCreated, Toast.LENGTH_LONG); - toast.show(); + + @Override + public void onCreate() { + super.onCreate(); + settings = getSharedPreferences(getString(R.string.PreferenceFileName), + MODE_PRIVATE); + login = settings.getString(getString(R.string.PreferenceLoginKey), ""); + password = settings.getString( + getString(R.string.PreferencePasswordKey), ""); + host = settings.getString(getString(R.string.PreferenceHostKey), ""); + notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } - - @Override - public void onCreate(){ - super.onCreate(); - settings = getSharedPreferences(getString(R.string.PreferenceFileName), MODE_PRIVATE); - login = settings.getString(getString(R.string.PreferenceLoginKey), ""); - password = settings.getString(getString(R.string.PreferencePasswordKey), ""); - host = settings.getString(getString(R.string.PreferenceHostKey), ""); - } - + @Override public void onStart(Intent intent, int startId) { - try { - connection = new XMPPConnection("10.0.2.2"); // address du pc host de l'emulateur - connection.connect(); - connection.login(login, password); - notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - showBasicNotification(R.string.BeemServiceCreated); - } catch (XMPPException e) { - // TODO Auto-generated catch block - Toast toast = Toast.makeText(this, "ERREUR " + e.getMessage(), Toast.LENGTH_LONG); - toast.show(); - e.printStackTrace(); - } + new Thread(connectingThread).start(); } - - private void closeConnection() { - if (connection != null) - connection.disconnect(); - } - + @Override public void onDestroy() { closeConnection(); - showBasicNotification(R.string.BeemServiceDestroyed); + showBasicNotification(R.string.BeemServiceDestroyed); + } + + private void showBasicNotification(int stringResource) { + String text = (String) getText(stringResource); + Notification notif = new Notification(R.drawable.logo, text, System + .currentTimeMillis()); + notif.defaults = Notification.DEFAULT_ALL; + notif.setLatestEventInfo(this, text, text, PendingIntent.getActivity( + this, 0, new Intent(), 0)); + notificationManager.notify(stringResource, notif); + } + + private void closeConnection() { + if (connection != null) + try { + connection.disconnect(); + } catch (RemoteException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } + 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 213b84d2e743 -r a49d1556772c src/com/beem/project/beem/service/IXMPPFacade.aidl --- a/src/com/beem/project/beem/service/IXMPPFacade.aidl Thu Mar 19 12:40:29 2009 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -package com.beem.project.beem.service; - -// import org.jivesoftware.smack.XMPPConnection; - -interface IXMPPFacade { - - // org.jivesoftware.smack.XMPPConnection getXMPPConnection(); - - List getContactList(); - -} diff -r 213b84d2e743 -r a49d1556772c src/com/beem/project/beem/service/XMPPConnectionAdapter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/service/XMPPConnectionAdapter.java Thu Mar 19 21:31:20 2009 +0100 @@ -0,0 +1,60 @@ +/** + * + */ +package com.beem.project.beem.service; + +import org.jivesoftware.smack.ConnectionConfiguration; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; + +import com.beem.project.beem.BeemException; +import com.beem.project.beem.service.aidl.IXMPPConnection; + +/** + * @author darisk + * + */ +public class XMPPConnectionAdapter extends IXMPPConnection.Stub { + private XMPPConnection adaptee; + private BeemException lastException; + + public XMPPConnectionAdapter(XMPPConnection con) { + adaptee = con; + } + + public XMPPConnectionAdapter(String serviceName) { + adaptee = new XMPPConnection(serviceName); + } + + public XMPPConnectionAdapter(ConnectionConfiguration config) { + adaptee = new XMPPConnection(config); + } + + public boolean connect() { + 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.login(username, password, resource); + lastException = null; + return true; + } catch (XMPPException e) { + lastException = new BeemException(e); + } + return false; + } +} diff -r 213b84d2e743 -r a49d1556772c src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl Thu Mar 19 21:31:20 2009 +0100 @@ -0,0 +1,15 @@ +package com.beem.project.beem.service.aidl; + +interface IBeemConnectionListener { + + void connectionClosed(); + + //void connectionClosedOnError(in Exception e); + void connectionClosedOnError(); + + void reconnectingIn(in int seconds); + + void reconnectionFailed(); + + void reconnectionSuccessful(); +} diff -r 213b84d2e743 -r a49d1556772c src/com/beem/project/beem/service/aidl/IXMPPConnection.aidl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/service/aidl/IXMPPConnection.aidl Thu Mar 19 21:31:20 2009 +0100 @@ -0,0 +1,11 @@ +package com.beem.project.beem.service.aidl; + +interface IXMPPConnection { + + boolean connect(); + + boolean disconnect(); + + boolean login(in String username, in String password, in String resource); + // void login(String username, String password, String resource); +} \ No newline at end of file diff -r 213b84d2e743 -r a49d1556772c src/com/beem/project/beem/service/aidl/IXMPPFacade.aidl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/beem/project/beem/service/aidl/IXMPPFacade.aidl Thu Mar 19 21:31:20 2009 +0100 @@ -0,0 +1,9 @@ +package com.beem.project.beem.service.aidl; + +import com.beem.project.beem.service.aidl.IXMPPConnection; + +interface IXMPPFacade { + + IXMPPConnection getXMPPConnection(); + +}