Initialize the xmpp connection config in a background thread.
authorDa Risk <da_risk@beem-project.com>
Fri, 27 Jan 2012 00:05:55 +0100
changeset 96489dbb4bee206
parent 963 24474027eb45
child 965 55c27ccbd9f5
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
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/service/XmppFacade.java
     1.1 --- a/src/com/beem/project/beem/BeemService.java	Thu Jan 26 23:54:46 2012 +0100
     1.2 +++ b/src/com/beem/project/beem/BeemService.java	Fri Jan 27 00:05:55 2012 +0100
     1.3 @@ -129,6 +129,8 @@
     1.4  
     1.5      private boolean mOnOffReceiverIsRegistered;
     1.6  
     1.7 +    private SSLContext sslContext;
     1.8 +
     1.9      /**
    1.10       * Constructor.
    1.11       */
    1.12 @@ -168,7 +170,8 @@
    1.13  	// maybe not the universal path, but it works on most devices (Samsung Galaxy, Google Nexus One)
    1.14  	mConnectionConfiguration.setTruststoreType("BKS");
    1.15  	mConnectionConfiguration.setTruststorePath("/system/etc/security/cacerts.bks");
    1.16 -	installMemorizingTrustManager(mConnectionConfiguration);
    1.17 +	if (sslContext != null)
    1.18 +	    mConnectionConfiguration.setCustomSSLContext(sslContext);
    1.19      }
    1.20  
    1.21      /**
    1.22 @@ -224,14 +227,12 @@
    1.23  	    mLogin = tmpJid;
    1.24  	}
    1.25  
    1.26 -	initConnectionConfig();
    1.27  	configure(ProviderManager.getInstance());
    1.28  
    1.29  	mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    1.30 -	mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this);
    1.31  
    1.32  	Roster.setDefaultSubscriptionMode(SubscriptionMode.manual);
    1.33 -	mBind = new XmppFacade(mConnection);
    1.34 +	mBind = new XmppFacade(this);
    1.35  	Log.d(TAG, "Create BeemService");
    1.36      }
    1.37  
    1.38 @@ -266,6 +267,19 @@
    1.39      }
    1.40  
    1.41      /**
    1.42 +     * Create the XmppConnectionAdapter.
    1.43 +     * This method makes a network request so it must not be called on the main thread.
    1.44 +     * @return the connection
    1.45 +     */
    1.46 +    public XmppConnectionAdapter createConnection() {
    1.47 +	if (mConnection == null) {
    1.48 +	    initConnectionConfig();
    1.49 +	    mConnection = new XmppConnectionAdapter(mConnectionConfiguration, mLogin, mPassword, this);
    1.50 +	}
    1.51 +	return mConnection;
    1.52 +    }
    1.53 +
    1.54 +    /**
    1.55       * Show a notification using the preference of the user.
    1.56       * @param id the id of the notification.
    1.57       * @param notif the notification to show
    1.58 @@ -335,12 +349,11 @@
    1.59       *
    1.60       * @param config the configuration to modify
    1.61       */
    1.62 -    private void installMemorizingTrustManager(ConnectionConfiguration config) {
    1.63 +    private void initMemorizingTrustManager(ConnectionConfiguration config) {
    1.64  	try {
    1.65 -	    SSLContext sc = SSLContext.getInstance("TLS");
    1.66 -	    sc.init(null, MemorizingTrustManager.getInstanceList(this),
    1.67 +	    sslContext = SSLContext.getInstance("TLS");
    1.68 +	    sslContext.init(null, MemorizingTrustManager.getInstanceList(this),
    1.69  		    new java.security.SecureRandom());
    1.70 -	    config.setCustomSSLContext(sc);
    1.71  	} catch (GeneralSecurityException e) {
    1.72  	    Log.w(TAG, "Unable to use MemorizingTrustManager", e);
    1.73  	}
     2.1 --- a/src/com/beem/project/beem/service/XmppFacade.java	Thu Jan 26 23:54:46 2012 +0100
     2.2 +++ b/src/com/beem/project/beem/service/XmppFacade.java	Fri Jan 27 00:05:55 2012 +0100
     2.3 @@ -43,11 +43,10 @@
     2.4  */
     2.5  package com.beem.project.beem.service;
     2.6  
     2.7 -import org.jivesoftware.smack.packet.Presence;
     2.8 -
     2.9  import android.net.Uri;
    2.10  import android.os.RemoteException;
    2.11  
    2.12 +import com.beem.project.beem.BeemService;
    2.13  import com.beem.project.beem.service.aidl.IChatManager;
    2.14  import com.beem.project.beem.service.aidl.IPrivacyListManager;
    2.15  import com.beem.project.beem.service.aidl.IRoster;
    2.16 @@ -55,20 +54,24 @@
    2.17  import com.beem.project.beem.service.aidl.IXmppFacade;
    2.18  import com.beem.project.beem.utils.PresenceType;
    2.19  
    2.20 +import org.jivesoftware.smack.packet.Presence;
    2.21 +
    2.22  /**
    2.23   * This class is a facade for the Beem Service.
    2.24   * @author darisk
    2.25   */
    2.26  public class XmppFacade extends IXmppFacade.Stub {
    2.27  
    2.28 -    private final XmppConnectionAdapter mConnexion;
    2.29 +    private XmppConnectionAdapter mConnexion;
    2.30 +    private final BeemService service;
    2.31  
    2.32      /**
    2.33 -     * Constructor for XMPPFacade.
    2.34 -     * @param connection the connection use by the facade
    2.35 +     * Create an XmppFacade.
    2.36 +     *
    2.37 +     * @param service the service providing the facade
    2.38       */
    2.39 -    public XmppFacade(final XmppConnectionAdapter connection) {
    2.40 -	this.mConnexion = connection;
    2.41 +    public XmppFacade(final BeemService service) {
    2.42 +    	this.service = service;
    2.43      }
    2.44  
    2.45      /**
    2.46 @@ -76,6 +79,7 @@
    2.47       */
    2.48      @Override
    2.49      public void changeStatus(int status, String msg) {
    2.50 +    	initConnection();
    2.51  	mConnexion.changeStatus(status, msg);
    2.52      }
    2.53  
    2.54 @@ -84,6 +88,7 @@
    2.55       */
    2.56      @Override
    2.57      public void connectAsync() throws RemoteException {
    2.58 +    	initConnection();
    2.59  	mConnexion.connectAsync();
    2.60      }
    2.61  
    2.62 @@ -92,6 +97,7 @@
    2.63       */
    2.64      @Override
    2.65      public void connectSync() throws RemoteException {
    2.66 +    	initConnection();
    2.67  	mConnexion.connectSync();
    2.68      }
    2.69  
    2.70 @@ -100,6 +106,7 @@
    2.71       */
    2.72      @Override
    2.73      public IXmppConnection createConnection() throws RemoteException {
    2.74 +    	initConnection();
    2.75  	return mConnexion;
    2.76      }
    2.77  
    2.78 @@ -108,6 +115,7 @@
    2.79       */
    2.80      @Override
    2.81      public void disconnect() throws RemoteException {
    2.82 +    	initConnection();
    2.83  	mConnexion.disconnect();
    2.84      }
    2.85  
    2.86 @@ -116,6 +124,7 @@
    2.87       */
    2.88      @Override
    2.89      public IChatManager getChatManager() throws RemoteException {
    2.90 +    	initConnection();
    2.91  	return mConnexion.getChatManager();
    2.92      }
    2.93  
    2.94 @@ -124,6 +133,7 @@
    2.95       */
    2.96      @Override
    2.97      public IRoster getRoster() throws RemoteException {
    2.98 +    	initConnection();
    2.99  	return mConnexion.getRoster();
   2.100      }
   2.101  
   2.102 @@ -132,11 +142,13 @@
   2.103       */
   2.104      @Override
   2.105      public IPrivacyListManager getPrivacyListManager() {
   2.106 +    	initConnection();
   2.107  	return mConnexion.getPrivacyListManager();
   2.108      }
   2.109  
   2.110      @Override
   2.111      public void sendPresencePacket(PresenceAdapter presence) throws RemoteException {
   2.112 +    	initConnection();
   2.113  	Presence presence2 = new Presence(PresenceType.getPresenceTypeFrom(presence.getType()));
   2.114  	presence2.setTo(presence.getTo());
   2.115  	mConnexion.getAdaptee().sendPacket(presence2);
   2.116 @@ -151,6 +163,7 @@
   2.117  
   2.118      @Override
   2.119      public boolean publishAvatar(Uri avatarUri) throws RemoteException {
   2.120 +    	initConnection();
   2.121  	BeemAvatarManager mgr = mConnexion.getAvatarManager();
   2.122  	if (mgr == null)
   2.123  	    return false;
   2.124 @@ -160,6 +173,7 @@
   2.125  
   2.126      @Override
   2.127      public void disableAvatarPublishing() throws RemoteException {
   2.128 +    	initConnection();
   2.129  	BeemAvatarManager mgr = mConnexion.getAvatarManager();
   2.130  	if (mgr != null)
   2.131  	    mgr.disableAvatarPublishing();
   2.132 @@ -167,6 +181,16 @@
   2.133  
   2.134      @Override
   2.135      public UserInfo getUserInfo() throws RemoteException {
   2.136 +    	initConnection();
   2.137  	return mConnexion.getUserInfo();
   2.138      }
   2.139 +
   2.140 +    /**
   2.141 +     * Initialize the connection.
   2.142 +     */
   2.143 +    private void initConnection() {
   2.144 +	if (mConnexion == null) {
   2.145 +	    mConnexion = service.createConnection();
   2.146 +	}
   2.147 +    }
   2.148  }