package tr.xip.wanikani.content.notification;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import tr.xip.wanikani.content.notification.NotificationPreferences;
import tr.xip.wanikani.content.notification.NotificationPublisher;
import tr.xip.wanikani.content.notification.NotificationScheduler;
import tr.xip.wanikani.database.DatabaseManager;
import tr.xip.wanikani.managers.PrefManager;

/**
 * This receiver is called on every screen unlock. It schedules an alarm to set off on the next review
 * time. It also checks whether a specific amount of time has passed since the last time a
 * notification was shown and calls {@link tr.xip.wanikani.content.notification.NotificationPublisher}
 * to display a new notification if any lessons are available.
 */
public class NotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        handleSituation(context);
    }

    public void handleSituation(Context context) {
        NotificationPreferences prefs = new NotificationPreferences(context);
        PrefManager prefManager = new PrefManager(context);

        if (!prefManager.isFirstLaunch() && prefManager.notificationsEnabled()) {
            /** Schedule an alarm if none is scheduled yet */
            if (!prefs.isAlarmSet() && new DatabaseManager(context).getStudyQueue().getAvailableReviewsCount() == 0)
                new NotificationScheduler(context).schedule();

            /** Show a notification anyways if a given period of time has passed */
            if (prefs.shouldShowNotification())
                new NotificationPublisher().publish(context);
        }
    }
}
@@ -442,16 +442,58 @@
 Manager;
+%0Aimport tr.xip.wanikani.models.StudyQueue;
 %0A%0A/**%0A *
@@ -1382,34 +1382,26 @@
     
-if (!prefs.isAlarmSet() &&
+StudyQueue queue =
  new
@@ -1441,16 +1441,139 @@
 yQueue()
+;%0A            //noinspection SimplifiableConditionalExpression%0A            if (!prefs.isAlarmSet() && queue != null ? queue
 .getAvai
@@ -1596,17 +1596,26 @@
 t() == 0
-)
+ : true) %7B
 %0A       
@@ -1670,17 +1670,30 @@
 dule();%0A
+            %7D
 %0A
-
         
@@ -1815,16 +1815,18 @@
 ation())
+ %7B
 %0A       
@@ -1872,32 +1872,46 @@
 blish(context);%0A
+            %7D%0A
         %7D%0A    %7D%0A

Fix occasional crash on creen unlock