Initialize the xmpp connection config in a background thread.
On android > 3.x network operations on the main thread crash the application.
This move the dns request made by the ConnectionConfiguration in another
thread by lazily initializing the XMPPConnection
--- a/src/com/beem/project/beem/BeemService.java Thu Jan 26 23:54:46 2012 +0100
+++ b/src/com/beem/project/beem/BeemService.java Fri Jan 27 00:05:55 2012 +0100
@@ -129,6 +129,8 @@
private boolean mOnOffReceiverIsRegistered;
+ private SSLContext sslContext;
+
/**
* Constructor.
*/
@@ -168,7 +170,8 @@
// maybe not the universal path, but it works on most devices (Samsung Galaxy, Google Nexus One)
mConnectionConfiguration.setTruststoreType("BKS");
mConnectionConfiguration.setTruststorePath("/system/etc/security/cacerts.bks");
- installMemorizingTrustManager(mConnectionConfiguration);
+ if (sslContext != null)
+ mConnectionConfiguration.setCustomSSLContext(sslContext);
}
/**
@@ -224,14 +227,12 @@
mLogin = tmpJid;
}
- initConnectionConfig();
configure(ProviderManager.getInstance());
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this);
Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
- mBind = new XmppFacade(mConnection);
+ mBind = new XmppFacade(this);
Log.d(TAG, "Create BeemService");
}
@@ -266,6 +267,19 @@
}
/**
+ * Create the XmppConnectionAdapter.
+ * This method makes a network request so it must not be called on the main thread.
+ * @return the connection
+ */
+ public XmppConnectionAdapter createConnection() {
+ if (mConnection == null) {
+ initConnectionConfig();
+ mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this);
+ }
+ return mConnection;
+ }
+
+ /**
* Show a notification using the preference of the user.
* @param id the id of the notification.
* @param notif the notification to show
@@ -335,12 +349,11 @@
*
* @param config the configuration to modify
*/
- private void installMemorizingTrustManager(ConnectionConfiguration config) {
+ private void initMemorizingTrustManager(ConnectionConfiguration config) {
try {
- SSLContext sc = SSLContext.getInstance("TLS");
- sc.init(null, MemorizingTrustManager.getInstanceList(this),
+ sslContext = SSLContext.getInstance("TLS");
+ sslContext.init(null, MemorizingTrustManager.getInstanceList(this),
new java.security.SecureRandom());
- config.setCustomSSLContext(sc);
} catch (GeneralSecurityException e) {
Log.w(TAG, "Unable to use MemorizingTrustManager", e);
}
--- a/src/com/beem/project/beem/service/XmppFacade.java Thu Jan 26 23:54:46 2012 +0100
+++ b/src/com/beem/project/beem/service/XmppFacade.java Fri Jan 27 00:05:55 2012 +0100
@@ -43,11 +43,10 @@
*/
package com.beem.project.beem.service;
-import org.jivesoftware.smack.packet.Presence;
-
import android.net.Uri;
import android.os.RemoteException;
+import com.beem.project.beem.BeemService;
import com.beem.project.beem.service.aidl.IChatManager;
import com.beem.project.beem.service.aidl.IPrivacyListManager;
import com.beem.project.beem.service.aidl.IRoster;
@@ -55,20 +54,24 @@
import com.beem.project.beem.service.aidl.IXmppFacade;
import com.beem.project.beem.utils.PresenceType;
+import org.jivesoftware.smack.packet.Presence;
+
/**
* This class is a facade for the Beem Service.
* @author darisk
*/
public class XmppFacade extends IXmppFacade.Stub {
- private final XmppConnectionAdapter mConnexion;
+ private XmppConnectionAdapter mConnexion;
+ private final BeemService service;
/**
- * Constructor for XMPPFacade.
- * @param connection the connection use by the facade
+ * Create an XmppFacade.
+ *
+ * @param service the service providing the facade
*/
- public XmppFacade(final XmppConnectionAdapter connection) {
- this.mConnexion = connection;
+ public XmppFacade(final BeemService service) {
+ this.service = service;
}
/**
@@ -76,6 +79,7 @@
*/
@Override
public void changeStatus(int status, String msg) {
+ initConnection();
mConnexion.changeStatus(status, msg);
}
@@ -84,6 +88,7 @@
*/
@Override
public void connectAsync() throws RemoteException {
+ initConnection();
mConnexion.connectAsync();
}
@@ -92,6 +97,7 @@
*/
@Override
public void connectSync() throws RemoteException {
+ initConnection();
mConnexion.connectSync();
}
@@ -100,6 +106,7 @@
*/
@Override
public IXmppConnection createConnection() throws RemoteException {
+ initConnection();
return mConnexion;
}
@@ -108,6 +115,7 @@
*/
@Override
public void disconnect() throws RemoteException {
+ initConnection();
mConnexion.disconnect();
}
@@ -116,6 +124,7 @@
*/
@Override
public IChatManager getChatManager() throws RemoteException {
+ initConnection();
return mConnexion.getChatManager();
}
@@ -124,6 +133,7 @@
*/
@Override
public IRoster getRoster() throws RemoteException {
+ initConnection();
return mConnexion.getRoster();
}
@@ -132,11 +142,13 @@
*/
@Override
public IPrivacyListManager getPrivacyListManager() {
+ initConnection();
return mConnexion.getPrivacyListManager();
}
@Override
public void sendPresencePacket(PresenceAdapter presence) throws RemoteException {
+ initConnection();
Presence presence2 = new Presence(PresenceType.getPresenceTypeFrom(presence.getType()));
presence2.setTo(presence.getTo());
mConnexion.getAdaptee().sendPacket(presence2);
@@ -151,6 +163,7 @@
@Override
public boolean publishAvatar(Uri avatarUri) throws RemoteException {
+ initConnection();
BeemAvatarManager mgr = mConnexion.getAvatarManager();
if (mgr == null)
return false;
@@ -160,6 +173,7 @@
@Override
public void disableAvatarPublishing() throws RemoteException {
+ initConnection();
BeemAvatarManager mgr = mConnexion.getAvatarManager();
if (mgr != null)
mgr.disableAvatarPublishing();
@@ -167,6 +181,16 @@
@Override
public UserInfo getUserInfo() throws RemoteException {
+ initConnection();
return mConnexion.getUserInfo();
}
+
+ /**
+ * Initialize the connection.
+ */
+ private void initConnection() {
+ if (mConnexion == null) {
+ mConnexion = service.createConnection();
+ }
+ }
}