Search in sources :

Example 1 with CafeteriaDao

use of de.tum.in.tumcampusapp.component.ui.cafeteria.CafeteriaDao in project TumCampusApp by TCA-Team.

the class FavoriteDishAlarmScheduler method onReceive.

/**
 * Can either receive a date or a boolean cancelNotifications value. This way other activities
 * can close the currently opened notifications and it is possible to schedule dates, where the
 * alarm has to check for favorite dishes.
 *
 * @param context
 * @param extra   Extra can either be "cancelNotifications" or a date, when the alarm should check, if there are any
 *                favorite dishes at a given date.
 */
@Override
public void onReceive(Context context, Intent extra) {
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    cancelFoodNotifications(mNotificationManager);
    if (extra.getBooleanExtra("cancelNotifications", false)) {
        return;
    }
    String triggeredAt = extra.getStringExtra("triggeredAt");
    Calendar triggeredCal = Calendar.getInstance();
    triggeredCal.setTime(DateUtils.getDate(triggeredAt));
    CafeteriaMenuManager cm = new CafeteriaMenuManager(context);
    HashMap<Integer, HashSet<CafeteriaMenu>> scheduledNow = cm.getServedFavoritesAtDate(triggeredAt);
    if (scheduledNow == null) {
        Utils.log("FavoriteDishAlarmScheduler: Scheduled now is null, onReceived aborted");
        return;
    }
    CafeteriaDao dao = TcaDb.getInstance(context).cafeteriaDao();
    for (Integer mensaId : scheduledNow.keySet()) {
        StringBuilder message = new StringBuilder();
        int menuCount = 0;
        for (CafeteriaMenu menu : scheduledNow.get(mensaId)) {
            message.append(menu.getName()).append('\n');
            menuCount++;
        }
        ACTIVE_NOTIFICATIONS.add(mensaId);
        String mensaName = dao.getMensaNameFromId(mensaId);
        Intent intent = new Intent(context, CafeteriaActivity.class);
        intent.putExtra(Const.MENSA_FOR_FAVORITEDISH, mensaId);
        PendingIntent pi = PendingIntent.getActivity(context, mensaId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, Const.NOTIFICATION_CHANNEL_CAFETERIA).setSmallIcon(R.drawable.ic_notification).setContentTitle(mensaName + (menuCount > 1 ? " (" + menuCount + ")" : "")).setStyle(new NotificationCompat.BigTextStyle().bigText(message.toString())).setContentText(message.toString()).setAutoCancel(true).setLargeIcon(Utils.getLargeIcon(context, R.drawable.ic_cutlery)).setContentIntent(pi).setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true);
        mNotificationManager.notify(IDENTIFIER_STRING, mensaId, mBuilder.build());
    }
}
Also used : CafeteriaMenu(de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu) NotificationManager(android.app.NotificationManager) Calendar(java.util.Calendar) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) CafeteriaMenuManager(de.tum.in.tumcampusapp.component.ui.cafeteria.controller.CafeteriaMenuManager) CafeteriaDao(de.tum.in.tumcampusapp.component.ui.cafeteria.CafeteriaDao) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) HashSet(java.util.HashSet)

Aggregations

NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 CafeteriaDao (de.tum.in.tumcampusapp.component.ui.cafeteria.CafeteriaDao)1 CafeteriaMenuManager (de.tum.in.tumcampusapp.component.ui.cafeteria.controller.CafeteriaMenuManager)1 CafeteriaMenu (de.tum.in.tumcampusapp.component.ui.cafeteria.model.CafeteriaMenu)1 Calendar (java.util.Calendar)1 HashSet (java.util.HashSet)1