net.noio.Reminder.NotificationService.java Source code

Java tutorial

Introduction

Here is the source code for net.noio.Reminder.NotificationService.java

Source

/*
*    Reminder
*    Copyright (C) 2014 Josten Moore
*
*    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 3 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, see <http://www.gnu.org/licenses/>.
*    
*    Truly "Free" agreement
*
*    This app and its source may not be profited off of.
*    This app and its source may not have ads.
*    This app and its source may not appear on non-free app stores.
*    You may not redistribute this app and its source asking for donations or any kind of fee
*    If distributing the binary form, this application you must link back to original bitbucket.org/josten/reminder repository.
*/

/*
 * This is the notification service for ReminderNotifications.
 * It runs every 15 minutes looking for alarms to setup.
 */

package net.noio.Reminder;

import java.util.LinkedList;
import android.app.AlarmManager;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

public class NotificationService extends IntentService {
    private final int ONE_HOUR = 3600;
    private final int SIX_HOUR = 21600;
    private final int TWELVE_HOUR = 43200;
    private final int TWENTYFOUR_HOUR = 86400;
    private final int VIBRATE_TIME = 1000; // milliseconds
    private static int loopcounter = 0;

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

    private void Notify(Intent intent) {
        // Notification on drop down bar
        PendingIntent pimain = PendingIntent.getActivity(getApplicationContext(), intent.getIntExtra("id", 0),
                intent, 0);
        NotificationCompat.Builder notif = new NotificationCompat.Builder(getApplicationContext());
        notif.setSmallIcon(R.drawable.ic_launcher);
        notif.setContentTitle(intent.getStringExtra("reminder"));
        notif.setContentText(intent.getStringExtra("string"));
        notif.setContentIntent(pimain);
        notif.setAutoCancel(true);

        NotificationManager notman = (NotificationManager) getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
        v.vibrate(this.VIBRATE_TIME);
        notman.notify(intent.getIntExtra("id", 0), notif.build());
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v("NotificationService", "Starting NotificationService run");

        LinkedList<String[]> list;
        list = ReminderDB.getReminderTimes(getApplicationContext());
        long system_time = System.currentTimeMillis();

        // Alarm notification
        AlarmManager ream = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        PendingIntent repi = PendingIntent.getService(this, 6600, intent, 0);
        ream.set(AlarmManager.RTC_WAKEUP, system_time + 60000, repi);

        for (int i = 0; i < list.size(); i++) {
            String[] data = list.get(i);

            if (data[2].equals("N")) {
                long delta = Long.parseLong(data[1]) - System.currentTimeMillis();

                Log.v("NotificationService", "TIME REMAINING: " + delta);
                Log.v("NotificationService", "1 HOUR: " + this.ONE_HOUR * 1000);
                Log.v("NotificationService", "6 HOUR: " + this.SIX_HOUR * 1000);
                Log.v("NotificationService", "12 HOUR: " + this.TWELVE_HOUR * 1000);
                Log.v("NotificationService", "24 HOUR: " + this.TWENTYFOUR_HOUR * 1000);
                Log.v("NotificationService", "In reminder loop");

                Intent intent1 = new Intent(this, MainActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                if (delta < this.ONE_HOUR * 1000) {
                    Log.v("NotificationService", "IN 1");
                    if (ReminderDB.checkNotify(getApplicationContext(), 1, data[0])) {
                        System.out.println("IN 1b");
                        intent1.putExtra("string", data[0]);
                        intent1.putExtra("reminder", "One-hour Reminder");
                        intent1.putExtra("id",
                                Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));
                        this.Notify(intent1);
                        ReminderDB.disableNotification(getApplicationContext(), 1, data[0]);
                    }
                } else if (delta < this.SIX_HOUR * 1000 && delta > this.ONE_HOUR * 1000) {
                    Log.v("NotificationService", "IN 6");
                    if (ReminderDB.checkNotify(getApplicationContext(), 2, data[0])) {
                        intent1.putExtra("string", data[0]);
                        intent1.putExtra("reminder", "Six-hour Reminder");
                        intent1.putExtra("id",
                                Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));
                        this.Notify(intent1);
                        ReminderDB.disableNotification(getApplicationContext(), 2, data[0]);
                    }
                } else if (delta < this.TWELVE_HOUR * 1000 && delta > this.SIX_HOUR * 1000) {
                    Log.v("NotificationService", "IN 12");
                    if (ReminderDB.checkNotify(getApplicationContext(), 3, data[0])) {
                        intent1.putExtra("string", data[0]);
                        intent1.putExtra("reminder", "Twelve-hour Reminder");
                        intent1.putExtra("id",
                                Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));
                        this.Notify(intent1);
                        ReminderDB.disableNotification(getApplicationContext(), 3, data[0]);
                    }
                } else if (delta < this.TWENTYFOUR_HOUR * 1000 && delta > this.TWELVE_HOUR * 1000) {
                    Log.v("NotificationService", "IN 24");
                    if (ReminderDB.checkNotify(getApplicationContext(), 4, data[0])) {
                        intent1.putExtra("string", data[0]);
                        intent1.putExtra("reminder", "Twentyfour-hour Reminder");
                        intent1.putExtra("id",
                                Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));
                        this.Notify(intent1);
                        ReminderDB.disableNotification(getApplicationContext(), 4, data[0]);
                    }
                }
            } else if (data[2].equals("Y")) {
                // every minute
                if (data[3].equals("Y")) {
                    Log.v("NotificationService", "Every minute reminder for " + data[0]);

                    intent.putExtra("string", data[0]);
                    intent.putExtra("reminder", "Every minute reminder");
                    intent.putExtra("id",
                            Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));

                    this.Notify(intent);
                }

                // every hour
                if (data[4].equals("Y")) {
                    // fix to be hourly
                    Log.v("NotificationService", "Hourly reminder for " + data[0]);

                    if (this.loopcounter == 60) {
                        this.loopcounter = 0;
                        intent.putExtra("string", data[0]);
                        intent.putExtra("reminder", "Hourly reminder");
                        intent.putExtra("id",
                                Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));

                        this.Notify(intent);
                    }

                    this.loopcounter += 1;
                }
            }
        }

        Log.v("NotificationService", "Ending NotificationService run");
    }
}