package net.somethingdreadful.MAL;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import net.somethingdreadful.MAL.account.AccountService;
import net.somethingdreadful.MAL.api.MALApi;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.ProgressDialog;

import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;

public class FirstTimeInit extends Activity {
    static EditText malUser;
    static EditText malPass;
    static String MalUser;
    static String MalPass;
    static ProgressDialog dialog;
    static Thread netThread;
    static Context context;
    static Handler messenger;
    static PrefManager prefManager;

    @SuppressLint("HandlerLeak")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_firstrun);

        malUser = (EditText) findViewById(R.id.edittext_malUser);
        malPass = (EditText) findViewById(R.id.edittext_malPass);
        Button connectButton = (Button) findViewById(R.id.button_connectToMal);
        Button registerButton = (Button) findViewById(R.id.registerButton);
        context = getApplicationContext();

        prefManager = new PrefManager(context);

        connectButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                MalUser = malUser.getText().toString().trim();
                MalPass = malPass.getText().toString().trim();
                tryConnection();
            }
        });

        registerButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://myanimelist.net/register.php"));
                startActivity(browserIntent);

            }
        });

        messenger = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 2) {
                    dialog.dismiss();
                    Crouton.makeText(FirstTimeInit.this, R.string.crouton_error_VerifyProblem, Style.ALERT).show();
                }
                if (msg.what == 3) {
                    AccountService.addAccount(context, MalUser, MalPass);
                    prefManager.setForceSync(true);
                    prefManager.commitChanges();
                    dialog.dismiss();
                    Intent goHome = new Intent(context, Home.class);
                    startActivity(goHome);
                    finish();
                }
                super.handleMessage(msg);
            }
        };

        NfcHelper.disableBeam(this);
    }

    private void tryConnection() {
        dialog = new ProgressDialog(this);
        dialog.setIndeterminate(true);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setTitle(getString(R.string.dialog_title_Verifying));
        dialog.setMessage(getString(R.string.dialog_message_Verifying));
        dialog.show();
        netThread = new networkThread();
        netThread.start();
    }

    public class networkThread extends Thread {
        @Override
        public void run() {
            boolean valid = new MALApi(MalUser, MalPass).isAuth();
            Message msg = new Message();
            if (!valid) {
                msg.what = 2;
                messenger.sendMessage(msg);
            } else {
                msg.what = 3;
                messenger.sendMessage(msg);
            }
        }
    }
}

@@ -186,62 +186,8 @@
 le;%0A
-import android.os.Handler;%0Aimport android.os.Message;%0A
 impo
@@ -407,18 +407,113 @@
 MAL.
-api.MALApi
+tasks.AuthenticationCheckFinishedListener;%0Aimport net.somethingdreadful.MAL.tasks.AuthenticationCheckTask
 ;%0A%0Ai
@@ -759,20 +759,60 @@
 ity 
-%7B%0A    static
+implements AuthenticationCheckFinishedListener %7B%0A   
  Edi
@@ -830,23 +830,16 @@
 er;%0A    
-static 
 EditText
@@ -848,31 +848,24 @@
 alPass;%0A    
-static 
 String MalUs
@@ -872,23 +872,16 @@
 er;%0A    
-static 
 String M
@@ -892,23 +892,16 @@
 ss;%0A    
-static 
 Progress
@@ -923,160 +923,54 @@
     
-static Thread netThread;%0A    static Context context;%0A    static Handler messenger;%0A    static PrefManager prefManager;%0A%0A    @SuppressLint(%22HandlerLeak%22)
+Context context;%0A    PrefManager prefManager;%0A
 %0A   
@@ -2115,806 +2115,8 @@
 );%0A%0A
-        messenger = new Handler() %7B%0A            @Override%0A            public void handleMessage(Message msg) %7B%0A                if (msg.what == 2) %7B%0A                    dialog.dismiss();%0A                    Crouton.makeText(FirstTimeInit.this, R.string.crouton_error_VerifyProblem, Style.ALERT).show();%0A                %7D%0A                if (msg.what == 3) %7B%0A                    AccountService.addAccount(context, MalUser, MalPass);%0A                    prefManager.setForceSync(true);%0A                    prefManager.commitChanges();%0A                    dialog.dismiss();%0A                    Intent goHome = new Intent(context, Home.class);%0A                    startActivity(goHome);%0A                    finish();%0A                %7D%0A                super.handleMessage(msg);%0A            %7D%0A        %7D;%0A%0A
     
@@ -2514,206 +2514,221 @@
   ne
-tThread = new networkThread();%0A        netThread.start();%0A    %7D%0A%0A    public class networkThread extends Thread %7B%0A        @Override%0A        public void run() %7B%0A            boolean valid = new MALApi(
+w AuthenticationCheckTask(this).execute(MalUser, MalPass);%0A    %7D%0A%0A    @Override%0A    public void onAuthenticationCheckFinished(boolean result) %7B%0A        if (result) %7B%0A            AccountService.addAccount(context, 
 MalU
@@ -2744,17 +2744,8 @@
 ass)
-.isAuth()
 ;%0A  
@@ -2758,245 +2758,369 @@
     
-Message msg = new Message();%0A            if (!valid) %7B%0A                msg.what = 2;%0A                messenger.sendMessage(msg);%0A            %7D else %7B%0A                msg.what = 3;%0A                messenger.sendMessage(msg);%0A            %7D
+prefManager.setForceSync(true);%0A            prefManager.commitChanges();%0A            dialog.dismiss();%0A            Intent goHome = new Intent(context, Home.class);%0A            startActivity(goHome);%0A            finish();%0A        %7D else %7B%0A            dialog.dismiss();%0A            Crouton.makeText(this, R.string.crouton_error_VerifyProblem, Style.ALERT).show();
 %0A   

Use AuthenticationCheckTask in FirstTimeInit