L'activite login anim. reussi a lancer une connection en asynchrone :)
authorDa Risk <darisk972@gmail.com>
Sat, 21 Nov 2009 01:18:23 +0100
changeset 541 a4bbfbfb586c
parent 540 45ee3f1b43ab
child 542 23e8095cace5
L'activite login anim. reussi a lancer une connection en asynchrone :)
src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl
src/com/beem/project/beem/ui/LoginAnim.java
--- a/src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl	Fri Nov 20 21:44:52 2009 +0100
+++ b/src/com/beem/project/beem/service/aidl/IBeemConnectionListener.aidl	Sat Nov 21 01:18:23 2009 +0100
@@ -10,35 +10,35 @@
      *  Callback to call when the connection is closed
      */
     void connectionClosed();
-    
+
     /**
      *  Callback to call when the connection occurs
      */
     void onConnect();
-    
+
     //void connectionClosedOnError(in Exception e);
     /**
      *  Callback to call when the connection is closed on error
      */
     void connectionClosedOnError();
-     
+
     /**
      * Callback to call when trying to reconnecting
      */
     void reconnectingIn(in int seconds);
-    
+
     /**
      *  Callback to call when the reconnection has failed
      */
     void reconnectionFailed();
-     
+
     /**
      *  Callback to call when the reconnection is successfull
-     */ 
+     */
     void reconnectionSuccessful();
-    
+
     /**
      *  Callback to call when the connection Failed
-     */ 
+     */
     void connectionFailed(in String errorMsg);
 }
--- a/src/com/beem/project/beem/ui/LoginAnim.java	Fri Nov 20 21:44:52 2009 +0100
+++ b/src/com/beem/project/beem/ui/LoginAnim.java	Sat Nov 21 01:18:23 2009 +0100
@@ -1,11 +1,21 @@
 package com.beem.project.beem.ui;
 
 import android.app.Activity;
+import android.content.ServiceConnection;
+import android.content.ComponentName;
+import android.content.Intent;
 import android.os.Bundle;
+import android.os.AsyncTask;
+import android.os.IBinder;
+import android.os.RemoteException;
 import android.view.animation.Animation;
 import android.widget.ImageView;
 import android.view.animation.AnimationUtils;
 import com.beem.project.beem.R;
+import android.util.Log;
+import com.beem.project.beem.service.aidl.IXmppFacade;
+import com.beem.project.beem.service.aidl.IXmppConnection;
+import com.beem.project.beem.service.aidl.IXmppFacade;
 
 /**
  * This class is an activity which display an animation during the connection
@@ -14,8 +24,17 @@
  */
 public class LoginAnim extends Activity {
 
+    private static final String TAG = "LoginAnim";
+    private static final Intent SERVICE_INTENT = new Intent();
+    static {
+	SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
+    }
     private ImageView mLogo;
     private Animation mRotateAnim;
+    private final ServiceConnection mServConn = new LoginServiceConnection();
+    private IXmppFacade mXmppFacade;
+    private AsyncTask<IXmppFacade, Void, Boolean>mTask;
+
     /**
      * Constructor.
      */
@@ -42,4 +61,69 @@
         mLogo.startAnimation(mRotateAnim);
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected void onResume() {
+	super.onResume();
+	if (mTask == null) {
+	    mTask = new LoginTask();
+	}
+	if (mXmppFacade == null) {
+	    bindService(LoginAnim.SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
+	}
+    }
+
+    @Override
+    protected void onPause() {
+	super.onPause();
+	if (mXmppFacade != null) { // and async task not en cours
+	    unbindService(mServConn);
+	    mXmppFacade = null;
+	}
+    }
+
+    class LoginTask extends AsyncTask<IXmppFacade, Void, Boolean> {
+
+	@Override
+	protected Boolean doInBackground(IXmppFacade ... params) {
+	    boolean result = true;
+	    IXmppFacade facade = params[0];
+	    try {
+		IXmppConnection connect = facade.createConnection();
+		if (!connect.isAuthentificated()) {
+		    result = connect.connectSync();
+		    if (!result) {
+			// set bad message ?
+		    }
+		}
+	    } catch (RemoteException e) {
+		Log.d(TAG, "Error while connecting", e);
+		// set bad message ?
+		result = false;
+	    }
+	    Log.d(TAG, "Connection result? " + result);
+	    return result;
+	}
+    }
+
+    private class LoginServiceConnection implements ServiceConnection {
+
+	public LoginServiceConnection() {
+	}
+
+	@Override
+	public void onServiceConnected(ComponentName name, IBinder service) {
+	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
+	    Log.d(TAG, "Launch the task");
+	    mTask = mTask.execute(mXmppFacade);
+	}
+	
+	@Override
+	public void onServiceDisconnected(ComponentName name) {
+	    Log.d(TAG, "Service disconnected.");
+	    mXmppFacade = null;
+	}
+    }
 }