petites majs sur la connection dans login
authorNikita Kozlov <nikita@beem-project.com>
Fri, 06 Nov 2009 18:00:19 +0100
changeset 492 b64155c6d0f3
parent 491 a94c384bc3ba
child 494 dd9ad5fec728
petites majs sur la connection dans login
res/values/strings.xml
src/com/beem/project/beem/BeemService.java
src/com/beem/project/beem/jingle/PCMTransmitter.java
src/com/beem/project/beem/ui/Login.java
--- a/res/values/strings.xml	Fri Nov 06 14:46:16 2009 +0100
+++ b/res/values/strings.xml	Fri Nov 06 18:00:19 2009 +0100
@@ -162,6 +162,7 @@
 	<string name="login_settings_button">Settings</string>
 	<string name="login_login_button">Log in</string>
 	<string name="login_login_progress">Connecting. Please wait...</string>
+	<string name="login_max_retry">Max retry</string>
 	<string name="login_error_msg">Unfortunately, an error occured.\n\nError detail:\n%s</string>
 	
 	<!--
--- a/src/com/beem/project/beem/BeemService.java	Fri Nov 06 14:46:16 2009 +0100
+++ b/src/com/beem/project/beem/BeemService.java	Fri Nov 06 18:00:19 2009 +0100
@@ -95,14 +95,15 @@
      */
     @Override
     public IBinder onBind(Intent intent) {
-	Log.e("BEEMSERVICE", "ONBIND()");
+	Log.d("BEEMSERVICE", "ONBIND()");
 	return mBind;
     }
 
     @Override
     public boolean onUnbind(Intent intent) {
-	Log.e("BEEMSERVICE", "ONUNBIND()");
+	Log.d("BEEMSERVICE", "ONUNBIND()");
 	if (!mConnection.getAdaptee().isConnected()) {
+	    Log.d("BEEMSERVICE","DESTROYED");
 	    this.stopSelf();
 	}
 	return true;
@@ -160,6 +161,7 @@
     @Override
     public void onStart(Intent intent, int startId) {
 	super.onStart(intent, startId);
+	Log.d("BEEMSERVICE","onStart");
 	try {
 	    mConnection.connectAsync();
 	} catch (RemoteException e) {
--- a/src/com/beem/project/beem/jingle/PCMTransmitter.java	Fri Nov 06 14:46:16 2009 +0100
+++ b/src/com/beem/project/beem/jingle/PCMTransmitter.java	Fri Nov 06 18:00:19 2009 +0100
@@ -26,8 +26,8 @@
     @Override
     void start() {
 	AudioRecord audRec = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, RATE_IN_HZ,
-	    AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT, AudioRecord.getMinBufferSize(
-		RATE_IN_HZ, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT));
+	    AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, AudioRecord.getMinBufferSize(
+		RATE_IN_HZ, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT) * 4);
 	byte[] audioData = new byte[BUFF_SIZE];
 	int byteReaded = 0;
 	while (!isKillme()) {
--- a/src/com/beem/project/beem/ui/Login.java	Fri Nov 06 14:46:16 2009 +0100
+++ b/src/com/beem/project/beem/ui/Login.java	Fri Nov 06 18:00:19 2009 +0100
@@ -6,6 +6,7 @@
 import android.content.ComponentName;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.os.Bundle;
@@ -25,6 +26,7 @@
 import com.beem.project.beem.service.aidl.IBeemConnectionListener;
 import com.beem.project.beem.service.aidl.IXmppConnection;
 import com.beem.project.beem.service.aidl.IXmppFacade;
+import com.beem.project.beem.utils.BeemBroadcastReceiver;
 import com.beem.project.beem.utils.Status;
 
 /**
@@ -51,6 +53,8 @@
     private SharedPreferences mSettings;
     private boolean mIsConfigured;
     private int mRetry;
+    private BeemBroadcastReceiver mReceiver;
+
 
     /**
      * Constructor.
@@ -80,9 +84,11 @@
     @Override
     public void onCreate(Bundle savedInstanceState) {
 	super.onCreate(savedInstanceState);
+	Log.d(TAG,"onCreate");
 	mSettings = PreferenceManager.getDefaultSharedPreferences(this);
 	setContentView(R.layout.login);
 	mProgressDialog = new ProgressDialog(this);
+	mReceiver = new BeemBroadcastReceiver(mServConn);
     }
 
     /**
@@ -91,8 +97,11 @@
     @Override
     protected void onStop() {
 	super.onStop();
+	Log.d(TAG,"onStop");
 	if (mIsConfigured && (mIsConnected || mXmppFacade != null)) {
+	    mReceiver.setBinded(false);
 	    unbindService(mServConn);
+	    mXmppFacade = null;
 	}
     }
     
@@ -101,7 +110,18 @@
      */
     protected void onPause() {
 	super.onPause();
+	Log.d(TAG,"onPause");
+	this.unregisterReceiver(mReceiver);
 	mConnectionHandler.removeCallbacks(mConnectionRunnable);
+	mProgressDialog.dismiss();
+    }
+    
+    /**
+     * {@inheritDoc}
+     */
+    protected void onResume() {
+	super.onResume();
+	this.registerReceiver(mReceiver, new IntentFilter(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED));
     }
 
     /**
@@ -110,9 +130,17 @@
     @Override
     public void onStart() {
 	super.onStart();
+	Log.d(TAG,"onStart");
 	mIsConfigured = mSettings.getBoolean("PreferenceIsConfigured", false);
-	if (mIsConfigured)
+	if (mIsConfigured && !mIsConnected) {
+	    try {
+		Thread.sleep(1000); //HACK: Pour éviter de rebinder un service pas encore arreter.
+	    } catch (InterruptedException e) {
+		e.printStackTrace();
+	    }
 	    bindService(Login.SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
+	    mReceiver.setBinded(true);
+	}	    
     }
 
     /**
@@ -163,11 +191,14 @@
 	@Override
 	public void run() {
 	    if (mRetry++ > 3) {
-		Toast.makeText(Login.this, "Max retry", Toast.LENGTH_LONG).show();
+		Toast.makeText(Login.this, R.string.login_max_retry, Toast.LENGTH_LONG).show();
+		mConnectionHandler.removeCallbacks(mConnectionRunnable);
 	    } else {
 		mIsConfigured = mSettings.getBoolean("PreferenceIsConfigured", false);
-		if (mIsConfigured)
+		if (mIsConfigured) {
 		    bindService(Login.SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
+		    mReceiver.setBinded(true);
+		}
 	    }
 
 	}
@@ -208,6 +239,7 @@
 	    Log.d(TAG, "CONNECTIONFAILLED");
 	    mIsConnected = false;
 	    if (mXmppFacade != null) {
+		mReceiver.setBinded(false);
 		Login.this.unbindService(mServConn);
 		Login.this.stopService(SERVICE_INTENT);
 		mXmppFacade = null;
@@ -294,7 +326,6 @@
 	@Override
 	public void onServiceConnected(ComponentName name, IBinder service) {
 	    Log.d(TAG, "service connected");
-	    mIsConnected = true;
 	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
 	    try {
 		mXmppConnection = mXmppFacade.createConnection();
@@ -308,6 +339,7 @@
 			    mProgressDialog.show();
 			}
 		    });
+		    
 		    Login.this.startService(Login.SERVICE_INTENT);
 		} else {
 		    startActivity(new Intent(Login.this, ContactList.class));