Show a progress dialog during connection with the server
authorDa Risk <darisk972@gmail.com>
Mon, 06 Apr 2009 18:12:30 +0200
changeset 72 cdedc3a25d39
parent 68 0c76c67a2b99
child 73 4d9067c5e08e
Show a progress dialog during connection with the server
src/com/beem/project/beem/BeemApplication.java
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/service/XmppFacade.java
src/com/beem/project/beem/service/aidl/IXmppFacade.aidl
src/com/beem/project/beem/ui/ContactList.java
src/com/beem/project/beem/ui/ContactListDialogSettings.java
--- a/src/com/beem/project/beem/BeemApplication.java	Sat Apr 04 19:02:11 2009 +0200
+++ b/src/com/beem/project/beem/BeemApplication.java	Mon Apr 06 18:12:30 2009 +0200
@@ -8,6 +8,7 @@
 
 import android.app.Activity;
 import android.app.Application;
+import android.app.ProgressDialog;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -17,22 +18,32 @@
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Message;
+import android.os.RemoteException;
 
+import com.beem.project.beem.service.aidl.IBeemConnectionListener;
+import com.beem.project.beem.service.aidl.IXmppConnection;
 import com.beem.project.beem.service.aidl.IXmppFacade;
 
 /**
- * The Beem application.
- * This class has some methods utiliy needs by the activities.
+ * The Beem application. This class has some methods utiliy needs by the activities.
  * @author darisk
  */
 public class BeemApplication extends Application {
 
     private static BeemApplication mBeemApp;
+    private static final Intent SERVICE_INTENT = new Intent();
     private IXmppFacade mFacade;
     private Context mApplicationContext;
     private Resources mPrivateResources;
     private List<Message> mQueue = new LinkedList<Message>();
     private boolean mIsConnected;
+    private IXmppConnection mConnection;
+    private ProgressDialog mProgressDialog;
+    private ConnectionListener mConnectionListener = new ConnectionListener();
+
+    static {
+	SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
+    }
 
     private ServiceConnection mServConn = new ServiceConnection() {
 
@@ -44,8 +55,19 @@
 
 	@Override
 	public void onServiceConnected(ComponentName name, IBinder service) {
-	    // TODO Auto-generated method stub
+	    mIsConnected = true;
 	    mFacade = IXmppFacade.Stub.asInterface(service);
+	    try {
+		mConnection = mFacade.createConnection();
+		if (!mConnection.isAuthentificated()) {
+		    mProgressDialog.show();
+		    mConnection.addConnectionListener(mConnectionListener);
+		    mApplicationContext.startService(BeemApplication.SERVICE_INTENT);
+		}
+	    } catch (RemoteException e) {
+		// TODO Auto-generated catch block
+		e.printStackTrace();
+	    }
 	    synchronized (mQueue) {
 		for (Message msg : mQueue) {
 		    msg.sendToTarget();
@@ -64,67 +86,27 @@
 
     /**
      * Get the Beem application for an activity.
-     * @param activity	the activity which want the Beem application
-     * @return		the Beem application
+     * @param activity the activity which want the Beem application
+     * @return the Beem application
      */
     public static BeemApplication getApplication(Activity activity) {
 	if (mBeemApp == null) {
 	    mBeemApp = new BeemApplication();
-	    mBeemApp.mApplicationContext = activity.getApplication();
-	    mBeemApp.mPrivateResources = activity.getResources();
-	    mBeemApp.onCreate();
 	}
+	mBeemApp.mProgressDialog = new ProgressDialog(activity);
+	mBeemApp.mApplicationContext = activity.getApplication();
+	mBeemApp.mPrivateResources = activity.getResources();
+	mBeemApp.onCreate();
 	return mBeemApp;
     }
 
     /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onConfigurationChanged(Configuration newConfig) {
-	// TODO Auto-generated method stub
-	super.onConfigurationChanged(newConfig);
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onCreate() {
-	// TODO Auto-generated method stub
-	super.onCreate();
-	mFacade = null;
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onLowMemory() {
-	// TODO Auto-generated method stub
-	super.onLowMemory();
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void onTerminate() {
-	// TODO Auto-generated method stub
-	super.onTerminate();
-    }
-
-    /**
      * Start the beem service.
      */
     public synchronized void startBeemService() {
 	if (!mIsConnected) {
-	    // Intent intent = new Intent(this, BeemService.class);
-	    Intent intent = new Intent();
-	    intent.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
-	    mApplicationContext.startService(intent);
-	    mApplicationContext.bindService(intent, mServConn, BIND_AUTO_CREATE);
-	    mIsConnected = true;
+	    // the connection will be made on service connect
+	    mApplicationContext.bindService(BeemApplication.SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
 	}
     }
 
@@ -134,8 +116,7 @@
     public synchronized void stopBeemService() {
 	if (mIsConnected) {
 	    Intent intent = new Intent();
-	    intent.setComponent(new ComponentName("com.beem.project.beem",
-		    "com.beem.project.beem.BeemService"));
+	    intent.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
 	    mApplicationContext.unbindService(mServConn);
 	    mApplicationContext.stopService(intent);
 	    mIsConnected = false;
@@ -144,17 +125,16 @@
 
     /**
      * Get the facade to use to access the Beem service.
-     * @return	the facade or null if the application is not connected to the beem service.
+     * @return the facade or null if the application is not connected to the beem service.
      */
     public IXmppFacade getXmppFacade() {
 	return mFacade;
     }
 
-
     /**
      * Add a methode to execute when the application is connected to the Beem service.
-     * @param target	the handler which will execute the callback
-     * @param callback	the callback to execute
+     * @param target the handler which will execute the callback
+     * @param callback the callback to execute
      */
     public void callWhenServiceConnected(Handler target, Runnable callback) {
 	Message msg = Message.obtain(target, callback);
@@ -169,10 +149,81 @@
     }
 
     /**
-     * Indique si on est connecte au service.
+     * Tell if we are connected with the Beem Service.
+     * @return true if connected false otherwise
      */
     public boolean isConnected() {
 	return mIsConnected;
     }
 
+    /**
+     * Connection listener use to hide the progress dialog.
+     * @author darisk
+     */
+    private class ConnectionListener extends IBeemConnectionListener.Stub {
+
+	/**
+	 * Constructor.
+	 */
+	public ConnectionListener() {
+
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void connectionClosed() throws RemoteException {
+	    // TODO Auto-generated method stub
+
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void connectionClosedOnError() throws RemoteException {
+	    // TODO Auto-generated method stub
+	    // lancer l'activity signin en lui passant le message d'erreur ^^
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void onConnect() throws RemoteException {
+	    // TODO Auto-generated method stub
+	    mProgressDialog.dismiss();
+	    mFacade.changeStatus();
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void reconnectingIn(int seconds) throws RemoteException {
+	    // TODO Auto-generated method stub
+	    mProgressDialog.show();
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void reconnectionFailed() throws RemoteException {
+	    // TODO Auto-generated method stub
+
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void reconnectionSuccessful() throws RemoteException {
+	    // TODO Auto-generated method stub
+
+	}
+
+    }
+
 }
--- a/src/com/beem/project/beem/BeemService.java	Sat Apr 04 19:02:11 2009 +0200
+++ b/src/com/beem/project/beem/BeemService.java	Mon Apr 06 18:12:30 2009 +0200
@@ -12,31 +12,44 @@
 
 import com.beem.project.beem.service.XmppConnectionAdapter;
 import com.beem.project.beem.service.XmppFacade;
+import com.beem.project.beem.service.aidl.IBeemConnectionListener;
 import com.beem.project.beem.service.aidl.IXmppFacade;
 import com.beem.project.beem.service.aidl.IXmppConnection;
+
 /**
  * This class is for the Beem service.
+ * The connection to the xmpp server will be made asynchronously when the service
+ * will start.
  * @author darisk
- *
  */
 public class BeemService extends Service {
 
+    /**
+     * The id to use for status notification.
+     */
+    public static final int NOTIFICATION_STATUS_ID = 1;
+
     private NotificationManager mNotificationManager;
-
     private IXmppConnection mConnection;
     private SharedPreferences mSettings;
     private String mLogin;
     private String mPassword;
     private String mHost;
 
+
     private IXmppFacade.Stub mBind;
 
     /**
+     * Constructor.
+     */
+    public BeemService() {
+    }
+
+    /**
      * {@inheritDoc}
      */
     @Override
     public IBinder onBind(Intent intent) {
-	showBasicNotification(R.string.BeemServiceCreated);
 	return mBind;
 	// to forbid a client to bind
 	// return null;
@@ -48,15 +61,13 @@
     @Override
     public void onCreate() {
 	super.onCreate();
-	mSettings = getSharedPreferences(getString(R.string.PreferenceFileName),
-		MODE_PRIVATE);
+	mSettings = getSharedPreferences(getString(R.string.PreferenceFileName), MODE_PRIVATE);
 	mLogin = mSettings.getString(getString(R.string.PreferenceLoginKey), "");
-	mPassword = mSettings.getString(
-		getString(R.string.PreferencePasswordKey), "");
+	mPassword = mSettings.getString(getString(R.string.PreferencePasswordKey), "");
 	mHost = mSettings.getString(getString(R.string.PreferenceHostKey), "");
 	mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 	mConnection = new XmppConnectionAdapter("10.0.2.2", mLogin, mPassword); // address
-	mBind = new XmppFacade((XmppConnectionAdapter) mConnection);
+	mBind = new XmppFacade(mConnection, this);
     }
 
     /**
@@ -65,7 +76,7 @@
     @Override
     public void onStart(Intent intent, int startId) {
 	try {
-	    mConnection.connectSync();
+	    mConnection.connectAsync();
 	} catch (RemoteException e) {
 	    // TODO Auto-generated catch block
 	    e.printStackTrace();
@@ -78,7 +89,7 @@
     @Override
     public void onDestroy() {
 	closeConnection();
-	showBasicNotification(R.string.BeemServiceDestroyed);
+	mNotificationManager.cancel(NOTIFICATION_STATUS_ID);
     }
 
     /**
@@ -95,21 +106,11 @@
     }
 
     /**
-     * Add a notification in the notification status bar.
-     * @param stringResource	the ressource of the text to show
+     * Show a notification.
+     * @param id 	the id of the notification.
+     * @param notif	the notification to show
      */
-    private void showBasicNotification(int stringResource) {
-        String text = (String) getText(stringResource);
-        Notification notif = new Notification(R.drawable.logo, text, System
-                .currentTimeMillis());
-        notif.defaults = Notification.DEFAULT_ALL;
-        notif.setLatestEventInfo(this, text, text, PendingIntent.getActivity(
-                this, 0, new Intent(), 0));
-        mNotificationManager.notify(stringResource, notif);
-        Toast toast = Toast.makeText(this, R.string.BeemServiceCreated,
-                Toast.LENGTH_LONG);
-        toast.show();
+    public void sendNotification(int id, Notification notif) {
+	mNotificationManager.notify(id, notif);
     }
-
-
 }
--- a/src/com/beem/project/beem/service/XmppFacade.java	Sat Apr 04 19:02:11 2009 +0200
+++ b/src/com/beem/project/beem/service/XmppFacade.java	Mon Apr 06 18:12:30 2009 +0200
@@ -1,10 +1,14 @@
 package com.beem.project.beem.service;
+
+import android.app.Notification;
+import android.app.PendingIntent;
+import android.content.Intent;
 import android.os.RemoteException;
-
+import com.beem.project.beem.BeemService;
 import com.beem.project.beem.service.aidl.IChatManager;
 import com.beem.project.beem.service.aidl.IRoster;
 import com.beem.project.beem.service.aidl.IXmppConnection;
-import com.beem.project.beem.service.aidl.IXmppFacade;;
+import com.beem.project.beem.service.aidl.IXmppFacade;
 
 /**
  * This class is a facade for the Beem Service.
@@ -12,14 +16,17 @@
  */
 public class XmppFacade extends IXmppFacade.Stub {
 
-    private XmppConnectionAdapter mConnexion;
+    private IXmppConnection mConnexion;
+    private BeemService mBeemService;
 
     /**
      * Constructor for XMPPFacade.
      * @param connection the connection use by the facade
+     * @param service the service which holds the facade
      */
-    public XmppFacade(final XmppConnectionAdapter connection) {
+    public XmppFacade(final IXmppConnection connection, final BeemService service) {
 	this.mConnexion = connection;
+	this.mBeemService = service;
     }
 
     /**
@@ -70,4 +77,20 @@
 	return mConnexion.getChatManager();
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public void changeStatus() {
+	Notification mStatusNotification;
+	String text = "Salut les amirs !!!!";
+	mStatusNotification = new Notification(com.beem.project.beem.R.drawable.logo, text, System.currentTimeMillis());
+	mStatusNotification.defaults = Notification.DEFAULT_ALL;
+	mStatusNotification.flags = Notification.FLAG_NO_CLEAR;
+
+	// TODO
+	// mStatusNotification.contentView = ;
+	mStatusNotification.setLatestEventInfo(mBeemService, "Beem Status", text, PendingIntent.getActivity(
+	    mBeemService, 0, new Intent(), 0));
+	mBeemService.sendNotification(BeemService.NOTIFICATION_STATUS_ID, mStatusNotification);
+    }
 }
--- a/src/com/beem/project/beem/service/aidl/IXmppFacade.aidl	Sat Apr 04 19:02:11 2009 +0200
+++ b/src/com/beem/project/beem/service/aidl/IXmppFacade.aidl	Mon Apr 06 18:12:30 2009 +0200
@@ -18,4 +18,7 @@
 
     IChatManager getChatManager();
     
+    // to ameliore
+    void changeStatus();
+    
 }
--- a/src/com/beem/project/beem/ui/ContactList.java	Sat Apr 04 19:02:11 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactList.java	Mon Apr 06 18:12:30 2009 +0200
@@ -88,6 +88,7 @@
     protected void onStart() {
 	super.onStart();
 	Log.i(TAG, "onStart");
+	//mBeemApplication.startBeemService();
     }
 
     @Override
--- a/src/com/beem/project/beem/ui/ContactListDialogSettings.java	Sat Apr 04 19:02:11 2009 +0200
+++ b/src/com/beem/project/beem/ui/ContactListDialogSettings.java	Mon Apr 06 18:12:30 2009 +0200
@@ -1,5 +1,6 @@
 package com.beem.project.beem.ui;
 
+import android.app.Activity;
 import android.app.Dialog;
 import android.content.Context;
 import android.content.SharedPreferences;
@@ -19,12 +20,10 @@
 	android.view.View.OnClickListener {
 
     private SharedPreferences mSettings;
-    private ContactList mContactList;
 
-    public ContactListDialogSettings(ContactList contactList,
+    public ContactListDialogSettings(Activity activity,
 	    SharedPreferences settings) {
-	super(contactList);
-	this.mContactList = contactList;
+	super(activity);
 	this.mSettings = settings;
     }
 
@@ -54,6 +53,7 @@
 	String JID = ctx.getString(R.string.PreferenceLoginKey) + "@"
 		+ ctx.getString(R.string.PreferenceHostKey);
 	editor.putString(ctx.getString(R.string.PreferenceJID), JID);
+	editor.putBoolean("ACCOUNT_CONFIGURED", true);
 	editor.commit();
 	dismiss();
     }