Use proxy when creating an account or when testing an existing account
authorDa Risk <da_risk@beem-project.com>
Mon, 24 Sep 2012 23:41:26 +0200
changeset 991 9a579769bb05
parent 990 b8430fb9b6dd
child 992 3808cae96552
Use proxy when creating an account or when testing an existing account
src/com/beem/project/beem/BeemApplication.java
src/com/beem/project/beem/ui/wizard/AccountConfigureFragment.java
src/com/beem/project/beem/ui/wizard/CreateAccountFragment.java
--- a/src/com/beem/project/beem/BeemApplication.java	Mon Sep 24 20:56:26 2012 +0200
+++ b/src/com/beem/project/beem/BeemApplication.java	Mon Sep 24 23:41:26 2012 +0200
@@ -63,6 +63,10 @@
     public static final String ACCOUNT_USERNAME_KEY = "account_username";
     /** Preference key for account password. */
     public static final String ACCOUNT_PASSWORD_KEY = "account_password";
+    /** Preference key set to true if using an Android account . */
+    public static final String USE_SYSTEM_ACCOUNT_KEY = "use_system_account";
+    /** Preference key for Android account type . */
+    public static final String ACCOUNT_SYSTEM_TYPE_KEY = "account_system_type";
     /** Preference key for status (available, busy, away, ...). */
     public static final String STATUS_KEY = "status";
     /** Preference key for status message. */
--- a/src/com/beem/project/beem/ui/wizard/AccountConfigureFragment.java	Mon Sep 24 20:56:26 2012 +0200
+++ b/src/com/beem/project/beem/ui/wizard/AccountConfigureFragment.java	Mon Sep 24 23:41:26 2012 +0200
@@ -79,6 +79,8 @@
 import org.jivesoftware.smack.ConnectionConfiguration;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.proxy.ProxyInfo;
+import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType;
 import org.jivesoftware.smack.util.StringUtils;
 
 /**
@@ -105,6 +107,8 @@
     private boolean mValidPassword;
     private String mSelectedAccountName;
     private String mSelectedAccountType;
+    private SharedPreferences settings;
+    private boolean useSystemAccount;
 
     private com.beem.project.beem.ui.wizard.AccountConfigureFragment.ConnectionTestTask task;
 
@@ -122,6 +126,8 @@
     	super.onCreate(savedInstanceState);
     	Log.d(TAG, "onCreate");
     	setRetainInstance(true);
+    	
+    	settings =  PreferenceManager.getDefaultSharedPreferences(getActivity());
     }
 
     @Override
@@ -145,22 +151,41 @@
     	mAccountJID.setFilters(newFilters);
     	mAccountJID.addTextChangedListener(mJidTextWatcher);
     	mAccountPassword.addTextChangedListener(mPasswordTextWatcher);
-    	if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH
-		|| true) // true to disable the feature until ready
+    	if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) // true to disable the feature until ready
     		v.findViewById(R.id.account_layout).setVisibility(View.GONE);
     	return v;
     }
 
+    @Override
+    public void onStart() {
+    	super.onStart();
+    	useSystemAccount = settings.getBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false);
+    	// temporaly disable jid watcher
+    	mAccountJID.removeTextChangedListener(mJidTextWatcher);
+		mAccountJID.setText(settings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, ""));
+    	if (useSystemAccount) {
+    		mAccountPassword.setText("*******"); //dummy password
+    		mAccountJID.setText(settings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, ""));
+    		mAccountPassword.setEnabled(false);
+    		mNextButton.setEnabled(true);
+    	}
+    	mAccountJID.addTextChangedListener(mJidTextWatcher);
+    }
 
     @TargetApi(14)
     @Override
     public void onClick(View v) {
 	if (v == mNextButton) {
-	    String jid = mAccountJID.getText().toString();
-	    jid = StringUtils.parseBareAddress(jid);
-	    String password = mAccountPassword.getText().toString();
-	    task = new ConnectionTestTask();
-	    task.execute(jid, password);
+		if (useSystemAccount) {
+			onDeviceAccountSelected(settings.getString(BeemApplication.ACCOUNT_USERNAME_KEY, ""),
+					settings.getString(BeemApplication.ACCOUNT_SYSTEM_TYPE_KEY, ""));
+		} else {
+			String jid = mAccountJID.getText().toString();
+		    jid = StringUtils.parseBareAddress(jid);
+		    String password = mAccountPassword.getText().toString();
+		    task = new ConnectionTestTask();
+		    task.execute(jid, password);
+		}
 	} else if (v == mManualConfigButton) {
 	    onManualConfigurationSelected();
 	} else if (v == mSelectAccountButton) {
@@ -235,8 +260,7 @@
      */
     private void onDeviceAccountSelected(String accountName, String accountType) {
 	Activity a = getActivity();
-	//TODO Save credential
-	Log.d(TAG, "Account selected is " + accountName + " from " + accountType);
+	saveCredentialAccount(accountName, accountType);
 	// launch login
 	Intent i = new Intent(a, Login.class);
 	i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
@@ -252,10 +276,25 @@
      *
      */
     private void saveCredential(String jid, String pass) {
-	SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
 	SharedPreferences.Editor edit = settings.edit();
 	edit.putString(BeemApplication.ACCOUNT_USERNAME_KEY, jid);
 	edit.putString(BeemApplication.ACCOUNT_PASSWORD_KEY, pass);
+	edit.putBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, false);
+	edit.commit();
+    }
+    
+    /**
+     * Save the user credentials.
+     *
+     * @param accountName the account name of the user
+     * @param accountType the account type of the user
+     *
+     */
+    private void saveCredentialAccount(String accountName, String accountType) {
+	SharedPreferences.Editor edit = settings.edit();
+	edit.putString(BeemApplication.ACCOUNT_USERNAME_KEY, accountName);
+	edit.putString(BeemApplication.ACCOUNT_SYSTEM_TYPE_KEY, accountType);
+	edit.putBoolean(BeemApplication.USE_SYSTEM_ACCOUNT_KEY, true);
 	edit.commit();
     }
 
@@ -325,6 +364,11 @@
 	public void afterTextChanged(Editable s) {
 	    checkUsername(s.toString());
 	    mNextButton.setEnabled(mValidJid && mValidPassword);
+	    if (useSystemAccount) {
+		    mAccountPassword.setEnabled(true);
+	    	mAccountPassword.setText("");
+	    }
+	    useSystemAccount = false;
 	}
 
 	@Override
@@ -408,15 +452,26 @@
 	 * @return the XMPPConnection prepared to connect
 	 */
 	private Connection prepareConnection(String jid, String server, int port) {
+		boolean useProxy = settings.getBoolean(BeemApplication.PROXY_USE_KEY, false);
+		ProxyInfo proxyinfo = ProxyInfo.forNoProxy();
+		if (useProxy) {
+		    String stype = settings.getString(BeemApplication.PROXY_TYPE_KEY, "HTTP");
+		    String phost = settings.getString(BeemApplication.PROXY_SERVER_KEY, "");
+		    String puser = settings.getString(BeemApplication.PROXY_USERNAME_KEY, "");
+		    String ppass = settings.getString(BeemApplication.PROXY_PASSWORD_KEY, "");
+		    int pport = Integer.parseInt(settings.getString(BeemApplication.PROXY_PORT_KEY, "1080"));
+		    ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
+		    proxyinfo = new ProxyInfo(type, phost, pport, puser, ppass);
+		}
 	    String serviceName = StringUtils.parseServer(jid);
 	    if (port != -1 || !TextUtils.isEmpty(server)) {
 		if (port == -1)
 		    port  = 5222;
 		if (TextUtils.isEmpty(server))
 		    server = serviceName;
-		config = new ConnectionConfiguration(server, port, serviceName);
+		config = new ConnectionConfiguration(server, port, serviceName, proxyinfo);
 	    } else {
-		config = new ConnectionConfiguration(serviceName);
+		config = new ConnectionConfiguration(serviceName, proxyinfo);
 	    }
 	    return new XMPPConnection(config);
 	}
--- a/src/com/beem/project/beem/ui/wizard/CreateAccountFragment.java	Mon Sep 24 20:56:26 2012 +0200
+++ b/src/com/beem/project/beem/ui/wizard/CreateAccountFragment.java	Mon Sep 24 23:41:26 2012 +0200
@@ -57,6 +57,8 @@
 import org.jivesoftware.smack.ConnectionConfiguration;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
+import org.jivesoftware.smack.proxy.ProxyInfo;
+import org.jivesoftware.smack.proxy.ProxyInfo.ProxyType;
 import org.jivesoftware.smack.util.StringUtils;
 
 
@@ -71,6 +73,7 @@
     private Button createButton;
     private CreateAccountTask task;
     private final NotEmptyTextWatcher mTextWatcher = new NotEmptyTextWatcher();
+    private SharedPreferences settings;
 
     /**
      * Create a CreateAccountFragment.
@@ -91,6 +94,8 @@
     public void onCreate(Bundle savedInstanceState) {
 	super.onCreate(savedInstanceState);
 	setRetainInstance(true);
+	settings =  PreferenceManager.getDefaultSharedPreferences(getActivity());
+
     }
 
     @Override
@@ -265,15 +270,26 @@
 	 * @return the XMPPConnection prepared to connect
 	 */
 	private Connection prepareConnection(String jid, String server, int port) {
+		boolean useProxy = settings.getBoolean(BeemApplication.PROXY_USE_KEY, false);
+		ProxyInfo proxyinfo = ProxyInfo.forNoProxy();
+		if (useProxy) {
+		    String stype = settings.getString(BeemApplication.PROXY_TYPE_KEY, "HTTP");
+		    String phost = settings.getString(BeemApplication.PROXY_SERVER_KEY, "");
+		    String puser = settings.getString(BeemApplication.PROXY_USERNAME_KEY, "");
+		    String ppass = settings.getString(BeemApplication.PROXY_PASSWORD_KEY, "");
+		    int pport = Integer.parseInt(settings.getString(BeemApplication.PROXY_PORT_KEY, "1080"));
+		    ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
+		    proxyinfo = new ProxyInfo(type, phost, pport, puser, ppass);
+		}
 	    String serviceName = StringUtils.parseServer(jid);
 	    if (port != -1 || !TextUtils.isEmpty(server)) {
 		if (port == -1)
 		    port = 5222;
 		if (TextUtils.isEmpty(server))
 		    server = serviceName;
-		config = new ConnectionConfiguration(server, port, serviceName);
+		config = new ConnectionConfiguration(server, port, serviceName, proxyinfo);
 	    } else {
-		config = new ConnectionConfiguration(serviceName);
+		config = new ConnectionConfiguration(serviceName, proxyinfo);
 	    }
 	    return new XMPPConnection(config);
 	}