org.maikelwever.droidpile.background.CheckForMailService.java Source code

Java tutorial

Introduction

Here is the source code for org.maikelwever.droidpile.background.CheckForMailService.java

Source

/*
 * This file is part of Droidpile: a Mailpile client for Android.
 *
 * You should have recieved a copy of the LICENSE file along with Droidpile.
 * If not: see <https://github.com/maikelwever/droidpile/blob/master/LICENSE>
 */
package org.maikelwever.droidpile.background;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.Html;
import android.util.Log;

import org.json.JSONException;
import org.maikelwever.droidpile.MainActivity;
import org.maikelwever.droidpile.R;
import org.maikelwever.droidpile.ViewMessageActivity;
import org.maikelwever.droidpile.backend.ApiConnecter;
import org.maikelwever.droidpile.backend.ApiConnecterFactory;
import org.maikelwever.droidpile.helpers.ContextToSettings;
import org.maikelwever.droidpile.models.ApiSearchResponse;
import org.maikelwever.droidpile.models.MailpileMessage;

import java.io.IOException;

/**
 */
public class CheckForMailService extends IntentService {
    public CheckForMailService() {
        super("CheckForMailService");
    }

    public CheckForMailService(String name) {
        super(name);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("droidpile", "Background task called!");
        ApiConnecter api = ApiConnecterFactory.getApiConnecter(getApplicationContext());
        try {
            int previousTotal = api.getRemoteTotal();
            ApiSearchResponse response = api.search(1, 2, "in:inbox");
            api.setRemoteTotal(response.totalItemCount);

            if (response.totalItemCount > previousTotal) {
                ApiSearchResponse response2 = api.search(1, Math.max(response.totalItemCount - previousTotal, 20),
                        "in:inbox");
                if (response2.messages.size() > 1) {
                    // Add notification for you have 'x' new messages
                    String newMessageCount = Integer.toString(response.totalItemCount - previousTotal);
                    if ((response.totalItemCount - previousTotal) > 20) {
                        newMessageCount = "20+";
                    }

                    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1337,
                            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
                    builder.setSmallIcon(R.drawable.ic_launcher);
                    builder.setContentTitle("You have " + newMessageCount + " new messages.");
                    builder.setContentText("Touch here to go to your inbox.");
                    builder.setContentIntent(pendingIntent);

                    NotificationManager notificationManager = (NotificationManager) getSystemService(
                            Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(1234, builder.build());

                } else if (response2.messages.size() == 1) {
                    // Add specific notification that'll redirect to message

                    MailpileMessage message = response2.messages.get(0);
                    Intent notificationIntent = new Intent(getApplicationContext(), ViewMessageActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1337,
                            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT, message.toBundle());

                    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
                    builder.setSmallIcon(R.drawable.ic_launcher);
                    builder.setContentTitle("You have a new message.");
                    builder.setContentText(Html.fromHtml("<b>" + message.title + "</b> " + message.snippet));
                    builder.setContentIntent(pendingIntent);

                    NotificationManager notificationManager = (NotificationManager) getSystemService(
                            Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(1234, builder.build());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.d("droidpile", "Background task finished");
        BootReciever.registerWithAlarmManager(getApplicationContext());
    }
}