Suppression beemapplication dans la notification changestatus.
authormarseille@marseille-desktop
Thu, 18 Jun 2009 20:51:12 +0200
changeset 242 11f8de688fcb
parent 241 a81519fc4024
child 243 644ead6eaf67
Suppression beemapplication dans la notification changestatus.
src/com/beem/project/beem/BeemApplication.java
src/com/beem/project/beem/ui/ChangeStatus.java
--- a/src/com/beem/project/beem/BeemApplication.java	Thu Jun 18 20:22:56 2009 +0200
+++ b/src/com/beem/project/beem/BeemApplication.java	Thu Jun 18 20:51:12 2009 +0200
@@ -30,111 +30,7 @@
  */
 public class BeemApplication extends Application {
 
-    /**
-     * 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 {
-	    Log.e(TAG, "Connection Close");
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public void connectionClosedOnError() throws RemoteException {
-	    Log.e(TAG, "ConnectionClosedOnError");
-	    // TODO afficher une notification et reafficher le progress dialog
-	}
-
-	@Override
-	public void connectionFailed(String errorMsg) throws RemoteException {
-	    Log.i(TAG, "Connection Failed");
-	    ConnectionRunnable cRun = new ConnectionRunnable(errorMsg);
-	    mBeemApp.mActivity.runOnUiThread(cRun);
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public void onConnect() throws RemoteException {
-	    // TODO Auto-generated method stub
-	    // TODO recuperer les informations de status dans les preferences
-	    mFacade.changeStatus(Status.CONTACT_STATUS_AVAILABLE, null);
-	    synchronized (mQueue) {
-		for (Message msg : mQueue) {
-		    msg.sendToTarget();
-		}
-		mQueue.clear();
-	    }
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public void reconnectingIn(int seconds) throws RemoteException {
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public void reconnectionFailed() throws RemoteException {
-	}
-
-	/**
-	 * {@inheritDoc}
-	 */
-	@Override
-	public void reconnectionSuccessful() throws RemoteException {
-	}
-
-    }
-
-    private class ConnectionRunnable implements Runnable {
-
-	private String mErrorMsg;
-
-	public ConnectionRunnable(String string) {
-	    this.mErrorMsg = string;
-	}
-
-	/**
-	 * @return the mErrorMsg
-	 */
-	public String getMErrorMsg() {
-	    return mErrorMsg;
-	}
-
-	@Override
-	public void run() {
-	}
-
-	/**
-	 * @param mErrorMsg
-	 *            the mErrorMsg to set
-	 */
-	public void setMErrorMsg(String mErrorMsg) {
-	    this.mErrorMsg = mErrorMsg;
-	}
-
-    }
-
+ 
     private static final Intent    SERVICE_INTENT = new Intent();
     private static BeemApplication mBeemApp;
     static {
@@ -282,4 +178,110 @@
 	}
     }
 
+    /**
+     * 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 {
+	    Log.e(TAG, "Connection Close");
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void connectionClosedOnError() throws RemoteException {
+	    Log.e(TAG, "ConnectionClosedOnError");
+	    // TODO afficher une notification et reafficher le progress dialog
+	}
+
+	@Override
+	public void connectionFailed(String errorMsg) throws RemoteException {
+	    Log.i(TAG, "Connection Failed");
+	    ConnectionRunnable cRun = new ConnectionRunnable(errorMsg);
+	    mBeemApp.mActivity.runOnUiThread(cRun);
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void onConnect() throws RemoteException {
+	    // TODO Auto-generated method stub
+	    // TODO recuperer les informations de status dans les preferences
+	    mFacade.changeStatus(Status.CONTACT_STATUS_AVAILABLE, null);
+	    synchronized (mQueue) {
+		for (Message msg : mQueue) {
+		    msg.sendToTarget();
+		}
+		mQueue.clear();
+	    }
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void reconnectingIn(int seconds) throws RemoteException {
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void reconnectionFailed() throws RemoteException {
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public void reconnectionSuccessful() throws RemoteException {
+	}
+
+    }
+
+    private class ConnectionRunnable implements Runnable {
+
+	private String mErrorMsg;
+
+	public ConnectionRunnable(String string) {
+	    this.mErrorMsg = string;
+	}
+
+	/**
+	 * @return the mErrorMsg
+	 */
+	public String getMErrorMsg() {
+	    return mErrorMsg;
+	}
+
+	@Override
+	public void run() {
+	}
+
+	/**
+	 * @param mErrorMsg
+	 *            the mErrorMsg to set
+	 */
+	public void setMErrorMsg(String mErrorMsg) {
+	    this.mErrorMsg = mErrorMsg;
+	}
+
+    }
+
+    
 }
--- a/src/com/beem/project/beem/ui/ChangeStatus.java	Thu Jun 18 20:22:56 2009 +0200
+++ b/src/com/beem/project/beem/ui/ChangeStatus.java	Thu Jun 18 20:51:12 2009 +0200
@@ -1,13 +1,14 @@
 package com.beem.project.beem.ui;
 
 import android.app.Activity;
+import android.content.ComponentName;
 import android.content.Intent;
+import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.os.Bundle;
-import android.os.Handler;
+import android.os.IBinder;
 import android.os.RemoteException;
-import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.ArrayAdapter;
@@ -16,7 +17,6 @@
 import android.widget.TextView;
 import android.widget.Toast;
 
-import com.beem.project.beem.BeemApplication;
 import com.beem.project.beem.BeemService;
 import com.beem.project.beem.R;
 import com.beem.project.beem.service.aidl.IXmppFacade;
@@ -24,80 +24,26 @@
 
 public class ChangeStatus extends Activity {
 
-    private TextView              mStatusText;
-    private Button                mOk;
-    private Button                mClear;
-    private Handler               mHandler;
-    private BeemApplication       mBeemApplication;
-    private IXmppFacade           mService               = null;
-    private Spinner               mSpinner;
-    private static final String[] STATUS                 = { "Available for chat", "Available", "Busy", "Away",
-	    "Unavailable", "Disconnected"               };
-    private static final int      DISCONNECTED_IDX       = 5;
-    private static final int      AVAILABLE_FOR_CHAT_IDX = 0;
-    private static final int      AVAILABLE_IDX          = 1;
-    private static final int      BUSY_IDX               = 2;
-    private static final int      AWAY_IDX               = 3;
-    private static final int      UNAVAILABLE_IDX        = 4;
-    private SharedPreferences     mSettings;
-    private ArrayAdapter<String>  mAdapter;
-    private Toast                 mToast;
-    private static Integer        nb                     = 0;
-
-    private final OnClickListener       mOnClickOk             = new OnClickListener() {
-
-	                                                     public void onClick(View v) {
-		                                                 if (v == mOk) {
-		                                                     if (textHasChanged() || statusHasChanged()) {
-			                                                 String msg = mStatusText.getText().toString();
-			                                                 int status = getStatusForService((String) mSpinner
-			                                                         .getSelectedItem());
-			                                                 Editor edit = mSettings.edit();
-			                                                 edit
-			                                                         .putString(
-			                                                                 getString(R.string.PreferenceStatusText),
-			                                                                 msg);
-			                                                 edit.putInt(
-			                                                         getString(R.string.PreferenceStatus),
-			                                                         mSpinner.getSelectedItemPosition());
-			                                                 edit.commit();
-			                                                 if (status == Status.CONTACT_STATUS_DISCONNECT) {
-			                                                     stopService(new Intent(ChangeStatus.this,
-			                                                             BeemService.class));
-			                                                 } else {
-			                                                     try {
-				                                                 mService.changeStatus(status, msg
-				                                                         .toString());
-			                                                     } catch (RemoteException e) {
-				                                                 // TODO
-				                                                 // Auto-generated
-				                                                 // catch block
-				                                                 e.printStackTrace();
-			                                                     }
-			                                                     mToast.show();
-			                                                 }
-		                                                     }
-		                                                     ChangeStatus.this.finish();
-		                                                 } else if (v == mClear) {
-		                                                     mStatusText.setText(null);
-		                                                 }
-
-	                                                     }
-
-	                                                     private boolean statusHasChanged() {
-		                                                 return (mSettings.getInt(
-		                                                         getString(R.string.PreferenceStatus), 0) != mSpinner
-		                                                         .getSelectedItemPosition());
-	                                                     }
-
-	                                                     private boolean textHasChanged() {
-		                                                 return (!mStatusText
-		                                                         .getText()
-		                                                         .toString()
-		                                                         .equals(
-		                                                                 getPreferenceString(R.string.PreferenceStatusText)));
-	                                                     }
-	                                                 };
+    private TextView mStatusText;
+    private Toast mToast;
+    private Button mOk;
+    private Button mClear;
+    private Spinner mSpinner;
+    
+    private static final String[] STATUS = { "Available for chat", "Available", "Busy", "Away", "Unavailable",
+	"Disconnected" };
+    private static final int DISCONNECTED_IDX = 5;
+    private static final int AVAILABLE_FOR_CHAT_IDX = 0;
+    private static final int AVAILABLE_IDX = 1;
+    private static final int BUSY_IDX = 2;
+    private static final int AWAY_IDX = 3;
+    private static final int UNAVAILABLE_IDX = 4;
+    
+    private SharedPreferences mSettings;
+    private ArrayAdapter<String> mAdapter;
+    private IXmppFacade xmppFacade = null;
+    private final ServiceConnection mServConn = new BeemServiceConnection();
+    private final OnClickListener mOnClickOk = new MyOnClickListener();
 
     private int getPreferenceStatusIndex() {
 	return mSettings.getInt(getString(R.string.PreferenceStatus), 0);
@@ -113,21 +59,21 @@
 	    String str = ChangeStatus.STATUS[i];
 	    if (str.equals(item)) {
 		switch (i) {
-		case ChangeStatus.DISCONNECTED_IDX:
-		    return Status.CONTACT_STATUS_DISCONNECT;
-		case ChangeStatus.AVAILABLE_FOR_CHAT_IDX:
-		    return Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT;
-		case ChangeStatus.AVAILABLE_IDX:
-		    return Status.CONTACT_STATUS_AVAILABLE;
-		case ChangeStatus.AWAY_IDX:
-		    return Status.CONTACT_STATUS_AWAY;
-		case ChangeStatus.BUSY_IDX:
-		    return Status.CONTACT_STATUS_BUSY;
-		case ChangeStatus.UNAVAILABLE_IDX:
-		    return Status.CONTACT_STATUS_UNAVAILABLE;
-		default:
-		    res = Status.CONTACT_STATUS_AVAILABLE;
-		    break;
+		    case ChangeStatus.DISCONNECTED_IDX:
+			return Status.CONTACT_STATUS_DISCONNECT;
+		    case ChangeStatus.AVAILABLE_FOR_CHAT_IDX:
+			return Status.CONTACT_STATUS_AVAILABLE_FOR_CHAT;
+		    case ChangeStatus.AVAILABLE_IDX:
+			return Status.CONTACT_STATUS_AVAILABLE;
+		    case ChangeStatus.AWAY_IDX:
+			return Status.CONTACT_STATUS_AWAY;
+		    case ChangeStatus.BUSY_IDX:
+			return Status.CONTACT_STATUS_BUSY;
+		    case ChangeStatus.UNAVAILABLE_IDX:
+			return Status.CONTACT_STATUS_UNAVAILABLE;
+		    default:
+			res = Status.CONTACT_STATUS_AVAILABLE;
+			break;
 		}
 	    }
 	}
@@ -138,12 +84,6 @@
     protected void onCreate(Bundle savedInstanceState) {
 	super.onCreate(savedInstanceState);
 
-	nb++;
-	Log.i("nb de oncreate changestatus", nb.toString());
-	// Beem Application specific
-	mHandler = new Handler();
-	mBeemApplication = BeemApplication.getApplication(this);
-
 	setContentView(R.layout.changestatus);
 	mStatusText = (TextView) findViewById(R.id.ChangeStatusText);
 	mOk = (Button) findViewById(R.id.ChangeStatusOk);
@@ -163,25 +103,13 @@
     @Override
     protected void onDestroy() {
 	super.onDestroy();
-	mBeemApplication.unbindBeemService();
+	unbindService(mServConn);
     }
 
     @Override
     protected void onResume() {
 	super.onResume();
-	mBeemApplication.callWhenConnectedToServer(mHandler, new Runnable() {
-
-	    @Override
-	    public void run() {
-		mService = mBeemApplication.getXmppFacade();
-	    }
-	});
-    }
-
-    @Override
-    protected void onStart() {
-	super.onStart();
-	mBeemApplication.startBeemService();
+	bindService(new Intent(this, BeemService.class), mServConn, BIND_AUTO_CREATE);
     }
 
     private void showSettings() {
@@ -189,4 +117,57 @@
 	mSpinner.setSelection(getPreferenceStatusIndex());
     }
 
+    private class BeemServiceConnection implements ServiceConnection {
+
+	@Override
+	public void onServiceConnected(ComponentName name, IBinder service) {
+	    xmppFacade = IXmppFacade.Stub.asInterface(service);
+	}
+
+	@Override
+	public void onServiceDisconnected(ComponentName name) {
+	    xmppFacade = null;
+	}
+    }
+
+    private class MyOnClickListener implements OnClickListener {
+
+	@Override
+	public void onClick(View v) {
+	    if (v == mOk) {
+		if (textHasChanged() || statusHasChanged()) {
+		    String msg = mStatusText.getText().toString();
+		    int status = getStatusForService((String) mSpinner.getSelectedItem());
+		    Editor edit = mSettings.edit();
+		    edit.putString(getString(R.string.PreferenceStatusText), msg);
+		    edit.putInt(getString(R.string.PreferenceStatus), mSpinner.getSelectedItemPosition());
+		    edit.commit();
+		    if (status == Status.CONTACT_STATUS_DISCONNECT) {
+			stopService(new Intent(ChangeStatus.this, BeemService.class));
+		    } else {
+			try {
+			    xmppFacade.changeStatus(status, msg.toString());
+			} catch (RemoteException e) {
+			    // TODO
+			    // Auto-generated
+			    // catch block
+			    e.printStackTrace();
+			}
+			mToast.show();
+		    }
+		}
+		ChangeStatus.this.finish();
+	    } else if (v == mClear) {
+		mStatusText.setText(null);
+	    }
+	}
+	private boolean statusHasChanged() {
+	    return (mSettings.getInt(getString(R.string.PreferenceStatus), 0) != mSpinner.getSelectedItemPosition());
+	}
+
+	private boolean textHasChanged() {
+	    return (!mStatusText.getText().toString().equals(getPreferenceString(R.string.PreferenceStatusText)));
+	}
+    }
+
 }