Search in sources :

Example 1 with AlertsPage

use of com.giua.pages.AlertsPage in project Giua-App by Giua-app.

the class CheckNewsReceiver method checkAndSendNotificationForAgenda.

/**
 * Controlla e invia le notifiche riguardanti compiti e verifiche.
 *
 * @return La somma dei compiti e delle verifiche notificate. Serve a non far notificare anche gli avvisi.
 */
private int checkAndSendNotificationForAgenda() throws IOException {
    loggerManager.d("Inizio a controllare gli avvisi per le notifiche di compiti e verifiche");
    com.giua.utils.JsonParser jsonParser = new JsonParser();
    boolean canSendHomeworkNotification = SettingsData.getSettingBoolean(context, SettingKey.HOMEWORKS_NOTIFICATION);
    boolean canSendTestNotification = SettingsData.getSettingBoolean(context, SettingKey.TESTS_NOTIFICATION);
    if (!canSendTestNotification && !canSendHomeworkNotification)
        // Se non puo inviare nessuna notifica lo blocco
        return 0;
    loggerManager.d("Leggo il json per vedere gli avvisi dei compiti e delle verifiche già notificati");
    AlertsPage alertsPage = gS.getAlertsPage(false);
    File f = new File(context.getCacheDir() + "/alertsToNotify.json");
    if (!f.exists()) {
        createJsonNotificationFile(f, alertsPage);
        // Return perchè andrebbe a notificare tutti i vecchi compiti
        return 0;
    }
    BufferedReader file = new BufferedReader(new FileReader(context.getCacheDir() + "/alertsToNotify.json"));
    StringBuilder oldAlertsString = new StringBuilder();
    String read = file.readLine();
    if (read == null) {
        createJsonNotificationFile(f, alertsPage);
        return 0;
    }
    while (read != null) {
        oldAlertsString.append(read);
        read = file.readLine();
    }
    file.close();
    loggerManager.d("Faccio il parsing del json");
    // Lista con gli avvisi già notificati
    List<Alert> oldAlerts;
    if (oldAlertsString.toString().equals("")) {
        loggerManager.w("oldAlertsString è vuoto");
        oldAlerts = new Vector<>();
    } else
        oldAlerts = jsonParser.parseJsonForAlerts(oldAlertsString.toString());
    // Lista degli avvisi da notificare
    List<Alert> alertsToNotify = alertsPage.getAlertsToNotify(oldAlerts);
    // Salva gli avvisi (compresi i nuovi) nel json
    JsonBuilder jsonBuilder = new JsonBuilder(context.getCacheDir() + "/alertsToNotify.json", gS);
    jsonBuilder.writeAlerts(alertsPage.getAllAlertsWithFilters(false, "per la materia"));
    jsonBuilder.saveJson();
    loggerManager.d("Conto i compiti e le verifiche da notificare");
    // Conta i compiti da notificare
    int homeworkCounter = 0;
    // Conta le verifiche da notificare
    int testCounter = 0;
    // Lista in cui ci sono tutte le date dei compiti da notificare
    List<String> homeworkDates = new Vector<>(40);
    // Lista in cui ci sono tutte le date delle verifiche da notificare
    List<String> testDates = new Vector<>(40);
    // Lista in cui ci sono tutte le materie dei compiti da notificare
    List<String> homeworkSubjects = new Vector<>(40);
    // Lista in cui ci sono tutte le materie delle verifiche da notificare
    List<String> testSubjects = new Vector<>(40);
    for (Alert alert : alertsToNotify) {
        if (alert.object.startsWith("C")) {
            homeworkDates.add(alert.date);
            homeworkSubjects.add(alert.object.split(" per la materia ")[1]);
            homeworkCounter++;
        } else if (alert.object.startsWith("V")) {
            testDates.add(alert.date);
            testSubjects.add(alert.object.split(" per la materia ")[1]);
            testCounter++;
        }
    }
    loggerManager.d("Preparo le notifiche");
    StringBuilder homeworkNotificationText;
    StringBuilder testNotificationText;
    Notification homeworkNotification = null;
    Notification testNotification = null;
    if (canSendHomeworkNotification && homeworkCounter > 0) {
        String contentText;
        if (homeworkCounter == 1) {
            contentText = "Clicca per andare all' agenda";
            homeworkNotificationText = new StringBuilder("È stato programmato un nuovo compito di " + homeworkSubjects.get(0) + " per il giorno " + homeworkDates.get(0));
        } else {
            contentText = "Clicca per andare all' agenda";
            homeworkNotificationText = new StringBuilder("Sono stati programmati nuovi compiti:\n");
            for (int i = 0; i < homeworkCounter; i++) {
                homeworkNotificationText.append(homeworkSubjects.get(i));
                homeworkNotificationText.append(" - ");
                homeworkNotificationText.append(homeworkDates.get(i));
                if (i != homeworkCounter - 1)
                    homeworkNotificationText.append("\n");
            }
        }
        homeworkNotification = createNotificationForAgenda("Nuovi compiti", contentText, homeworkNotificationText.toString());
    }
    if (canSendTestNotification && testCounter > 0) {
        String contentText;
        if (testCounter == 1) {
            contentText = "Clicca per andare all' agenda";
            testNotificationText = new StringBuilder("È stata programmata una nuova verifica di " + testSubjects.get(0) + " per il giorno " + testDates.get(0));
        } else {
            contentText = "Clicca per andare all' agenda";
            testNotificationText = new StringBuilder("Sono state programmate nuove verifiche:\n");
            for (int i = 0; i < testCounter; i++) {
                testNotificationText.append(testSubjects.get(i));
                testNotificationText.append(" - ");
                testNotificationText.append(testDates.get(i));
                if (i != testCounter - 1)
                    testNotificationText.append("\n");
            }
        }
        testNotification = createNotificationForAgenda("Nuove verifiche", contentText, testNotificationText.toString());
    }
    loggerManager.d("Invio le notifiche");
    if (canSendHomeworkNotification && homeworkNotification != null)
        notificationManager.notify(13, homeworkNotification);
    if (canSendTestNotification && testNotification != null)
        notificationManager.notify(14, testNotification);
    return testCounter + homeworkCounter;
}
Also used : AlertsPage(com.giua.pages.AlertsPage) Notification(android.app.Notification) JsonBuilder(com.giua.utils.JsonBuilder) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Alert(com.giua.objects.Alert) JsonParser(com.giua.utils.JsonParser) File(java.io.File) Vector(java.util.Vector) JsonParser(com.giua.utils.JsonParser)

Aggregations

Notification (android.app.Notification)1 Alert (com.giua.objects.Alert)1 AlertsPage (com.giua.pages.AlertsPage)1 JsonBuilder (com.giua.utils.JsonBuilder)1 JsonParser (com.giua.utils.JsonParser)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileReader (java.io.FileReader)1 Vector (java.util.Vector)1