// Path: app/src/main/java/de/mygrades/util/Constants.java
// public class Constants {
// 
//     // secure preferences constants
//     public static final String NOT_SO_SECURE_PREF_FILE = "userdata";
//     public static final String PREF_KEY_USERNAME = "username";
//     public static final String PREF_KEY_PASSWORD = "password";
// 
//     // normal preferences constants
//     public static final String PREF_KEY_UNIVERSITY_ID = "university_id";
//     public static final String PREF_KEY_RULE_ID = "rule_id";
//     public static final String PREF_KEY_INITIAL_LOADING_DONE = "initial_loading_done";
//     public static final String PREF_KEY_LAST_UPDATED_AT = "last_updated_at";
//     public static final String PREF_KEY_SCRAPING_FAILS_INTERVAL = "scraping_fails_interval";
//     public static final String PREF_KEY_ALARM_ERROR_SCRAPING_COUNT = "alarm_error_scraping_count";
//     public static final String PREF_KEY_DISMISSED_NOTIFICATION_INFO = "dismissed_notification_info";
//     public static final String PREF_KEY_DISMISSED_DONATION_INFO = "dismissed_donation_info";
//     public static final String PREF_KEY_DISMISSED_RATING_INFO = "dismissed_rating_info";
//     public static final String PREF_KEY_DISMISSED_EDIT_MODE_INFO = "dismissed_edit_mode_info";
//     public static final String PREF_KEY_APPLICATION_LAUNCHES_COUNTER = "application_launches_counter";
// 
//     // Scraping status instance state
//     public static final String INSTANCE_IS_SCRAPING_STATE = "is_scraping_state";
//     public static final String INSTANCE_PROGRESS_STATE = "progress_state";
// 
//     // grade entry 'seen' state
//     public static final int GRADE_ENTRY_SEEN = 0;
//     public static final int GRADE_ENTRY_NEW = 1;
//     public static final int GRADE_ENTRY_UPDATED = 2;
// }


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import de.mygrades.R;
import de.mygrades.util.Constants;

        // minutes * 60 secs * 1000 msec
        long interval = intervalMinutes * 60 * 1000;
        // first alarm triggers minimum 10 minutes after being set
        long trigger = 10 * 60 * 1000;

        // we can't use setInexactRepeating, because of the rare predefined intervals
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + trigger, interval, alarmIntent);
        Log.d(TAG, "Alarm set. Interval: " + interval + ", Trigger: " + trigger);

        // enable boot receiver to set alarm after reboot
        packageManager.setComponentEnabledSetting(bootReceiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
        Log.d(TAG, "BootReceiver enabled!");
    }

    /**
     * Sets an one time fallback alarm if necessary.
     * It is set in 1/4 of the original interval time.
     * So at maximum there can be 3 one time fallback alarms within
     * an interval of original repeating alarm.
     * @param isError is this one time alarm caused by an error?
     */
    public void setOneTimeFallbackAlarm(boolean isError) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();

        if (isError) {
// Path: app/src/main/java/de/mygrades/util/Constants.java
// public class Constants {
// 
//     // secure preferences constants
//     public static final String NOT_SO_SECURE_PREF_FILE = "userdata";
//     public static final String PREF_KEY_USERNAME = "username";
//     public static final String PREF_KEY_PASSWORD = "password";
// 
//     // normal preferences constants
//     public static final String PREF_KEY_UNIVERSITY_ID = "university_id";
//     public static final String PREF_KEY_RULE_ID = "rule_id";
//     public static final String PREF_KEY_INITIAL_LOADING_DONE = "initial_loading_done";
//     public static final String PREF_KEY_LAST_UPDATED_AT = "last_updated_at";
//     public static final String PREF_KEY_SCRAPING_FAILS_INTERVAL = "scraping_fails_interval";
//     public static final String PREF_KEY_ALARM_ERROR_SCRAPING_COUNT = "alarm_error_scraping_count";
//     public static final String PREF_KEY_DISMISSED_NOTIFICATION_INFO = "dismissed_notification_info";
//     public static final String PREF_KEY_DISMISSED_DONATION_INFO = "dismissed_donation_info";
//     public static final String PREF_KEY_DISMISSED_RATING_INFO = "dismissed_rating_info";
//     public static final String PREF_KEY_DISMISSED_EDIT_MODE_INFO = "dismissed_edit_mode_info";
//     public static final String PREF_KEY_APPLICATION_LAUNCHES_COUNTER = "application_launches_counter";
// 
//     // Scraping status instance state
//     public static final String INSTANCE_IS_SCRAPING_STATE = "is_scraping_state";
//     public static final String INSTANCE_PROGRESS_STATE = "progress_state";
// 
//     // grade entry 'seen' state
//     public static final int GRADE_ENTRY_SEEN = 0;
//     public static final int GRADE_ENTRY_NEW = 1;
//     public static final int GRADE_ENTRY_UPDATED = 2;
// }


// Path: app/src/main/java/de/mygrades/main/alarm/ScrapeAlarmManager.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import de.mygrades.R;
import de.mygrades.util.Constants;


        // minutes * 60 secs * 1000 msec
        long interval = intervalMinutes * 60 * 1000;
        // first alarm triggers minimum 10 minutes after being set
        long trigger = 10 * 60 * 1000;

        // we can't use setInexactRepeating, because of the rare predefined intervals
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + trigger, interval, alarmIntent);
        Log.d(TAG, "Alarm set. Interval: " + interval + ", Trigger: " + trigger);

        // enable boot receiver to set alarm after reboot
        packageManager.setComponentEnabledSetting(bootReceiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
        Log.d(TAG, "BootReceiver enabled!");
    }

    /**
     * Sets an one time fallback alarm if necessary.
     * It is set in 1/4 of the original interval time.
     * So at maximum there can be 3 one time fallback alarms within
     * an interval of original repeating alarm.
     * @param isError is this one time alarm caused by an error?
     */
    public void setOneTimeFallbackAlarm(boolean isError) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();

        if (isError) {
            int errorCount = prefs.getInt(Constants.PREF_KEY_ALARM_ERROR_SCRAPING_COUNT, 0);