Example usage for android.os AsyncTask get

List of usage examples for android.os AsyncTask get

Introduction

In this page you can find the example usage for android.os AsyncTask get.

Prototype

public final Result get() throws InterruptedException, ExecutionException 

Source Link

Document

Waits if necessary for the computation to complete, and then retrieves its result.

Usage

From source file:org.jorge.lolin1.func.chat.ChatIntentService.java

private Boolean login(String upperCaseRealm) {
    ChatServer chatServer;/* w w w  .  j a  v  a 2s .  com*/
    switch (upperCaseRealm) {
    case "NA":
        chatServer = ChatServer.NA;
        break;
    case "EUW":
        chatServer = ChatServer.EUW;
        break;
    case "EUNE":
        chatServer = ChatServer.EUNE;
        break;
    case "TR":
        chatServer = ChatServer.TR;
        break;
    case "BR":
        chatServer = ChatServer.BR;
        break;
    case "OCE":
        chatServer = ChatServer.OCE;
        break;
    case "LAN":
        chatServer = ChatServer.LAN;
        break;
    case "LAS":
        chatServer = ChatServer.LAS;
        break;
    case "KR":
        chatServer = ChatServer.KR;
        break;
    case "RU":
        chatServer = ChatServer.RU;
        break;
    default:
        throw new IllegalArgumentException("Region " + upperCaseRealm + " not yet implemented");
    }
    try {
        logString("debug", "Assigning api...");
        api = new LoLChat(chatServer, Boolean.FALSE);
    } catch (IOException e) {
        logString("debug", "Exception when constructing the object!");
        e.printStackTrace(System.err);
        if (!(e instanceof SSLException)) {
            launchBroadcastLoginUnsuccessful();
        }
        return Boolean.FALSE;
    }
    final AccountManager accountManager = AccountManager.get(getApplicationContext());
    Account[] accounts = accountManager
            .getAccountsByType(LoLin1Utils.getString(getApplicationContext(), "account_type", null));
    Account thisRealmAccount = null;
    for (Account acc : accounts) {
        if (acc.name.contentEquals(upperCaseRealm)) {
            thisRealmAccount = acc;
            break;
        }
    }
    if (thisRealmAccount == null) {
        return Boolean.FALSE;//There's no account associated to this realm
    }
    AsyncTask<Account, Void, String[]> credentialsTask = new AsyncTask<Account, Void, String[]>() {
        @Override
        protected String[] doInBackground(Account... params) {
            String[] processedAuthToken = null;
            try {
                processedAuthToken = accountManager.blockingGetAuthToken(params[0], "none", Boolean.TRUE)
                        .split(AccountAuthenticator.TOKEN_GENERATION_JOINT);
            } catch (OperationCanceledException | IOException | AuthenticatorException e) {
                Crashlytics.logException(e);
            }
            return processedAuthToken;
        }
    };
    //It's necessary to run the task in an executor because the main one is already full and if we add this one a livelock will occur
    ExecutorService loginExecutor = Executors.newFixedThreadPool(1);
    credentialsTask.executeOnExecutor(loginExecutor, thisRealmAccount);
    String[] processedAuthToken = new String[0];
    try {
        processedAuthToken = credentialsTask.get();
    } catch (InterruptedException | ExecutionException e) {
        Crashlytics.logException(e);
    }
    Boolean loginSuccess = Boolean.FALSE;
    try {
        loginSuccess = api.login(processedAuthToken[0], processedAuthToken[1]);
    } catch (IOException e) {
        Crashlytics.logException(e);
    }
    if (loginSuccess) {
        api.reloadRoster();
        isConnected = Boolean.TRUE;
        return Boolean.TRUE;
    } else {
        isConnected = Boolean.FALSE;
        return Boolean.FALSE;
    }
}