ca.zadrox.dota2esportticker.util.steamapi.SAPILiveGamesLoader.java Source code

Java tutorial

Introduction

Here is the source code for ca.zadrox.dota2esportticker.util.steamapi.SAPILiveGamesLoader.java

Source

/*
 * Copyright 2014 Nicholas Liu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ca.zadrox.dota2esportticker.util.steamapi;

import android.content.AsyncTaskLoader;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.google.gson.Gson;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;

import java.io.IOException;

import ca.zadrox.dota2esportticker.data.gson.model.DotaGameData;
import ca.zadrox.dota2esportticker.data.gson.model.LiveLeagueGames;
import ca.zadrox.dota2esportticker.util.LogUtils;

/**
 * Created by Acco on 11/3/2014.
 */
public class SAPILiveGamesLoader extends AsyncTaskLoader<DotaGameData[]> {

    private final String TAG = SAPILiveGamesLoader.class.getSimpleName();

    DotaGameData[] mGame;
    long matchId;

    public SAPILiveGamesLoader(Context context, long mId) {
        super(context);
        matchId = mId;
    }

    @Override
    protected void onStopLoading() {

        cancelLoad();

    }

    @Override
    public void deliverResult(DotaGameData[] data) {
        if (isReset()) {
            if (mGame != null) {
                onReleaseResources(mGame);
            }
        }

        super.deliverResult(data);
    }

    @Override
    protected void onReset() {
        super.onReset();

        onStopLoading();

        if (mGame != null) {
            onReleaseResources(mGame);
            mGame = null;
        }
    }

    @Override
    public void onCanceled(DotaGameData[] data) {
        super.onCanceled(data);

        onReleaseResources(data);
    }

    @Override
    protected void onStartLoading() {
        if (mGame != null) {
            deliverResult(mGame);
        } else {
            forceLoad();
        }
    }

    /**
     * @return DotaGameData[], an array of all live games, if matchId wasn't supplied, or if
     * a matchId was supplied, an array of length 1, containing just the live game specified,
     * or if no games were found, null.
     */
    @Override
    public DotaGameData[] loadInBackground() {

        String steamAPIKey = SteamAuth.getSteamApiKey(getContext());

        String getLiveLeagueGamesEndpoint = "http://api.steampowered.com/IDOTA2Match_570/GetLiveLeagueGames/v1/";

        try {
            OkHttpClient okHttpClient = new OkHttpClient();
            String rawJson = okHttpClient.newCall(new Request.Builder().url(getLiveLeagueGamesEndpoint + "?key="
                    + steamAPIKey + (matchId != 0 ? "&match_id=" + matchId : "")).build()).execute().body()
                    .string();

            if (rawJson.contains("<title>Forbidden</title>")) {
                LocalBroadcastManager.getInstance(getContext())
                        .sendBroadcast(new Intent(SteamAuth.STEAM_API_KEY_INCORRECT));

                return null;
            }

            Gson gson = new Gson();
            try {
                LiveLeagueGames liveLeagueGames = gson.fromJson(rawJson, LiveLeagueGames.class);

                if (liveLeagueGames == null) {
                    return null; // no live data.
                } else if (liveLeagueGames.result == null) {
                    return null;
                } else if (liveLeagueGames.result.games == null) {
                    return null;
                } else if (liveLeagueGames.result.games.length == 0) {
                    return null;
                } else {
                    mGame = liveLeagueGames.result.games; // return all live games.
                    return mGame;
                }
            } catch (RuntimeException e) {
                LogUtils.LOGD(TAG, "Steam Web API is down");
            }

        } catch (IOException e) {
            LogUtils.LOGD(TAG, "Could not grab match json");
        }

        return null;
    }

    protected void onReleaseResources(DotaGameData[] data) {

    }
}