# HG changeset patch # User Da Risk # Date 1276288472 -7200 # Node ID e3a68ccfb4ef9c00dbbcdd732a22ea8dd3e58a0b # Parent 0fb9a16d7c32e34560b8a9b48f6d48cf6e62802a A better auto reconnection process. diff -r 0fb9a16d7c32 -r e3a68ccfb4ef res/layout/preferences.xml --- a/res/layout/preferences.xml Thu Jun 10 20:08:03 2010 +0200 +++ b/res/layout/preferences.xml Fri Jun 11 22:34:32 2010 +0200 @@ -91,7 +91,8 @@ + android:numeric="integer" + android:defaultValue="2" /> + */ + private class ConnectivityReceiver extends BroadcastReceiver { + /** + * Constructor. + */ + public ConnectivityReceiver() { + } + + @Override + public void onReceive(final Context context, final Intent intent) { + String intentAction = intent.getAction(); + if (intentAction.equals(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED)) { + CharSequence message = intent.getCharSequenceExtra("message"); + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); + } else if (intentAction.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { + boolean connectivity = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false); + Log.d(TAG, "connectivity change " + connectivity); + if (!connectivity) { + setReconnectionProcess(false); + } else if (mReconnecting) { + setReconnectionProcess(true); + } + } + } + } + } diff -r 0fb9a16d7c32 -r e3a68ccfb4ef src/com/beem/project/beem/service/XmppConnectionAdapter.java --- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java Thu Jun 10 20:08:03 2010 +0200 +++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java Fri Jun 11 22:34:32 2010 +0200 @@ -171,11 +171,12 @@ } @Override - public boolean connect() throws RemoteException { + public synchronized boolean connect() throws RemoteException { if (mAdaptee.isConnected()) return true; else { try { + Log.d(TAG, "connect"); mAdaptee.connect(); mAdaptee.addConnectionListener(mConListener); return true; @@ -199,37 +200,17 @@ } @Override - public boolean login() throws RemoteException { - if (mAdaptee.isAuthenticated()) - return true; + public synchronized boolean login() throws RemoteException { if (!mAdaptee.isConnected()) return false; try { - mAdaptee.login(mLogin, mPassword, mResource); - mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService); - mPrivacyListManager = new PrivacyListManagerAdapter(PrivacyListManager.getInstanceFor(mAdaptee)); - - this.initFeatures(); // pour declarer les features xmpp qu'on - // supporte - - 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(mSubscribePacketListener, filter); - + Log.d(TAG, "login"); + if (!mAdaptee.isAuthenticated()) { + mAdaptee.login(mLogin, mPassword, mResource); + initConnection(); + } mService.resetStatus(); - mService.initJingle(mAdaptee); - + mService.disableReconnection(); mApplication.setConnected(true); changeStatus(Status.CONTACT_STATUS_AVAILABLE, mService.getServicePreference().getString("status_text", "")); return true; @@ -347,7 +328,7 @@ * {@inheritDoc} */ @Override - public boolean disconnect() { + public synchronized boolean disconnect() { if (mAdaptee != null && mAdaptee.isConnected()) mAdaptee.disconnect(); return true; @@ -437,6 +418,33 @@ } /** + * Initialize after the first connection. + */ + private void initConnection() { + mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService); + mPrivacyListManager = new PrivacyListManagerAdapter(PrivacyListManager.getInstanceFor(mAdaptee)); + + this.initFeatures(); // pour declarer les features xmpp qu'on + // supporte + + 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(mSubscribePacketListener, filter); + mService.initJingle(mAdaptee); + } + + /** * Listener for XMPP connection events. It will calls the remote listeners for connection events. * @author darisk */ @@ -461,6 +469,7 @@ mService.sendBroadcast(intent); mService.stopSelf(); mApplication.setConnected(false); + mService.disableReconnection(); } /** @@ -473,7 +482,7 @@ Intent intent = new Intent(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED); intent.putExtra("message", exception.getMessage()); mService.sendBroadcast(intent); - mService.stopSelf(); + mService.enableReconnection(); mApplication.setConnected(false); } @@ -550,40 +559,7 @@ */ @Override public void reconnectionSuccessful() { - Log.d(TAG, "reconnectionSuccessful"); - 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.flags = Notification.FLAG_AUTO_CANCEL; - Intent intent = new Intent(mService, Subscription.class); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .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); - + Log.v(TAG, "reconnectionSuccessful"); final int n = mRemoteConnListeners.beginBroadcast(); for (int i = 0; i < n; i++) { diff -r 0fb9a16d7c32 -r e3a68ccfb4ef src/com/beem/project/beem/utils/BeemBroadcastReceiver.java --- a/src/com/beem/project/beem/utils/BeemBroadcastReceiver.java Thu Jun 10 20:08:03 2010 +0200 +++ b/src/com/beem/project/beem/utils/BeemBroadcastReceiver.java Fri Jun 11 22:34:32 2010 +0200 @@ -47,11 +47,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.net.ConnectivityManager; -import android.widget.Toast; -import com.beem.project.beem.BeemService; -import com.beem.project.beem.R; /** * Manage broadcast disconnect intent. @@ -75,19 +71,11 @@ public void onReceive(final Context context, final Intent intent) { String intentAction = intent.getAction(); if (intentAction.equals(BEEM_CONNECTION_CLOSED)) { - CharSequence message = intent.getCharSequenceExtra("message"); - Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); if (context instanceof Activity) { Activity act = (Activity) context; act.finish(); // The service will be unbinded in the destroy of the activity. } - } else if (intentAction.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { - 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)); - } } } }