# HG changeset patch # User Da Risk # Date 1271711246 -7200 # Node ID 28e72fa1b801e709375d680c27d51dcd7d9b253b # Parent 344a06703472764521347275ea52c6f746404fe8 Improve the ReconnectionManager. In fact, thanks to Nicholas Maniscalco , the ReconnectionManager is really used. diff -r 344a06703472 -r 28e72fa1b801 src/com/beem/project/beem/BeemService.java --- a/src/com/beem/project/beem/BeemService.java Sun Apr 11 19:34:31 2010 +0200 +++ b/src/com/beem/project/beem/BeemService.java Mon Apr 19 23:07:26 2010 +0200 @@ -43,6 +43,7 @@ */ package com.beem.project.beem; +import org.jivesoftware.smack.ReconnectionManager; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.Roster; import org.jivesoftware.smack.XMPPConnection; @@ -109,6 +110,24 @@ private boolean mOnOffReceiverIsRegistered; + static { + try { + // This is a hack. + // + // Force the ReconnectionManager class to be loaded, thereby forcing + // its static initialization block to execute. + // + // ReconnectionManager is responsible for reconnecting broken + // connections. It works by adding a ConnectionCreationListener in + // its static initialization block. By loading the class, we force + // its static initialization block to execute. Simply importing the + // class is not sufficient. + Class.forName("org.jivesoftware.smack.ReconnectionManager"); + } catch (ClassNotFoundException ex) { + Log.w("Can't load ReconnectionManager", ex); + } + } + /** * Constructor. */ @@ -162,15 +181,9 @@ @Override public boolean onUnbind(Intent intent) { Log.d(TAG, "ONUNBIND()"); - if (!mConnection.getAdaptee().isConnected()) { - Log.d(TAG, "DESTROYED"); - this.stopSelf(); - } return true; } - - /** * {@inheritDoc} */ diff -r 344a06703472 -r 28e72fa1b801 src/com/beem/project/beem/service/LoginAsyncTask.java --- a/src/com/beem/project/beem/service/LoginAsyncTask.java Sun Apr 11 19:34:31 2010 +0200 +++ b/src/com/beem/project/beem/service/LoginAsyncTask.java Mon Apr 19 23:07:26 2010 +0200 @@ -48,6 +48,7 @@ import android.util.Log; import com.beem.project.beem.service.aidl.IXmppConnection; import com.beem.project.beem.service.aidl.IXmppFacade; +import com.beem.project.beem.service.aidl.IBeemConnectionListener; /** * This is an asynchronous task that will launch a connection to the XMPP server. @@ -85,30 +86,39 @@ } @Override - protected Boolean doInBackground(IXmppFacade... params) { - boolean result = true; - IXmppFacade facade = params[0]; - try { - publishProgress(STATE_CONNECTION_RUNNING); - mConnection = facade.createConnection(); - if (!mConnection.connect()) { - mErrorMessage = mConnection.getErrorMessage(); - return false; + protected Boolean doInBackground(IXmppFacade... params) { + boolean result = true; + IXmppFacade facade = params[0]; + try { + publishProgress(STATE_CONNECTION_RUNNING); + mConnection = facade.createConnection(); + if (!mConnection.isReconnecting()) { + if (!mConnection.connect()) { + mErrorMessage = mConnection.getErrorMessage(); + return false; + } + publishProgress(STATE_LOGIN_RUNNING); + + if (!mConnection.login()) { + mErrorMessage = mConnection.getErrorMessage(); + publishProgress(STATE_LOGIN_FAILED); + return false; + } + } else { + // snif + while (mConnection.isReconnecting()) + Thread.sleep(1000); + } + publishProgress(STATE_LOGIN_SUCCESS); + } catch (RemoteException e) { + mErrorMessage = "Exception during connection :" + e; + result = false; + } catch (InterruptedException e) { + mErrorMessage = "Exception during connection :" + e; + result = false; } - publishProgress(STATE_LOGIN_RUNNING); - - if (!mConnection.login()) { - mErrorMessage = mConnection.getErrorMessage(); - publishProgress(STATE_LOGIN_FAILED); - return false; - } - publishProgress(STATE_LOGIN_SUCCESS); - } catch (RemoteException e) { - mErrorMessage = "Exception during connection :" + e; - result = false; + return result; } - return result; - } /** * Make sur to call the parent method when overriding this method. @@ -131,4 +141,5 @@ public String getErrorMessage() { return mErrorMessage; } + } diff -r 344a06703472 -r 28e72fa1b801 src/com/beem/project/beem/service/XmppConnectionAdapter.java --- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java Sun Apr 11 19:34:31 2010 +0200 +++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java Mon Apr 19 23:07:26 2010 +0200 @@ -111,6 +111,7 @@ private final SubscribePacketListener mSubscribePacketListener = new SubscribePacketListener(); private final ConnexionListenerAdapter mConListener = new ConnexionListenerAdapter(); + private boolean mIsReconnecting; /** * Constructor. @@ -310,6 +311,11 @@ changeStatusAndPriority(status, msg, mPreviousPriority); } + @Override + public boolean isReconnecting() { + return mIsReconnecting; + } + /** * get the previous status. * @return previous status. @@ -468,35 +474,10 @@ @Override public void connectionClosedOnError(Exception exception) { Log.d(TAG, "connectionClosedOnError"); - mRoster = null; Intent intent = new Intent(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED); intent.putExtra("message", exception.getMessage()); mService.sendBroadcast(intent); - mService.stopSelf(); - mApplication.setConnected(false); - } - - /** - * Connection failed callback. - * @param errorMsg smack failure message - */ - public void connectionFailed(String errorMsg) { - Log.d(TAG, "Connection Failed"); - final int n = mRemoteConnListeners.beginBroadcast(); - - for (int i = 0; i < n; i++) { - IBeemConnectionListener listener = mRemoteConnListeners.getBroadcastItem(i); - try { - if (listener != null) - listener.connectionFailed(errorMsg); - } catch (RemoteException e) { - // The RemoteCallbackList will take care of removing the - // dead listeners. - Log.w(TAG, "Error while triggering remote connection listeners", e); - } - } - mRemoteConnListeners.finishBroadcast(); - mService.stopSelf(); +// mService.stopSelf(); mApplication.setConnected(false); } @@ -506,6 +487,7 @@ @Override public void reconnectingIn(int arg0) { Log.d(TAG, "reconnectingIn"); + mIsReconnecting = true; final int n = mRemoteConnListeners.beginBroadcast(); for (int i = 0; i < n; i++) { @@ -550,38 +532,8 @@ @Override public void reconnectionSuccessful() { Log.d(TAG, "reconnectionSuccessful"); + mIsReconnecting = false; mApplication.setConnected(true); - PacketFilter filter = new PacketFilter() { - - @Override - public boolean accept(Packet packet) { - if (packet instanceof Presence) { - Presence pres = (Presence) packet; - if (pres.getType() == Presence.Type.subscribe) - return true; - } - return false; - } - }; - - mAdaptee.addPacketListener(new PacketListener() { - - @Override - public void processPacket(Packet packet) { - String from = packet.getFrom(); - Notification notif = new Notification(android.R.drawable.stat_notify_more, mService.getString( - R.string.AcceptContactRequest, from), System.currentTimeMillis()); - notif.defaults = Notification.DEFAULT_ALL; - notif.flags = Notification.FLAG_AUTO_CANCEL; - Intent intent = new Intent(mService, Subscription.class); - intent.putExtra("from", from); - notif.setLatestEventInfo(mService, from, mService - .getString(R.string.AcceptContactRequestFrom, from), PendingIntent.getActivity(mService, 0, - intent, PendingIntent.FLAG_ONE_SHOT)); - int id = packet.hashCode(); - mService.sendNotification(id, notif); - } - }, filter); final int n = mRemoteConnListeners.beginBroadcast(); diff -r 344a06703472 -r 28e72fa1b801 src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl --- a/src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl Sun Apr 11 19:34:31 2010 +0200 +++ b/src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl Mon Apr 19 23:07:26 2010 +0200 @@ -60,7 +60,6 @@ */ //void onConnect(); - //void connectionClosedOnError(in Exception e); /** * Callback to call when the connection is closed on error */ @@ -83,6 +82,7 @@ /** * Callback to call when the connection Failed + * @deprecated */ - void connectionFailed(in String errorMsg); + // void connectionFailed(in String errorMsg); } diff -r 344a06703472 -r 28e72fa1b801 src/com/beem/project/beem/service/aidl/IXmppConnection.aidl --- a/src/com/beem/project/beem/service/aidl/IXmppConnection.aidl Sun Apr 11 19:34:31 2010 +0200 +++ b/src/com/beem/project/beem/service/aidl/IXmppConnection.aidl Mon Apr 19 23:07:26 2010 +0200 @@ -60,6 +60,8 @@ boolean disconnect(); + boolean isReconnecting(); + IRoster getRoster(); void addConnectionListener(in IBeemConnectionListener listen); diff -r 344a06703472 -r 28e72fa1b801 src/com/beem/project/beem/utils/BeemBroadcastReceiver.java --- a/src/com/beem/project/beem/utils/BeemBroadcastReceiver.java Sun Apr 11 19:34:31 2010 +0200 +++ b/src/com/beem/project/beem/utils/BeemBroadcastReceiver.java Mon Apr 19 23:07:26 2010 +0200 @@ -86,7 +86,7 @@ if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) { Toast.makeText(context, context.getString(R.string.BeemBroadcastReceiverDisconnect), Toast.LENGTH_SHORT).show(); - context.stopService(new Intent(context, BeemService.class)); + //TODO stop the connection and relaunch it when the connectivity comes back } } }