// Path: app/src/ru/bsuirhelper/android/core/schedule/Lesson.java
// public class Lesson {
//     public final HashMap<String, String> fields = new HashMap<String, String>();
//     public int id;
// 
//     public Lesson() {
//         fields.put("faculty", "");
//         fields.put("year", "");
//         fields.put("course", "");
//         fields.put("term", "");
//         fields.put("stream", "");
//         fields.put("s_group", "");
//         fields.put("subgroup", "");
//         fields.put("weekDay", "");
//         fields.put("timePeriod", "");
//         fields.put("weekList", "");
//         fields.put("subject", "");
//         fields.put("subjectType", "");
//         fields.put("auditorium", "");
//         fields.put("teacher", "");
//         fields.put("date", "");
//         fields.put("timePeriodStart", "");
//         fields.put("timePeriodEnd", "");
//     }
// }
// 
// Path: app/src/ru/bsuirhelper/android/core/schedule/ScheduleManager.java
// public class ScheduleManager {
//     private final ScheduleDatabase mScheduleDatabase;
//     private StudentCalendar mStudentCalendar = new StudentCalendar();
//     private static ScheduleManager instance;
// 
//     public static ScheduleManager getInstance(Context context) {
//         if (instance == null) {
//             instance = new ScheduleManager(context);
//         }
//         return instance;
//     }
// 
//     public ScheduleManager(Context context) {
//         mScheduleDatabase = new ScheduleDatabase(context);
//         mStudentCalendar = new StudentCalendar();
//     }
// 
//     public ArrayList<StudentGroup> getGroups() {
//         return mScheduleDatabase.getGroups();
//     }
// 
//     public void addSchedule(String groupId, ArrayList<Lesson> lessons) {
//         mScheduleDatabase.addSchedule(lessons, groupId);
//     }
// 
//     public Lesson[] getLessonsOfDay(String groupId, DateTime dayOfYear, int subgroup) {
//         return mScheduleDatabase.getLessonsOfDay(groupId, dayOfYear, subgroup);
//     }
// 
//     public void deleteSchedule(String groupId) {
//         mScheduleDatabase.deleteSchedule(groupId);
//     }
// 
//     public boolean isLessonsEndToday(String groupId, int subgroup) {
//         DateTime currentTime = new DateTime();
//         Lesson[] lessons = mScheduleDatabase.getLessonsOfDay(groupId, DateTime.now(), subgroup);
//         if (lessons.length > 0) {
//             Lesson lesson = lessons[lessons.length - 1];
// 
//             DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
//             DateTime dt = formatter.parseDateTime(getFinishTimeOfLesson(lesson));
//             if (currentTime.getHourOfDay() + 1 > dt.getHourOfDay()) {
//                 return true;
//             }
//         } else {
//             return true;
//         }
//         return false;
//     }
// 
//     private String getFinishTimeOfLesson(Lesson lesson) {
//         String time = lesson.fields.get("timePeriod");
//         char c = '-';
//         int pos = -1;
//         while (time.charAt(++pos) != c) ;
//         String result = time.substring(pos + 1, time.length());
//         return result;
//     }
// }
// 
// Path: app/src/ru/bsuirhelper/android/core/schedule/ScheduleParser.java
// public class ScheduleParser {
// 
//     public static ArrayList<Lesson> parseXmlSchedule(File xmlFile) {
//         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//         DocumentBuilder dBuilder;
//         Document doc = null;
//         try {
//             dBuilder = dbFactory.newDocumentBuilder();
//             doc = dBuilder.parse(xmlFile);
//         } catch (Exception e) {
//             e.printStackTrace();
//         }
//         doc.getDocumentElement().normalize();
// 
//         NodeList list = doc.getElementsByTagName("ROW");
//         ArrayList<Lesson> lessons = new ArrayList<Lesson>();
//         for (int i = 0; i < list.getLength(); i++) {
//             Lesson lesson = new Lesson();
//             Element element = (Element) list.item(i);
//             NamedNodeMap attrs = element.getAttributes();
//             for (int j = 0; j < attrs.getLength(); j++) {
//                 Node attribute = attrs.item(j);
//                 //In xml file attribute name group, but group it is keyword of sqlite
//                 if (attribute.getNodeName().equals("group")) {
//                     lesson.fields.put("s_group", attribute.getNodeValue());
//                     continue;
//                 }
//                 lesson.fields.put(attribute.getNodeName(), attribute.getNodeValue());
//             }
//             lessons.add(lesson);
//         }
//         return lessons;
//     }
// }


import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import ru.bsuirhelper.android.core.schedule.Lesson;
import ru.bsuirhelper.android.core.schedule.ScheduleManager;
import ru.bsuirhelper.android.core.schedule.ScheduleParser;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
            output = context.openFileOutput(TEMP_FILE_NAME, Context.MODE_PRIVATE);

            byte data[] = new byte[4096];
            int count;
            while ((count = input.read(data)) != -1) {
                output.write(data, 0, count);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (connection != null)
                connection.disconnect();
        }
        return new File(context.getApplicationContext().getFilesDir() + "/" + TEMP_FILE_NAME);
    }


    @Override
    protected String doInBackground(String... urls) {
        String groupId = urls[0];
// Path: app/src/ru/bsuirhelper/android/core/schedule/Lesson.java
// public class Lesson {
//     public final HashMap<String, String> fields = new HashMap<String, String>();
//     public int id;
// 
//     public Lesson() {
//         fields.put("faculty", "");
//         fields.put("year", "");
//         fields.put("course", "");
//         fields.put("term", "");
//         fields.put("stream", "");
//         fields.put("s_group", "");
//         fields.put("subgroup", "");
//         fields.put("weekDay", "");
//         fields.put("timePeriod", "");
//         fields.put("weekList", "");
//         fields.put("subject", "");
//         fields.put("subjectType", "");
//         fields.put("auditorium", "");
//         fields.put("teacher", "");
//         fields.put("date", "");
//         fields.put("timePeriodStart", "");
//         fields.put("timePeriodEnd", "");
//     }
// }
// 
// Path: app/src/ru/bsuirhelper/android/core/schedule/ScheduleManager.java
// public class ScheduleManager {
//     private final ScheduleDatabase mScheduleDatabase;
//     private StudentCalendar mStudentCalendar = new StudentCalendar();
//     private static ScheduleManager instance;
// 
//     public static ScheduleManager getInstance(Context context) {
//         if (instance == null) {
//             instance = new ScheduleManager(context);
//         }
//         return instance;
//     }
// 
//     public ScheduleManager(Context context) {
//         mScheduleDatabase = new ScheduleDatabase(context);
//         mStudentCalendar = new StudentCalendar();
//     }
// 
//     public ArrayList<StudentGroup> getGroups() {
//         return mScheduleDatabase.getGroups();
//     }
// 
//     public void addSchedule(String groupId, ArrayList<Lesson> lessons) {
//         mScheduleDatabase.addSchedule(lessons, groupId);
//     }
// 
//     public Lesson[] getLessonsOfDay(String groupId, DateTime dayOfYear, int subgroup) {
//         return mScheduleDatabase.getLessonsOfDay(groupId, dayOfYear, subgroup);
//     }
// 
//     public void deleteSchedule(String groupId) {
//         mScheduleDatabase.deleteSchedule(groupId);
//     }
// 
//     public boolean isLessonsEndToday(String groupId, int subgroup) {
//         DateTime currentTime = new DateTime();
//         Lesson[] lessons = mScheduleDatabase.getLessonsOfDay(groupId, DateTime.now(), subgroup);
//         if (lessons.length > 0) {
//             Lesson lesson = lessons[lessons.length - 1];
// 
//             DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm");
//             DateTime dt = formatter.parseDateTime(getFinishTimeOfLesson(lesson));
//             if (currentTime.getHourOfDay() + 1 > dt.getHourOfDay()) {
//                 return true;
//             }
//         } else {
//             return true;
//         }
//         return false;
//     }
// 
//     private String getFinishTimeOfLesson(Lesson lesson) {
//         String time = lesson.fields.get("timePeriod");
//         char c = '-';
//         int pos = -1;
//         while (time.charAt(++pos) != c) ;
//         String result = time.substring(pos + 1, time.length());
//         return result;
//     }
// }
// 
// Path: app/src/ru/bsuirhelper/android/core/schedule/ScheduleParser.java
// public class ScheduleParser {
// 
//     public static ArrayList<Lesson> parseXmlSchedule(File xmlFile) {
//         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//         DocumentBuilder dBuilder;
//         Document doc = null;
//         try {
//             dBuilder = dbFactory.newDocumentBuilder();
//             doc = dBuilder.parse(xmlFile);
//         } catch (Exception e) {
//             e.printStackTrace();
//         }
//         doc.getDocumentElement().normalize();
// 
//         NodeList list = doc.getElementsByTagName("ROW");
//         ArrayList<Lesson> lessons = new ArrayList<Lesson>();
//         for (int i = 0; i < list.getLength(); i++) {
//             Lesson lesson = new Lesson();
//             Element element = (Element) list.item(i);
//             NamedNodeMap attrs = element.getAttributes();
//             for (int j = 0; j < attrs.getLength(); j++) {
//                 Node attribute = attrs.item(j);
//                 //In xml file attribute name group, but group it is keyword of sqlite
//                 if (attribute.getNodeName().equals("group")) {
//                     lesson.fields.put("s_group", attribute.getNodeValue());
//                     continue;
//                 }
//                 lesson.fields.put(attribute.getNodeName(), attribute.getNodeValue());
//             }
//             lessons.add(lesson);
//         }
//         return lessons;
//     }
// }


// Path: app/src/ru/bsuirhelper/android/ui/DownloadScheduleTask.java
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import ru.bsuirhelper.android.core.schedule.Lesson;
import ru.bsuirhelper.android.core.schedule.ScheduleManager;
import ru.bsuirhelper.android.core.schedule.ScheduleParser;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

            output = context.openFileOutput(TEMP_FILE_NAME, Context.MODE_PRIVATE);

            byte data[] = new byte[4096];
            int count;
            while ((count = input.read(data)) != -1) {
                output.write(data, 0, count);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (connection != null)
                connection.disconnect();
        }
        return new File(context.getApplicationContext().getFilesDir() + "/" + TEMP_FILE_NAME);
    }


    @Override
    protected String doInBackground(String... urls) {
        String groupId = urls[0];
        File xmlFile = downloadScheduleFromInternet(groupId);