de.egore911.drilog.tasks.JsonLoadTask.java Source code

Java tutorial

Introduction

Here is the source code for de.egore911.drilog.tasks.JsonLoadTask.java

Source

/* @(#)JsonLoadTask.java
 *
 * Copyright (C) 2011-2012 Christoph Brill
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package de.egore911.drilog.tasks;

import android.os.AsyncTask;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.util.Pair;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import de.egore911.drilog.R;
import de.egore911.drilog.ShowActivity;
import de.egore911.drilog.adapter.ShowDataAdapter;
import de.egore911.drilog.fragments.LogviewFragment;
import de.egore911.drilog.model.Comment;
import de.egore911.drilog.model.ListElement;
import de.egore911.drilog.util.DateUtil;
import de.egore911.drilog.util.HttpUtil;

import javax.annotation.Nonnull;

/**
 * Load the logging data from the logging server.
 *
 * @author Christoph Brill <egore911@egore911.de>
 * @since 0.1
 */
public class JsonLoadTask extends AsyncTask<String, Void, String> {

    private final ShowActivity activity;
    private final LogviewFragment logviewFragment;
    private final FragmentManager fragmentManager;
    private final Set<String> watchList;
    private final Menu mMenu;

    private String exceptionMessage;

    public JsonLoadTask(@Nonnull ShowActivity activity, @Nonnull LogviewFragment logviewFragment,
            @Nonnull List<String> watchList, @Nonnull FragmentManager fragmentManager, Menu menu) {
        this.activity = activity;
        this.logviewFragment = logviewFragment;
        this.fragmentManager = fragmentManager;
        mMenu = menu;
        this.watchList = new HashSet<>(watchList);
    }

    @Override
    protected void onPreExecute() {
        // FIXME crashes on Android 21: activity.setSupportProgressBarIndeterminateVisibility(true);
    }

    @Override
    protected String doInBackground(String... params) {
        exceptionMessage = null;
        String url = (String) params[0];
        try {
            Pair<String, URL> result = HttpUtil.getPlainTextFromUrl(url);
            return result.first;
        } catch (IOException e) {
            Log.e(getClass().getSimpleName(), e.getMessage(), e);
            exceptionMessage = e.getClass().getSimpleName() + ": " + e.getLocalizedMessage();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String json) {
        ShowDataAdapter result;

        JSONObject o = null;
        try {
            if (json != null) {
                o = new JSONObject(json);
            }
        } catch (JSONException e) {
            Log.e(getClass().getSimpleName(), e.getMessage(), e);
            exceptionMessage = e.getClass().getSimpleName() + ": " + e.getLocalizedMessage();
        }

        result = (ShowDataAdapter) logviewFragment.getListAdapter();
        if (result == null) {
            result = new ShowDataAdapter(activity, o, watchList);
        } else {
            result.updateElements(o, watchList);
        }

        if (exceptionMessage != null) {
            Toast.makeText(activity, exceptionMessage, Toast.LENGTH_LONG).show();
        }
        activity.setTitle(result.channel);
        activity.updateDateText(DateUtil.getDateString(result.date == null ? new Date() : result.date));
        ListView listView = null;
        int oldposition;
        try {
            listView = logviewFragment.getListView();
            oldposition = listView.getFirstVisiblePosition();
        } catch (IllegalStateException e) {
            oldposition = -1;
        }
        logviewFragment.setListAdapter(result);

        // Now do some scrolling
        String anchor = activity.getAnchor();
        activity.setAnchor(null);
        if (anchor != null) {
            List<ListElement> listElements = result.getListElements();
            int position = 0;
            for (ListElement listElement : listElements) {
                if (listElement instanceof Comment) {
                    Comment c = (Comment) listElement;
                    if (c.anchor.compareTo(anchor) >= 0) {
                        result.anchor = c;
                        break;
                    }
                }
                position++;
            }
            if (listView != null) {
                //listView.smoothScrollToPosition(position);
                listView.setSelection(position);
            }
        } else {
            if (oldposition >= 0 && listView != null) {
                //listView.smoothScrollToPosition(oldposition);
            }
        }

        fragmentManager.beginTransaction().replace(R.id.content_frame, logviewFragment).commit();

        // FIXME crashes on Android 21: activity.setSupportProgressBarIndeterminateVisibility(false);
    }

}