MIse en place du JID dans createaccount
authorMarseille
Wed, 01 Jul 2009 16:21:16 +0200
changeset 325 cb5293558d3c
parent 324 5601d949c5aa
child 326 c5f000965df8
MIse en place du JID dans createaccount
res/values-en/strings.xml
res/values-fr/strings.xml
src/com/beem/project/beem/ui/CreateAccount.java
--- a/res/values-en/strings.xml	Wed Jul 01 14:15:06 2009 +0200
+++ b/res/values-en/strings.xml	Wed Jul 01 16:21:16 2009 +0200
@@ -61,15 +61,6 @@
 	<string name="CDSure2DeleteYes">Yes</string>
 	<string name="CDSure2DeleteNo">No</string>
 	
-	<!-- AccountCreation class -->
-	<string name="ACLogin">Login:</string>
-	<string name="ACEmail">Email:</string>
-	<string name="ACPassword">Password:</string>
-	<string name="ACRepeat">Repeat:</string>
-	<string name="ACOkButton">Ok</string>
-	<string name="ACOkLoginButton">Log with</string>
-	<string name="ACBadForm">Bad form</string>
-	<string name="ACCreated">Account created</string>
 
 	<!-- AddContact class -->
 	<string name="AddCActTitle">Beem - Add contact</string>
@@ -220,6 +211,7 @@
 	<string name="create_account_err_dialog_settings_button">Change my settings</string>
 	<string name="create_account_close_dialog_button">Close</string>
 	<string name="create_account_successfull_after">has been created successfully</string>
+	<string name="create_account_err_username">Bad JabberID</string>
 	<string name="create_account_err_passwords">Passwords do not match.</string>
 	<string name="create_account_username">Username</string>
 	<string name="create_account_password">Password</string>
--- a/res/values-fr/strings.xml	Wed Jul 01 14:15:06 2009 +0200
+++ b/res/values-fr/strings.xml	Wed Jul 01 16:21:16 2009 +0200
@@ -58,16 +58,6 @@
 	<string name="CDSure2Delete">Êtes-vous sûr de vouloir supprimer ce contact ?</string>
 	<string name="CDSure2DeleteYes">Oui</string>
 	<string name="CDSure2DeleteNo">Non</string>
-	
-	<!-- AccountCreation class -->
-	<string name="ACLogin">Nom d'utilisateur:</string>
-	<string name="ACEmail">Courriel:</string>
-	<string name="ACPassword">Mot de passe:</string>
-	<string name="ACRepeat">Répéter:</string>
-	<string name="ACOkButton">Ok</string>
-	<string name="ACOkLoginButton">Se connecter avec</string>
-	<string name="ACBadForm">Mauvais formulaire</string>
-	<string name="ACCreated">Compte créé</string>
 
 	<!-- AddContact class -->
 	<string name="AddCActTitle">Beem - Ajouter un contact</string>
@@ -214,6 +204,7 @@
 	<string name="create_account_err_dialog_settings_button">Changer mes paramètres</string>
 	<string name="create_account_close_dialog_button">Fermer</string>
 	<string name="create_account_successfull_after">a été créé avec succès</string>
+	<string name="create_account_err_username">Mauvais JabberID</string>
 	<string name="create_account_err_passwords">Les mots de passe ne correspondent pas</string>
 	<string name="create_account_username">Nom d\'utilisateur</string>
 	<string name="create_account_password">Mot de passe</string>
--- a/src/com/beem/project/beem/ui/CreateAccount.java	Wed Jul 01 14:15:06 2009 +0200
+++ b/src/com/beem/project/beem/ui/CreateAccount.java	Wed Jul 01 16:21:16 2009 +0200
@@ -1,10 +1,13 @@
 package com.beem.project.beem.ui;
 
+import java.util.regex.Pattern;
+
 import org.jivesoftware.smack.AccountManager;
 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.util.StringUtils;
 
 import android.app.Activity;
 import android.app.AlertDialog;
@@ -15,29 +18,27 @@
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
+import android.widget.TextView;
 import android.widget.Toast;
 
 import com.beem.project.beem.R;
 
 /**
- * This class represents an activity which allows the user to create an account on the XMPP server
- * saved in settings
- * 
+ * This class represents an activity which allows the user to create an account on the XMPP server saved in settings
  * @author dasilvj
  */
 public class CreateAccount extends Activity {
 
     private final static boolean DEFAULT_BOOLEAN_VALUE = false;
-    private final static String  DEFAULT_STRING_VALUE  = "";
-    private final static int     DEFAULT_INT_VALUE     = 0;
+    private final static String DEFAULT_STRING_VALUE = "";
+    private final static int DEFAULT_INT_VALUE = 0;
 
-    private final static String  DEFAULT_XMPP_PORT     = "5222";
+    private final static String DEFAULT_XMPP_PORT = "5222";
 
-    private final static int     NOTIFICATION_DURATION = Toast.LENGTH_SHORT;
+    private final static int NOTIFICATION_DURATION = Toast.LENGTH_SHORT;
 
-    private SharedPreferences    settings              = null;
-    private Button               createAccountButton   = null;
-
+    private SharedPreferences settings = null;
+    private Button createAccountButton = null;
 
     /**
      * {@inheritDoc}
@@ -51,21 +52,6 @@
     }
 
     /**
-     * Check if the fields password and confirm password match
-     * 
-     * @return return true if password & confirm password fields match, else false
-     */
-    private boolean checkPasswords() {
-	final String passwordFieldValue = ((EditText) findViewById(R.id.create_account_password)).getText().toString();
-	final String passwordConfirmFielddValue = ((EditText) findViewById(R.id.create_account_confirm_password))
-	.getText().toString();
-
-	if (passwordFieldValue.equals(passwordConfirmFielddValue))
-	    return (true);
-	return (false);
-    }
-
-    /**
      * Create an account on the XMPP server specified in settings
      */
     private boolean createAccount(String username, String password) {
@@ -73,10 +59,9 @@
 	ConnectionConfiguration connectionConfiguration = null;
 
 	if (getRegisteredProxy() != null) {
-	    connectionConfiguration = new ConnectionConfiguration(getRegisteredXMPPServer(), getRegisteredXMPPPort(),
-		getRegisteredProxy());
+	    connectionConfiguration = new ConnectionConfiguration(getXMPPServer(), 5222, getRegisteredProxy());
 	} else {
-	    connectionConfiguration = new ConnectionConfiguration(getRegisteredXMPPServer(), getRegisteredXMPPPort());
+	    connectionConfiguration = new ConnectionConfiguration(getXMPPServer(), 5222);
 	}
 	if (getRegisteredXMPPTLSUse())
 	    connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
@@ -100,9 +85,7 @@
 
     /**
      * Create a dialog containing an error message
-     * 
-     * @param errMsg
-     *            the error message
+     * @param errMsg the error message
      */
     private void createErrorDialog(String errMsg) {
 	Log.v(getString(R.string.create_account_tag), errMsg);
@@ -121,7 +104,6 @@
 
     /**
      * Retrive proxy informations from the preferences
-     * 
      * @return Registered proxy informations
      */
     private ProxyInfo getRegisteredProxy() {
@@ -135,7 +117,6 @@
 
     /**
      * Retrieve proxy password from the preferences
-     * 
      * @return Registered proxy password
      */
     private String getRegisteredProxyPassword() {
@@ -144,7 +125,6 @@
 
     /**
      * Retrieve proxy port from the preferences
-     * 
      * @return Registered proxy port
      */
     private int getRegisteredProxyPort() {
@@ -153,7 +133,6 @@
 
     /**
      * Retrieve proxy server from the preferences
-     * 
      * @return Registered proxy server
      */
     private String getRegisteredProxyServer() {
@@ -162,7 +141,6 @@
 
     /**
      * Retrieve proxy type from the preferences
-     * 
      * @return Registered proxy type
      */
     private ProxyInfo.ProxyType getRegisteredProxyType() {
@@ -181,7 +159,6 @@
 
     /**
      * Retrieve proxy use from the preferences
-     * 
      * @return Registered proxy use
      */
     private boolean getRegisteredProxyUse() {
@@ -190,7 +167,6 @@
 
     /**
      * Retrieve proxy username from the preferences
-     * 
      * @return Registered proxy username
      */
     private String getRegisteredProxyUsername() {
@@ -198,26 +174,17 @@
     }
 
     /**
-     * Retrieve xmpp port from the preferences
-     * 
-     * @return Registered xmpp port
-     */
-    private int getRegisteredXMPPPort() {
-	return (Integer.parseInt(settings.getString(getString(R.string.settings_key_xmpp_port), DEFAULT_XMPP_PORT)));
-    }
-
-    /**
      * Retrieve xmpp server from the preferences
-     * 
      * @return Registered xmpp server
      */
-    private String getRegisteredXMPPServer() {
-	return (settings.getString(getString(R.string.settings_key_xmpp_server), DEFAULT_STRING_VALUE));
+    private String getXMPPServer() {
+	TextView xmppServerTextView = (TextView) findViewById(R.id.create_account_username);
+	String xmppServer = StringUtils.parseServer(xmppServerTextView.getText().toString());
+	return xmppServer;
     }
 
     /**
      * Retrieve TLS use from the preferences
-     * 
      * @return Registered TLS use
      */
     private boolean getRegisteredXMPPTLSUse() {
@@ -225,6 +192,25 @@
     }
 
     /**
+     * Check if the fields password and confirm password match
+     * @return return true if password & confirm password fields match, else false
+     */
+    private boolean checkPasswords() {
+	final String passwordFieldValue = ((EditText) findViewById(R.id.create_account_password)).getText().toString();
+	final String passwordConfirmFielddValue = ((EditText) findViewById(R.id.create_account_confirm_password))
+	    .getText().toString();
+
+	if (passwordFieldValue.equals(passwordConfirmFielddValue))
+	    return (true);
+	return (false);
+    }
+
+    private boolean checkEmail() {
+	String email = ((TextView) findViewById(R.id.create_account_username)).getText().toString();
+	return (Pattern.matches("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+[a-zA-Z]{2,4}", email));
+    }
+
+    /**
      * Initialize the "Create this account" button which allows the user to create an account
      */
     private void initCreateAccountButton() {
@@ -232,30 +218,39 @@
 	createAccountButton.setOnClickListener(new View.OnClickListener() {
 	    public void onClick(View v) {
 		String usernameFieldValue = ((EditText) findViewById(R.id.create_account_username)).getText()
-		.toString();
+		    .toString();
 		String passwordFieldValue = ((EditText) findViewById(R.id.create_account_password)).getText()
-		.toString();
+		    .toString();
+		String username = StringUtils.parseName(usernameFieldValue);
+		if (!checkEmail())
+		    createErrorDialog(getString(R.string.create_account_err_username));
+		else if (!checkPasswords())
+		    createErrorDialog(getString(R.string.create_account_err_passwords));
+		else {
+		    if (createAccount(username, passwordFieldValue))
+			finish();
+		}
 
-		if (!checkPasswords())
-		    createErrorDialog(getString(R.string.create_account_err_passwords));
-		else if (createAccount(usernameFieldValue, passwordFieldValue))
-			finish();
 	    }
 	});
 	Button createAccountLoginButton = (Button) findViewById(R.id.create_account_login_button);
 	createAccountLoginButton.setOnClickListener(new View.OnClickListener() {
 	    public void onClick(View v) {
 		String usernameFieldValue = ((EditText) findViewById(R.id.create_account_username)).getText()
-		.toString();
+		    .toString();
+		String username = StringUtils.parseName(usernameFieldValue);
+		String server= StringUtils.parseServer(usernameFieldValue);
 		String passwordFieldValue = ((EditText) findViewById(R.id.create_account_password)).getText()
-		.toString();
-
-		if (!checkPasswords())
+		    .toString();
+		if (!checkEmail())
+		    createErrorDialog(getString(R.string.create_account_err_username));
+		else if (!checkPasswords())
 		    createErrorDialog(getString(R.string.create_account_err_passwords));
 		else {
-		    if (createAccount(usernameFieldValue, passwordFieldValue)) {
+		    if (createAccount(username, passwordFieldValue)) {
 			SharedPreferences.Editor settingsEditor = settings.edit();
-			settingsEditor.putString(getString(R.string.settings_key_account_username), usernameFieldValue);
+			settingsEditor.putString(getString(R.string.settings_key_account_username), username);
+			settingsEditor.putString(getString(R.string.settings_account_server), server);
 			settingsEditor.putString(getString(R.string.settings_key_account_password), passwordFieldValue);
 			settingsEditor.commit();
 			finish();
@@ -263,5 +258,5 @@
 		}
 	    }
 	});
-    }	
+    }
 }