com.manning.androidhacks.hack023.net.NetworkUtilities.java Source code

Java tutorial

Introduction

Here is the source code for com.manning.androidhacks.hack023.net.NetworkUtilities.java

Source

/*
50AH-code
=========
    
50 Android Hacks (http://manning.com/sessa/) book source code
    
    
Copyright (c) 2012 Manning
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
    
    
*/

/*******************************************************************************
 * Copyright (c) 2012 Manning
 * See the file license.txt for copying permission.
 ******************************************************************************/
package com.manning.androidhacks.hack023.net;

import android.content.Context;
import android.os.Handler;

import com.manning.androidhacks.hack023.api.LoginServiceImpl;
import com.manning.androidhacks.hack023.authenticator.AuthenticatorActivity;
import com.manning.androidhacks.hack023.exception.AndroidHacksException;

public class NetworkUtilities {

    public static Thread performOnBackgroundThread(final Runnable runnable) {
        final Thread t = new Thread() {
            @Override
            public void run() {
                try {
                    runnable.run();
                } finally {

                }
            }
        };
        t.start();
        return t;
    }

    public static Thread attemptAuth(final String username, final String password, final Handler handler,
            final Context context) {
        final Runnable runnable = new Runnable() {
            public void run() {
                authenticate(username, password, handler, context);
            }

        };
        return NetworkUtilities.performOnBackgroundThread(runnable);
    }

    private static void authenticate(String username, String password, Handler handler, Context context) {
        boolean hasLoggedIn = false;

        try {
            String response = LoginServiceImpl.sendCredentials(username, password);
            hasLoggedIn = LoginServiceImpl.hasLoggedIn(response);

            if (hasLoggedIn) {
                sendResult(true, handler, context);
            } else {
                sendResult(false, handler, context);
            }
        } catch (AndroidHacksException e) {
            sendResult(false, handler, context);
        }
    }

    private static void sendResult(final Boolean result, final Handler handler, final Context context) {
        if (handler == null || context == null) {
            return;
        }
        handler.post(new Runnable() {
            public void run() {
                ((AuthenticatorActivity) context).onAuthenticationResult(result);
            }
        });
    }
}