com.none.tom.simplerssreader.service.FeedUpdateBackgroundService.java Source code

Java tutorial

Introduction

Here is the source code for com.none.tom.simplerssreader.service.FeedUpdateBackgroundService.java

Source

// Copyright (c) 2017-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.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
import android.support.v4.app.NotificationCompat;

import com.none.tom.simplerssreader.R;
import com.none.tom.simplerssreader.activity.MainActivity;
import com.none.tom.simplerssreader.parser.FeedParser;
import com.none.tom.simplerssreader.net.FeedDownloader;
import com.none.tom.simplerssreader.utils.ErrorHandler;
import com.none.tom.simplerssreader.utils.SharedPrefUtils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class FeedUpdateBackgroundService extends JobIntentService {
    private static final String NOTIFICATION_CHANNEL_ID = "com.none.tom.simplerssreader.service";
    private static final String NOTIFICATION_CHANNEL_NAME = "Feed Updates";

    private static final int ID_NOTIFICATION = 1;

    @Override
    protected void onHandleWork(@NonNull final Intent work) {
        final Context context = getApplicationContext();

        final List<String> subscriptionUpdates = new ArrayList<>();
        final int subscriptionsCnt = SharedPrefUtils.getSubscriptionsCnt(context);

        for (int i = 0; i < subscriptionsCnt; i++) {
            final String url = SharedPrefUtils.getSubscriptionUrlAt(context, i);
            final String id = SharedPrefUtils.getSubscriptionIdAt(context, i);

            final InputStream in = FeedDownloader.getInputStream(context, url);

            if (ErrorHandler.getErrno() != 0) {
                continue;
            }

            try {
                final FeedParser.Result result = FeedParser.isFeedUpdateAvailable(in, id);

                if (result.isUpdateAvailable()) {
                    SharedPrefUtils.updateSubscriptionIdAt(context, result.getId(), i);
                    subscriptionUpdates.add(SharedPrefUtils.getSubscriptionTitleAt(context, i));
                }
            } catch (final Exception ignored) {
            }
        }

        if (!subscriptionUpdates.isEmpty()) {
            showNotification(context, subscriptionUpdates);
        }
    }

    @SuppressWarnings({ "ConstantConditions", "deprecation" })
    private static void showNotification(final Context context, final List<String> payload) {
        final NotificationManager manager = context.getSystemService(NotificationManager.class);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            final NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
                    NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);

            channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            channel.setBypassDnd(false);
            channel.enableLights(true);
            channel.setShowBadge(true);
            channel.enableVibration(true);

            manager.createNotificationChannel(channel);
        }

        final PendingIntent intent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
                PendingIntent.FLAG_UPDATE_CURRENT);

        final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle()
                .setBigContentTitle(context.getString(R.string.notification_title_big));

        final int size = payload.size();

        for (int i = 0; i < size; i++) {
            style.addLine(payload.get(i));
        }

        manager.notify(ID_NOTIFICATION, new NotificationCompat.Builder(context)
                .setChannelId(NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.ic_rss_feed_white_24dp)
                .setContentTitle(context.getString(R.string.notification_title))
                .setContentText(context.getString(R.string.notification_text)).setContentIntent(intent)
                .setStyle(style).setWhen(System.currentTimeMillis()).setAutoCancel(true).setShowWhen(true).build());
    }
}