Le bouton cancel marche
authorDa Risk <darisk972@gmail.com>
Sat, 21 Nov 2009 03:46:23 +0100
changeset 543 28d200758114
parent 542 23e8095cace5
child 544 24276024c388
Le bouton cancel marche
res/layout/login_anim.xml
src/com/beem/project/beem/service/XmppConnectionAdapter.java
src/com/beem/project/beem/ui/AddContact.java
src/com/beem/project/beem/ui/LoginAnim.java
src/com/beem/project/beem/utils/BeemBroadcastReceiver.java
--- a/res/layout/login_anim.xml	Sat Nov 21 02:08:35 2009 +0100
+++ b/res/layout/login_anim.xml	Sat Nov 21 03:46:23 2009 +0100
@@ -4,10 +4,11 @@
     android:layout_height="fill_parent"
     android:layout_width="fill_parent"
     android:orientation="vertical" >
-    <ImageView android:id="@+id/logo_anim" android:src="@drawable/beem_launcher_icon_color" android:layout_height="fill_parent" android:layout_width="fill_parent" 
+    <ImageView android:id="@+id/loginanim_logo_anim" android:src="@drawable/beem_launcher_icon_color" android:layout_height="fill_parent" android:layout_width="fill_parent" 
 	android:layout_weight="1"/>
     <!-- TODO traduction -->
-    <Button android:layout_height="wrap_content" android:layout_width="fill_parent"
+    <Button android:id= "@+id/loginanim_cancel_button"
+	android:layout_height="wrap_content" android:layout_width="fill_parent"
 	android:layout_gravity="bottom"
 	android:text="Cancel" />
 </LinearLayout>
--- a/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Sat Nov 21 02:08:35 2009 +0100
+++ b/src/com/beem/project/beem/service/XmppConnectionAdapter.java	Sat Nov 21 03:46:23 2009 +0100
@@ -120,6 +120,8 @@
      */
     @Override
     public final void connectAsync() throws RemoteException {
+	if (mAdaptee.isConnected() || mAdaptee.isAuthenticated())
+	    return;
 	Thread t = new Thread(new Runnable() {
 
 	    @Override
--- a/src/com/beem/project/beem/ui/AddContact.java	Sat Nov 21 02:08:35 2009 +0100
+++ b/src/com/beem/project/beem/ui/AddContact.java	Sat Nov 21 03:46:23 2009 +0100
@@ -129,6 +129,7 @@
 
     private final OnClickListener mOkListener = new OnClickListener() {
 
+	@Override
 	public void onClick(View v) {
 	    String login;
 	    login = getWidgetText(R.id.addc_login);
--- a/src/com/beem/project/beem/ui/LoginAnim.java	Sat Nov 21 02:08:35 2009 +0100
+++ b/src/com/beem/project/beem/ui/LoginAnim.java	Sat Nov 21 03:46:23 2009 +0100
@@ -8,6 +8,9 @@
 import android.os.AsyncTask;
 import android.os.IBinder;
 import android.os.RemoteException;
+import android.view.View.OnClickListener;
+import android.view.View;
+import android.widget.Button;
 import android.view.animation.Animation;
 import android.widget.ImageView;
 import android.view.animation.AnimationUtils;
@@ -34,6 +37,7 @@
     private final ServiceConnection mServConn = new LoginServiceConnection();
     private IXmppFacade mXmppFacade;
     private AsyncTask<IXmppFacade, Void, Boolean>mTask;
+    private Button mCancelBt;
 
     /**
      * Constructor.
@@ -48,8 +52,10 @@
     protected void onCreate(Bundle savedInstanceState) {
 	super.onCreate(savedInstanceState);
 	setContentView(R.layout.login_anim);
-	mLogo = (ImageView) findViewById(R.id.logo_anim);
+	mLogo = (ImageView) findViewById(R.id.loginanim_logo_anim);
 	mRotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_and_scale);
+	mCancelBt = (Button) findViewById(R.id.loginanim_cancel_button);
+	mCancelBt.setOnClickListener( new ClickListener());
     }
 
     /**
@@ -86,14 +92,16 @@
 
     class LoginTask extends AsyncTask<IXmppFacade, Void, Boolean> {
 
+	private IXmppConnection mConnection;
+
 	@Override
 	protected Boolean doInBackground(IXmppFacade ... params) {
 	    boolean result = true;
 	    IXmppFacade facade = params[0];
 	    try {
-		IXmppConnection connect = facade.createConnection();
-		if (!connect.isAuthentificated()) {
-		    result = connect.connectSync();
+		mConnection = facade.createConnection();
+		if (!mConnection.isAuthentificated()) {
+		    result = mConnection.connectSync();
 		    if (!result) {
 			// set bad message ?
 		    }
@@ -112,11 +120,26 @@
 	    if (result == null || !result) { // Task cancelled on exception
 		Log.d(TAG, "Exception or cancelation of the connection");
 	    } else {
+		LoginAnim.this.startService(LoginAnim.SERVICE_INTENT);
 		LoginAnim.this.startActivity(new Intent(LoginAnim.this, ContactList.class));
 		LoginAnim.this.finish();
 	    }
 	}
 
+	@Override
+	protected void onCancelled() {
+	    mCancelBt.setText("Cancelled. Retry");
+	    try {
+		if (mConnection.isAuthentificated()) {
+		    mConnection.disconnect();
+		}
+	    } catch (RemoteException e) {
+		Log.d(TAG, "Remote exception", e);
+	    }
+	    LoginAnim.this.stopService(LoginAnim.SERVICE_INTENT);
+	    LoginAnim.this.finish();
+	}
+
     }
 
     private class LoginServiceConnection implements ServiceConnection {
@@ -137,4 +160,16 @@
 	    mXmppFacade = null;
 	}
     }
+
+    private class ClickListener implements OnClickListener {
+	
+	@Override
+	public void onClick(View v) {
+	    if (v == mCancelBt) {
+		if (! mTask.cancel(true)) {
+		    Log.d(TAG, "Can't interrupt the connection");
+		}
+	    }
+	}
+    }
 }
--- a/src/com/beem/project/beem/utils/BeemBroadcastReceiver.java	Sat Nov 21 02:08:35 2009 +0100
+++ b/src/com/beem/project/beem/utils/BeemBroadcastReceiver.java	Sat Nov 21 03:46:23 2009 +0100
@@ -41,9 +41,10 @@
 	if (mIsBinded)
 	    context.unbindService(mService);
 	setBinded(false);
+	// TODO ce code est commenté car il empeche de gerer un cancel sur une connexion
 	// start activity if unexpected disconnection
-	if (!intent.getBooleanExtra("normally", false))
-	    context.startActivity(new Intent(context, Login.class));
+//         if (!intent.getBooleanExtra("normally", false))
+//             context.startActivity(new Intent(context, Login.class));
 	CharSequence message = intent.getCharSequenceExtra("message");
 	Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
 	if (context instanceof Activity) {