com.none.tom.simplerssreader.loader.IsFeedUpdateAvailableLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.none.tom.simplerssreader.loader.IsFeedUpdateAvailableLoader.java

Source

// Copyright (c) 2018, Tom Geiselmann
//
// 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.

package com.none.tom.simplerssreader.loader;

import android.content.AsyncTaskLoader;
import android.content.Context;

import com.none.tom.simplerssreader.feed.CurrentFeed;
import com.none.tom.simplerssreader.net.FeedDownloader;
import com.none.tom.simplerssreader.parser.FeedParser;
import com.none.tom.simplerssreader.utils.ErrorHandler;
import com.none.tom.simplerssreader.utils.SharedPrefUtils;
import com.rometools.rome.io.FeedException;

import org.apache.commons.io.IOUtils;
import org.xmlpull.v1.XmlPullParserException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import static com.none.tom.simplerssreader.utils.Constants.CURRENT_FEED_SIZE_LIMIT;

public class IsFeedUpdateAvailableLoader extends AsyncTaskLoader<String> {
    public static final int ID = 2;

    public IsFeedUpdateAvailableLoader(final Context context) {
        super(context);
    }

    @Override
    public String loadInBackground() {
        final Context context = getContext();

        final String feedUrl = SharedPrefUtils.getCurrentFeedUrl(context);
        final InputStream in = FeedDownloader.getInputStream(context, feedUrl);

        if (in == null) {
            return null;
        }

        final ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            if (IOUtils.copy(in, out) > CURRENT_FEED_SIZE_LIMIT) {
                ErrorHandler.setErrno(ErrorHandler.ERROR_HTTP_FILE_SIZE);
                return null;
            }

            final byte[] outArray = out.toByteArray();
            final InputStream feed = new ByteArrayInputStream(outArray);

            final int position = SharedPrefUtils.getCurrentFeedPosition(context);
            final String id = SharedPrefUtils.getSubscriptionIdAt(context, position);

            final FeedParser.Result result = FeedParser.isFeedUpdateAvailable(feed, id);

            if (!result.isUpdateAvailable()) {
                ErrorHandler.setErrno(0);
            } else {
                CurrentFeed.set(FeedParser.parseCurrentFeed(new ByteArrayInputStream(outArray)));
                return result.getId();
            }
        } catch (final UnsupportedEncodingException e) {
            ErrorHandler.setErrno(ErrorHandler.ERROR_UNSUPPORTED_ENCODING, e);
        } catch (final XmlPullParserException | IOException | FeedException e) {
            ErrorHandler.setErrno(ErrorHandler.ERROR_PARSER, e);
        }

        return null;
    }

    @Override
    public void deliverResult(final String result) {
        if (isStarted()) {
            super.deliverResult(result);
        }
    }

    @Override
    protected void onStartLoading() {
        forceLoad();
    }

    @Override
    protected void onStopLoading() {
        cancelLoad();
    }

    @Override
    public void onCanceled(final String result) {
        super.onCanceled(result);
    }

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

        onStopLoading();
    }
}