package com.asafge.newsblurplus;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.asafge.newsblurplus.R;
import com.noinnion.android.reader.api.ReaderExtension;

public class LoginActivity extends Activity implements OnClickListener {
	
	protected ProgressDialog	mBusy;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Context c = getApplicationContext();
		setResult(RESULT_CANCELED);	
		
		String action = getIntent().getAction();
		if (action != null && action.equals(ReaderExtension.ACTION_LOGOUT)) {
			logout();
		}
		if (Prefs.isLoggedIn(c)) {
			setResult(ReaderExtension.RESULT_LOGIN);
			finish();
		}
		else {
			setContentView(R.layout.login_newsblur);
			findViewById(R.id.ok_button).setOnClickListener(this);
		}
	}

	// Function for logging out - remove cookie
	private void logout() {
		final Context c = getApplicationContext();
		Prefs.setLoggedIn(c, false);
		Prefs.setSessionID(c, "", "");
		setResult(ReaderExtension.RESULT_LOGOUT);
		finish();
	}
	
	// Login function, saves cookie when login is successful
	private void login(String user, String pass) {
		final Context c = getApplicationContext();
		APICall ac = new APICall(APICall.API_URL_LOGIN, c);
		ac.addPostParam("username", user);
		ac.addPostParam("password", pass);
		if (ac.sync()) {
			Prefs.setSessionID(c, ac.Status.getCookies().get(0).getName(), ac.Status.getCookies().get(0).getValue());
			Prefs.setLoggedIn(c, true);
			setResult(ReaderExtension.RESULT_LOGIN);
			finish();
		}
		else {
			Toast.makeText(c, "Error:" + ac.Status.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.ok_button:
				final EditText user = (EditText)findViewById(R.id.username_text);
				final EditText pass = (EditText)findViewById(R.id.password_text);
				new Thread(new Runnable() {
					@Override
					public void run() {
						login(user.getText().toString(), pass.getText().toString()); 
					}
				}).start();			
				break;
		}
	}
}

@@ -225,16 +225,69 @@
 tener;%0D%0A
+import android.view.inputmethod.InputMethodManager;%0D%0A
 import a
@@ -2052,16 +2052,196 @@
 utton:%0D%0A
+%09%09%09%09InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);%0D%0A%09%09%09    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);%0D%0A%09%09%09    %0D%0A
 %09%09%09%09fina
@@ -2541,19 +2541,16 @@
 start();
-%09%09%09
 %0D%0A%09%09%09%09br

Remove keyboard after login click