// Path: app/src/main/java/io/github/droidkaigi/confsched2017/receiver/NotificationReceiver.java
// public class NotificationReceiver extends BroadcastReceiver {
// 
//     private static final String TAG = NotificationReceiver.class.getSimpleName();
// 
//     private static final String KEY_SESSION_ID = "session_id";
// 
//     private static final String KEY_TITLE = "title";
// 
//     private static final String KEY_TEXT = "text";
// 
//     private static final String GROUP_NAME = "droidkaigi";
//     private static final int GROUP_NOTIFICATION_ID = 0;
// 
//     public static Intent createIntent(Context context, int sessionId, String title, String text) {
//         Intent intent = new Intent(context, NotificationReceiver.class);
//         intent.putExtra(NotificationReceiver.KEY_SESSION_ID, sessionId);
//         intent.putExtra(NotificationReceiver.KEY_TITLE, title);
//         intent.putExtra(NotificationReceiver.KEY_TEXT, text);
//         return intent;
//     }
// 
//     @Override
//     public void onReceive(Context context, Intent intent) {
//         DefaultPrefs prefs = DefaultPrefs.get(context);
//         if (!prefs.getNotificationFlag()) {
//             Timber.tag(TAG).v("Notification is disabled.");
//             return;
//         }
// 
//         showGroupNotification(context);
// 
//         showChildNotification(context, intent, prefs.getHeadsUpFlag());
//     }
// 
//     /**
//      * Show group notification
//      * @param context Context
//      */
//     private void showGroupNotification(Context context) {
//         // Group notification is supported on Android N and Android Wear.
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//             Notification groupNotification = new NotificationCompat.Builder(context)
//                     .setSmallIcon(R.drawable.ic_notification)
//                     .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
//                     .setColor(ContextCompat.getColor(context, R.color.theme))
//                     .setAutoCancel(true)
//                     .setGroup(GROUP_NAME)
//                     .setGroupSummary(true)
//                     .build();
//             NotificationManagerCompat.from(context).notify(GROUP_NOTIFICATION_ID, groupNotification);
//         }
//     }
// 
//     /**
//      * Show child notification
//      * @param context Context
//      * @param intent Intent
//      */
//     private void showChildNotification(Context context, Intent intent, boolean headsUp) {
//         int sessionId = intent.getIntExtra(KEY_SESSION_ID, 0);
//         String title = intent.getStringExtra(KEY_TITLE);
//         String text = intent.getStringExtra(KEY_TEXT);
//         int priority = headsUp ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_DEFAULT;
//         Intent openIntent = SessionDetailActivity.createIntent(context, sessionId, MainActivity.class);
//         openIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//         NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
//                 .setTicker(title)
//                 .setContentTitle(title)
//                 .setContentText(text)
//                 .setSmallIcon(R.drawable.ic_notification)
//                 .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
//                 .setColor(ContextCompat.getColor(context, R.color.theme))
//                 .setAutoCancel(true)
//                 .setContentIntent(pendingIntent)
//                 .setDefaults(NotificationCompat.DEFAULT_ALL)
//                 .setPriority(priority);
//         // Group notification is supported on Android N and Android Wear.
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//             builder.setGroup(GROUP_NAME).setGroupSummary(false);
//         }
//         NotificationManagerCompat.from(context).notify(sessionId, builder.build());
//     }
// }


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import io.github.droidkaigi.confsched2017.R;
import io.github.droidkaigi.confsched2017.model.Session;
import io.github.droidkaigi.confsched2017.pref.DefaultPrefs;
import io.github.droidkaigi.confsched2017.receiver.NotificationReceiver;
            time = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(5);
        }

        if (System.currentTimeMillis() < time) {
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, createAlarmIntent(context, session));
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, time, createAlarmIntent(context, session));
            }
        }
    }

    public static void unregisterAlarm(@NonNull Context context, @NonNull Session session) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(createAlarmIntent(context, session));
    }

    private static PendingIntent createAlarmIntent(@NonNull Context context, @NonNull Session session) {
        String title = context.getString(R.string.notification_title, session.title);
        Date displaySTime = LocaleUtil.getDisplayDate(session.stime, context);
        Date displayETime = LocaleUtil.getDisplayDate(session.etime, context);
        String room = "";
        if (session.room != null) {
            room = session.room.name;
        }
        String text = context.getString(R.string.notification_message,
                DateUtil.getHourMinute(displaySTime),
                DateUtil.getHourMinute(displayETime),
                room);
// Path: app/src/main/java/io/github/droidkaigi/confsched2017/receiver/NotificationReceiver.java
// public class NotificationReceiver extends BroadcastReceiver {
// 
//     private static final String TAG = NotificationReceiver.class.getSimpleName();
// 
//     private static final String KEY_SESSION_ID = "session_id";
// 
//     private static final String KEY_TITLE = "title";
// 
//     private static final String KEY_TEXT = "text";
// 
//     private static final String GROUP_NAME = "droidkaigi";
//     private static final int GROUP_NOTIFICATION_ID = 0;
// 
//     public static Intent createIntent(Context context, int sessionId, String title, String text) {
//         Intent intent = new Intent(context, NotificationReceiver.class);
//         intent.putExtra(NotificationReceiver.KEY_SESSION_ID, sessionId);
//         intent.putExtra(NotificationReceiver.KEY_TITLE, title);
//         intent.putExtra(NotificationReceiver.KEY_TEXT, text);
//         return intent;
//     }
// 
//     @Override
//     public void onReceive(Context context, Intent intent) {
//         DefaultPrefs prefs = DefaultPrefs.get(context);
//         if (!prefs.getNotificationFlag()) {
//             Timber.tag(TAG).v("Notification is disabled.");
//             return;
//         }
// 
//         showGroupNotification(context);
// 
//         showChildNotification(context, intent, prefs.getHeadsUpFlag());
//     }
// 
//     /**
//      * Show group notification
//      * @param context Context
//      */
//     private void showGroupNotification(Context context) {
//         // Group notification is supported on Android N and Android Wear.
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//             Notification groupNotification = new NotificationCompat.Builder(context)
//                     .setSmallIcon(R.drawable.ic_notification)
//                     .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
//                     .setColor(ContextCompat.getColor(context, R.color.theme))
//                     .setAutoCancel(true)
//                     .setGroup(GROUP_NAME)
//                     .setGroupSummary(true)
//                     .build();
//             NotificationManagerCompat.from(context).notify(GROUP_NOTIFICATION_ID, groupNotification);
//         }
//     }
// 
//     /**
//      * Show child notification
//      * @param context Context
//      * @param intent Intent
//      */
//     private void showChildNotification(Context context, Intent intent, boolean headsUp) {
//         int sessionId = intent.getIntExtra(KEY_SESSION_ID, 0);
//         String title = intent.getStringExtra(KEY_TITLE);
//         String text = intent.getStringExtra(KEY_TEXT);
//         int priority = headsUp ? NotificationCompat.PRIORITY_HIGH : NotificationCompat.PRIORITY_DEFAULT;
//         Intent openIntent = SessionDetailActivity.createIntent(context, sessionId, MainActivity.class);
//         openIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, openIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//         NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
//                 .setTicker(title)
//                 .setContentTitle(title)
//                 .setContentText(text)
//                 .setSmallIcon(R.drawable.ic_notification)
//                 .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
//                 .setColor(ContextCompat.getColor(context, R.color.theme))
//                 .setAutoCancel(true)
//                 .setContentIntent(pendingIntent)
//                 .setDefaults(NotificationCompat.DEFAULT_ALL)
//                 .setPriority(priority);
//         // Group notification is supported on Android N and Android Wear.
//         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//             builder.setGroup(GROUP_NAME).setGroupSummary(false);
//         }
//         NotificationManagerCompat.from(context).notify(sessionId, builder.build());
//     }
// }


// Path: app/src/main/java/io/github/droidkaigi/confsched2017/util/AlarmUtil.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import io.github.droidkaigi.confsched2017.R;
import io.github.droidkaigi.confsched2017.model.Session;
import io.github.droidkaigi.confsched2017.pref.DefaultPrefs;
import io.github.droidkaigi.confsched2017.receiver.NotificationReceiver;

            time = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(5);
        }

        if (System.currentTimeMillis() < time) {
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, createAlarmIntent(context, session));
            } else {
                alarmManager.set(AlarmManager.RTC_WAKEUP, time, createAlarmIntent(context, session));
            }
        }
    }

    public static void unregisterAlarm(@NonNull Context context, @NonNull Session session) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(createAlarmIntent(context, session));
    }

    private static PendingIntent createAlarmIntent(@NonNull Context context, @NonNull Session session) {
        String title = context.getString(R.string.notification_title, session.title);
        Date displaySTime = LocaleUtil.getDisplayDate(session.stime, context);
        Date displayETime = LocaleUtil.getDisplayDate(session.etime, context);
        String room = "";
        if (session.room != null) {
            room = session.room.name;
        }
        String text = context.getString(R.string.notification_message,
                DateUtil.getHourMinute(displaySTime),
                DateUtil.getHourMinute(displayETime),
                room);
        Intent intent = NotificationReceiver.createIntent(context, session.id, title, text);