// Path: app/src/main/java/com/almoturg/sprog/data/UpdateHelpers.java
// public class UpdateHelpers {
//     // These are the times when an update should be available on the server
//     private static final int FIRST_UPDATE_HOUR = 2;
//     private static final int SECOND_UPDATE_HOUR = 14;
//     private static final long MIN_HOURS_BETWEEN_UPDATES = 11; // some margin if it runs faster
//     private static final long MAX_DAYS_BETWEEN_LOADING_POEMS = 3;
//     private static final long MAX_DAYS_BETWEEN_FULL_UPDATES = 30;
// 
//     // minimum length of poems.json file in bytes such that it is assumed to be complete
//     // and therefore the cancel button is shown when updating
//     private static final int MIN_FILE_LENGTH = 1000 * 1000;
// 
//     public static boolean isUpdateTime(Calendar now,
//                                        long last_update_tstamp, long last_fcm_tstamp) {
//         // Always load JSON when more than MAX_DAYS_BETWEEN_LOADING_POEMS days have passed
//         // This is mainly to get updated scores/gold counts and to remove deleted poems.
//         if (now.getTimeInMillis() - last_update_tstamp >
//                 MAX_DAYS_BETWEEN_LOADING_POEMS * 24 * 60 * 60 * 1000) {
//             return true;
//         }
// 
//         // last_fcm_tstamp is the time when the last FCM message was sent
//         // if it showed that updates were available this function would not have been called
//         if (now.getTimeInMillis() - last_fcm_tstamp < MIN_HOURS_BETWEEN_UPDATES * 60 * 60 * 1000) {
//             return false;
//         }
// 
//         Calendar last_update_cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//         last_update_cal.setTimeInMillis(last_update_tstamp);
//         long diff_in_ms = now.getTimeInMillis() - last_update_tstamp;
//         long ms_today = now.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000
//                 + now.get(Calendar.MINUTE) * 60 * 1000
//                 + now.get(Calendar.SECOND) * 1000
//                 + now.get(Calendar.MILLISECOND);
// 
//         return (now.get(Calendar.HOUR_OF_DAY) >= FIRST_UPDATE_HOUR
//                 && diff_in_ms > ms_today - FIRST_UPDATE_HOUR * 60 * 60 * 1000)
//                 ||
//                 (now.get(Calendar.HOUR_OF_DAY) >= SECOND_UPDATE_HOUR
//                         && diff_in_ms > ms_today - SECOND_UPDATE_HOUR * 60 * 60 * 1000);
//     }
// 
//     public static boolean poemsFullFileExists(Context context) {
//         File file = new File(context.getExternalFilesDir(
//                 Environment.DIRECTORY_DOWNLOADS),
//                 PoemsLoader.getFilename(
//                         PoemsLoader.UpdateType.FULL,
//                         PoemsLoader.FileType.CURRENT));
//         File old_file = new File(context.getExternalFilesDir(
//                 Environment.DIRECTORY_DOWNLOADS),
//                 PoemsLoader.getFilename(
//                         PoemsLoader.UpdateType.FULL,
//                         PoemsLoader.FileType.PREV));
//         return (file.exists() && file.length() > MIN_FILE_LENGTH) ||
//                 (old_file.exists() && old_file.length() > MIN_FILE_LENGTH);
//     }
// 
//     public static boolean poems60DaysFileExists(Context context) {
//         File file = new File(context.getExternalFilesDir(
//                 Environment.DIRECTORY_DOWNLOADS),
//                 PoemsLoader.getFilename(
//                         PoemsLoader.UpdateType.PARTIAL,
//                         PoemsLoader.FileType.CURRENT));
//         File old_file = new File(context.getExternalFilesDir(
//                 Environment.DIRECTORY_DOWNLOADS),
//                 PoemsLoader.getFilename(
//                         PoemsLoader.UpdateType.PARTIAL,
//                         PoemsLoader.FileType.PREV));
//         return (file.exists() && file.length() > 1) ||
//                 (old_file.exists() && old_file.length() > 1);
//     }
// 
//     public static boolean isConnected(Context context) {
//         ConnectivityManager cm =
//                 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// 
//         NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
//         return activeNetwork != null &&
//                 activeNetwork.isConnected();
//     }
// 
//     public static PoemsLoader.UpdateType getUpdateType(Calendar now, long last_full_update_tstamp) {
//         if (now.getTimeInMillis() - last_full_update_tstamp >
//                 MAX_DAYS_BETWEEN_FULL_UPDATES * 24 * 60 * 60 * 1000) {
//             return PoemsLoader.UpdateType.FULL;
//         }
//         return PoemsLoader.UpdateType.PARTIAL;
//     }
// }


import static org.junit.Assert.*;
import com.almoturg.sprog.data.UpdateHelpers;
import org.junit.Test;
import java.util.GregorianCalendar;
package com.almoturg.sprog;





public class UpdateHelpersTest {
    @Test
    public void isUpdateTimeTest(){
        GregorianCalendar now;
        long last_update;
        long last_fcm;

        now = new GregorianCalendar(2017, 1, 15, 14, 45);
        last_update = new GregorianCalendar(2017, 1, 13, 13, 25).getTimeInMillis();
        last_fcm = new GregorianCalendar(2017, 1, 14, 13, 17).getTimeInMillis();
        assertEquals(true, UpdateHelpers.isUpdateTime(now, last_update, last_fcm));

        now = new GregorianCalendar(2017, 1, 15, 14, 45);
        last_update = new GregorianCalendar(2017, 1, 15, 13, 34).getTimeInMillis();
        last_fcm = new GregorianCalendar(2017, 1, 15, 13, 17).getTimeInMillis();
        assertEquals(false, UpdateHelpers.isUpdateTime(now, last_update, last_fcm));

        now = new GregorianCalendar(2017, 1, 15, 14, 45);
// Path: app/src/main/java/com/almoturg/sprog/data/UpdateHelpers.java
// public class UpdateHelpers {
//     // These are the times when an update should be available on the server
//     private static final int FIRST_UPDATE_HOUR = 2;
//     private static final int SECOND_UPDATE_HOUR = 14;
//     private static final long MIN_HOURS_BETWEEN_UPDATES = 11; // some margin if it runs faster
//     private static final long MAX_DAYS_BETWEEN_LOADING_POEMS = 3;
//     private static final long MAX_DAYS_BETWEEN_FULL_UPDATES = 30;
// 
//     // minimum length of poems.json file in bytes such that it is assumed to be complete
//     // and therefore the cancel button is shown when updating
//     private static final int MIN_FILE_LENGTH = 1000 * 1000;
// 
//     public static boolean isUpdateTime(Calendar now,
//                                        long last_update_tstamp, long last_fcm_tstamp) {
//         // Always load JSON when more than MAX_DAYS_BETWEEN_LOADING_POEMS days have passed
//         // This is mainly to get updated scores/gold counts and to remove deleted poems.
//         if (now.getTimeInMillis() - last_update_tstamp >
//                 MAX_DAYS_BETWEEN_LOADING_POEMS * 24 * 60 * 60 * 1000) {
//             return true;
//         }
// 
//         // last_fcm_tstamp is the time when the last FCM message was sent
//         // if it showed that updates were available this function would not have been called
//         if (now.getTimeInMillis() - last_fcm_tstamp < MIN_HOURS_BETWEEN_UPDATES * 60 * 60 * 1000) {
//             return false;
//         }
// 
//         Calendar last_update_cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//         last_update_cal.setTimeInMillis(last_update_tstamp);
//         long diff_in_ms = now.getTimeInMillis() - last_update_tstamp;
//         long ms_today = now.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000
//                 + now.get(Calendar.MINUTE) * 60 * 1000
//                 + now.get(Calendar.SECOND) * 1000
//                 + now.get(Calendar.MILLISECOND);
// 
//         return (now.get(Calendar.HOUR_OF_DAY) >= FIRST_UPDATE_HOUR
//                 && diff_in_ms > ms_today - FIRST_UPDATE_HOUR * 60 * 60 * 1000)
//                 ||
//                 (now.get(Calendar.HOUR_OF_DAY) >= SECOND_UPDATE_HOUR
//                         && diff_in_ms > ms_today - SECOND_UPDATE_HOUR * 60 * 60 * 1000);
//     }
// 
//     public static boolean poemsFullFileExists(Context context) {
//         File file = new File(context.getExternalFilesDir(
//                 Environment.DIRECTORY_DOWNLOADS),
//                 PoemsLoader.getFilename(
//                         PoemsLoader.UpdateType.FULL,
//                         PoemsLoader.FileType.CURRENT));
//         File old_file = new File(context.getExternalFilesDir(
//                 Environment.DIRECTORY_DOWNLOADS),
//                 PoemsLoader.getFilename(
//                         PoemsLoader.UpdateType.FULL,
//                         PoemsLoader.FileType.PREV));
//         return (file.exists() && file.length() > MIN_FILE_LENGTH) ||
//                 (old_file.exists() && old_file.length() > MIN_FILE_LENGTH);
//     }
// 
//     public static boolean poems60DaysFileExists(Context context) {
//         File file = new File(context.getExternalFilesDir(
//                 Environment.DIRECTORY_DOWNLOADS),
//                 PoemsLoader.getFilename(
//                         PoemsLoader.UpdateType.PARTIAL,
//                         PoemsLoader.FileType.CURRENT));
//         File old_file = new File(context.getExternalFilesDir(
//                 Environment.DIRECTORY_DOWNLOADS),
//                 PoemsLoader.getFilename(
//                         PoemsLoader.UpdateType.PARTIAL,
//                         PoemsLoader.FileType.PREV));
//         return (file.exists() && file.length() > 1) ||
//                 (old_file.exists() && old_file.length() > 1);
//     }
// 
//     public static boolean isConnected(Context context) {
//         ConnectivityManager cm =
//                 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
// 
//         NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
//         return activeNetwork != null &&
//                 activeNetwork.isConnected();
//     }
// 
//     public static PoemsLoader.UpdateType getUpdateType(Calendar now, long last_full_update_tstamp) {
//         if (now.getTimeInMillis() - last_full_update_tstamp >
//                 MAX_DAYS_BETWEEN_FULL_UPDATES * 24 * 60 * 60 * 1000) {
//             return PoemsLoader.UpdateType.FULL;
//         }
//         return PoemsLoader.UpdateType.PARTIAL;
//     }
// }


// Path: app/src/test/java/com/almoturg/sprog/UpdateHelpersTest.java
import static org.junit.Assert.*;
import com.almoturg.sprog.data.UpdateHelpers;
import org.junit.Test;
import java.util.GregorianCalendar;

package com.almoturg.sprog;





public class UpdateHelpersTest {
    @Test
    public void isUpdateTimeTest(){
        GregorianCalendar now;
        long last_update;
        long last_fcm;

        now = new GregorianCalendar(2017, 1, 15, 14, 45);
        last_update = new GregorianCalendar(2017, 1, 13, 13, 25).getTimeInMillis();
        last_fcm = new GregorianCalendar(2017, 1, 14, 13, 17).getTimeInMillis();
        assertEquals(true, UpdateHelpers.isUpdateTime(now, last_update, last_fcm));

        now = new GregorianCalendar(2017, 1, 15, 14, 45);
        last_update = new GregorianCalendar(2017, 1, 15, 13, 34).getTimeInMillis();
        last_fcm = new GregorianCalendar(2017, 1, 15, 13, 17).getTimeInMillis();
        assertEquals(false, UpdateHelpers.isUpdateTime(now, last_update, last_fcm));

        now = new GregorianCalendar(2017, 1, 15, 14, 45);
        last_update = -1;