ajout de la creation de compte, il faut encore rendre le message un peu plus verbose en cas d'echec.
authornikita@nikita-rack
Tue, 21 Apr 2009 19:08:32 +0200
changeset 122 619465682697
parent 121 fc47ca00ad95
child 123 2c4be059fc8b
ajout de la creation de compte, il faut encore rendre le message un peu plus verbose en cas d'echec. Il faudrait aussi faire une connection auto peut etre.
res/layout/accountcreation.xml
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/ui/AccountCreation.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/layout/accountcreation.xml	Tue Apr 21 19:08:32 2009 +0200
@@ -0,0 +1,103 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              android:orientation="vertical"
+              android:layout_width="fill_parent"
+              android:layout_height="fill_parent"
+        >
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+                  android:orientation="horizontal"
+                  android:layout_width="fill_parent"
+                  android:layout_height="wrap_content"
+            >
+        <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Login:"
+                android:minWidth="70dp"
+                />
+                
+        <EditText android:id="@+id/login"
+                  android:layout_width="wrap_content"
+                  android:layout_height="wrap_content"
+                  android:singleLine="true"
+                  android:textSize="16sp"
+                  android:autoText="false"
+                  android:capitalize="none"
+                  android:minWidth="150dp"
+                  android:scrollHorizontally="true"/>
+    </LinearLayout>
+
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+                  android:orientation="horizontal"
+                  android:layout_width="fill_parent"
+                  android:layout_height="wrap_content"
+            >
+        <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Email:"
+                android:minWidth="70dp"
+                />
+        <EditText android:id="@+id/email"
+                  android:layout_width="wrap_content"
+                  android:layout_height="wrap_content"
+                  android:singleLine="true"
+                  android:textSize="16sp"
+                  android:autoText="false"
+                  android:minWidth="250dp"
+                  android:capitalize="none"
+                  android:scrollHorizontally="true"/>
+    </LinearLayout>
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+                  android:orientation="horizontal"
+                  android:layout_width="fill_parent"
+                  android:layout_height="wrap_content"
+            >
+        <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Password:"
+                android:minWidth="70dp"
+                />
+        <EditText android:id="@+id/password"
+                  android:layout_width="wrap_content"
+                  android:layout_height="wrap_content"
+                  android:singleLine="true"
+                  android:password="true"
+                  android:textSize="16sp"
+                  android:autoText="false"
+                  android:minWidth="250dp"
+                  android:capitalize="none"
+                  android:scrollHorizontally="true"/>
+    </LinearLayout>
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+                  android:orientation="horizontal"
+                  android:layout_width="fill_parent"
+                  android:layout_height="wrap_content"
+            >
+        <TextView
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="Repeat:"
+                android:minWidth="70dp"
+                />
+        <EditText android:id="@+id/password2"
+                  android:layout_width="wrap_content"
+                  android:layout_height="wrap_content"
+                  android:singleLine="true"
+                  android:password="true"
+                  android:textSize="16sp"
+                  android:autoText="false"
+                  android:minWidth="250dp"
+                  android:capitalize="none"
+                  android:scrollHorizontally="true"/>
+    </LinearLayout>
+
+    <Button android:id="@+id/ok"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_alignParentRight="true"
+            android:text="OK">
+        <requestFocus/>
+    </Button>
+</LinearLayout>
\ No newline at end of file
--- a/src/com/beem/project/beem/BeemService.java	Tue Apr 21 16:38:50 2009 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Tue Apr 21 19:08:32 2009 +0200
@@ -72,6 +72,7 @@
      */
     @Override
     public void onCreate() {
+	
 	super.onCreate();
 	mSettings = getSharedPreferences(getString(R.string.PreferenceFileName), MODE_PRIVATE);
 	mLogin = mSettings.getString(getString(R.string.PreferenceLoginKey), "");
@@ -182,6 +183,7 @@
 
 		}
 
+		
 		@Override
 		public void onConnect() throws RemoteException {
 		    // TODO Auto-generated method stub
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/ui/AccountCreation.java	Tue Apr 21 19:08:32 2009 +0200
@@ -0,0 +1,111 @@
+/**
+ * 
+ */
+package com.beem.project.beem.ui;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jivesoftware.smack.AccountManager;
+import org.jivesoftware.smack.ConnectionConfiguration;
+import org.jivesoftware.smack.XMPPConnection;
+import org.jivesoftware.smack.XMPPException;
+
+import com.beem.project.beem.R;
+
+import android.app.Activity;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.Toast;
+
+/**
+ * @author nikita
+ *
+ */
+public class AccountCreation extends Activity {    
+
+    protected static final String TAG = "AccountCreation";
+    private SharedPreferences mSettings;
+    Map<String, String> mAttributes = new HashMap<String, String>();
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+	super.onCreate(savedInstanceState);
+	setContentView(R.layout.accountcreation);
+	Button ok = (Button) findViewById(R.id.ok);
+	mSettings = getSharedPreferences(
+		getString(R.string.PreferenceFileName), MODE_PRIVATE);
+	//mAccountManager = new AccountManager();
+	ok.setOnClickListener(mOkListener);
+    }
+
+    private OnClickListener mOkListener = new OnClickListener() {
+	
+	@Override
+	public void onClick(View v) {
+	    boolean valid = true;
+	    if (getWidgetText(R.id.login).length() == 0) {
+		Log.d(TAG, "login pas ok");
+		valid = false;
+	    } else {
+		mAttributes.put("login", getWidgetText(R.id.login));
+		mAttributes.put("name", getWidgetText(R.id.login));
+	    }
+	    if (getWidgetText(R.id.password).length() == 0 || !getWidgetText(R.id.password).contains(getWidgetText(R.id.password2))) {
+		valid = false;
+		
+	    } else {
+		mAttributes.put("password", getWidgetText(R.id.password));
+	    }
+	    if (getWidgetText(R.id.email).length() == 0) {
+		valid = false;
+	    } else {
+		mAttributes.put("email", getWidgetText(R.id.email));
+	    }
+	    
+	    if (valid) {
+		setResult(RESULT_OK);
+		try {
+		    createAccount();
+		    Toast.makeText(AccountCreation.this, "Account created",
+	                    Toast.LENGTH_SHORT).show();
+		    finish();
+		} catch (XMPPException e) {
+		    Log.e(TAG, "Account creation failed", e);
+		    Toast.makeText(AccountCreation.this, e.getMessage(),
+	                    Toast.LENGTH_SHORT).show();
+		    e.printStackTrace();
+		}
+	    } else {
+		Toast.makeText(AccountCreation.this, "Form error",
+                    Toast.LENGTH_SHORT).show();
+		setResult(RESULT_CANCELED);
+	    }
+	    
+	}
+    };
+
+    protected void createAccount() throws XMPPException {
+	String mHost = mSettings.getString(getString(R.string.PreferenceHostKey), "");
+	XMPPConnection xmmpCo = new XMPPConnection(new ConnectionConfiguration(mHost));
+	xmmpCo.connect();
+	AccountManager accM = new AccountManager(xmmpCo);
+	accM.createAccount(mAttributes.get("login"), mAttributes.get("password"), mAttributes);
+	
+	SharedPreferences.Editor editor = mSettings.edit();
+	
+	editor.putString(getString(R.string.PreferenceLoginKey), mAttributes.get("login"));
+	editor.putString(getString(R.string.PreferencePasswordKey), mAttributes.get("password"));
+	editor.commit();	
+    }
+    
+    private String getWidgetText(int id) {
+	EditText widget = (EditText) this.findViewById(id);
+	return widget.getText().toString();
+    }
+}
\ No newline at end of file