Create an extern AsyncTask for the Login.
authorDa Risk <darisk972@gmail.com>
Fri, 08 Jan 2010 17:14:37 +0100
changeset 625 fd6d31c4460e
parent 624 c3585f53b86e
child 626 aa1bad380eec
Create an extern AsyncTask for the Login.
default.properties
src/com/beem/project/beem/service/LoginAsyncTask.java
src/com/beem/project/beem/service/XmppConnectionAdapter.java
src/com/beem/project/beem/service/aidl/IXmppConnection.aidl
src/com/beem/project/beem/ui/LoginAnim.java
--- a/default.properties	Fri Jan 08 11:27:34 2010 +0100
+++ b/default.properties	Fri Jan 08 17:14:37 2010 +0100
@@ -10,5 +10,5 @@
 # Indicates whether an apk should be generated for each density.
 split.density=false
 # Project target.
-target=android-6
+target=android-5
 apk-configurations=
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/com/beem/project/beem/service/LoginAsyncTask.java	Fri Jan 08 17:14:37 2010 +0100
@@ -0,0 +1,122 @@
+/*
+    BEEM is a videoconference application on the Android Platform.
+
+    Copyright (C) 2009 by Frederic-Charles Barthelery,
+                          Jean-Manuel Da Silva,
+                          Nikita Kozlov,
+                          Philippe Lago,
+                          Jean Baptiste Vergely,
+                          Vincent Veronis.
+
+    This file is part of BEEM.
+
+    BEEM is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    BEEM is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with BEEM.  If not, see <http://www.gnu.org/licenses/>.
+
+    Please send bug reports with examples or suggestions to
+    contact@beem-project.com or http://dev.beem-project.com/
+
+    Epitech, hereby disclaims all copyright interest in the program "Beem"
+    written by Frederic-Charles Barthelery,
+               Jean-Manuel Da Silva,
+               Nikita Kozlov,
+               Philippe Lago,
+               Jean Baptiste Vergely,
+               Vincent Veronis.
+
+    Nicolas Sadirac, November 26, 2009
+    President of Epitech.
+
+    Flavien Astraud, November 26, 2009
+    Head of the EIP Laboratory.
+
+*/
+package com.beem.project.beem.service;
+
+import android.os.AsyncTask;
+import android.os.RemoteException;
+import android.util.Log;
+import com.beem.project.beem.service.aidl.IXmppConnection;
+import com.beem.project.beem.service.aidl.IXmppFacade;
+
+/**
+ * This is an asynchronous task that will launch a connection to the XMPP server.
+ * @see android.os.AsyncTask
+ * @author Da Risk <da_risk@elyzion.net>
+ */
+public class LoginAsyncTask extends AsyncTask<IXmppFacade, Integer, Boolean> {
+
+    public static final String TAG = "BeemLoginTask";
+
+    public static final int STATE_CONNECTION_RUNNING = 1;
+    public static final int STATE_LOGIN_RUNNING = 2;
+    public static final int STATE_LOGIN_SUCCESS = 3;
+    public static final int STATE_LOGIN_FAILED = 4;
+
+    private IXmppConnection mConnection;
+    private String mErrorMessage;
+
+    /**
+     * Constructor.
+     */
+    public LoginAsyncTask() {
+    }
+
+    @Override
+    protected Boolean doInBackground(IXmppFacade... params) {
+	boolean result = true;
+	IXmppFacade facade = params[0];
+	try {
+	    publishProgress(STATE_CONNECTION_RUNNING);
+	    mConnection = facade.createConnection();
+	    if (!mConnection.connect()) {
+		mErrorMessage = mConnection.getErrorMessage();
+		return false;
+	    }
+	    publishProgress(STATE_LOGIN_RUNNING);
+
+	    if (!mConnection.login()) {
+		mErrorMessage = mConnection.getErrorMessage();
+		publishProgress(STATE_LOGIN_FAILED);
+		return false;
+	    }
+	    publishProgress(STATE_LOGIN_SUCCESS);
+	} catch (RemoteException e) {
+	    mErrorMessage= "Exception during connection :" + e;
+	    result = false;
+	}
+	return result;
+    }
+
+    /**
+     * Make sur to call the parent method when overriding this method.
+     */
+    @Override
+    protected void onCancelled() {
+	try {
+	    if (mConnection != null && mConnection.isAuthentificated()) {
+		mConnection.disconnect();
+	    }
+	} catch (RemoteException e) {
+	    Log.d(TAG, "Remote exception", e);
+	}
+    }
+
+    /**
+     * Get the error Message.
+     * @return the error message. null if no error
+     */
+    public String getErrorMessage() {
+	return mErrorMessage;
+    }
+}
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Fri Jan 08 11:27:34 2010 +0100
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Fri Jan 08 17:14:37 2010 +0100
@@ -100,6 +100,7 @@
     private PrivacyListManagerAdapter mPrivacyListManager;
     private final BeemService mService;
     private final RemoteCallbackList<IBeemConnectionListener> mRemoteConnListeners = new RemoteCallbackList<IBeemConnectionListener>();
+    private final SubscribePacketListener mSubscribePacketListener = new SubscribePacketListener();
 
     private final ConnexionListenerAdapter mConListener = new ConnexionListenerAdapter();
 
@@ -159,6 +160,70 @@
 	    mRemoteConnListeners.register(listen);
     }
 
+    @Override
+    public boolean connect() throws RemoteException {
+	if (mAdaptee.isConnected())
+	    return true;
+	else {
+	    try {
+		mAdaptee.connect();
+		mAdaptee.addConnectionListener(mConListener);
+		return true;
+	    } catch (XMPPException e) {
+		Log.e(TAG, "Error while connecting", e);
+		if (!"".equals(e.getMessage())) 
+		    mErrorMsg = e.getMessage();
+		else
+		    mErrorMsg = e.toString();
+	    }
+	    return false;
+	}
+    }
+
+    @Override
+    public boolean login() throws RemoteException {
+	if (mAdaptee.isAuthenticated())
+	    return true;
+	try {
+	    mAdaptee.login(mLogin, mPassword, mResource);
+	    mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService);
+	    mPrivacyListManager = new PrivacyListManagerAdapter(PrivacyListManager.getInstanceFor(mAdaptee));
+
+	    this.initFeatures(); // pour declarer les features xmpp qu'on
+	    // supporte
+	    ChatStateManager.getInstance(mAdaptee);
+
+	    PacketFilter filter = new PacketFilter() {
+
+		@Override
+		public boolean accept(Packet packet) {
+		    if (packet instanceof Presence) {
+			Presence pres = (Presence) packet;
+			if (pres.getType() == Presence.Type.subscribe)
+			    return true;
+		    }
+		    return false;
+		}
+	    };
+
+	    mAdaptee.addPacketListener(mSubscribePacketListener, filter);
+
+	    mService.resetStatus();
+	    mService.initJingle(mAdaptee);
+
+	  //  triggerAsynchronousConnectEvent();
+	    changeStatus(Status.CONTACT_STATUS_AVAILABLE, mService.getServicePreference().getString("status_text", ""));
+	    return true;
+	} catch (XMPPException e) {
+	    Log.e(TAG, "Error while connecting", e);
+	    if (!"".equals(e.getMessage())) 
+		mErrorMsg = e.getMessage();
+	    else
+		mErrorMsg = e.toString();
+	    return false;
+	}
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -185,39 +250,8 @@
      */
     @Override
     public boolean connectSync() throws RemoteException {
-	try {
-	    mAdaptee.connect();
-	    mAdaptee.addConnectionListener(mConListener);
-	    mAdaptee.login(mLogin, mPassword, mResource);
-
-	    mChatManager = new BeemChatManager(mAdaptee.getChatManager(), mService);
-	    mPrivacyListManager = new PrivacyListManagerAdapter(PrivacyListManager.getInstanceFor(mAdaptee));
-
-	    this.initFeatures(); // pour declarer les features xmpp qu'on
-	    // supporte
-	    ChatStateManager.getInstance(mAdaptee);
-
-	    triggerAsynchronousConnectEvent();
-	    changeStatus(Status.CONTACT_STATUS_AVAILABLE, mService.getServicePreference().getString("status_text", ""));
-	    return true;
-	} catch (XMPPException e) {
-	    Log.d(TAG, "Error while connecting", e);
-	    if (e.getXMPPError() != null && e.getXMPPError().getMessage() != null) {
-		mErrorMsg = e.getXMPPError().getMessage();
-		Log.d(TAG, "XMPP Error " + e.getXMPPError().getCode() + "message :" + e.getXMPPError().getMessage());
-	    } else if (e.getStreamError() != null) {
-		mErrorMsg = e.getStreamError().toString();
-		Log.d(TAG, "Stream Error " + e.getStreamError().getCode() + "message :" + e.getStreamError());
-	    } else if (e.getMessage() != null) {
-		// SASL !!
-		mErrorMsg = e.getMessage();
-		Log.d(TAG, "Error " + e.getMessage());
-	    } else
-		mErrorMsg = "Error On Connection";
-	    mConListener.connectionFailed(mErrorMsg);
-	} catch (IllegalStateException e) {
-	    mConListener.connectionFailed(e.getMessage());
-	}
+	if (connect())
+	    return login();
 	return false;
     }
 
@@ -281,7 +315,6 @@
 	mStatusNotification.setLatestEventInfo(mService, "Beem Status", text, PendingIntent.getActivity(mService, 0,
 	    new Intent(mService, ChangeStatus.class), 0));
 	mService.sendNotification(BeemService.NOTIFICATION_STATUS_ID, mStatusNotification);
-
     }
 
     /**
@@ -608,4 +641,31 @@
 	}
     }
 
+    /**
+     * This PacketListener will set a notification when you got a subscribtion request.
+     * @author Da Risk <da_risk@elyzion.net>
+     */
+    private class SubscribePacketListener implements PacketListener {
+	@Override
+	public void processPacket(Packet packet) {
+	    if ( ! (packet instanceof Presence))
+		return;
+	    Presence p = (Presence) packet;
+	    if (p.getType() != Presence.Type.subscribe)
+		return;
+	    String from = p.getFrom();
+	    Notification notif = new Notification(android.R.drawable.stat_notify_more, mService.getString(
+		R.string.AcceptContactRequest, from), System.currentTimeMillis());
+	    notif.defaults = Notification.DEFAULT_ALL;
+	    notif.flags = Notification.FLAG_AUTO_CANCEL;
+	    Intent intent = new Intent(mService, Subscription.class);
+	    intent.putExtra("from", from);
+	    notif.setLatestEventInfo(mService, from, mService
+		.getString(R.string.AcceptContactRequestFrom, from), PendingIntent.getActivity(mService, 0,
+		    intent, PendingIntent.FLAG_ONE_SHOT));
+	    int id = p.hashCode();
+	    mService.sendNotification(id, notif);
+	}
+    }
+
 }
--- a/src/com/beem/project/beem/service/aidl/IXmppConnection.aidl	Fri Jan 08 11:27:34 2010 +0100
+++ b/src/com/beem/project/beem/service/aidl/IXmppConnection.aidl	Fri Jan 08 17:14:37 2010 +0100
@@ -50,6 +50,10 @@
 
 interface IXmppConnection {
 
+    boolean connect();
+
+    boolean login();
+
     boolean connectSync();
 
     void connectAsync();
--- a/src/com/beem/project/beem/ui/LoginAnim.java	Fri Jan 08 11:27:34 2010 +0100
+++ b/src/com/beem/project/beem/ui/LoginAnim.java	Fri Jan 08 17:14:37 2010 +0100
@@ -63,6 +63,7 @@
 import com.beem.project.beem.R;
 import com.beem.project.beem.service.aidl.IXmppConnection;
 import com.beem.project.beem.service.aidl.IXmppFacade;
+import com.beem.project.beem.service.LoginAsyncTask;
 
 /**
  * This class is an activity which display an animation during the connection with the server.
@@ -79,7 +80,7 @@
     private Animation mRotateAnim;
     private final ServiceConnection mServConn = new LoginServiceConnection();
     private IXmppFacade mXmppFacade;
-    private AsyncTask<IXmppFacade, Void, Boolean> mTask;
+    private AsyncTask<IXmppFacade, Integer, Boolean> mTask;
     private Button mCancelBt;
 
     /**
@@ -175,36 +176,12 @@
     /**
      * Asynchronous class for connection.
      */
-    class LoginTask extends AsyncTask<IXmppFacade, Void, Boolean> {
-
-	private IXmppConnection mConnection;
-	private String mMsg;
+    private class LoginTask extends LoginAsyncTask{
 
 	/**
-     * Constructor.
-     */
-	LoginTask() {
-	}
-
-	/* (non-Javadoc)
-	 * @see android.os.AsyncTask#doInBackground(Params[])
+	 * Constructor.
 	 */
-	@Override
-	protected Boolean doInBackground(IXmppFacade... params) {
-	    boolean result = true;
-	    IXmppFacade facade = params[0];
-	    try {
-		mConnection = facade.createConnection();
-		if (!mConnection.isAuthentificated()) {
-		    result = mConnection.connectSync();
-		    if (!result)
-			mMsg = mConnection.getErrorMessage();
-		}
-	    } catch (RemoteException e) {
-		mMsg = "Exception during connection";
-		result = false;
-	    }
-	    return result;
+	LoginTask() {
 	}
 
 	/* (non-Javadoc)
@@ -216,7 +193,7 @@
 	    if (result == null || !result) { // Task cancelled or exception
 		if (!result) {
 		    Intent i = new Intent();
-		    i.putExtra("message", mMsg);
+		    i.putExtra("message", getErrorMessage());
 		    LoginAnim.this.setResult(Activity.RESULT_CANCELED, i);
 		} else
 		    LoginAnim.this.setResult(Activity.RESULT_CANCELED);
@@ -229,18 +206,17 @@
 	    }
 	}
 
+	@Override
+	protected void onProgressUpdate(Integer ... values) {
+	    Log.d(TAG, "onProgress " + values[0]);
+	}
+
 	/* (non-Javadoc)
 	 * @see android.os.AsyncTask#onCancelled()
 	 */
 	@Override
 	protected void onCancelled() {
-	    try {
-		if (mConnection != null && mConnection.isAuthentificated()) {
-		    mConnection.disconnect();
-		}
-	    } catch (RemoteException e) {
-		Log.d(TAG, "Remote exception", e);
-	    }
+	    super.onCancelled();
 	    LoginAnim.this.stopService(LoginAnim.SERVICE_INTENT);
 	}