org.akop.crosswords.service.CrosswordFetchService.java Source code

Java tutorial

Introduction

Here is the source code for org.akop.crosswords.service.CrosswordFetchService.java

Source

// Copyright (c) 2014-2015 Akop Karapetyan
//
// 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 org.akop.crosswords.service;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;

import org.akop.crosswords.Crosswords;
import org.akop.crosswords.Storage;
import org.akop.xross.object.Crossword;

import java.util.ArrayList;
import java.util.List;

public class CrosswordFetchService extends IntentService {
    public static final String ACTION_FETCH_COMPLETE = "org.akop.crosswords.action.Fetch";

    public static final String INTENT_RESULTS = "results";

    public CrosswordFetchService() {
        super("CrosswordFetchService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Parcelable[] parcelables = intent.getParcelableArrayExtra("requests");
        boolean writeToStorage = intent.getBooleanExtra("writeToStorage", false);
        boolean broadcastResults = intent.getBooleanExtra("broadcastResults", false);

        Storage storage = Storage.getInstance();
        CrosswordFetchRunnable.Request[] requests = new CrosswordFetchRunnable.Request[parcelables.length];
        System.arraycopy(parcelables, 0, requests, 0, parcelables.length);

        CrosswordFetchRunnable.Result[] resultArray = new CrosswordFetchRunnable.Result[requests.length];

        List<CrosswordFetchRunnable.Result> successes = new ArrayList<>();
        List<CrosswordFetchRunnable.Result> failures = new ArrayList<>();

        for (int i = 0, n = requests.length; i < n; i++) {
            CrosswordFetchRunnable f = new CrosswordFetchRunnable(requests[i]);
            f.run();

            CrosswordFetchRunnable.Result result = f.getResult();
            if (result.success()) {
                if (writeToStorage) {
                    storage.write(Storage.FOLDER_INBOX, result.getCrossword());
                }
                successes.add(result);
            } else {
                failures.add(result);
            }

            resultArray[i] = result;
        }

        if (broadcastResults) {
            Intent outgoing = new Intent(ACTION_FETCH_COMPLETE);
            outgoing.putExtra(INTENT_RESULTS, resultArray);

            LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
            lbm.sendBroadcast(outgoing);
        }

        if (failures.size() < 1) {
            notifySuccesses(successes);
        } else {
            notifyFailures(failures);
        }
    }

    private void notifySuccesses(List<CrosswordFetchRunnable.Result> results) {
        // FIXME
        // R.string.puzzle_downloaded_successfully
        //CrosswordActivity.launch(getActivity(), puzzleId);
        Crosswords.logv("FIXME - FetchService has %d successes", results.size());
    }

    private void notifyFailures(List<CrosswordFetchRunnable.Result> results) {
        // FIXME
        Crosswords.logv("FIXME - FetchService has %d errors", results.size());
    }

    public static void startService(Context context, CrosswordFetchRunnable.Request[] requests,
            boolean broadcastResults, boolean writeToStorage) {
        Intent intent = new Intent(context, CrosswordFetchService.class);
        intent.putExtra("requests", requests);
        intent.putExtra("writeToStorage", writeToStorage);
        intent.putExtra("broadcastResults", broadcastResults);
        context.startService(intent);
    }
}