src/com/beem/project/beem/ui/LoginAnim.java
changeset 550 443a5937ad7a
parent 548 9b0d1638a6e6
child 552 a560f82d48b0
equal deleted inserted replaced
549:7fcb224d2c3c 550:443a5937ad7a
     1 package com.beem.project.beem.ui;
     1 package com.beem.project.beem.ui;
     2 
     2 
     3 import android.app.Activity;
     3 import android.app.Activity;
     4 import android.content.ServiceConnection;
       
     5 import android.content.ComponentName;
     4 import android.content.ComponentName;
     6 import android.content.Intent;
     5 import android.content.Intent;
       
     6 import android.content.ServiceConnection;
       
     7 import android.os.AsyncTask;
     7 import android.os.Bundle;
     8 import android.os.Bundle;
     8 import android.os.AsyncTask;
       
     9 import android.os.IBinder;
     9 import android.os.IBinder;
    10 import android.os.RemoteException;
    10 import android.os.RemoteException;
       
    11 import android.util.Log;
       
    12 import android.view.KeyEvent;
       
    13 import android.view.View;
    11 import android.view.View.OnClickListener;
    14 import android.view.View.OnClickListener;
    12 import android.view.View;
    15 import android.view.animation.Animation;
       
    16 import android.view.animation.AnimationUtils;
    13 import android.widget.Button;
    17 import android.widget.Button;
    14 import android.view.animation.Animation;
       
    15 import android.widget.ImageView;
    18 import android.widget.ImageView;
    16 import android.widget.TextView;
       
    17 import android.widget.ProgressBar;
    19 import android.widget.ProgressBar;
    18 import android.view.animation.AnimationUtils;
    20 
    19 import android.view.KeyEvent;
       
    20 import com.beem.project.beem.R;
    21 import com.beem.project.beem.R;
    21 import android.util.Log;
       
    22 import com.beem.project.beem.service.aidl.IXmppFacade;
       
    23 import com.beem.project.beem.service.aidl.IXmppConnection;
    22 import com.beem.project.beem.service.aidl.IXmppConnection;
    24 import com.beem.project.beem.service.aidl.IXmppFacade;
    23 import com.beem.project.beem.service.aidl.IXmppFacade;
    25 
    24 
    26 /**
    25 /**
    27  * This class is an activity which display an animation during the connection
    26  * This class is an activity which display an animation during the connection with the server.
    28  * with the server.
       
    29  * @author Da Risk <darisk972@gmail.com>
    27  * @author Da Risk <darisk972@gmail.com>
    30  */
    28  */
    31 public class LoginAnim extends Activity {
    29 public class LoginAnim extends Activity {
    32 
    30 
    33     private static final String TAG = "LoginAnim";
    31     private static final String TAG = "LoginAnim";
    35     static {
    33     static {
    36 	SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
    34 	SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
    37     }
    35     }
    38     private ImageView mLogo;
    36     private ImageView mLogo;
    39     private Animation mRotateAnim;
    37     private Animation mRotateAnim;
    40     private TextView mErrorLabel;
       
    41     private final ServiceConnection mServConn = new LoginServiceConnection();
    38     private final ServiceConnection mServConn = new LoginServiceConnection();
    42     private IXmppFacade mXmppFacade;
    39     private IXmppFacade mXmppFacade;
    43     private AsyncTask<IXmppFacade, Void, Boolean>mTask;
    40     private AsyncTask<IXmppFacade, Void, Boolean> mTask;
    44     private Button mCancelBt;
    41     private Button mCancelBt;
    45     private ProgressBar mProgressBar;
    42     private ProgressBar mProgressBar;
    46 
    43 
    47     /**
    44     /**
    48      * Constructor.
    45      * Constructor.
    52 
    49 
    53     @Override
    50     @Override
    54     public boolean onKeyDown(int keyCode, KeyEvent event) {
    51     public boolean onKeyDown(int keyCode, KeyEvent event) {
    55 	// TODO use onBackPressed on Eclair (2.0)
    52 	// TODO use onBackPressed on Eclair (2.0)
    56 	if (keyCode == KeyEvent.KEYCODE_BACK && mTask.getStatus() != AsyncTask.Status.FINISHED) {
    53 	if (keyCode == KeyEvent.KEYCODE_BACK && mTask.getStatus() != AsyncTask.Status.FINISHED) {
    57 	    if (! mTask.cancel(true)) {
    54 	    if (!mTask.cancel(true)) {
    58 		Log.d(TAG, "Can't interrupt the connection");
    55 		Log.d(TAG, "Can't interrupt the connection");
    59 	    }
    56 	    }
    60 	    setResult(Activity.RESULT_CANCELED);
    57 	    setResult(Activity.RESULT_CANCELED);
    61 	}
    58 	}
    62 	return super.onKeyDown(keyCode, event);
    59 	return super.onKeyDown(keyCode, event);
    68     @Override
    65     @Override
    69     protected void onCreate(Bundle savedInstanceState) {
    66     protected void onCreate(Bundle savedInstanceState) {
    70 	super.onCreate(savedInstanceState);
    67 	super.onCreate(savedInstanceState);
    71 	setContentView(R.layout.login_anim);
    68 	setContentView(R.layout.login_anim);
    72 	mLogo = (ImageView) findViewById(R.id.loginanim_logo_anim);
    69 	mLogo = (ImageView) findViewById(R.id.loginanim_logo_anim);
    73 	mErrorLabel = (TextView) findViewById(R.id.loginanim_error_label);
       
    74 	mRotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_and_scale);
    70 	mRotateAnim = AnimationUtils.loadAnimation(this, R.anim.rotate_and_scale);
    75 	mCancelBt = (Button) findViewById(R.id.loginanim_cancel_button);
    71 	mCancelBt = (Button) findViewById(R.id.loginanim_cancel_button);
    76 	mCancelBt.setOnClickListener( new ClickListener());
    72 	mCancelBt.setOnClickListener(new ClickListener());
    77 	mProgressBar = (ProgressBar) findViewById(R.id.loginanim_progressbar);
    73 	mProgressBar = (ProgressBar) findViewById(R.id.loginanim_progressbar);
    78 	mProgressBar.setIndeterminate(true);
    74 	mProgressBar.setIndeterminate(true);
    79     }
    75     }
    80 
    76 
    81     /**
    77     /**
    82      * {@inheritDoc}
    78      * {@inheritDoc}
    83      */
    79      */
    84     @Override
    80     @Override
    85     protected void onStart() {
    81     protected void onStart() {
    86 	super.onStart();
    82 	super.onStart();
    87         mLogo.startAnimation(mRotateAnim);
    83 	mLogo.startAnimation(mRotateAnim);
    88     }
    84     }
    89 
    85 
    90     /**
    86     /**
    91      * {@inheritDoc}
    87      * {@inheritDoc}
    92      */
    88      */
   108 	    unbindService(mServConn);
   104 	    unbindService(mServConn);
   109 	    mXmppFacade = null;
   105 	    mXmppFacade = null;
   110 	}
   106 	}
   111     }
   107     }
   112 
   108 
       
   109     private class ClickListener implements OnClickListener {
       
   110 
       
   111 	@Override
       
   112 	public void onClick(View v) {
       
   113 	    if (v == mCancelBt) {
       
   114 		if (!mTask.cancel(true)) {
       
   115 		    Log.d(TAG, "Can't interrupt the connection");
       
   116 		}
       
   117 		setResult(Activity.RESULT_CANCELED);
       
   118 		finish();
       
   119 	    }
       
   120 	}
       
   121     }
       
   122 
   113     class LoginTask extends AsyncTask<IXmppFacade, Void, Boolean> {
   123     class LoginTask extends AsyncTask<IXmppFacade, Void, Boolean> {
   114 
   124 
   115 	private IXmppConnection mConnection;
   125 	private IXmppConnection mConnection;
   116 
   126 	private String mMsg;
   117 	@Override
   127 
   118 	protected Boolean doInBackground(IXmppFacade ... params) {
   128 	@Override
       
   129 	protected Boolean doInBackground(IXmppFacade... params) {
       
   130 	    Log.d(TAG, "Launch the task");
   119 	    boolean result = true;
   131 	    boolean result = true;
   120 	    IXmppFacade facade = params[0];
   132 	    IXmppFacade facade = params[0];
   121 	    try {
   133 	    try {
   122 		mConnection = facade.createConnection();
   134 		mConnection = facade.createConnection();
   123 		if (!mConnection.isAuthentificated()) {
   135 		if (!mConnection.isAuthentificated()) {
   124 		    result = mConnection.connectSync();
   136 		    result = mConnection.connectSync();
   125 		    if (!result) {
   137 		    if (!result)
   126 			// set bad message ?
   138 			mMsg = mConnection.getErrorMessage();
   127 		    }
       
   128 		}
   139 		}
   129 	    } catch (RemoteException e) {
   140 	    } catch (RemoteException e) {
   130 		Log.d(TAG, "Error while connecting", e);
   141 		Log.d(TAG, "Error while connecting", e);
   131 		// set bad message ?
   142 		mMsg = "Exception during connection";
   132 		result = false;
   143 		result = false;
   133 	    }
   144 	    }
   134 	    Log.d(TAG, "Connection result? " + result);
   145 	    Log.d(TAG, "Connection result? " + result);
   135 	    return result;
   146 	    return result;
   136 	}
   147 	}
   137 
   148 
   138 	@Override
   149 	@Override
   139 	protected void onPostExecute(Boolean result) {
   150 	protected void onPostExecute(Boolean result) {
       
   151 
   140 	    if (result == null || !result) { // Task cancelled on exception
   152 	    if (result == null || !result) { // Task cancelled on exception
   141 		String msg;
       
   142 		Log.d(TAG, "Exception or cancelation of the connection");
   153 		Log.d(TAG, "Exception or cancelation of the connection");
   143 		try {
   154 		if (result == false) {
   144 		    if (!result)
   155 		    Intent i = new Intent();
   145 			msg = mConnection.getErrorMessage();
   156 		    i.putExtra("message", mMsg);
   146 		    else
   157 		    LoginAnim.this.setResult(Activity.RESULT_CANCELED, i);
   147 			msg = "Exception during connection";
   158 		} else
   148 		} catch (RemoteException e) {
   159 		    LoginAnim.this.setResult(Activity.RESULT_CANCELED);
   149 		    Log.d(TAG, "Exception or cancelation of the connection");
       
   150 		    msg = "Remote Exception";
       
   151 		}
       
   152 		//TODO create an intent and store the error msg in. Return this intent on result.
       
   153 		LoginAnim.this.mErrorLabel.setText("Error !!!\n" + msg);
       
   154 		LoginAnim.this.setResult(Activity.RESULT_CANCELED);
       
   155 		LoginAnim.this.finish();
   160 		LoginAnim.this.finish();
   156 	    } else {
   161 	    } else {
   157 		LoginAnim.this.startService(LoginAnim.SERVICE_INTENT);
   162 		LoginAnim.this.startService(LoginAnim.SERVICE_INTENT);
   158 		//LoginAnim.this.startActivity(new Intent(LoginAnim.this, ContactList.class));
       
   159 		LoginAnim.this.setResult(Activity.RESULT_OK);
   163 		LoginAnim.this.setResult(Activity.RESULT_OK);
   160 		LoginAnim.this.finish();
   164 		LoginAnim.this.finish();
   161 	    }
   165 	    }
   162 	}
   166 	}
   163 
   167 
   164 	@Override
   168 	@Override
   165 	protected void onCancelled() {
   169 	protected void onCancelled() {
   166 	    mCancelBt.setText("Cancelled. Retry");
       
   167 	    try {
   170 	    try {
   168 		if (mConnection != null && mConnection.isAuthentificated()) {
   171 		if (mConnection != null && mConnection.isAuthentificated()) {
   169 		    mConnection.disconnect();
   172 		    mConnection.disconnect();
   170 		}
   173 		}
   171 	    } catch (RemoteException e) {
   174 	    } catch (RemoteException e) {
   182 	}
   185 	}
   183 
   186 
   184 	@Override
   187 	@Override
   185 	public void onServiceConnected(ComponentName name, IBinder service) {
   188 	public void onServiceConnected(ComponentName name, IBinder service) {
   186 	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
   189 	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
   187 	    Log.d(TAG, "Launch the task");
       
   188 	    if (mTask.getStatus() == AsyncTask.Status.PENDING)
   190 	    if (mTask.getStatus() == AsyncTask.Status.PENDING)
   189 		mTask = mTask.execute(mXmppFacade);
   191 		mTask = mTask.execute(mXmppFacade);
   190 	}
   192 	}
   191 	
   193 
   192 	@Override
   194 	@Override
   193 	public void onServiceDisconnected(ComponentName name) {
   195 	public void onServiceDisconnected(ComponentName name) {
   194 	    Log.d(TAG, "Service disconnected.");
   196 	    Log.d(TAG, "Service disconnected.");
   195 	    mXmppFacade = null;
   197 	    mXmppFacade = null;
   196 	}
   198 	}
   197     }
   199     }
   198 
       
   199     private class ClickListener implements OnClickListener {
       
   200 	
       
   201 	@Override
       
   202 	public void onClick(View v) {
       
   203 	    if (v == mCancelBt) {
       
   204 		if (! mTask.cancel(true)) {
       
   205 		    Log.d(TAG, "Can't interrupt the connection");
       
   206 		}
       
   207 		setResult(Activity.RESULT_CANCELED);
       
   208 		finish();
       
   209 	    }
       
   210 	}
       
   211     }
       
   212 }
   200 }