Refactor the initConnectionConfig method
authorDa Risk <da_risk@beem-project.com>
Mon, 07 Jan 2013 21:09:26 +0100
changeset 1013 3f4c77587fb9
parent 1012 b76c110b3c73
child 1014 5d3c8519d297
Refactor the initConnectionConfig method
src/com/beem/project/beem/BeemService.java
tools/checkstyle.xml
--- a/src/com/beem/project/beem/BeemService.java	Mon Jan 07 14:42:41 2013 +0100
+++ b/src/com/beem/project/beem/BeemService.java	Mon Jan 07 21:09:26 2013 +0100
@@ -120,7 +120,6 @@
 
     private static final String TAG = "BeemService";
     private static final int DEFAULT_XMPP_PORT = 5222;
-    //private static final String COMMAND_NAMESPACE = "http://jabber.org/protocol/commands";
 
     private NotificationManager mNotificationManager;
     private XmppConnectionAdapter mConnection;
@@ -130,8 +129,6 @@
     private String mService;
     private int mPort;
     private ConnectionConfiguration mConnectionConfiguration;
-    private ProxyInfo mProxyInfo;
-    private boolean mUseProxy;
     private IXmppFacade.Stub mBind;
 
     private BeemBroadcastReceiver mReceiver = new BeemBroadcastReceiver();
@@ -154,41 +151,26 @@
     private void initConnectionConfig() {
 	// TODO add an option for this ?
 //         SmackConfiguration.setPacketReplyTimeout(30000);
-	mUseProxy = mSettings.getBoolean(BeemApplication.PROXY_USE_KEY, false);
-	if (mUseProxy) {
-	    String stype = mSettings.getString(BeemApplication.PROXY_TYPE_KEY, "HTTP");
-	    String phost = mSettings.getString(BeemApplication.PROXY_SERVER_KEY, "");
-	    String puser = mSettings.getString(BeemApplication.PROXY_USERNAME_KEY, "");
-	    String ppass = mSettings.getString(BeemApplication.PROXY_PASSWORD_KEY, "");
-	    int pport = Integer.parseInt(mSettings.getString(BeemApplication.PROXY_PORT_KEY, "1080"));
-	    ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
-	    mProxyInfo = new ProxyInfo(type, phost, pport, puser, ppass);
-	} else {
-	    mProxyInfo = ProxyInfo.forNoProxy();
-	}
+	ProxyInfo proxyInfo = getProxyConfiguration();
 	boolean useSystemAccount = mSettings.getBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false);
-	if (mSettings.getBoolean(BeemApplication.ACCOUNT_SPECIFIC_SERVER_KEY, false))
-	    mConnectionConfiguration = new ConnectionConfiguration(mHost, mPort, mService, mProxyInfo);
 	if (useSystemAccount) {
+	    // when using system account, using SPECIFIC_SERVER settings is not a supported configuration.
 	    SASLAuthentication.supportSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME);
 	    String accountType = mSettings.getString(BeemApplication.ACCOUNT_SYSTEM_TYPE_KEY, "");
 	    String accountName = mSettings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
 	    Account account = getAccount(accountName, accountType);
 	    if (account == null) {
-		mSettings.edit().putString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
-		mConnectionConfiguration = new ConnectionConfiguration(mService, mProxyInfo);
-		mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this));
-	    } else if ("com.google".equals(accountType)) {
-		mConnectionConfiguration = new ConnectionConfiguration("talk.google.com", 5222, mProxyInfo);
-		mConnectionConfiguration.setServiceName(StringUtils.parseServer(accountName));
-		mConnectionConfiguration.setCallbackHandler(new AccountAuthenticator(this, account));
-		mConnectionConfiguration.setSecurityMode(SecurityMode.required);
+		mSettings.edit().putString(BeemApplication.ACCOUNT_USERNAME_KEY, "").commit();
 	    } else
-		mConnectionConfiguration.setCallbackHandler(new AccountAuthenticator(this, account));
-	} else {
+		mConnectionConfiguration = getConnectionConfigurationForAccount(account, proxyInfo);
+	}
+	if (!useSystemAccount || mConnectionConfiguration == null) {
 	    SASLAuthentication.unsupportSASLMechanism(SASLGoogleOAuth2Mechanism.MECHANISM_NAME);
-	    mConnectionConfiguration = new ConnectionConfiguration(mService, mProxyInfo);
-		mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this));
+	    if (mSettings.getBoolean(BeemApplication.ACCOUNT_SPECIFIC_SERVER_KEY, false))
+		mConnectionConfiguration = new ConnectionConfiguration(mHost, mPort, mService, proxyInfo);
+	    else
+		mConnectionConfiguration = new ConnectionConfiguration(mService, proxyInfo);
+	    mConnectionConfiguration.setCallbackHandler(new PreferenceAuthenticator(this));
 	}
 
 	if (mSettings.getBoolean("settings_key_xmpp_tls_use", false)
@@ -206,6 +188,44 @@
     }
 
     /**
+     * Get the save proxy configuration.
+     *
+     * @return the proxy configuration
+     */
+    private ProxyInfo getProxyConfiguration() {
+	boolean useProxy = mSettings.getBoolean(BeemApplication.PROXY_USE_KEY, false);
+	if (useProxy) {
+	    String stype = mSettings.getString(BeemApplication.PROXY_TYPE_KEY, "HTTP");
+	    String phost = mSettings.getString(BeemApplication.PROXY_SERVER_KEY, "");
+	    String puser = mSettings.getString(BeemApplication.PROXY_USERNAME_KEY, "");
+	    String ppass = mSettings.getString(BeemApplication.PROXY_PASSWORD_KEY, "");
+	    int pport = Integer.parseInt(mSettings.getString(BeemApplication.PROXY_PORT_KEY, "1080"));
+	    ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
+	    return new ProxyInfo(type, phost, pport, puser, ppass);
+	} else {
+	    return ProxyInfo.forNoProxy();
+	}
+    }
+
+    /**
+     * Get the connection configuration for an Android system account.
+     *
+     * @param account the Android account
+     * @param proxy the proxy to use
+     * @return the ConnectionConfiguration or null if the account is not supported
+     */
+    private ConnectionConfiguration getConnectionConfigurationForAccount(Account account, ProxyInfo proxy) {
+	ConnectionConfiguration result = null;
+	if ("com.google".equals(account.type)) {
+	    result = new ConnectionConfiguration("talk.google.com", DEFAULT_XMPP_PORT, proxy);
+	    result.setServiceName(StringUtils.parseServer(account.name));
+	    result.setCallbackHandler(new AccountAuthenticator(this, account));
+	    result.setSecurityMode(SecurityMode.required);
+	}
+	return result;
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
@@ -565,11 +585,11 @@
 	File f = new File(getCacheDir(), "entityCaps");
 	f.mkdirs();
 	try {
-		EntityCapsManager.setPersistentCache(new SimpleDirectoryPersistentCache(f));
+	    EntityCapsManager.setPersistentCache(new SimpleDirectoryPersistentCache(f));
 	} catch (IllegalStateException e) {
-		Log.v(TAG, "EntityCapsManager already initialized", e);
+	    Log.v(TAG, "EntityCapsManager already initialized", e);
 	} catch (IOException e) {
-	    	Log.w(TAG, "EntityCapsManager not able to reuse persistent cache");
+	    Log.w(TAG, "EntityCapsManager not able to reuse persistent cache");
 	}
     }
 
--- a/tools/checkstyle.xml	Mon Jan 07 14:42:41 2013 +0100
+++ b/tools/checkstyle.xml	Mon Jan 07 21:09:26 2013 +0100
@@ -345,7 +345,9 @@
 	<module name="FinalParameters">
 	    <property name="tokens" value="CTOR_DEF"/>
 	</module>
-	<module name="Indentation"/>
+	<module name="Indentation">
+	    <property name="severity" value="error"/>
+	</module>
 
 	<module name="TodoComment"/>
 	<module name="UpperEll"/>