diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/App.java b/app/src/main/java/pl/szczodrzynski/edziennik/App.java index 4841732b..b975aff9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/App.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/App.java @@ -68,11 +68,6 @@ import me.leolin.shortcutbadger.ShortcutBadger; import okhttp3.ConnectionSpec; import okhttp3.OkHttpClient; import okhttp3.TlsVersion; -import pl.szczodrzynski.edziennik.data.api.Edziennik; -import pl.szczodrzynski.edziennik.data.api.Iuczniowie; -import pl.szczodrzynski.edziennik.data.api.Librus; -import pl.szczodrzynski.edziennik.data.api.Mobidziennik; -import pl.szczodrzynski.edziennik.data.api.Vulcan; import pl.szczodrzynski.edziennik.data.db.AppDb; import pl.szczodrzynski.edziennik.data.db.modules.debuglog.DebugLog; import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore; @@ -141,12 +136,6 @@ public class App extends androidx.multidex.MultiDexApplication implements Config public PersistentCookieJar cookieJar; public OkHttpClient http; public OkHttpClient httpLazy; - //public Jakdojade apiJakdojade; - public Edziennik apiEdziennik; - public Mobidziennik apiMobidziennik; - public Iuczniowie apiIuczniowie; - public Librus apiLibrus; - public Vulcan apiVulcan; public SharedPreferences appSharedPrefs; // sharedPreferences for APPCONFIG + JOBS STORE public AppConfig appConfig; // APPCONFIG: common for all profiles @@ -204,12 +193,6 @@ public class App extends androidx.multidex.MultiDexApplication implements Config db = AppDb.getDatabase(this); gson = new Gson(); networkUtils = new NetworkUtils(this); - apiEdziennik = new Edziennik(this); - //apiJakdojade = new Jakdojade(this); - apiMobidziennik = new Mobidziennik(this); - apiIuczniowie = new Iuczniowie(this); - apiLibrus = new Librus(this); - apiVulcan = new Vulcan(this); Iconics.init(getApplicationContext()); Iconics.registerFont(SzkolnyFont.INSTANCE); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Edziennik.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Edziennik.java deleted file mode 100644 index c73ccd80..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Edziennik.java +++ /dev/null @@ -1,1184 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api; - -import android.app.Activity; -import android.appwidget.AppWidgetManager; -import android.content.ClipData; -import android.content.ClipboardManager; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.os.AsyncTask; -import android.os.Build; -import android.os.Handler; -import android.os.Looper; -import android.text.Html; -import android.util.Base64; -import android.util.Log; -import android.webkit.CookieManager; -import android.webkit.CookieSyncManager; -import android.webkit.WebView; -import android.widget.Toast; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.StringRes; - -import com.afollestad.materialdialogs.DialogAction; -import com.afollestad.materialdialogs.MaterialDialog; -import com.danimahardhika.cafebar.CafeBar; -import com.google.android.gms.common.util.ArrayUtils; -import com.google.firebase.iid.FirebaseInstanceId; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.mikepenz.iconics.IconicsDrawable; -import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import pl.szczodrzynski.edziennik.App; -import pl.szczodrzynski.edziennik.BuildConfig; -import pl.szczodrzynski.edziennik.MainActivity; -import pl.szczodrzynski.edziennik.R; -import pl.szczodrzynski.edziennik.WidgetTimetable; -import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface; -import pl.szczodrzynski.edziennik.data.api.interfaces.SyncCallback; -import pl.szczodrzynski.edziennik.data.db.modules.announcements.AnnouncementFull; -import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance; -import pl.szczodrzynski.edziennik.data.db.modules.attendance.AttendanceFull; -import pl.szczodrzynski.edziennik.data.db.modules.events.Event; -import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull; -import pl.szczodrzynski.edziennik.data.db.modules.events.EventType; -import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeFull; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonFull; -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore; -import pl.szczodrzynski.edziennik.data.db.modules.messages.Message; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull; -import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata; -import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice; -import pl.szczodrzynski.edziennik.data.db.modules.notices.NoticeFull; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileFull; -import pl.szczodrzynski.edziennik.data.db.modules.teams.Team; -import pl.szczodrzynski.edziennik.network.ServerRequest; -import pl.szczodrzynski.edziennik.utils.Themes; -import pl.szczodrzynski.edziennik.utils.models.Date; -import pl.szczodrzynski.edziennik.utils.models.Notification; -import pl.szczodrzynski.edziennik.widgets.luckynumber.WidgetLuckyNumber; -import pl.szczodrzynski.edziennik.widgets.notifications.WidgetNotifications; - -import static android.content.Context.CLIPBOARD_SERVICE; -import static com.mikepenz.iconics.utils.IconicsConvertersKt.colorInt; -import static com.mikepenz.iconics.utils.IconicsConvertersKt.sizeDp; -import static pl.szczodrzynski.edziennik.App.APP_URL; -import static pl.szczodrzynski.edziennik.MainActivity.DRAWER_ITEM_HOME; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_OK; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_OTHER; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_PROFILE_ARCHIVED; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_PROFILE_NOT_FOUND; -import static pl.szczodrzynski.edziennik.data.api.AppError.stringErrorCode; -import static pl.szczodrzynski.edziennik.data.api.AppError.stringErrorType; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_AGENDA; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_ALL; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_ANNOUNCEMENTS; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_ATTENDANCE; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_GRADES; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_HOMEWORK; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_MESSAGES_INBOX; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_MESSAGES_OUTBOX; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_NOTICES; -import static pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface.FEATURE_TIMETABLE; -import static pl.szczodrzynski.edziennik.data.db.modules.events.Event.TYPE_HOMEWORK; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER2_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER2_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_YEAR_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_YEAR_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore.LOGIN_TYPE_IUCZNIOWIE; -import static pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore.LOGIN_TYPE_LIBRUS; -import static pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore.LOGIN_TYPE_MOBIDZIENNIK; -import static pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore.LOGIN_TYPE_VULCAN; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_AUTO_ARCHIVING; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_LUCKY_NUMBER; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_ANNOUNCEMENT; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_ATTENDANCE; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_EVENT; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_GRADE; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_HOMEWORK; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_MESSAGE; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_NOTICE; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_SHARED_EVENT; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_NEW_SHARED_HOMEWORK; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_SERVER_MESSAGE; -import static pl.szczodrzynski.edziennik.data.db.modules.notification.Notification.TYPE_TIMETABLE_LESSON_CHANGE; -import static pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile.REGISTRATION_ENABLED; -import static pl.szczodrzynski.edziennik.utils.Utils.d; -import static pl.szczodrzynski.edziennik.utils.Utils.ns; - -public class Edziennik { - //public static final int CODE_NULL = 0; - private App app; - - private static final String TAG = "Edziennik"; - private static boolean registerEmpty; - public static int oldLuckyNumber; - - public static EdziennikInterface getApi(App app, int loginType) { - switch (loginType) { - default: - case LOGIN_TYPE_MOBIDZIENNIK: - return app.apiMobidziennik; - case LOGIN_TYPE_LIBRUS: - return app.apiLibrus; - case LOGIN_TYPE_IUCZNIOWIE: - return app.apiIuczniowie; - case LOGIN_TYPE_VULCAN: - return app.apiVulcan; - } - } - - public Edziennik(App app) { - this.app = app; - } - - @SuppressWarnings("deprecation") - public static void clearCookies(Context context, String url) { - //Log.d(TAG, "Cookies: " + yahooCookies); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { - //Log.d(TAG, "Using clearCookies code for API >=" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1)); - CookieManager.getInstance().removeAllCookies(null); - CookieManager.getInstance().flush(); - } else { - //Log.d(TAG, "Using clearCookies code for API <" + String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1)); - CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context); - cookieSyncManager.startSync(); - CookieManager cookieManager = CookieManager.getInstance(); - cookieManager.removeAllCookie(); - cookieManager.removeSessionCookie(); - cookieSyncManager.stopSync(); - cookieSyncManager.sync(); - } - } - - public void initMessagesWebView(WebView webView, App app, boolean fullVersion, boolean clearCookies) { - if (!app.profile.getEmpty() && app.profile.getLoginStoreType() == LoginStore.LOGIN_TYPE_MOBIDZIENNIK) { - String url; - if (fullVersion) { - url = "https://" + app.profile.getLoginData("serverName", "") + ".mobidziennik.pl/mobile/wiadomosci"; - } else { - url = "https://" + app.profile.getLoginData("serverName", "") + ".mobidziennik.pl/api/"; - } - - String str1 = "login=" + app.profile.getLoginData("username", "") + "&haslo=" + app.profile.getLoginData("password", ""); - - if (!fullVersion) { - str1 += "&ip=" + app.deviceId + "&wersja=20&token=&webview_wiadomosci=1"; - } - - - if (-1L != -1L) { - str1 += "&id_wiadomosci=" + -1L; - } - - if (clearCookies) - clearCookies(app, "https://" + app.profile.getLoginData("serverName", "") + ".mobidziennik.pl"); - - //Toast.makeText(app, "URL "+url, Toast.LENGTH_SHORT).show(); - webView.postUrl(url, str1.getBytes()); - } else if (!app.profile.getEmpty() && app.profile.getLoginStoreType() == LoginStore.LOGIN_TYPE_IUCZNIOWIE) { - String url = "https://iuczniowie.progman.pl/idziennik/mod_panelRodzica/Komunikator.aspx"; - webView.loadUrl(url); - /* - if (app.profile.loginServerName.equals("") || app.profile.loginUsername.equals("") || app.profile.loginPassword.equals("")) { - webView.loadData("

"+app.getString(R.string.api_error_code_invalid_login)+"

", "text/html", "UTF-8"); - return; - } - - if (app.appConfig.deviceId == null || app.appConfig.deviceId.equals("")) { - app.appConfig.deviceId = Settings.Secure.getString(app.getContentResolver(), Settings.Secure.ANDROID_ID); - app.appConfig.savePending = true; - } - - //Ion.getDefault(app.getContext()).getCookieMiddleware().getCookieStore().removeAll(); // TODO remove only cookies for this domain - String finalLoginServerName = app.profile.loginServerName; - String finalLoginUsername = app.profile.loginUsername; - String finalLoginPassword = app.profile.loginPassword; - Ion.with(app.getContext()) - .load("https://iuczniowie.progman.pl/idziennik/login.aspx") - .setTimeout(REQUEST_TIMEOUT) - .setHeader("User-Agent", Iuczniowie.userAgent) - //.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") - .asString() - .setCallback((e, result) -> { - if (e instanceof java.util.concurrent.TimeoutException) { - webView.loadData("

"+app.getString(R.string.api_error_code_timeout)+"

", "text/html", "UTF-8"); - return; - } - app.profile.loggedIn = (result != null && result.equals("ok")); - if (result == null || result.equals("")) { // for safety - webView.loadData("

"+app.getString(R.string.api_error_code_no_internet)+"

", "text/html", "UTF-8"); - return; - } - if (clearCookies) - clearCookies(app, "https://iuczniowie.progman.pl"); - - String post = ""; - try { - post += "ctl00$ContentPlaceHolder$nazwaPrzegladarki="+URLEncoder.encode(Iuczniowie.userAgent, "UTF-8"); - post += "ctl00$ContentPlaceHolder$NazwaSzkoly="+finalLoginServerName; - post += "ctl00$ContentPlaceHolder$UserName="+URLEncoder.encode(finalLoginUsername, "UTF-8"); - post += "ctl00$ContentPlaceHolder$Password="+URLEncoder.encode(finalLoginPassword, "UTF-8"); - post += "ctl00$ContentPlaceHolder$Logowanie=Zaloguj"; - } catch (UnsupportedEncodingException e1) { - e1.printStackTrace(); - } - - webView.getSettings().setUserAgentString(Iuczniowie.userAgent); - - });*/ - } else if (app.profile.getEmpty()) { - webView.loadData("

" + app.getString(R.string.sync_error_invalid_login) + "

", "text/html", "UTF-8"); - } else { - webView.loadData("

" + app.getString(R.string.settings_register_login_not_implemented_text) + "

", "text/html", "UTF-8"); - } - } - - - - /* _____ _______ _ - | __ \ /\ |__ __| | | - | |__) | __ ___ ___ ___ ___ ___ / \ ___ _ _ _ __ ___| | __ _ ___| | __ - | ___/ '__/ _ \ / __/ _ \/ __/ __| / /\ \ / __| | | | '_ \ / __| |/ _` / __| |/ / - | | | | | (_) | (_| __/\__ \__ \/ ____ \\__ \ |_| | | | | (__| | (_| \__ \ < - |_| |_| \___/ \___\___||___/___/_/ \_\___/\__, |_| |_|\___|_|\__,_|___/_|\_\ - __/ | - |__*/ - - /** - * A task for creating notifications and downloading shared events. - */ - private static class ProcessAsyncTask extends AsyncTask { - private App app; - private WeakReference activityContext; - private SyncCallback callback; - private Exception e = null; - private String apiResponse = null; - private int profileId; - private ProfileFull profile; - - public ProcessAsyncTask(App app, Context activityContext, SyncCallback callback, int profileId, ProfileFull profile) { - //d(TAG, "Thread/ProcessAsyncTask/constructor/"+Thread.currentThread().getName()); - this.app = app; - this.activityContext = new WeakReference<>(activityContext); - this.callback = callback; - this.profileId = profileId; - this.profile = profile; - } - - @Override - protected Integer doInBackground(Void... voids) { - Context activityContext = this.activityContext.get(); - //d(TAG, "Thread/ProcessAsyncTask/doInBackground/"+Thread.currentThread().getName()); - try { - - // UPDATE FCM TOKEN IF EMPTY - if (app.appConfig.fcmToken == null || app.appConfig.fcmToken.equals("")) { - FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(instanceIdResult -> { - app.appConfig.fcmToken = instanceIdResult.getToken(); - app.appConfig.savePending = true; - }); - } - - callback.onProgress(1); - if (profile.getSyncNotifications()) { - new Handler(activityContext.getMainLooper()).post(() -> { - callback.onActionStarted(R.string.sync_action_creating_notifications); - }); - - for (LessonFull change : app.db.lessonChangeDao().getNotNotifiedNow(profileId)) { - String text = app.getContext().getString(R.string.notification_lesson_change_format, change.changeTypeStr(app.getContext()), change.lessonDate == null ? "" : change.lessonDate.getFormattedString(), change.subjectLongName); - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.getId(), profile.getName()) - .withType(TYPE_TIMETABLE_LESSON_CHANGE) - .withFragmentRedirect(MainActivity.DRAWER_ITEM_TIMETABLE) - .withLongExtra("timetableDate", change.lessonDate.getValue()) - .withAddedDate(change.addedDate) - ); - } - for (EventFull event : app.db.eventDao().getNotNotifiedNow(profileId)) { - String text; - if (event.type == TYPE_HOMEWORK) - text = app.getContext().getString(R.string.notification_homework_format, ns(app.getString(R.string.notification_event_no_subject), event.subjectLongName), event.eventDate.getFormattedString()); - else - text = app.getContext().getString(R.string.notification_event_format, event.typeName, event.eventDate.getFormattedString(), ns(app.getString(R.string.notification_event_no_subject), event.subjectLongName)); - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.getId(), profile.getName()) - .withType(event.type == TYPE_HOMEWORK ? TYPE_NEW_HOMEWORK : TYPE_NEW_EVENT) - .withFragmentRedirect(event.type == TYPE_HOMEWORK ? MainActivity.DRAWER_ITEM_HOMEWORK : MainActivity.DRAWER_ITEM_AGENDA) - .withLongExtra("eventId", event.id) - .withLongExtra("eventDate", event.eventDate.getValue()) - .withAddedDate(event.addedDate) - ); - // student's rights abuse - disabled, because this was useless - /*if (!event.addedManually && event.type == RegisterEvent.TYPE_EXAM && event.eventDate.combineWith(event.startTime) - event.addedDate < 7 * 24 * 60 * 60 * 1000) { - text = app.getContext().getString(R.string.notification_abuse_format, event.typeString(app, app.profile), event.subjectLongName, event.eventDate.getFormattedString()); - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.id, profile.name) - .withType(Notification.TYPE_GENERAL) - .withFragmentRedirect(MainActivity.DRAWER_ITEM_NOTIFICATIONS) - ); - }*/ - } - - Date today = Date.getToday(); - int todayValue = today.getValue(); - profile.setCurrentSemester(profile.dateToSemester(today)); - - for (GradeFull grade : app.db.gradeDao().getNotNotifiedNow(profileId)) { - String gradeName = grade.name; - if (grade.type == TYPE_SEMESTER1_PROPOSED - || grade.type == TYPE_SEMESTER2_PROPOSED) { - gradeName = (app.getString(R.string.grade_semester_proposed_format_2, grade.name)); - } else if (grade.type == TYPE_SEMESTER1_FINAL - || grade.type == TYPE_SEMESTER2_FINAL) { - gradeName = (app.getString(R.string.grade_semester_final_format_2, grade.name)); - } else if (grade.type == TYPE_YEAR_PROPOSED) { - gradeName = (app.getString(R.string.grade_year_proposed_format_2, grade.name)); - } else if (grade.type == TYPE_YEAR_FINAL) { - gradeName = (app.getString(R.string.grade_year_final_format_2, grade.name)); - } - String text = app.getContext().getString(R.string.notification_grade_format, gradeName, grade.subjectLongName); - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.getId(), profile.getName()) - .withType(TYPE_NEW_GRADE) - .withFragmentRedirect(MainActivity.DRAWER_ITEM_GRADES) - .withLongExtra("gradesSubjectId", grade.subjectId) - .withAddedDate(grade.addedDate) - ); - } - for (NoticeFull notice : app.db.noticeDao().getNotNotifiedNow(profileId)) { - String noticeTypeStr = (notice.type == Notice.TYPE_POSITIVE ? app.getString(R.string.notification_notice_praise) : (notice.type == Notice.TYPE_NEGATIVE ? app.getString(R.string.notification_notice_warning) : app.getString(R.string.notification_notice_new))); - String text = app.getContext().getString(R.string.notification_notice_format, noticeTypeStr, notice.teacherFullName, Date.fromMillis(notice.addedDate).getFormattedString()); - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.getId(), profile.getName()) - .withType(TYPE_NEW_NOTICE) - .withFragmentRedirect(MainActivity.DRAWER_ITEM_BEHAVIOUR) - .withLongExtra("noticeId", notice.id) - .withAddedDate(notice.addedDate) - ); - } - for (AttendanceFull attendance : app.db.attendanceDao().getNotNotifiedNow(profileId)) { - String attendanceTypeStr = app.getString(R.string.notification_type_attendance); - switch (attendance.type) { - case Attendance.TYPE_ABSENT: - attendanceTypeStr = app.getString(R.string.notification_absence); - break; - case Attendance.TYPE_ABSENT_EXCUSED: - attendanceTypeStr = app.getString(R.string.notification_absence_excused); - break; - case Attendance.TYPE_BELATED: - attendanceTypeStr = app.getString(R.string.notification_belated); - break; - case Attendance.TYPE_BELATED_EXCUSED: - attendanceTypeStr = app.getString(R.string.notification_belated_excused); - break; - case Attendance.TYPE_RELEASED: - attendanceTypeStr = app.getString(R.string.notification_release); - break; - } - String text = app.getContext().getString(R.string.notification_attendance_format, attendanceTypeStr, attendance.subjectLongName, attendance.lessonDate.getFormattedString()); - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.getId(), profile.getName()) - .withType(TYPE_NEW_ATTENDANCE) - .withFragmentRedirect(MainActivity.DRAWER_ITEM_ATTENDANCE) - .withLongExtra("attendanceId", attendance.id) - .withAddedDate(attendance.addedDate) - ); - } - for (AnnouncementFull announcement : app.db.announcementDao().getNotNotifiedNow(profileId)) { - String text = app.getContext().getString(R.string.notification_announcement_format, announcement.subject); - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.getId(), profile.getName()) - .withType(TYPE_NEW_ANNOUNCEMENT) - .withFragmentRedirect(MainActivity.DRAWER_ITEM_ANNOUNCEMENTS) - .withLongExtra("announcementId", announcement.id) - .withAddedDate(announcement.addedDate) - ); - } - for (MessageFull message : app.db.messageDao().getReceivedNotNotifiedNow(profileId)) { - String text = app.getContext().getString(R.string.notification_message_format, message.senderFullName, message.subject); - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.getId(), profile.getName()) - .withType(TYPE_NEW_MESSAGE) - .withFragmentRedirect(MainActivity.DRAWER_ITEM_MESSAGES) - .withLongExtra("messageType", Message.TYPE_RECEIVED) - .withLongExtra("messageId", message.id) - .withAddedDate(message.addedDate) - ); - } - - if (profile.getLuckyNumber() != oldLuckyNumber - && profile.getLuckyNumber() != -1 - && profile.getLuckyNumberDate() != null - && profile.getLuckyNumberDate().getValue() >= todayValue) { - String text; - if (profile.getLuckyNumberDate().getValue() == todayValue) { // LN for today - text = app.getString((profile.getStudentNumber() != -1 && profile.getStudentNumber() == profile.getLuckyNumber() ? R.string.notification_lucky_number_yours_format : R.string.notification_lucky_number_format), profile.getLuckyNumber()); - } else if (profile.getLuckyNumberDate().getValue() == todayValue + 1) { // LN for tomorrow - text = app.getString((profile.getStudentNumber() != -1 && profile.getStudentNumber() == profile.getLuckyNumber() ? R.string.notification_lucky_number_yours_tomorrow_format : R.string.notification_lucky_number_tomorrow_format), profile.getLuckyNumber()); - } else { // LN for later - text = app.getString((profile.getStudentNumber() != -1 && profile.getStudentNumber() == profile.getLuckyNumber() ? R.string.notification_lucky_number_yours_later_format : R.string.notification_lucky_number_later_format), profile.getLuckyNumberDate().getFormattedString(), profile.getLuckyNumber()); - } - app.notifier.add(new Notification(app.getContext(), text) - .withProfileData(profile.getId(), profile.getName()) - .withType(TYPE_LUCKY_NUMBER) - .withFragmentRedirect(MainActivity.DRAWER_ITEM_HOME) - ); - oldLuckyNumber = profile.getLuckyNumber(); - } - } - - - app.db.metadataDao().setAllNotified(profileId, true); - callback.onProgress(1); - - // SEND WEB PUSH, if registration allowed - // otherwise, UNREGISTER THE USER - if (profile.getRegistration() == REGISTRATION_ENABLED) { - new Handler(activityContext.getMainLooper()).post(() -> { - callback.onActionStarted(R.string.sync_action_syncing_shared_events); - }); - //if (profile.registrationUsername == null || profile.registrationUsername.equals("")) { - //} - ServerRequest syncRequest = new ServerRequest(app, app.requestScheme + APP_URL + "main.php?sync", "Edziennik/REG", profile); - - if (registerEmpty) { - syncRequest.setBodyParameter("first_run", "true"); - } - - // ALSO SEND NEW DATA TO BROWSER *excluding* all Shared Events !!! - // because they will be sent by the server, as soon as it's shared, by FCM - - if (app.appConfig.webPushEnabled) { - int position = 0; - for (Notification notification : app.appConfig.notifications) { - //Log.d(TAG, notification.text); - if (!notification.notified) { - if (notification.type != TYPE_NEW_SHARED_EVENT - && notification.type != TYPE_SERVER_MESSAGE - && notification.type != TYPE_NEW_SHARED_HOMEWORK) // these are automatically sent to the browser by the server - { - //Log.d(TAG, "Adding notify[" + position + "]"); - syncRequest.setBodyParameter("notify[" + position + "][type]", Integer.toString(notification.type)); - syncRequest.setBodyParameter("notify[" + position + "][title]", notification.title); - syncRequest.setBodyParameter("notify[" + position + "][text]", notification.text); - position++; - } - } - } - } - - callback.onProgress(1); - - if (app.appConfig.webPushEnabled || profile.getEnableSharedEvents()) { - JsonObject result = syncRequest.runSync(); - callback.onProgress(1); - //Log.d(TAG, "Executed request"); - if (result == null) { - return AppError.CODE_APP_SERVER_ERROR; - } - apiResponse = result.toString(); - if (!result.get("success").getAsString().equals("true")) { - return AppError.CODE_APP_SERVER_ERROR; - } - // HERE PROCESS ALL THE RECEIVED EVENTS - // add them to the profile and create appropriate notifications - for (JsonElement jEventEl : result.getAsJsonArray("events")) { - JsonObject jEvent = jEventEl.getAsJsonObject(); - String teamCode = jEvent.get("team").getAsString(); - //d(TAG, "An event is there! "+jEvent.toString()); - // get the target Team from teamCode - Team team = app.db.teamDao().getByCodeNow(profile.getId(), teamCode); - if (team != null) { - //d(TAG, "The target team is "+team.name+", ID "+team.id); - // create the event from Json. Add the missing teamId and !!profileId!! - Event event = app.gson.fromJson(jEvent.toString(), Event.class); - // proguard. disable for Event.class - if (event.eventDate == null) { - apiResponse += "\n\nEventDate == null\n" + jEvent.toString(); - throw new Exception("null eventDate"); - } - event.profileId = profile.getId(); - event.teamId = team.id; - event.addedManually = true; - //d(TAG, "Created the event! "+event); - - if (event.sharedBy != null && event.sharedBy.equals(profile.getUsernameId())) { - //d(TAG, "Shared by self! Changing name"); - event.sharedBy = "self"; - event.sharedByName = profile.getStudentNameLong(); - } - - EventType type = app.db.eventTypeDao().getByIdNow(profileId, event.type); - - //d(TAG, "Finishing adding event "+event); - app.db.eventDao().add(event); - Metadata metadata = new Metadata(profile.getId(), event.type == TYPE_HOMEWORK ? Metadata.TYPE_HOMEWORK : Metadata.TYPE_EVENT, event.id, registerEmpty, true, jEvent.get("addedDate").getAsLong()); - long metadataId = app.db.metadataDao().add(metadata); - if (metadataId != -1 && !registerEmpty) { - app.notifier.add(new Notification(app.getContext(), app.getString(R.string.notification_shared_event_format, event.sharedByName, type != null ? type.name : "wydarzenie", event.eventDate == null ? "nieznana data" : event.eventDate.getFormattedString(), event.topic)) - .withProfileData(profile.getId(), profile.getName()) - .withType(event.type == TYPE_HOMEWORK ? TYPE_NEW_SHARED_HOMEWORK : TYPE_NEW_SHARED_EVENT) - .withFragmentRedirect(event.type == TYPE_HOMEWORK ? MainActivity.DRAWER_ITEM_HOMEWORK : MainActivity.DRAWER_ITEM_AGENDA) - .withLongExtra("eventDate", event.eventDate.getValue()) - ); - } - } - } - callback.onProgress(5); - return CODE_OK; - } else { - callback.onProgress(6); - return CODE_OK; - } - } else { - // the user does not want to be registered - callback.onProgress(7); - return CODE_OK; - } - } catch (Exception e) { - e.printStackTrace(); - this.e = e; - return null; - } - //return null; - } - - @Override - protected void onPostExecute(Integer errorCode) { - //d(TAG, "Thread/ProcessAsyncTask/onPostExecute/"+Thread.currentThread().getName()); - Context activityContext = this.activityContext.get(); - app.profileSaveFull(profile); - if (app.profile != null && profile.getId() == app.profile.getId()) { - app.profile = profile; - } - if (errorCode == null) { - // this means an Exception was thrown - callback.onError(activityContext, new AppError(TAG, 513, CODE_OTHER, e, apiResponse)); - return; - } - //Log.d(TAG, "Finishing"); - - - callback.onProgress(1); - - if (errorCode == CODE_OK) - callback.onSuccess(activityContext, profile); - else { - try { - // oh that's useless - throw new RuntimeException(stringErrorCode(app, errorCode, "")); - } catch (Exception e) { - callback.onError(activityContext, new AppError(TAG, 528, errorCode, e, (String) null)); - } - } - super.onPostExecute(errorCode); - } - } - - public void notifyAndReload() { - // TODO \/ - - Intent intent = new Intent(app.getContext(), WidgetTimetable.class); - intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - int[] ids = AppWidgetManager.getInstance(app).getAppWidgetIds(new ComponentName(app, WidgetTimetable.class)); - intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); - app.sendBroadcast(intent); - - intent = new Intent(app.getContext(), WidgetNotifications.class); - intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - ids = AppWidgetManager.getInstance(app).getAppWidgetIds(new ComponentName(app, WidgetNotifications.class)); - intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); - app.sendBroadcast(intent); - - intent = new Intent(app.getContext(), WidgetLuckyNumber.class); - intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); - ids = AppWidgetManager.getInstance(app).getAppWidgetIds(new ComponentName(app, WidgetLuckyNumber.class)); - intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids); - app.sendBroadcast(intent); - } - - /* _____ - / ____| - | (___ _ _ _ __ ___ - \___ \| | | | '_ \ / __| - ____) | |_| | | | | (__ - |_____/ \__, |_| |_|\___| - __/ | - |__*/ - // DataCallbacks that are *not* in Edziennik.sync need to be executed on the main thread. - // EdziennikInterface.sync is executed on a worker thread - // in Edziennik.sync/newCallback methods are called on a worker thread - - // callback passed to Edziennik.sync is executed on the main thread - // thus, callback which is in guiSync is also on the main thread - - /** - * Sync all Edziennik data. - * Used in services, login form and {@code guiSync} - *

- * May be ran on worker thread. - * {@link EdziennikInterface}.sync is ran always on worker thread. - * Every callback is ran on the UI thread. - * - * @param app - * @param activityContext - * @param callback - * @param profileId - */ - public void sync(@NonNull App app, @NonNull Context activityContext, @NonNull SyncCallback callback, int profileId) { - sync(app, activityContext, callback, profileId, (int[])null); - } - public void sync(@NonNull App app, @NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable int ... featureList) { - // empty: no unread notifications, all shared events (current+past) - // only if there is no data, and we are not logged in yet - SyncCallback newCallback = new SyncCallback() { - @Override - public void onLoginFirst(List profileList, LoginStore loginStore) { - new Handler(activityContext.getMainLooper()).post(() -> { - callback.onLoginFirst(profileList, loginStore); - }); - } - - @Override - public void onSuccess(Context activityContext, ProfileFull profileFull) { - new Handler(activityContext.getMainLooper()).post(() -> { - new ProcessAsyncTask(app, activityContext, callback, profileId, profileFull).execute(); - }); - } - - @Override - public void onError(Context activityContext, AppError error) { - new Handler(activityContext.getMainLooper()).post(() -> { - callback.onError(activityContext, error); - }); - } - - @Override - public void onProgress(int progressStep) { - new Handler(activityContext.getMainLooper()).post(() -> { - callback.onProgress(progressStep); - }); - } - - @Override - public void onActionStarted(int stringResId) { - new Handler(activityContext.getMainLooper()).post(() -> { - callback.onActionStarted(stringResId); - }); - } - }; - AsyncTask.execute(() -> { - ProfileFull profile = app.db.profileDao().getFullByIdNow(profileId); - if (profile != null) { - - if (profile.getArchived()) { - newCallback.onError(activityContext, new AppError(TAG, 678, CODE_PROFILE_ARCHIVED, profile.getName())); - return; - } - else if (profile.getDateYearEnd() != null && Date.getToday().getValue() >= profile.getDateYearEnd().getValue()) { - profile.setArchived(true); - app.notifier.add(new Notification(app.getContext(), app.getString(R.string.profile_auto_archiving_format, profile.getName(), profile.getDateYearEnd().getFormattedString())) - .withProfileData(profile.getId(), profile.getName()) - .withType(TYPE_AUTO_ARCHIVING) - .withFragmentRedirect(DRAWER_ITEM_HOME) - .withLongExtra("autoArchiving", 1L) - ); - app.notifier.postAll(null); - app.db.profileDao().add(profile); - if (App.profileId == profile.getId()) { - app.profile.setArchived(true); - } - newCallback.onSuccess(activityContext, profile); - return; - } - - registerEmpty = profile.getEmpty(); - oldLuckyNumber = profile.getLuckyNumber(); - getApi(app, profile.getLoginStoreType()).syncFeature(activityContext, newCallback, profile, featureList); - } else { - new Handler(activityContext.getMainLooper()).post(() -> callback.onError(activityContext, new AppError(TAG, 609, CODE_PROFILE_NOT_FOUND, (String) null))); - } - }); - } - - /* _____ _ _ _____ - / ____| | | |_ _| - | | __| | | | | | __ ___ __ __ _ _ __ _ __ ___ _ __ ___ - | | |_ | | | | | | \ \ /\ / / '__/ _` | '_ \| '_ \ / _ \ '__/ __| - | |__| | |__| |_| |_ \ V V /| | | (_| | |_) | |_) | __/ | \__ \ - \_____|\____/|_____| \_/\_/ |_| \__,_| .__/| .__/ \___|_| |___/ - | | | | - |_| |*/ - /** - * Sync all Edziennik data while showing a progress dialog. - * A wrapper for {@code sync} - * - * Does not switch between threads. - * All callbacks have to be executed on the UI thread. - * - * @param app an App singleton instance - * @param activity a parent activity - * @param profileId ID of the profile to sync - * @param dialogTitle a title of the dialog to show - * @param dialogText dialog's content - * @param successText a toast to show on success - */ - public void guiSync(@NonNull App app, @NonNull Activity activity, int profileId, @StringRes int dialogTitle, @StringRes int dialogText, @StringRes int successText) { - guiSync(app, activity, profileId, dialogTitle, dialogText, successText, (int[])null); - } - public void guiSync(@NonNull App app, @NonNull Activity activity, int profileId, @StringRes int dialogTitle, @StringRes int dialogText, @StringRes int successText, int ... featureList) { - MaterialDialog progressDialog = new MaterialDialog.Builder(activity) - .title(dialogTitle) - .content(dialogText) - .progress(false, 110, false) - .canceledOnTouchOutside(false) - .show(); - SyncCallback guiSyncCallback = new SyncCallback() { - @Override - public void onLoginFirst(List profileList, LoginStore loginStore) { - - } - - @Override - public void onSuccess(Context activityContext, ProfileFull profileFull) { - progressDialog.dismiss(); - Toast.makeText(activityContext, successText, Toast.LENGTH_SHORT).show(); - notifyAndReload(); - // profiles are saved automatically, during app.saveConfig in processFinish - /*if (activityContext instanceof MainActivity) { - //((MainActivity) activityContext).reloadCurrentFragment("GuiSync"); - ((MainActivity) activityContext).accountHeaderAddProfiles(); - }*/ - } - - @Override - public void onError(Context activityContext, AppError error) { - progressDialog.dismiss(); - guiShowErrorDialog((Activity) activityContext, error, R.string.sync_error_dialog_title); - } - - @Override - public void onProgress(int progressStep) { - progressDialog.incrementProgress(progressStep); - } - - @Override - public void onActionStarted(int stringResId) { - progressDialog.setContent(activity.getString(R.string.sync_action_format, activity.getString(stringResId))); - } - }; - app.apiEdziennik.sync(app, activity, guiSyncCallback, profileId, featureList); - } - /** - * Sync all Edziennik data in background. - * A callback is executed on main thread. - * A wrapper for {@code sync} - * - * @param app an App singleton instance - * @param activity a parent activity - * @param profileId ID of the profile to sync - * @param syncCallback a callback - * @param feature a feature to sync - */ - public void guiSyncSilent(@NonNull App app, @NonNull Activity activity, int profileId, SyncCallback syncCallback, int feature) { - SyncCallback guiSyncCallback = new SyncCallback() { - @Override - public void onLoginFirst(List profileList, LoginStore loginStore) { - - } - - @Override - public void onSuccess(Context activityContext, ProfileFull profileFull) { - notifyAndReload(); - syncCallback.onSuccess(activityContext, profileFull); - } - - @Override - public void onError(Context activityContext, AppError error) { - syncCallback.onError(activityContext, error); - } - - @Override - public void onProgress(int progressStep) { - syncCallback.onProgress(progressStep); - } - - @Override - public void onActionStarted(int stringResId) { - syncCallback.onActionStarted(stringResId); - } - }; - app.apiEdziennik.sync(app, activity, guiSyncCallback, profileId, feature == FEATURE_ALL ? null : new int[]{feature}); - } - - /** - * Show a dialog allowing the user to choose which features to sync. - * Handles everything including pre-selecting the features basing on the current fragment. - * - * Will execute {@code sync} after the selection is made. - * - * A normal progress dialog is shown during the sync. - * - * @param app an App singleton instance - * @param activity a parent activity - * @param profileId ID of the profile to sync - * @param dialogTitle a title of the dialog to show - * @param dialogText dialog's content - * @param successText a toast to show on success - * @param currentFeature a feature id representing the currently opened fragment or caller - */ - public void guiSyncFeature(@NonNull App app, - @NonNull Activity activity, - int profileId, - @StringRes int dialogTitle, - @StringRes int dialogText, - @StringRes int successText, - int currentFeature) { - - String[] items = new String[]{ - app.getString(R.string.menu_timetable), - app.getString(R.string.menu_agenda), - app.getString(R.string.menu_grades), - app.getString(R.string.menu_homework), - app.getString(R.string.menu_notices), - app.getString(R.string.menu_attendance), - app.getString(R.string.title_messages_inbox_single), - app.getString(R.string.title_messages_sent_single), - app.getString(R.string.menu_announcements) - }; - int[] itemsIds = new int[]{ - FEATURE_TIMETABLE, - FEATURE_AGENDA, - FEATURE_GRADES, - FEATURE_HOMEWORK, - FEATURE_NOTICES, - FEATURE_ATTENDANCE, - FEATURE_MESSAGES_INBOX, - FEATURE_MESSAGES_OUTBOX, - FEATURE_ANNOUNCEMENTS - }; - int[] selectedIndices; - if (currentFeature == FEATURE_ALL) { - selectedIndices = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8}; - } - else { - selectedIndices = new int[]{Arrays.binarySearch(itemsIds, currentFeature)}; - } - - MaterialDialog dialog = new MaterialDialog.Builder(activity) - .title(R.string.sync_feature_title) - .content(R.string.sync_feature_text) - .positiveText(R.string.ok) - .negativeText(R.string.cancel) - .neutralText(R.string.sync_feature_all) - .items(items) - .itemsIds(itemsIds) - .itemsCallbackMultiChoice(ArrayUtils.toWrapperArray(selectedIndices), (dialog1, which, text) -> { - dialog1.getActionButton(DialogAction.POSITIVE).setEnabled(which.length > 0); - return true; - }) - .alwaysCallMultiChoiceCallback() - .onPositive(((dialog1, which) -> { - List featureList = new ArrayList<>(); - for (int i: dialog1.getSelectedIndices()) { - featureList.add(itemsIds[i]); - } - guiSync(app, activity, profileId, dialogTitle, dialogText, successText, ArrayUtils.toPrimitiveArray(featureList)); - })) - .onNeutral(((dialog1, which) -> { - guiSync(app, activity, profileId, dialogTitle, dialogText, successText); - })) - .show(); - - - - } - - public void guiShowArchivedDialog(Activity activity, String profileName) { - new MaterialDialog.Builder(activity) - .title(R.string.profile_archived_dialog_title) - .content(activity.getString(R.string.profile_archived_dialog_text_format, profileName)) - .positiveText(R.string.ok) - .onPositive(((dialog, which) -> dialog.dismiss())) - .autoDismiss(false) - .show(); - } - - /* _____ _ _ _____ - / ____| | | |_ _| - | | __| | | | | | ___ _ __ _ __ ___ _ __ ___ - | | |_ | | | | | | / _ \ '__| '__/ _ \| '__/ __| - | |__| | |__| |_| |_ | __/ | | | | (_) | | \__ \ - \_____|\____/|_____| \___|_| |_| \___/|_| |__*/ - /** - * Used for reporting an exception somewhere in the code that is not part of Edziennik APIs. - * - * @param activity a parent activity - * @param errorLine the line of code where the error occurred - * @param e an Exception object - */ - public void guiReportException(Activity activity, int errorLine, Exception e) { - guiReportError(activity, new AppError(TAG, errorLine, CODE_OTHER, "Błąd wewnętrzny aplikacji ("+errorLine+")", null, null, e, null), null); - } - - public void guiShowErrorDialog(Activity activity, @NonNull AppError error, @StringRes int dialogTitle) { - if (error.errorCode == CODE_PROFILE_ARCHIVED) { - guiShowArchivedDialog(activity, error.errorText); - return; - } - error.changeIfCodeOther(); - new MaterialDialog.Builder(activity) - .title(dialogTitle) - .content(error.asReadableString(activity)) - .positiveText(R.string.ok) - .onPositive(((dialog, which) -> dialog.dismiss())) - .neutralText(R.string.sync_error_dialog_report_button) - .onNeutral(((dialog, which) -> { - guiReportError(activity, error, dialog); - })) - .autoDismiss(false) - .show(); - } - public void guiShowErrorSnackbar(MainActivity activity, @NonNull AppError error) { - if (error.errorCode == CODE_PROFILE_ARCHIVED) { - guiShowArchivedDialog(activity, error.errorText); - return; - } - - // TODO: 2019-08-28 - IconicsDrawable icon = new IconicsDrawable(activity) - .icon(CommunityMaterial.Icon.cmd_alert_circle); - sizeDp(icon, 20); - colorInt(icon, Themes.INSTANCE.getPrimaryTextColor(activity)); - - error.changeIfCodeOther(); - CafeBar.builder(activity) - .to(activity.findViewById(R.id.coordinator)) - .content(error.asReadableString(activity)) - .icon(icon) - .positiveText(R.string.more) - .positiveColor(0xff4caf50) - .negativeText(R.string.ok) - .negativeColor(0x66ffffff) - .onPositive((cafeBar -> guiReportError(activity, error, null))) - .onNegative((cafeBar -> cafeBar.dismiss())) - .autoDismiss(false) - .swipeToDismiss(true) - .floating(true) - .show(); - } - public void guiReportError(Activity activity, AppError error, @Nullable MaterialDialog parentDialogToDisableNeutral) { - String errorDetails = error.getDetails(activity); - String htmlErrorDetails = ""+errorDetails+""; - htmlErrorDetails = htmlErrorDetails.replaceAll(activity.getPackageName(), ""+activity.getPackageName()+""); - htmlErrorDetails = htmlErrorDetails.replaceAll("\n", "
"); - - new MaterialDialog.Builder(activity) - .title(R.string.sync_report_dialog_title) - .content(Html.fromHtml(htmlErrorDetails)) - .typeface(null, "RobotoMono-Regular.ttf") - .negativeText(R.string.close) - .onNegative(((dialog1, which1) -> dialog1.dismiss())) - .neutralText(R.string.copy_to_clipboard) - .onNeutral((dialog1, which1) -> { - ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(CLIPBOARD_SERVICE); - if (clipboard != null) { - ClipData clip = ClipData.newPlainText("Error report", errorDetails); - clipboard.setPrimaryClip(clip); - Toast.makeText(activity, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show(); - } - }) - .autoDismiss(false) - .positiveText(R.string.sync_report_dialog_button) - .checkBoxPromptRes(R.string.sync_report_dialog_include_api_response, true, null) - .onPositive(((dialog1, which1) -> AsyncTask.execute(() -> error.getApiResponse(activity, apiResponse -> { - new ServerRequest(app, app.requestScheme + APP_URL + "main.php?report", "Edziennik/Report") - .setBodyParameter("base64_encoded", Base64.encodeToString(errorDetails.getBytes(), Base64.DEFAULT)) - .setBodyParameter("api_response", dialog1.isPromptCheckBoxChecked() ? Base64.encodeToString(apiResponse.getBytes(), Base64.DEFAULT) : "VW5jaGVja2Vk"/*Unchecked*/) - .run((e, result) -> { - new Handler(activity.getMainLooper()).post(() -> { - if (result != null) - { - if (result.get("success").getAsBoolean()) { - Toast.makeText(activity, activity.getString(R.string.crash_report_sent), Toast.LENGTH_SHORT).show(); - dialog1.getActionButton(DialogAction.POSITIVE).setEnabled(false); - if (parentDialogToDisableNeutral != null) - parentDialogToDisableNeutral.getActionButton(DialogAction.NEUTRAL).setEnabled(false); - } - else { - Toast.makeText(activity, activity.getString(R.string.crash_report_cannot_send) + ": " + result.get("reason").getAsString(), Toast.LENGTH_LONG).show(); - } - } - else - { - Toast.makeText(activity, activity.getString(R.string.crash_report_cannot_send)+" brak internetu", Toast.LENGTH_LONG).show(); - } - }); - }); - })))) - .show(); - } - - /** - * A method that displays a dialog allowing the user to report an error that has occurred. - * - * @param activity a parent activity - * @param errorCode self-explanatory - * @param errorText additional error information, that replaces text based on {@code errorCode} if it's {@code CODE_OTHER} - * @param throwable a {@link Throwable} containing the error details - * @param apiResponse response of the Edziennik API - * @param parentDialogToDisableNeutral if not null, an instance of {@link MaterialDialog} in which the neutral button should be disabled after submitting an error report - */ - public void guiReportError(Activity activity, int errorCode, String errorText, Throwable throwable, String apiResponse, @Nullable MaterialDialog parentDialogToDisableNeutral) { - // build a string containing the stack trace and the device name + user's registration data - String contentPlain = "Application Internal Error "+stringErrorType(errorCode)+":\n"+stringErrorCode(activity, errorCode, "")+"\n"+errorText+"\n\n"; - contentPlain += Log.getStackTraceString(throwable); - String content = ""+contentPlain+""; - content = content.replaceAll(activity.getPackageName(), ""+activity.getPackageName()+""); - content = content.replaceAll("\n", "
"); - - contentPlain += "\n"+Build.MANUFACTURER+"\n"+Build.BRAND+"\n"+Build.MODEL+"\n"+Build.DEVICE+"\n"; - if (app.profile != null && app.profile.getRegistration() == REGISTRATION_ENABLED) { - contentPlain += "U: "+app.profile.getUsernameId()+"\nS: "+ app.profile.getStudentNameLong() +"\nT: "+app.profile.loginStoreType()+"\n"; - } - contentPlain += BuildConfig.VERSION_NAME+" "+BuildConfig.BUILD_TYPE+"\nAndroid "+Build.VERSION.RELEASE; - - d(TAG, contentPlain); - d(TAG, apiResponse == null ? "API Response = null" : apiResponse); - - - // show a dialog containing the error details in HTML - String finalContentPlain = contentPlain; - new MaterialDialog.Builder(activity) - .title(R.string.sync_report_dialog_title) - .content(Html.fromHtml(content)) - .typeface(null, "RobotoMono-Regular.ttf") - .negativeText(R.string.close) - .onNegative(((dialog1, which1) -> dialog1.dismiss())) - .neutralText(R.string.copy_to_clipboard) - .onNeutral((dialog1, which1) -> { - ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(CLIPBOARD_SERVICE); - if (clipboard != null) { - ClipData clip = ClipData.newPlainText("Error report", finalContentPlain); - clipboard.setPrimaryClip(clip); - Toast.makeText(activity, R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show(); - } - }) - .autoDismiss(false) - .positiveText(R.string.sync_report_dialog_button) - .checkBoxPromptRes(R.string.sync_report_dialog_include_api_response, true, null) - .onPositive(((dialog1, which1) -> { - // send the error report - new ServerRequest(app, app.requestScheme + APP_URL + "main.php?report", "Edziennik/Report") - .setBodyParameter("base64_encoded", Base64.encodeToString(finalContentPlain.getBytes(), Base64.DEFAULT)) - .setBodyParameter("api_response", dialog1.isPromptCheckBoxChecked() ? apiResponse == null ? Base64.encodeToString("NULL XD".getBytes(), Base64.DEFAULT) : Base64.encodeToString(apiResponse.getBytes(), Base64.DEFAULT) : "VW5jaGVja2Vk"/*Unchecked*/) - .run((e, result) -> { - new Handler(Looper.getMainLooper()).post(() -> { - if (result != null) - { - if (result.get("success").getAsBoolean()) { - Toast.makeText(activity, activity.getString(R.string.crash_report_sent), Toast.LENGTH_SHORT).show(); - dialog1.getActionButton(DialogAction.POSITIVE).setEnabled(false); - if (parentDialogToDisableNeutral != null) - parentDialogToDisableNeutral.getActionButton(DialogAction.NEUTRAL).setEnabled(false); - } - else { - Toast.makeText(activity, activity.getString(R.string.crash_report_cannot_send) + ": " + result.get("reason").getAsString(), Toast.LENGTH_LONG).show(); - } - } - else - { - Toast.makeText(activity, activity.getString(R.string.crash_report_cannot_send)+" JsonObject equals null", Toast.LENGTH_LONG).show(); - } - }); - }); - })) - .show(); - } - - /* _____ __ _ _ _ - | __ \ / _(_) | | | - | |__) | __ ___ | |_ _| | ___ _ __ ___ _ __ ___ _____ ____ _| | - | ___/ '__/ _ \| _| | |/ _ \ | '__/ _ \ '_ ` _ \ / _ \ \ / / _` | | - | | | | | (_) | | | | | __/ | | | __/ | | | | | (_) \ V / (_| | | - |_| |_| \___/|_| |_|_|\___| |_| \___|_| |_| |_|\___/ \_/ \__,_|*/ - public void guiRemoveProfile(MainActivity activity, int profileId, String profileName) { - new MaterialDialog.Builder(activity) - .title(R.string.profile_menu_remove_confirm) - .content(activity.getString(R.string.profile_menu_remove_confirm_text_format, profileName, profileName)) - .positiveText(R.string.remove) - .negativeText(R.string.cancel) - .onPositive(((dialog, which) -> { - AsyncTask.execute(() -> { - removeProfile(profileId); - activity.runOnUiThread(() -> { - //activity.drawer.loadItem(DRAWER_ITEM_HOME, null, "ProfileRemoving"); - //activity.recreate(DRAWER_ITEM_HOME); - activity.reloadTarget(); - Toast.makeText(activity, "Profil został usunięty.", Toast.LENGTH_LONG).show(); - }); - }); - })) - .show(); - } - public void removeProfile(int profileId) { - Profile profileObject = app.db.profileDao().getByIdNow(profileId); - if (profileObject == null) - return; - app.db.announcementDao().clear(profileId); - app.db.attendanceDao().clear(profileId); - app.db.eventDao().clear(profileId); - app.db.eventTypeDao().clear(profileId); - app.db.gradeDao().clear(profileId); - app.db.gradeCategoryDao().clear(profileId); - app.db.lessonDao().clear(profileId); - app.db.lessonChangeDao().clear(profileId); - app.db.luckyNumberDao().clear(profileId); - app.db.noticeDao().clear(profileId); - app.db.subjectDao().clear(profileId); - app.db.teacherDao().clear(profileId); - app.db.teamDao().clear(profileId); - app.db.messageRecipientDao().clear(profileId); - app.db.messageDao().clear(profileId); - app.db.endpointTimerDao().clear(profileId); - app.db.attendanceTypeDao().clear(profileId); - app.db.classroomDao().clear(profileId); - app.db.lessonRangeDao().clear(profileId); - app.db.noticeTypeDao().clear(profileId); - app.db.teacherAbsenceDao().clear(profileId); - app.db.teacherAbsenceTypeDao().clear(profileId); - - int loginStoreId = profileObject.getLoginStoreId(); - List profilesUsingLoginStore = app.db.profileDao().getIdsByLoginStoreIdNow(loginStoreId); - if (profilesUsingLoginStore.size() == 1) { - app.db.loginStoreDao().remove(loginStoreId); - } - app.db.profileDao().remove(profileId); - app.db.metadataDao().deleteAll(profileId); - - List toRemove = new ArrayList<>(); - for (Notification notification: app.appConfig.notifications) { - if (notification.profileId == profileId) { - toRemove.add(notification); - } - } - app.appConfig.notifications.removeAll(toRemove); - - app.profile = null; - App.profileId = -1; - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Iuczniowie.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Iuczniowie.java deleted file mode 100644 index 6d1beed8..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Iuczniowie.java +++ /dev/null @@ -1,1709 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api; - -import android.content.Context; -import android.graphics.Color; -import android.os.AsyncTask; -import android.os.Handler; -import android.util.Log; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.core.util.Pair; - -import com.crashlytics.android.Crashlytics; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import im.wangchao.mhttp.Request; -import im.wangchao.mhttp.Response; -import im.wangchao.mhttp.body.MediaTypeUtils; -import im.wangchao.mhttp.callback.JsonArrayCallbackHandler; -import im.wangchao.mhttp.callback.JsonCallbackHandler; -import im.wangchao.mhttp.callback.TextCallbackHandler; -import okhttp3.Cookie; -import okhttp3.HttpUrl; -import pl.szczodrzynski.edziennik.App; -import pl.szczodrzynski.edziennik.BuildConfig; -import pl.szczodrzynski.edziennik.R; -import pl.szczodrzynski.edziennik.data.api.interfaces.AttachmentGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface; -import pl.szczodrzynski.edziennik.data.api.interfaces.LoginCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.MessageGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.RecipientListGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.SyncCallback; -import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement; -import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance; -import pl.szczodrzynski.edziennik.data.db.modules.events.Event; -import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange; -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore; -import pl.szczodrzynski.edziennik.data.db.modules.luckynumber.LuckyNumber; -import pl.szczodrzynski.edziennik.data.db.modules.messages.Message; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipientFull; -import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata; -import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileFull; -import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject; -import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher; -import pl.szczodrzynski.edziennik.data.db.modules.teams.Team; -import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesComposeInfo; -import pl.szczodrzynski.edziennik.utils.models.Date; -import pl.szczodrzynski.edziennik.utils.models.Endpoint; -import pl.szczodrzynski.edziennik.utils.models.Time; -import pl.szczodrzynski.edziennik.utils.models.Week; - -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_INVALID_LOGIN; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_INVALID_SCHOOL_NAME; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_MAINTENANCE; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_OTHER; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_ABSENT; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_ABSENT_EXCUSED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_BELATED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_PRESENT; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_RELEASED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_YEAR_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_YEAR_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange.TYPE_CANCELLED; -import static pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange.TYPE_CHANGE; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_DELETED; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_RECEIVED; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT; -import static pl.szczodrzynski.edziennik.data.db.modules.notices.Notice.TYPE_NEGATIVE; -import static pl.szczodrzynski.edziennik.data.db.modules.notices.Notice.TYPE_NEUTRAL; -import static pl.szczodrzynski.edziennik.data.db.modules.notices.Notice.TYPE_POSITIVE; -import static pl.szczodrzynski.edziennik.utils.Utils.crc16; -import static pl.szczodrzynski.edziennik.utils.Utils.crc32; -import static pl.szczodrzynski.edziennik.utils.Utils.d; -import static pl.szczodrzynski.edziennik.utils.Utils.getWordGradeValue; - -public class Iuczniowie implements EdziennikInterface { - public Iuczniowie(App app) { - this.app = app; - } - - private static final String TAG = "api.Iuczniowie"; - private static String IDZIENNIK_URL = "https://iuczniowie.progman.pl/idziennik"; - private static final String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36"; - - private App app; - private Context activityContext = null; - private SyncCallback callback = null; - private int profileId = -1; - private Profile profile = null; - private LoginStore loginStore = null; - private boolean fullSync = true; - private Date today = Date.getToday(); - private List targetEndpoints = new ArrayList<>(); - - // PROGRESS - private static final int PROGRESS_LOGIN = 10; - private int PROGRESS_COUNT = 1; - private int PROGRESS_STEP = (90/PROGRESS_COUNT); - - private int onlyFeature = FEATURE_ALL; - - private List teamList; - private List teacherList; - private List subjectList; - private List lessonList; - private List lessonChangeList; - private List gradeList; - private List eventList; - private List noticeList; - private List attendanceList; - private List announcementList; - private List messageList; - private List messageRecipientList; - private List messageRecipientIgnoreList; - private List metadataList; - private List messageMetadataList; - - private static boolean fakeLogin = false; - private String lastLogin = ""; - private long lastLoginTime = -1; - private String lastResponse = null; - private String loginSchoolName = null; - private String loginUsername = null; - private String loginPassword = null; - private String loginBearerToken = null; - private int loginRegisterId = -1; - private int loginSchoolYearId = -1; - private String loginStudentId = null; - private int teamClassId = -1; - - private boolean prepare(@NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable Profile profile, @NonNull LoginStore loginStore) { - this.activityContext = activityContext; - this.callback = callback; - this.profileId = profileId; - // here we must have a login store: either with a correct ID or -1 - // there may be no profile and that's when onLoginFirst happens - this.profile = profile; - this.loginStore = loginStore; - this.fullSync = profile == null || profile.getEmpty() || profile.shouldFullSync(activityContext); - this.today = Date.getToday(); - - this.loginSchoolName = loginStore.getLoginData("schoolName", ""); - this.loginUsername = loginStore.getLoginData("username", ""); - this.loginPassword = loginStore.getLoginData("password", ""); - if (loginSchoolName.equals("") || loginUsername.equals("") || loginPassword.equals("")) { - finishWithError(new AppError(TAG, 162, CODE_INVALID_LOGIN, "Login field is empty")); - return false; - } - fakeLogin = BuildConfig.DEBUG && loginUsername.startsWith("FAKE"); - IDZIENNIK_URL = fakeLogin ? "http://szkolny.eu/idziennik" : "https://iuczniowie.progman.pl/idziennik"; - - teamList = profileId == -1 ? new ArrayList<>() : app.db.teamDao().getAllNow(profileId); - teacherList = profileId == -1 ? new ArrayList<>() : app.db.teacherDao().getAllNow(profileId); - subjectList = profileId == -1 ? new ArrayList<>() : app.db.subjectDao().getAllNow(profileId); - lessonList = new ArrayList<>(); - lessonChangeList = new ArrayList<>(); - gradeList = new ArrayList<>(); - eventList = new ArrayList<>(); - noticeList = new ArrayList<>(); - attendanceList = new ArrayList<>(); - announcementList = new ArrayList<>(); - messageList = new ArrayList<>(); - messageRecipientList = new ArrayList<>(); - messageRecipientIgnoreList = new ArrayList<>(); - metadataList = new ArrayList<>(); - messageMetadataList = new ArrayList<>(); - - return true; - } - - @Override - public void sync(@NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable Profile profile, @NonNull LoginStore loginStore) { - if (!prepare(activityContext, callback, profileId, profile, loginStore)) - return; - - login(() -> { - targetEndpoints = new ArrayList<>(); - targetEndpoints.add("LuckyNumberAndSemesterDates"); - targetEndpoints.add("Timetable"); - targetEndpoints.add("Grades"); - targetEndpoints.add("PropositionGrades"); - targetEndpoints.add("Exams"); - targetEndpoints.add("Notices"); - targetEndpoints.add("Announcements"); - targetEndpoints.add("Attendance"); - targetEndpoints.add("MessagesInbox"); - targetEndpoints.add("MessagesOutbox"); - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - @Override - public void syncFeature(@NonNull Context activityContext, @NonNull SyncCallback callback, @NonNull ProfileFull profile, int ... featureList) { - if (featureList == null) { - sync(activityContext, callback, profile.getId(), profile, LoginStore.fromProfileFull(profile)); - return; - } - if (!prepare(activityContext, callback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - login(() -> { - targetEndpoints = new ArrayList<>(); - if (featureList.length == 1) - onlyFeature = featureList[0]; - targetEndpoints.add("LuckyNumberAndSemesterDates"); - for (int feature: featureList) { - switch (feature) { - case FEATURE_TIMETABLE: - targetEndpoints.add("Timetable"); - break; - case FEATURE_AGENDA: - targetEndpoints.add("Exams"); - break; - case FEATURE_GRADES: - targetEndpoints.add("Grades"); - targetEndpoints.add("PropositionGrades"); - break; - case FEATURE_HOMEWORK: - targetEndpoints.add("Homework"); - break; - case FEATURE_NOTICES: - targetEndpoints.add("Notices"); - break; - case FEATURE_ATTENDANCE: - targetEndpoints.add("Attendance"); - break; - case FEATURE_MESSAGES_INBOX: - targetEndpoints.add("MessagesInbox"); - break; - case FEATURE_MESSAGES_OUTBOX: - targetEndpoints.add("MessagesOutbox"); - break; - case FEATURE_ANNOUNCEMENTS: - targetEndpoints.add("Announcements"); - break; - } - } - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - - private void begin() { - List cookieList = app.cookieJar.loadForRequest(HttpUrl.get(IDZIENNIK_URL)); - for (Cookie cookie: cookieList) { - if (cookie.name().equalsIgnoreCase("Bearer")) { - loginBearerToken = cookie.value(); - } - } - loginStudentId = profile.getStudentData("studentId", null); - loginSchoolYearId = profile.getStudentData("schoolYearId", -1); - loginRegisterId = profile.getStudentData("registerId", -1); - - if (loginRegisterId == -1) { - finishWithError(new AppError(TAG, 212, CODE_OTHER, app.getString(R.string.error_register_id_not_found), "loginRegisterId == -1")); - return; - } - if (loginSchoolYearId == -1) { - finishWithError(new AppError(TAG, 216, CODE_OTHER, app.getString(R.string.error_school_year_not_found), "loginSchoolYearId == -1")); - return; - } - if (loginStudentId == null) { - if (lastResponse == null) { - lastLoginTime = -1; - lastLogin = ""; - finishWithError(new AppError(TAG, 223, CODE_OTHER, app.getString(R.string.error_student_id_not_found), "loginStudentId == null && lastResponse == null")); - return; - } - Matcher selectMatcher = Pattern.compile("", Pattern.DOTALL).matcher(lastResponse); - if (!selectMatcher.find()) { - finishWithError(new AppError(TAG, 228, CODE_OTHER, app.getString(R.string.error_register_id_not_found), lastResponse)); - return; - } - Matcher idMatcher = Pattern.compile(".*?", Pattern.DOTALL).matcher(selectMatcher.group(0)); - while (idMatcher.find()) { - loginStudentId = idMatcher.group(1); - profile.putStudentData("studentId", loginStudentId); - } - } - - this.attendanceMonth = today.month; - this.attendanceYear = today.year; - this.attendancePrevMonthChecked = false; - this.examsMonth = today.month; - this.examsYear = today.year; - this.examsMonthsChecked = 0; - this.examsNextMonthChecked = false; - - callback.onProgress(PROGRESS_LOGIN); - - r("get", null); - } - - private void r(String type, String endpoint) { - // endpoint == null when beginning - if (endpoint == null) - endpoint = targetEndpoints.get(0); - int index = -1; - for (String request: targetEndpoints) { - index++; - if (request.equals(endpoint)) { - break; - } - } - if (type.equals("finish")) { - // called when finishing the action - callback.onProgress(PROGRESS_STEP); - index++; - } - d(TAG, "Called r("+type+", "+endpoint+"). Getting "+targetEndpoints.get(index)); - switch (targetEndpoints.get(index)) { - case "LuckyNumberAndSemesterDates": - getLuckyNumberAndSemesterDates(); - break; - case "Timetable": - getTimetable(); - break; - case "Grades": - getGrades(); - break; - case "PropositionGrades": - getPropositionGrades(); - break; - case "Exams": - getExams(); - break; - case "Notices": - getNotices(); - break; - case "Announcements": - getAnnouncements(); - break; - case "Attendance": - getAttendance(); - break; - case "MessagesInbox": - getMessagesInbox(); - break; - case "MessagesOutbox": - getMessagesOutbox(); - break; - case "Finish": - finish(); - break; - } - } - private void saveData() { - if (teamList.size() > 0) { - //app.db.teamDao().clear(profileId); - app.db.teamDao().addAll(teamList); - } - if (teacherList.size() > 0) - app.db.teacherDao().addAll(teacherList); - if (subjectList.size() > 0) - app.db.subjectDao().addAll(subjectList); - if (lessonList.size() > 0) { - app.db.lessonDao().clear(profileId); - app.db.lessonDao().addAll(lessonList); - } - if (lessonChangeList.size() > 0) - app.db.lessonChangeDao().addAll(lessonChangeList); - if (gradeList.size() > 0) { - app.db.gradeDao().clear(profileId); - app.db.gradeDao().addAll(gradeList); - } - if (eventList.size() > 0) { - app.db.eventDao().removeFuture(profileId, today); - app.db.eventDao().addAll(eventList); - } - if (noticeList.size() > 0) { - app.db.noticeDao().clear(profileId); - app.db.noticeDao().addAll(noticeList); - } - if (attendanceList.size() > 0) - app.db.attendanceDao().addAll(attendanceList); - if (announcementList.size() > 0) - app.db.announcementDao().addAll(announcementList); - if (messageList.size() > 0) - app.db.messageDao().addAllIgnore(messageList); - if (messageRecipientList.size() > 0) - app.db.messageRecipientDao().addAll(messageRecipientList); - if (messageRecipientIgnoreList.size() > 0) - app.db.messageRecipientDao().addAllIgnore(messageRecipientIgnoreList); - if (metadataList.size() > 0) - app.db.metadataDao().addAllIgnore(metadataList); - if (messageMetadataList.size() > 0) - app.db.metadataDao().setSeen(messageMetadataList); - } - private void finish() { - try { - saveData(); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 363, CODE_OTHER, app.getString(R.string.sync_error_saving_data), null, null, e, null)); - } - if (fullSync) { - profile.setLastFullSync(System.currentTimeMillis()); - fullSync = false; - } - profile.setEmpty(false); - callback.onSuccess(activityContext, new ProfileFull(profile, loginStore)); - } - private void finishWithError(AppError error) { - try { - saveData(); - } - catch (Exception e) { - Crashlytics.logException(e); - } - callback.onError(activityContext, error); - } - - /* _ _ - | | (_) - | | ___ __ _ _ _ __ - | | / _ \ / _` | | '_ \ - | |___| (_) | (_| | | | | | - |______\___/ \__, |_|_| |_| - __/ | - |__*/ - private void login(@NonNull LoginCallback loginCallback) { - if (lastLogin.equals(loginSchoolName +":"+ loginUsername) - && System.currentTimeMillis() - lastLoginTime < 5 * 60 * 1000 - && profile != null) { // less than 5 minutes, use the already logged in account - loginCallback.onSuccess(); - return; - } - app.cookieJar.clearForDomain("iuczniowie.progman.pl"); - callback.onActionStarted(R.string.sync_action_logging_in); - Request.builder() - .url(IDZIENNIK_URL +"/login.aspx") - .userAgent(userAgent) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 389, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data1, Response response1) { - if (data1 == null || data1.equals("")) { // for safety - finishWithError(new AppError(TAG, 395, CODE_MAINTENANCE, response1)); - return; - } - //Log.d(TAG, "r:"+data); - Request.Builder builder = Request.builder() - .url(IDZIENNIK_URL +"/login.aspx") - .userAgent(userAgent) - //.withClient(app.httpLazy) - .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/png,*/*;q=0.8") - .addHeader("Cache-Control", "max-age=0") - .addHeader("Origin", "https://iuczniowie.progman.pl") - .addHeader("Referer", "https://iuczniowie.progman.pl/idziennik/login.aspx") - .addHeader("Upgrade-Insecure-Requests", "1") - .contentType(MediaTypeUtils.APPLICATION_FORM) - .addParameter("ctl00$ContentPlaceHolder$nazwaPrzegladarki", userAgent) - .addParameter("ctl00$ContentPlaceHolder$NazwaSzkoly", loginSchoolName) - .addParameter("ctl00$ContentPlaceHolder$UserName", loginUsername) - .addParameter("ctl00$ContentPlaceHolder$Password", loginPassword) - .addParameter("ctl00$ContentPlaceHolder$captcha", "") - .addParameter("ctl00$ContentPlaceHolder$Logowanie", "Zaloguj") - .post(); - - // extract hidden form fields __VIEWSTATE __VIEWSTATEGENERATOR __EVENTVALIDATION - //Pattern pattern = Pattern.compile("<.+?name=\"__VIEWSTATE\".+?value=\"([A-z0-9+/=]+)\".+?name=\"__VIEWSTATEGENERATOR\".+?value=\"([A-z0-9+/=]+)\".+?name=\"__EVENTVALIDATION\".+?value=\"([A-z0-9+/=]+)\".+?>", Pattern.DOTALL); - //Pattern pattern = Pattern.compile("]* name=[\"']([^'\"]*)|)(?=[^>]* value=[\"']([^'\"]*)|)", Pattern.DOTALL); - Pattern pattern = Pattern.compile("", Pattern.DOTALL); - Matcher matcher = pattern.matcher(data1); - while (matcher.find()) { - //Log.d(TAG, "Match: "+matcher.group(1)+"="+matcher.group(2)); - builder.addParameter(matcher.group(1), matcher.group(2)); - } - - builder.callback(new TextCallbackHandler() { - @Override - public void onSuccess(String data2, Response response2) { - callback.onProgress(PROGRESS_LOGIN); - Pattern errorPattern = Pattern.compile("id=\"spanErrorMessage\">(.*?)", Pattern.DOTALL).matcher(data2); - if (!selectMatcher.find()) { - finishWithError(new AppError(TAG, 473, CODE_OTHER, app.getString(R.string.error_register_id_not_found), response2, data2)); - return; - } - Log.d(TAG, "g" + selectMatcher.group(0)); - Matcher idMatcher = Pattern.compile("(.+?)\\s(.+?)\\s*\\((.+?),\\s*(.+?)\\)", Pattern.DOTALL).matcher(selectMatcher.group(0)); - while (idMatcher.find()) { - if (loginRegisterId != Integer.parseInt(idMatcher.group(1))) - continue; - String teamClassName = idMatcher.group(4) + " " + idMatcher.group(5); - teamClassId = crc16(teamClassName.getBytes()); - app.db.teamDao().add(new Team( - profileId, - teamClassId, - teamClassName, - 1, - loginSchoolName+":"+teamClassName, - -1 - )); - } - loginCallback.onSuccess(); - return; - } - try { - Matcher yearMatcher = Pattern.compile("name=\"ctl00\\$dxComboRokSzkolny\".+?selected=\"selected\".*?value=\"([0-9]+)\"", Pattern.DOTALL).matcher(data2); - if (yearMatcher.find()) { - try { - loginSchoolYearId = Integer.parseInt(yearMatcher.group(1)); - } catch (Exception ex) { - finishWithError(new AppError(TAG, 501, CODE_OTHER, response2, ex, data2)); - return; - } - } else { - if (data2.contains("Hasło dostępu do systemu wygasło")) { - finishWithError(new AppError(TAG, 504, CODE_OTHER, app.getString(R.string.error_must_change_password), response2, data2)); - return; - } - finishWithError(new AppError(TAG, 507, CODE_OTHER, app.getString(R.string.error_school_year_not_found), response2, data2)); - return; - } - - List studentIds = new ArrayList<>(); - List registerIds = new ArrayList<>(); - List studentNamesLong = new ArrayList<>(); - List studentNamesShort = new ArrayList<>(); - List studentTeams = new ArrayList<>(); - - Matcher selectMatcher = Pattern.compile("", Pattern.DOTALL).matcher(data2); - if (!selectMatcher.find()) { - finishWithError(new AppError(TAG, 519, CODE_OTHER, app.getString(R.string.error_register_id_not_found), response2, data2)); - return; - } - Log.d(TAG, "g" + selectMatcher.group(0)); - Matcher idMatcher = Pattern.compile("(.+?)\\s(.+?)\\s*\\((.+?),\\s*(.+?)\\)", Pattern.DOTALL).matcher(selectMatcher.group(0)); - while (idMatcher.find()) { - registerIds.add(Integer.parseInt(idMatcher.group(1))); - String studentId = idMatcher.group(2); - String studentFirstName = idMatcher.group(3); - String studentLastName = idMatcher.group(4); - String teamClassName = idMatcher.group(5) + " " + idMatcher.group(6); - studentIds.add(studentId); - studentNamesLong.add(studentFirstName + " " + studentLastName); - studentNamesShort.add(studentFirstName + " " + studentLastName.charAt(0) + "."); - studentTeams.add(teamClassName); - } - Collections.reverse(studentIds); - Collections.reverse(registerIds); - Collections.reverse(studentNamesLong); - Collections.reverse(studentNamesShort); - Collections.reverse(studentTeams); - - List profileList = new ArrayList<>(); - for (int index = 0; index < registerIds.size(); index++) { - Profile newProfile = new Profile(); - newProfile.setStudentNameLong(studentNamesLong.get(index)); - newProfile.setStudentNameShort(studentNamesShort.get(index)); - newProfile.setName(newProfile.getStudentNameLong()); - newProfile.setSubname(loginUsername); - newProfile.setEmpty(true); - newProfile.putStudentData("studentId", studentIds.get(index)); - newProfile.putStudentData("registerId", registerIds.get(index)); - newProfile.putStudentData("schoolYearId", loginSchoolYearId); - profileList.add(newProfile); - } - - callback.onLoginFirst(profileList, loginStore); - } catch (Exception ex) { - finishWithError(new AppError(TAG, 557, CODE_OTHER, response2, ex, data2)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 563, CODE_OTHER, response, throwable, data1)); - } - }).build().enqueue(); - } - }).build().enqueue(); - } - - /* _ _ _ _ _ _ _ - | | | | | | ___ | | | | | | - | |__| | ___| |_ __ ___ _ __ ___ ( _ ) ___ __ _| | | |__ __ _ ___| | _____ - | __ |/ _ \ | '_ \ / _ \ '__/ __| / _ \/\ / __/ _` | | | '_ \ / _` |/ __| |/ / __| - | | | | __/ | |_) | __/ | \__ \ | (_> < | (_| (_| | | | |_) | (_| | (__| <\__ \ - |_| |_|\___|_| .__/ \___|_| |___/ \___/\/ \___\__,_|_|_|_.__/ \__,_|\___|_|\_\___/ - | | - |*/ - private interface ApiRequestCallback { - void onSuccess(JsonObject result, Response response); - } - private void apiRequest(Request.Builder requestBuilder, ApiRequestCallback apiRequestCallback) { - requestBuilder.callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - finishWithError(new AppError(TAG, 578, CODE_MAINTENANCE, response)); - return; - } - try { - apiRequestCallback.onSuccess(data, response); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 583, CODE_OTHER, response, e, data)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 592, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - private interface ApiRequestArrayCallback { - void onSuccess(JsonArray result, Response response); - } - private void apiRequestArray(Request.Builder requestBuilder, ApiRequestArrayCallback apiRequestArrayCallback) { - requestBuilder.callback(new JsonArrayCallbackHandler() { - @Override - public void onSuccess(JsonArray data, Response response) { - if (data == null) { - finishWithError(new AppError(TAG, 603, CODE_MAINTENANCE, response)); - return; - } - try { - apiRequestArrayCallback.onSuccess(data, response); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 610, CODE_OTHER, response, e, data.toString())); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 616, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private Subject searchSubject(String name, long id, String shortName) { - Subject subject; - if (id == -1) - subject = Subject.getByName(subjectList, name); - else - subject = Subject.getById(subjectList, id); - - if (subject == null) { - subject = new Subject(profileId, (id == -1 ? crc16(name.getBytes()) : id), name, shortName); - subjectList.add(subject); - } - return subject; - } - - private Teacher searchTeacher(String firstName, String lastName) { - Teacher teacher = Teacher.getByFullName(teacherList, firstName+" "+lastName); - return validateTeacher(teacher, firstName, lastName); - } - private Teacher searchTeacher(char firstNameChar, String lastName) { - Teacher teacher = Teacher.getByShortName(teacherList, firstNameChar+"."+lastName); - return validateTeacher(teacher, String.valueOf(firstNameChar), lastName); - } - @NonNull - private Teacher validateTeacher(Teacher teacher, String firstName, String lastName) { - if (teacher == null) { - teacher = new Teacher(profileId, -1, firstName, lastName); - teacher.id = crc16(teacher.getShortName().getBytes()); - teacherList.add(teacher); - } - if (firstName.length() > 1) - teacher.name = firstName; - teacher.surname = lastName; - return teacher; - } - - private Teacher searchTeacherByLastFirst(String nameLastFirst) { - String[] nameParts = nameLastFirst.split(" ", Integer.MAX_VALUE); - if (nameParts.length == 1) - return searchTeacher(nameParts[0], ""); - return searchTeacher(nameParts[1], nameParts[0]); - } - private Teacher searchTeacherByFirstLast(String nameFirstLast) { - String[] nameParts = nameFirstLast.split(" ", Integer.MAX_VALUE); - if (nameParts.length == 1) - return searchTeacher(nameParts[0], ""); - return searchTeacher(nameParts[0], nameParts[1]); - } - private Teacher searchTeacherByFDotLast(String nameFDotLast) { - String[] nameParts = nameFDotLast.split("\\.", Integer.MAX_VALUE); - if (nameParts.length == 1) - return searchTeacher(nameParts[0], ""); - return searchTeacher(nameParts[0].charAt(0), nameParts[1]); - } - private Teacher searchTeacherByFDotSpaceLast(String nameFDotSpaceLast) { - String[] nameParts = nameFDotSpaceLast.split("\\. ", Integer.MAX_VALUE); - if (nameParts.length == 1) - return searchTeacher(nameParts[0], ""); - return searchTeacher(nameParts[0].charAt(0), nameParts[1]); - } - - /* _____ _ _____ _ - | __ \ | | | __ \ | | - | | | | __ _| |_ __ _ | |__) |___ __ _ _ _ ___ ___| |_ ___ - | | | |/ _` | __/ _` | | _ // _ \/ _` | | | |/ _ \/ __| __/ __| - | |__| | (_| | || (_| | | | \ \ __/ (_| | |_| | __/\__ \ |_\__ \ - |_____/ \__,_|\__\__,_| |_| \_\___|\__, |\__,_|\___||___/\__|___/ - | | - |*/ - private void getTimetable() { - callback.onActionStarted(R.string.sync_action_syncing_timetable); - Date weekStart = Week.getWeekStart(); - if (Date.getToday().getWeekDay() > 4) { - weekStart.stepForward(0, 0, 7); - } - apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/mod_panelRodzica/plan/WS_Plan.asmx/pobierzPlanZajec") - .userAgent(userAgent) - .addParameter("idPozDziennika", loginRegisterId) - .addParameter("pidRokSzkolny", loginSchoolYearId) - .addParameter("data", weekStart.getStringY_m_d()+"T10:00:00.000Z") - .postJson(), (result, response) -> { - JsonObject data = result.getAsJsonObject("d"); - if (data == null) { - finishWithError(new AppError(TAG, 697, CODE_MAINTENANCE, response, result)); - return; - } - List> lessonHours = new ArrayList<>(); - for (JsonElement jLessonHourEl : data.getAsJsonArray("GodzinyLekcyjne")) { - JsonObject jLessonHour = jLessonHourEl.getAsJsonObject(); - // jLessonHour - lessonHours.add(new Pair<>(Time.fromH_m(jLessonHour.get("Poczatek").getAsString()), Time.fromH_m(jLessonHour.get("Koniec").getAsString()))); - } - - for (JsonElement jLessonEl : data.getAsJsonArray("Przedmioty")) { - JsonObject jLesson = jLessonEl.getAsJsonObject(); - // jLesson - Subject rSubject = searchSubject(jLesson.get("Nazwa").getAsString(), jLesson.get("Id").getAsInt(), jLesson.get("Skrot").getAsString()); - Teacher rTeacher = searchTeacherByFDotLast(jLesson.get("Nauczyciel").getAsString()); - - int weekDay = jLesson.get("DzienTygodnia").getAsInt() - 1; - Pair lessonHour = lessonHours.get(jLesson.get("Godzina").getAsInt()); - if (lessonHour == null || lessonHour.first == null || lessonHour.second == null) - continue; - Lesson lessonObject = new Lesson( - profileId, - weekDay, - lessonHour.first, - lessonHour.second - ); - lessonObject.subjectId = rSubject.id; - lessonObject.teacherId = rTeacher.id; - lessonObject.teamId = teamClassId; - lessonObject.classroomName = jLesson.get("NazwaSali").getAsString(); - - lessonList.add(lessonObject); - - int type = jLesson.get("TypZastepstwa").getAsInt(); - if (type != -1) { - // we have a lesson change to process - LessonChange lessonChangeObject = new LessonChange( - profileId, - weekStart.clone().stepForward(0, 0, weekDay), - lessonObject.startTime, - lessonObject.endTime - ); - - lessonChangeObject.teamId = lessonObject.teamId; - lessonChangeObject.teacherId = lessonObject.teacherId; - lessonChangeObject.subjectId = lessonObject.subjectId; - lessonChangeObject.classroomName = lessonObject.classroomName; - switch (type) { - case 0: - lessonChangeObject.type = TYPE_CANCELLED; - break; - case 1: - case 2: - case 3: - case 4: - case 5: - lessonChangeObject.type = TYPE_CHANGE; - String newTeacher = jLesson.get("NauZastepujacy").getAsString(); - String newSubject = jLesson.get("PrzedmiotZastepujacy").getAsString(); - if (!newTeacher.equals("")) { - lessonChangeObject.teacherId = searchTeacherByFDotLast(newTeacher).id; - } - if (!newSubject.equals("")) { - lessonChangeObject.subjectId = searchSubject(newSubject, -1, "").id; - } - break; - } - - lessonChangeList.add(lessonChangeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_LESSON_CHANGE, lessonChangeObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - } - r("finish", "Timetable"); - }); - } - - private void getGrades() { - callback.onActionStarted(R.string.sync_action_syncing_grades); - apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/mod_panelRodzica/oceny/WS_ocenyUcznia.asmx/pobierzOcenyUcznia") - .userAgent(userAgent) - .addParameter("idPozDziennika", loginRegisterId) - .postJson(), (result, response) -> { - JsonObject data = result.getAsJsonObject("d"); - if (data == null) { - finishWithError(new AppError(TAG, 782, CODE_MAINTENANCE, response, result)); - return; - } - JsonArray jSubjects = data.getAsJsonArray("Przedmioty"); - for (JsonElement jSubjectEl : jSubjects) { - JsonObject jSubject = jSubjectEl.getAsJsonObject(); - // jSubject - Subject rSubject = searchSubject(jSubject.get("Przedmiot").getAsString(), jSubject.get("IdPrzedmiotu").getAsInt(), jSubject.get("Przedmiot").getAsString()); - for (JsonElement jGradeEl : jSubject.getAsJsonArray("Oceny")) { - JsonObject jGrade = jGradeEl.getAsJsonObject(); - // jGrade - Teacher rTeacher = searchTeacherByLastFirst(jGrade.get("Wystawil").getAsString()); - - boolean countToTheAverage = jGrade.get("DoSredniej").getAsBoolean(); - float value = jGrade.get("WartoscDoSred").getAsFloat(); - - String gradeColor = jGrade.get("Kolor").getAsString(); - int colorInt = 0xff2196f3; - if (!gradeColor.isEmpty()) { - colorInt = Color.parseColor("#"+gradeColor); - } - - Grade gradeObject = new Grade( - profileId, - jGrade.get("idK").getAsLong(), - jGrade.get("Kategoria").getAsString(), - colorInt, - "", - jGrade.get("Ocena").getAsString(), - value, - value > 0 && countToTheAverage ? jGrade.get("Waga").getAsFloat() : 0, - jGrade.get("Semestr").getAsInt(), - rTeacher.id, - rSubject.id); - - switch (jGrade.get("Typ").getAsInt()) { - case 0: - JsonElement historyEl = jGrade.get("Historia"); - JsonArray history; - if (historyEl instanceof JsonArray && (history = historyEl.getAsJsonArray()).size() > 0) { - float sum = gradeObject.value * gradeObject.weight; - float count = gradeObject.weight; - for (JsonElement historyItemEl: history) { - JsonObject historyItem = historyItemEl.getAsJsonObject(); - - countToTheAverage = historyItem.get("DoSredniej").getAsBoolean(); - value = historyItem.get("WartoscDoSred").getAsFloat(); - float weight = historyItem.get("Waga").getAsFloat(); - - if (value > 0 && countToTheAverage) { - sum += value * weight; - count += weight; - } - - Grade historyObject = new Grade( - profileId, - gradeObject.id * -1, - historyItem.get("Kategoria").getAsString(), - Color.parseColor("#"+historyItem.get("Kolor").getAsString()), - historyItem.get("Uzasadnienie").getAsString(), - historyItem.get("Ocena").getAsString(), - value, - value > 0 && countToTheAverage ? weight * -1 : 0, - historyItem.get("Semestr").getAsInt(), - rTeacher.id, - rSubject.id); - historyObject.parentId = gradeObject.id; - - gradeList.add(historyObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, historyObject.id, true, true, Date.fromY_m_d(historyItem.get("Data_wystaw").getAsString()).getInMillis())); - } - // update the current grade's value with an average of all historical grades and itself - if (sum > 0 && count > 0) { - gradeObject.value = sum / count; - } - gradeObject.isImprovement = true; // gradeObject is the improved grade. Originals are historyObjects - } - break; - case 1: - gradeObject.type = TYPE_SEMESTER1_FINAL; - gradeObject.name = Integer.toString((int) gradeObject.value); - gradeObject.weight = 0; - break; - case 2: - gradeObject.type = TYPE_YEAR_FINAL; - gradeObject.name = Integer.toString((int) gradeObject.value); - gradeObject.weight = 0; - break; - } - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), Date.fromY_m_d(jGrade.get("Data_wystaw").getAsString()).getInMillis())); - } - } - r("finish", "Grades"); - }); - } - - private void getPropositionGrades() { - callback.onActionStarted(R.string.sync_action_syncing_proposition_grades); - apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/mod_panelRodzica/brak_ocen/WS_BrakOcenUcznia.asmx/pobierzBrakujaceOcenyUcznia") - .userAgent(userAgent) - .addParameter("idPozDziennika", loginRegisterId) - .postJson(), (result, response) -> { - JsonObject data = result.getAsJsonObject("d"); - if (data == null) { - finishWithError(new AppError(TAG, 836, CODE_MAINTENANCE, response, result)); - return; - } - JsonArray jSubjects = data.getAsJsonArray("Przedmioty"); - for (JsonElement jSubjectEl : jSubjects) { - JsonObject jSubject = jSubjectEl.getAsJsonObject(); - // jSubject - Subject rSubject = searchSubject(jSubject.get("Przedmiot").getAsString(), -1, jSubject.get("Przedmiot").getAsString()); - String semester1Proposed = jSubject.get("OcenaSem1").getAsString(); - String semester2Proposed = jSubject.get("OcenaSem2").getAsString(); - int semester1Value = getWordGradeValue(semester1Proposed); - int semester2Value = getWordGradeValue(semester2Proposed); - long semester1Id = rSubject.id * -100 - 1; - long semester2Id = rSubject.id * -100 - 2; - - if (!semester1Proposed.equals("")) { - Grade gradeObject = new Grade( - profileId, - semester1Id, - "", - -1, - "", - Integer.toString(semester1Value), - semester1Value, - 0, - 1, - -1, - rSubject.id); - - gradeObject.type = TYPE_SEMESTER1_PROPOSED; - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - - if (!semester2Proposed.equals("")) { - Grade gradeObject = new Grade( - profileId, - semester2Id, - "", - -1, - "", - Integer.toString(semester2Value), - semester2Value, - 0, - 2, - -1, - rSubject.id); - - gradeObject.type = TYPE_YEAR_PROPOSED; - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - } - r("finish", "PropositionGrades"); - }); - } - - private int examsYear = Date.getToday().year; - private int examsMonth = Date.getToday().month; - private int examsMonthsChecked = 0; - private boolean examsNextMonthChecked = false; // TO DO temporary // no more // idk - private void getExams() { - callback.onActionStarted(R.string.sync_action_syncing_exams); - JsonObject postData = new JsonObject(); - postData.addProperty("idP", loginRegisterId); - postData.addProperty("rok", examsYear); - postData.addProperty("miesiac", examsMonth); - JsonObject param = new JsonObject(); - param.addProperty("strona", 1); - param.addProperty("iloscNaStrone", "99"); - param.addProperty("iloscRekordow", -1); - param.addProperty("kolumnaSort", "ss.Nazwa,sp.Data_sprawdzianu"); - param.addProperty("kierunekSort", 0); - param.addProperty("maxIloscZaznaczonych", 0); - param.addProperty("panelFiltrow", 0); - postData.add("param", param); - - apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/mod_panelRodzica/sprawdziany/mod_sprawdzianyPanel.asmx/pobierzListe") - .userAgent(userAgent) - .setJsonBody(postData), (result, response) -> { - JsonObject data = result.getAsJsonObject("d"); - if (data == null) { - finishWithError(new AppError(TAG, 921, CODE_MAINTENANCE, response, result)); - return; - } - for (JsonElement jExamEl : data.getAsJsonArray("ListK")) { - JsonObject jExam = jExamEl.getAsJsonObject(); - // jExam - long eventId = jExam.get("_recordId").getAsLong(); - Subject rSubject = searchSubject(jExam.get("przedmiot").getAsString(), -1, ""); - Teacher rTeacher = searchTeacherByLastFirst(jExam.get("wpisal").getAsString()); - Date examDate = Date.fromY_m_d(jExam.get("data").getAsString()); - Lesson lessonObject = Lesson.getByWeekDayAndSubject(lessonList, examDate.getWeekDay(), rSubject.id); - Time examTime = lessonObject == null ? null : lessonObject.startTime; - - int eventType = (jExam.get("rodzaj").getAsString().equals("sprawdzian/praca klasowa") ? Event.TYPE_EXAM : Event.TYPE_SHORT_QUIZ); - Event eventObject = new Event( - profileId, - eventId, - examDate, - examTime, - jExam.get("zakres").getAsString(), - -1, - eventType, - false, - rTeacher.id, - rSubject.id, - teamClassId - ); - - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_EVENT, eventObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - - if (profile.getEmpty() && examsMonthsChecked < 3 /* how many months backwards to check? */) { - examsMonthsChecked++; - examsMonth--; - if (examsMonth < 1) { - examsMonth = 12; - examsYear--; - } - r("get", "Exams"); - } else if (!examsNextMonthChecked /* get also one month forward */) { - Date showDate = Date.getToday().stepForward(0, 1, 0); - examsYear = showDate.year; - examsMonth = showDate.month; - examsNextMonthChecked = true; - r("get", "Exams"); - } else { - r("finish", "Exams"); - } - }); - } - - private void getNotices() { - callback.onActionStarted(R.string.sync_action_syncing_notices); - apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/mod_panelRodzica/uwagi/WS_uwagiUcznia.asmx/pobierzUwagiUcznia") - .userAgent(userAgent) - .addParameter("idPozDziennika", loginRegisterId) - .postJson(), (result, response) -> { - JsonObject data = result.getAsJsonObject("d"); - if (data == null) { - finishWithError(new AppError(TAG, 982, CODE_MAINTENANCE, response, result)); - return; - } - for (JsonElement jNoticeEl : data.getAsJsonArray("SUwaga")) { - JsonObject jNotice = jNoticeEl.getAsJsonObject(); - // jExam - long noticeId = crc16(jNotice.get("id").getAsString().getBytes()); - - Teacher rTeacher = searchTeacherByLastFirst(jNotice.get("Nauczyciel").getAsString()); - Date addedDate = Date.fromY_m_d(jNotice.get("Data").getAsString()); - - int nType = TYPE_NEUTRAL; - String jType = jNotice.get("Typ").getAsString(); - if (jType.equals("n")) { - nType = TYPE_NEGATIVE; - } else if (jType.equals("p")) { - nType = TYPE_POSITIVE; - } - - Notice noticeObject = new Notice( - profileId, - noticeId, - jNotice.get("Tresc").getAsString(), - jNotice.get("Semestr").getAsInt(), - nType, - rTeacher.id); - noticeList.add(noticeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_NOTICE, noticeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate.getInMillis())); - } - r("finish", "Notices"); - }); - } - - private void getAnnouncements() { - callback.onActionStarted(R.string.sync_action_syncing_announcements); - if (loginStudentId == null) { - r("finish", "Announcements"); - return; - } - JsonObject postData = new JsonObject(); - postData.addProperty("uczenId", loginStudentId); - JsonObject param = new JsonObject(); - param.add("parametryFiltrow", new JsonArray()); - postData.add("param", param); - - apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/mod_panelRodzica/tabOgl/WS_tablicaOgloszen.asmx/GetOgloszenia") - .userAgent(userAgent) - .setJsonBody(postData), (result, response) -> { - JsonObject data = result.getAsJsonObject("d"); - if (data == null) { - finishWithError(new AppError(TAG, 1033, CODE_MAINTENANCE, response, result)); - return; - } - for (JsonElement jAnnouncementEl : data.getAsJsonArray("ListK")) { - JsonObject jAnnouncement = jAnnouncementEl.getAsJsonObject(); - // jAnnouncement - long announcementId = jAnnouncement.get("Id").getAsLong(); - - Teacher rTeacher = searchTeacherByFirstLast(jAnnouncement.get("Autor").getAsString()); - long addedDate = Long.parseLong(jAnnouncement.get("DataDodania").getAsString().replaceAll("[^\\d]", "")); - Date startDate = Date.fromMillis(Long.parseLong(jAnnouncement.get("DataWydarzenia").getAsString().replaceAll("[^\\d]", ""))); - - Announcement announcementObject = new Announcement( - profileId, - announcementId, - jAnnouncement.get("Temat").getAsString(), - jAnnouncement.get("Tresc").getAsString(), - startDate, - null, - rTeacher.id - ); - announcementList.add(announcementObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_ANNOUNCEMENT, announcementObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - r("finish", "Announcements"); - }); - } - - private int attendanceYear; - private int attendanceMonth; - private boolean attendancePrevMonthChecked = false; - private void getAttendance() { - callback.onActionStarted(R.string.sync_action_syncing_attendance); - apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/mod_panelRodzica/obecnosci/WS_obecnosciUcznia.asmx/pobierzObecnosciUcznia") - .userAgent(userAgent) - .addParameter("idPozDziennika", loginRegisterId) - .addParameter("mc", attendanceMonth) - .addParameter("rok", attendanceYear) - .addParameter("dataTygodnia", "") - .postJson(), (result, response) -> { - JsonObject data = result.getAsJsonObject("d"); - if (data == null) { - finishWithError(new AppError(TAG, 1076, CODE_MAINTENANCE, response, result)); - return; - } - for (JsonElement jAttendanceEl : data.getAsJsonArray("Obecnosci")) { - JsonObject jAttendance = jAttendanceEl.getAsJsonObject(); - // jAttendance - int attendanceTypeIdziennik = jAttendance.get("TypObecnosci").getAsInt(); - if (attendanceTypeIdziennik == 5 || attendanceTypeIdziennik == 7) - continue; - Date attendanceDate = Date.fromY_m_d(jAttendance.get("Data").getAsString()); - Time attendanceTime = Time.fromH_m(jAttendance.get("OdDoGodziny").getAsString()); - if (attendanceDate.combineWith(attendanceTime) > System.currentTimeMillis()) - continue; - - long attendanceId = crc16(jAttendance.get("IdLesson").getAsString().getBytes()); - Subject rSubject = searchSubject(jAttendance.get("Przedmiot").getAsString(), jAttendance.get("IdPrzedmiot").getAsLong(), ""); - Teacher rTeacher = searchTeacherByFDotSpaceLast(jAttendance.get("PrzedmiotNauczyciel").getAsString()); - - String attendanceName = "obecność"; - int attendanceType = Attendance.TYPE_CUSTOM; - - switch (attendanceTypeIdziennik) { - case 1: /* nieobecność usprawiedliwiona */ - attendanceName = "nieobecność usprawiedliwiona"; - attendanceType = TYPE_ABSENT_EXCUSED; - break; - case 2: /* spóźnienie */ - attendanceName = "spóźnienie"; - attendanceType = TYPE_BELATED; - break; - case 3: /* nieobecność nieusprawiedliwiona */ - attendanceName = "nieobecność nieusprawiedliwiona"; - attendanceType = TYPE_ABSENT; - break; - case 4: /* zwolnienie */ - case 9: /* zwolniony / obecny */ - attendanceType = TYPE_RELEASED; - if (attendanceTypeIdziennik == 4) - attendanceName = "zwolnienie"; - if (attendanceTypeIdziennik == 9) - attendanceName = "zwolnienie / obecność"; - break; - case 0: /* obecny */ - case 8: /* Wycieczka */ - attendanceType = TYPE_PRESENT; - if (attendanceTypeIdziennik == 8) - attendanceName = "wycieczka"; - break; - } - - int semester = profile.dateToSemester(attendanceDate); - - Attendance attendanceObject = new Attendance( - profileId, - attendanceId, - rTeacher.id, - rSubject.id, - semester, - attendanceName, - attendanceDate, - attendanceTime, - attendanceType - ); - - attendanceList.add(attendanceObject); - if (attendanceObject.type != TYPE_PRESENT) { - metadataList.add(new Metadata(profileId, Metadata.TYPE_ATTENDANCE, attendanceObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - } - - int attendanceDateValue = attendanceYear *10000 + attendanceMonth *100; - if (profile.getEmpty() && attendanceDateValue > profile.getSemesterStart(1).getValue()) { - attendancePrevMonthChecked = true; // do not need to check prev month later - attendanceMonth--; - if (attendanceMonth < 1) { - attendanceMonth = 12; - attendanceYear--; - } - r("get", "Attendance"); - } else if (!attendancePrevMonthChecked /* get also the previous month */) { - attendanceMonth--; - if (attendanceMonth < 1) { - attendanceMonth = 12; - attendanceYear--; - } - attendancePrevMonthChecked = true; - r("get", "Attendance"); - } else { - r("finish", "Attendance"); - } - }); - } - - private void getLuckyNumberAndSemesterDates() { - if (profile.getLuckyNumberDate() != null && profile.getLuckyNumber() != -1 && profile.getLuckyNumberDate().getValue() == Date.getToday().getValue()) { - r("finish", "LuckyNumberAndSemesterDates"); - return; - } - if (loginBearerToken == null || loginStudentId == null) { - r("finish", "LuckyNumberAndSemesterDates"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_lucky_number); - apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/api/Uczniowie/"+loginStudentId+"/AktualnyDziennik") - .header("Authorization", "Bearer "+loginBearerToken) - .userAgent(userAgent), (data, response) -> { - JsonObject settings = data.getAsJsonObject("ustawienia"); - if (settings == null) { - finishWithError(new AppError(TAG, 1188, CODE_MAINTENANCE, response, data)); - return; - } - // data, settings - profile.setLuckyNumber(-1); - profile.setLuckyNumberDate(today); - JsonElement luckyNumberEl = data.get("szczesliwyNumerek"); - if (luckyNumberEl != null && !(luckyNumberEl instanceof JsonNull)) { - profile.setLuckyNumber(luckyNumberEl.getAsInt()); - Time publishTime = Time.fromH_m(settings.get("godzinaPublikacjiSzczesliwegoLosu").getAsString()); - if (Time.getNow().getValue() > publishTime.getValue()) { - profile.getLuckyNumberDate().stepForward(0, 0, 1); // the lucky number is already for tomorrow - } - app.db.luckyNumberDao().add(new LuckyNumber(profileId, profile.getLuckyNumberDate(), profile.getLuckyNumber())); - } - - profile.setDateSemester1Start(Date.fromY_m_d(settings.get("poczatekSemestru1").getAsString())); - profile.setDateSemester2Start(Date.fromY_m_d(settings.get("koniecSemestru1").getAsString()).stepForward(0, 0, 1)); - profile.setDateYearEnd(Date.fromY_m_d(settings.get("koniecSemestru2").getAsString())); - - r("finish", "LuckyNumberAndSemesterDates"); - }); - } - - private void getMessagesInbox() { - if (loginBearerToken == null) { - r("finish", "MessagesInbox"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_messages); - apiRequestArray(Request.builder() - .url(IDZIENNIK_URL +"/api/Wiadomosci/Odebrane") - .header("Authorization", "Bearer "+loginBearerToken) - .userAgent(userAgent), (data, response) -> { - for (JsonElement jMessageEl: data) { - JsonObject jMessage = jMessageEl.getAsJsonObject(); - - String subject = jMessage.get("tytul").getAsString(); - if (subject.contains("(") && subject.startsWith("iDziennik - ")) - continue; - if (subject.startsWith("Uwaga dla ucznia (klasa:")) - continue; - - String messageIdStr = jMessage.get("id").getAsString(); - long messageId = crc32((messageIdStr+"0").getBytes()); - - String body = "[META:"+messageIdStr+";-1]"; - body += jMessage.get("tresc").getAsString().replaceAll("\n", "
"); - - long readDate = jMessage.get("odczytana").getAsBoolean() ? Date.fromIso(jMessage.get("wersjaRekordu").getAsString()) : 0; - long sentDate = Date.fromIso(jMessage.get("dataWyslania").getAsString()); - - JsonObject sender = jMessage.getAsJsonObject("nadawca"); - Teacher rTeacher = searchTeacher(sender.get("imie").getAsString(), sender.get("nazwisko").getAsString()); - rTeacher.loginId = sender.get("id").getAsString()+":"+sender.get("usr").getAsString(); - - Message message = new Message( - profileId, - messageId, - subject, - body, - jMessage.get("rekordUsuniety").getAsBoolean() ? TYPE_DELETED : TYPE_RECEIVED, - rTeacher.id, - -1 - ); - - MessageRecipient messageRecipient = new MessageRecipient( - profileId, - -1 /* me */, - -1, - readDate, - /*messageId*/ messageId - ); - - messageList.add(message); - messageRecipientList.add(messageRecipient); - messageMetadataList.add(new Metadata(profileId, Metadata.TYPE_MESSAGE, message.id, readDate > 0, readDate > 0 || profile.getEmpty(), sentDate)); - } - - r("finish", "MessagesInbox"); - }); - } - - private void getMessagesOutbox() { - if (loginBearerToken == null) { - r("finish", "MessagesOutbox"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_messages); - apiRequestArray(Request.builder() - .url(IDZIENNIK_URL +"/api/Wiadomosci/Wyslane") - .header("Authorization", "Bearer "+loginBearerToken) - .userAgent(userAgent), (data, response) -> { - for (JsonElement jMessageEl: data) { - JsonObject jMessage = jMessageEl.getAsJsonObject(); - - String messageIdStr = jMessage.get("id").getAsString(); - long messageId = crc32((messageIdStr+"1").getBytes()); - - String subject = jMessage.get("tytul").getAsString(); - - String body = "[META:"+messageIdStr+";-1]"; - body += jMessage.get("tresc").getAsString().replaceAll("\n", "
"); - - long sentDate = Date.fromIso(jMessage.get("dataWyslania").getAsString()); - - Message message = new Message( - profileId, - messageId, - subject, - body, - TYPE_SENT, - -1, - -1 - ); - - for (JsonElement recipientEl: jMessage.getAsJsonArray("odbiorcy")) { - JsonObject recipient = recipientEl.getAsJsonObject(); - String firstName = recipient.get("imie").getAsString(); - String lastName = recipient.get("nazwisko").getAsString(); - if (firstName.isEmpty() || lastName.isEmpty()) { - firstName = "usunięty"; - lastName = "użytkownik"; - } - Teacher rTeacher = searchTeacher(firstName, lastName); - rTeacher.loginId = recipient.get("id").getAsString()+":"+recipient.get("usr").getAsString(); - - MessageRecipient messageRecipient = new MessageRecipient( - profileId, - rTeacher.id, - -1, - -1, - /*messageId*/ messageId - ); - messageRecipientIgnoreList.add(messageRecipient); - } - - messageList.add(message); - metadataList.add(new Metadata(profileId, Metadata.TYPE_MESSAGE, message.id, true, true, sentDate)); - } - - r("finish", "MessagesOutbox"); - }); - } - - @Override - public Map getConfigurableEndpoints(Profile profile) { - return null; - } - - @Override - public boolean isEndpointEnabled(Profile profile, boolean defaultActive, String name) { - return defaultActive; - } - - @Override - public void syncMessages(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile) - { - } - - /* __ __ - | \/ | - | \ / | ___ ___ ___ __ _ __ _ ___ ___ - | |\/| |/ _ \/ __/ __|/ _` |/ _` |/ _ \/ __| - | | | | __/\__ \__ \ (_| | (_| | __/\__ \ - |_| |_|\___||___/___/\__,_|\__, |\___||___/ - __/ | - |__*/ - @Override - public void getMessage(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull MessageFull message, @NonNull MessageGetCallback messageCallback) { - if (message.body == null) - return; - String messageIdStr = null; - long messageIdBefore = -1; - Matcher matcher = Pattern.compile("\\[META:([A-z0-9]+);([0-9-]+)]").matcher(message.body); - if (matcher.find()) { - messageIdStr = matcher.group(1); - messageIdBefore = Long.parseLong(matcher.group(2)); - } - if (messageIdBefore != -1) { - boolean readByAll = true; - // load this message's recipient(s) data - message.recipients = app.db.messageRecipientDao().getAllByMessageId(profile.getId(), message.id); - for (MessageRecipientFull recipient: message.recipients) { - if (recipient.id == -1) - recipient.fullName = profile.getStudentNameLong(); - if (message.type == TYPE_SENT && recipient.readDate < 1) - readByAll = false; - } - if (!message.seen) { - app.db.metadataDao().setSeen(profile.getId(), message, true); - } - if (readByAll) { - // if a sent msg is not read by everyone, download it again to check the read status - new Handler(activityContext.getMainLooper()).post(() -> messageCallback.onSuccess(message)); - return; - } - } - - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - String finalMessageIdStr = messageIdStr; - login(() -> apiRequest(Request.builder() - .url(IDZIENNIK_URL +"/mod_komunikator/WS_wiadomosci.asmx/PobierzWiadomosc") - .userAgent(userAgent) - .addParameter("idWiadomosci", finalMessageIdStr) - .addParameter("typWiadomosci", message.type == TYPE_SENT ? 1 : 0) - .postJson(), (result, response) -> { - JsonObject data = result.getAsJsonObject("d"); - if (data == null) { - finishWithError(new AppError(TAG, 1418, CODE_MAINTENANCE, response, result)); - return; - } - JsonObject jMessage = data.getAsJsonObject("Wiadomosc"); - if (jMessage == null) { - finishWithError(new AppError(TAG, 1423, CODE_MAINTENANCE, response, result)); - return; - } - - List messageRecipientList = new ArrayList<>(); - - long messageId = jMessage.get("_recordId").getAsLong(); - - message.body = message.body.replaceAll("\\[META:[A-z0-9]+;[0-9-]+]", "[META:"+ finalMessageIdStr +";"+ messageId +"]"); - - message.clearAttachments(); - for (JsonElement jAttachmentEl: jMessage.getAsJsonArray("ListaZal")) { - JsonObject jAttachment = jAttachmentEl.getAsJsonObject(); - message.addAttachment(jAttachment.get("Id").getAsLong(), jAttachment.get("Nazwa").getAsString(), -1); - } - - if (message.type == TYPE_RECEIVED) { - MessageRecipientFull recipient = new MessageRecipientFull(profileId, -1, message.id); - - String readDateStr = jMessage.get("DataOdczytania").getAsString(); - recipient.readDate = readDateStr.isEmpty() ? System.currentTimeMillis() : Date.fromIso(readDateStr); - - recipient.fullName = profile.getStudentNameLong(); - messageRecipientList.add(recipient); - } - else if (message.type == TYPE_SENT) { - teacherList = app.db.teacherDao().getAllNow(profileId); - for (JsonElement jReceiverEl: jMessage.getAsJsonArray("ListaOdbiorcow")) { - JsonObject jReceiver = jReceiverEl.getAsJsonObject(); - String receiverLastFirstName = jReceiver.get("NazwaOdbiorcy").getAsString(); - - Teacher teacher = searchTeacherByLastFirst(receiverLastFirstName); - - MessageRecipientFull recipient = new MessageRecipientFull(profileId, teacher.id, message.id); - - recipient.readDate = jReceiver.get("Status").getAsInt(); - - recipient.fullName = teacher.getFullName(); - messageRecipientList.add(recipient); - } - } - - if (!message.seen) { - app.db.metadataDao().setSeen(profileId, message, true); - } - app.db.messageDao().add(message); - app.db.messageRecipientDao().addAll((List)(List) messageRecipientList); // not addAllIgnore - - message.recipients = messageRecipientList; - - new Handler(activityContext.getMainLooper()).post(() -> messageCallback.onSuccess(message)); - })); - } - - @Override - public void getAttachment(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull MessageFull message, long attachmentId, @NonNull AttachmentGetCallback attachmentCallback) { - if (message.body == null) - return; - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - login(() -> { - String fileName = message.attachmentNames.get(message.attachmentIds.indexOf(attachmentId)); - - long messageId = -1; - Matcher matcher = Pattern.compile("\\[META:([A-z0-9]+);([0-9-]+)]").matcher(message.body); - if (matcher.find()) { - messageId = Long.parseLong(matcher.group(2)); - } - - Request.Builder builder = Request.builder() - .url("https://iuczniowie.progman.pl/idziennik/mod_komunikator/Download.ashx") - .post() - .contentType(MediaTypeUtils.APPLICATION_FORM) - .addParameter("id", messageId) - .addParameter("fileName", fileName); - - new Handler(activityContext.getMainLooper()).post(() -> attachmentCallback.onSuccess(builder)); - }); - } - - @Override - public void getRecipientList(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull RecipientListGetCallback recipientListGetCallback) { - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - if (System.currentTimeMillis() - profile.getLastReceiversSync() < 24 * 60 * 60 * 1000) { - AsyncTask.execute(() -> { - List teacherList = app.db.teacherDao().getAllNow(profileId); - new Handler(activityContext.getMainLooper()).post(() -> recipientListGetCallback.onSuccess(teacherList)); - }); - return; - } - - login(() -> { - List cookieList = app.cookieJar.loadForRequest(HttpUrl.get(IDZIENNIK_URL)); - for (Cookie cookie: cookieList) { - if (cookie.name().equalsIgnoreCase("Bearer")) { - loginBearerToken = cookie.value(); - } - } - loginStudentId = profile.getStudentData("studentId", null);// TODO: 2019-06-12 temporary duplicated token & ID extraction - - apiRequestArray(Request.builder() - .url(IDZIENNIK_URL + "/api/Wiadomosci/Odbiorcy?idUcznia="+loginStudentId) - .header("Authorization", "Bearer " + loginBearerToken) - .userAgent(userAgent), (result, response) -> { - teacherList = app.db.teacherDao().getAllNow(profileId); - for (JsonElement recipientEl: result) { - JsonObject recipient = recipientEl.getAsJsonObject(); - String name = recipient.get("nazwaKontaktu").getAsString(); - String loginId = recipient.get("idUzytkownika").getAsString(); - JsonArray typesArray = recipient.getAsJsonArray("typOsoby"); - List types = new ArrayList<>(); - for (JsonElement typeEl: typesArray) { - types.add(typeEl.getAsInt()); - } - String delimiter; - if (types.size() == 1 && types.get(0) >= 6) /* parent or student */ - delimiter = " ("; - else - delimiter = ": "; - String nameFirstLast = name.substring(0, name.indexOf(delimiter)); - Teacher teacher = searchTeacherByFirstLast(nameFirstLast); - teacher.loginId = loginId; - teacher.type = 0; - for (int type: types) { - switch (type) { - case 0: - teacher.setType(Teacher.TYPE_SCHOOL_ADMIN); - break; - case 1: - teacher.setType(Teacher.TYPE_SECRETARIAT); - break; - case 2: - teacher.setType(Teacher.TYPE_TEACHER); - teacher.typeDescription = name.substring(name.indexOf(": ")+2); - break; - case 3: - teacher.setType(Teacher.TYPE_PRINCIPAL); - break; - case 4: - teacher.setType(Teacher.TYPE_EDUCATOR); - break; - case 5: - teacher.setType(Teacher.TYPE_PEDAGOGUE); - break; - case 6: - teacher.setType(Teacher.TYPE_PARENT); - teacher.typeDescription = name.substring(name.indexOf(" (")+2, name.lastIndexOf(" (")); - break; - case 7: - teacher.setType(Teacher.TYPE_STUDENT); - break; - } - } - } - app.db.teacherDao().addAll(teacherList); - - profile.setLastReceiversSync(System.currentTimeMillis()); - app.db.profileDao().add(profile); - - new Handler(activityContext.getMainLooper()).post(() -> recipientListGetCallback.onSuccess(new ArrayList<>(teacherList))); - }); - }); - } - - @Override - public MessagesComposeInfo getComposeInfo(@NonNull ProfileFull profile) { - return new MessagesComposeInfo(0, 0, 180, 1983); - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java deleted file mode 100644 index b9c6c4aa..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Librus.java +++ /dev/null @@ -1,3868 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api; - -import android.content.Context; -import android.graphics.Color; -import android.os.AsyncTask; -import android.os.Handler; -import android.util.Base64; -import android.util.Pair; -import android.util.SparseArray; -import android.util.SparseIntArray; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.crashlytics.android.Crashlytics; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; -import com.google.gson.reflect.TypeToken; - -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.parser.Parser; -import org.jsoup.select.Elements; - -import java.text.DateFormat; -import java.text.DecimalFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import im.wangchao.mhttp.Request; -import im.wangchao.mhttp.Response; -import im.wangchao.mhttp.body.MediaTypeUtils; -import im.wangchao.mhttp.callback.JsonCallbackHandler; -import im.wangchao.mhttp.callback.TextCallbackHandler; -import pl.szczodrzynski.edziennik.App; -import pl.szczodrzynski.edziennik.BuildConfig; -import pl.szczodrzynski.edziennik.R; -import pl.szczodrzynski.edziennik.data.api.interfaces.AttachmentGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface; -import pl.szczodrzynski.edziennik.data.api.interfaces.LoginCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.MessageGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.RecipientListGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.SyncCallback; -import pl.szczodrzynski.edziennik.data.db.modules.announcements.Announcement; -import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance; -import pl.szczodrzynski.edziennik.data.db.modules.events.Event; -import pl.szczodrzynski.edziennik.data.db.modules.events.EventType; -import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade; -import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange; -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore; -import pl.szczodrzynski.edziennik.data.db.modules.luckynumber.LuckyNumber; -import pl.szczodrzynski.edziennik.data.db.modules.messages.Message; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipientFull; -import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata; -import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileFull; -import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject; -import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher; -import pl.szczodrzynski.edziennik.data.db.modules.teachers.TeacherAbsence; -import pl.szczodrzynski.edziennik.data.db.modules.teams.Team; -import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesComposeInfo; -import pl.szczodrzynski.edziennik.utils.Utils; -import pl.szczodrzynski.edziennik.utils.models.Date; -import pl.szczodrzynski.edziennik.utils.models.Endpoint; -import pl.szczodrzynski.edziennik.utils.models.Time; -import pl.szczodrzynski.edziennik.utils.models.Week; - -import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; -import static java.net.HttpURLConnection.HTTP_FORBIDDEN; -import static java.net.HttpURLConnection.HTTP_GONE; -import static java.net.HttpURLConnection.HTTP_NOT_FOUND; -import static java.net.HttpURLConnection.HTTP_OK; -import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_INVALID_LOGIN; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_LIBRUS_DISCONNECTED; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_LIBRUS_NOT_ACTIVATED; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_MAINTENANCE; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_OTHER; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_PROFILE_NOT_FOUND; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_SYNERGIA_NOT_ACTIVATED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_ABSENT; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_ABSENT_EXCUSED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_BELATED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_PRESENT; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_RELEASED; -import static pl.szczodrzynski.edziennik.data.db.modules.events.Event.TYPE_PT_MEETING; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_NORMAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER2_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER2_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_YEAR_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_YEAR_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange.TYPE_CANCELLED; -import static pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange.TYPE_CHANGE; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_RECEIVED; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT; -import static pl.szczodrzynski.edziennik.data.db.modules.notices.Notice.TYPE_NEGATIVE; -import static pl.szczodrzynski.edziennik.data.db.modules.notices.Notice.TYPE_NEUTRAL; -import static pl.szczodrzynski.edziennik.data.db.modules.notices.Notice.TYPE_POSITIVE; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_EDUCATOR; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_LIBRARIAN; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_OTHER; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_PARENTS_COUNCIL; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_PEDAGOGUE; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_SCHOOL_ADMIN; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_SCHOOL_PARENTS_COUNCIL; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_SECRETARIAT; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_SUPER_ADMIN; -import static pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher.TYPE_TEACHER; -import static pl.szczodrzynski.edziennik.utils.Utils.bs; -import static pl.szczodrzynski.edziennik.utils.Utils.c; -import static pl.szczodrzynski.edziennik.utils.Utils.contains; -import static pl.szczodrzynski.edziennik.utils.Utils.crc16; -import static pl.szczodrzynski.edziennik.utils.Utils.d; -import static pl.szczodrzynski.edziennik.utils.Utils.getGradeValue; -import static pl.szczodrzynski.edziennik.utils.Utils.strToInt; - -public class Librus implements EdziennikInterface { - public Librus(App app) { - this.app = app; - } - - private static final String TAG = "api.Librus"; - private static final String CLIENT_ID = "wmSyUMo8llDAs4y9tJVYY92oyZ6h4lAt7KCuy0Gv"; - private static final String REDIRECT_URL = "http://localhost/bar"; - private static final String AUTHORIZE_URL = "https://portal.librus.pl/oauth2/authorize?client_id="+CLIENT_ID+"&redirect_uri="+REDIRECT_URL+"&response_type=code"; - private static final String LOGIN_URL = "https://portal.librus.pl/rodzina/login/action"; - private static final String TOKEN_URL = "https://portal.librus.pl/oauth2/access_token"; - private static final String ACCOUNTS_URL = "https://portal.librus.pl/api/v2/SynergiaAccounts"; - private static final String ACCOUNT_URL = "https://portal.librus.pl/api/v2/SynergiaAccounts/fresh/"; // + login - private static final String API_URL = "https://api.librus.pl/2.0/"; - private static final String SYNERGIA_URL = "https://wiadomosci.librus.pl/module/"; - private static final String SYNERGIA_SANDBOX_URL = "https://sandbox.librus.pl/index.php?action="; - private static final String userAgent = "Dalvik/2.1.0 Android LibrusMobileApp"; - private static final String synergiaUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Gecko/20100101 Firefox/62.0"; - - private App app; - private Context activityContext = null; - private SyncCallback callback = null; - private int profileId = -1; - private Profile profile = null; - private LoginStore loginStore = null; - private boolean fullSync = true; - private Date today; - private List targetEndpoints = new ArrayList<>(); - - // PROGRESS - private static final int PROGRESS_LOGIN = 10; - private int PROGRESS_COUNT = 1; - private int PROGRESS_STEP = (90/PROGRESS_COUNT); - - private int onlyFeature = FEATURE_ALL; - - private List teamList; - private List teacherList; - private List teacherAbsenceList; - private List subjectList; - private List lessonList; - private List lessonChangeList; - private List gradeCategoryList; - private List gradeList; - private List eventList; - private List eventTypeList; - private List noticeList; - private List attendanceList; - private List announcementList; - private List messageList; - private List messageRecipientList; - private List messageRecipientIgnoreList; - private List metadataList; - private List messageMetadataList; - - private static boolean fakeLogin = false; - private String librusEmail = null; - private String librusPassword = null; - private String synergiaLogin = null; - private String synergiaPassword = null; - private String synergiaLastLogin = null; - private long synergiaLastLoginTime = -1; - private boolean premium = false; - private boolean enableStandardGrades = true; - private boolean enablePointGrades = false; - private boolean enableDescriptiveGrades = false; - private boolean enableTextGrades = false; - private boolean enableBehaviourGrades = true; - private long unitId = -1; - private int startPointsSemester1 = 0; - private int startPointsSemester2 = 0; - - private boolean prepare(@NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable Profile profile, @NonNull LoginStore loginStore) { - this.activityContext = activityContext; - this.callback = callback; - this.profileId = profileId; - // here we must have a login store: either with a correct ID or -1 - // there may be no profile and that's when onLoginFirst happens - this.profile = profile; - this.loginStore = loginStore; - this.fullSync = profile == null || profile.getEmpty() || profile.shouldFullSync(activityContext); - this.today = Date.getToday(); - - this.librusEmail = loginStore.getLoginData("email", ""); - this.librusPassword = loginStore.getLoginData("password", ""); - if (profile == null) { - this.synergiaLogin = null; - this.synergiaPassword = null; - } - else { - this.synergiaLogin = profile.getStudentData("accountLogin", null); - this.synergiaPassword = profile.getStudentData("accountPassword", null); - } - if (librusEmail.equals("") || librusPassword.equals("")) { - finishWithError(new AppError(TAG, 214, AppError.CODE_INVALID_LOGIN, "Login field is empty")); - return false; - } - this.premium = profile != null && profile.getStudentData("isPremium", false); - this.failed = 0; - fakeLogin = BuildConfig.DEBUG && librusEmail.toLowerCase().startsWith("fake"); - this.synergiaLastLogin = null; - this.synergiaLastLoginTime = -1; - - this.refreshTokenFailed = false; - - teamList = profileId == -1 ? new ArrayList<>() : app.db.teamDao().getAllNow(profileId); - teacherList = profileId == -1 ? new ArrayList<>() : app.db.teacherDao().getAllNow(profileId); - teacherAbsenceList = new ArrayList<>(); - subjectList = new ArrayList<>(); - lessonList = new ArrayList<>(); - lessonChangeList = new ArrayList<>(); - gradeCategoryList = new ArrayList<>(); - gradeList = new ArrayList<>(); - eventList = new ArrayList<>(); - eventTypeList = new ArrayList<>(); - noticeList = new ArrayList<>(); - attendanceList = new ArrayList<>(); - announcementList = new ArrayList<>(); - messageList = new ArrayList<>(); - messageRecipientList = new ArrayList<>(); - messageRecipientIgnoreList = new ArrayList<>(); - metadataList = new ArrayList<>(); - messageMetadataList = new ArrayList<>(); - - return true; - } - - @Override - public void sync(@NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable Profile profile, @NonNull LoginStore loginStore) { - if (!prepare(activityContext, callback, profileId, profile, loginStore)) - return; - - login(() -> { - targetEndpoints = new ArrayList<>(); - targetEndpoints.add("Me"); - targetEndpoints.add("Schools"); - targetEndpoints.add("Classes"); - targetEndpoints.add("VirtualClasses"); - targetEndpoints.add("Units"); - targetEndpoints.add("Users"); - targetEndpoints.add("Subjects"); - targetEndpoints.add("Classrooms"); - targetEndpoints.add("Substitutions"); - targetEndpoints.add("Timetables"); - targetEndpoints.add("Colors"); - - targetEndpoints.add("SavedGradeCategories"); - targetEndpoints.add("GradesCategories"); - targetEndpoints.add("PointGradesCategories"); - targetEndpoints.add("DescriptiveGradesCategories"); - //targetEndpoints.add("TextGradesCategories"); - targetEndpoints.add("BehaviourGradesCategories"); // TODO: 2019-04-30 - targetEndpoints.add("SaveGradeCategories"); - - targetEndpoints.add("Grades"); - targetEndpoints.add("PointGrades"); - targetEndpoints.add("DescriptiveGrades"); - targetEndpoints.add("TextGrades"); - targetEndpoints.add("BehaviourGrades"); - targetEndpoints.add("GradesComments"); - - targetEndpoints.add("Events"); - targetEndpoints.add("TeacherFreeDays"); - targetEndpoints.add("CustomTypes"); - targetEndpoints.add("Homework"); - targetEndpoints.add("LuckyNumbers"); - targetEndpoints.add("Notices"); - targetEndpoints.add("AttendanceTypes"); - targetEndpoints.add("Attendance"); - targetEndpoints.add("Announcements"); - targetEndpoints.add("PtMeetings"); - - /*if (isEndpointEnabled(profile, true, "SchoolFreeDays")) - targetEndpoints.add("SchoolFreeDays"); - if (isEndpointEnabled(profile, true, "ClassFreeDays")) - targetEndpoints.add("ClassFreeDays");*/ - targetEndpoints.add("MessagesLogin"); - targetEndpoints.add("MessagesInbox"); - targetEndpoints.add("MessagesOutbox"); - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - @Override - public void syncFeature(@NonNull Context activityContext, @NonNull SyncCallback callback, @NonNull ProfileFull profile, int ... featureList) { - if (featureList == null) { - sync(activityContext, callback, profile.getId(), profile, LoginStore.fromProfileFull(profile)); - return; - } - if (!prepare(activityContext, callback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - login(() -> { - targetEndpoints = new ArrayList<>(); - if (featureList.length == 1) - onlyFeature = featureList[0]; - targetEndpoints.add("Me"); - targetEndpoints.add("Schools"); - targetEndpoints.add("Classes"); - targetEndpoints.add("VirtualClasses"); - targetEndpoints.add("Units"); - targetEndpoints.add("Users"); - targetEndpoints.add("Subjects"); - targetEndpoints.add("Colors"); - boolean hasMessagesLogin = false; - for (int feature: featureList) { - switch (feature) { - case FEATURE_TIMETABLE: - targetEndpoints.add("Classrooms"); - targetEndpoints.add("Substitutions"); - targetEndpoints.add("Timetables"); - break; - case FEATURE_AGENDA: - targetEndpoints.add("Events"); - targetEndpoints.add("CustomTypes"); - targetEndpoints.add("PtMeetings"); - targetEndpoints.add("SchoolFreeDays"); - targetEndpoints.add("TeacherFreeDays"); - break; - case FEATURE_GRADES: - targetEndpoints.add("SavedGradeCategories"); - targetEndpoints.add("GradesCategories"); - targetEndpoints.add("PointGradesCategories"); - targetEndpoints.add("DescriptiveGradesCategories"); - //targetEndpoints.add("TextGradesCategories"); - targetEndpoints.add("BehaviourGradesCategories"); // TODO: 2019-04-30 - targetEndpoints.add("SaveGradeCategories"); - - targetEndpoints.add("Grades"); - targetEndpoints.add("PointGrades"); - targetEndpoints.add("DescriptiveGrades"); - targetEndpoints.add("TextGrades"); - targetEndpoints.add("BehaviourGrades"); - - targetEndpoints.add("GradesComments"); - break; - case FEATURE_HOMEWORK: - targetEndpoints.add("Homework"); - break; - case FEATURE_NOTICES: - targetEndpoints.add("Notices"); - break; - case FEATURE_ATTENDANCE: - targetEndpoints.add("AttendanceTypes"); - targetEndpoints.add("Attendance"); - break; - case FEATURE_MESSAGES_INBOX: - if (!hasMessagesLogin) { - hasMessagesLogin = true; - targetEndpoints.add("MessagesLogin"); - } - targetEndpoints.add("MessagesInbox"); - break; - case FEATURE_MESSAGES_OUTBOX: - if (!hasMessagesLogin) { - hasMessagesLogin = true; - targetEndpoints.add("MessagesLogin"); - } - targetEndpoints.add("MessagesOutbox"); - break; - case FEATURE_ANNOUNCEMENTS: - targetEndpoints.add("Announcements"); - break; - } - } - targetEndpoints.add("LuckyNumbers"); - - /*if (isEndpointEnabled(profile, true, "SchoolFreeDays")) - targetEndpoints.add("SchoolFreeDays"); - if (isEndpointEnabled(profile, true, "ClassFreeDays")) - targetEndpoints.add("ClassFreeDays");*/ - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - - private void begin() { - if (profile == null) { - finishWithError(new AppError(TAG, 214, AppError.CODE_PROFILE_NOT_FOUND, "Profile == null WTF???")); - return; - } - String accountToken = profile.getStudentData("accountToken", null); - d(TAG, "Beginning account "+ profile.getStudentNameLong() +" sync with token "+accountToken+". Full sync enabled "+fullSync); - synergiaAccessToken = accountToken; - - callback.onProgress(PROGRESS_LOGIN); - - r("get", null); - } - - private void r(String type, String endpoint) { - // endpoint == null when beginning - if (endpoint == null) - endpoint = targetEndpoints.get(0); - int index = -1; - for (String request: targetEndpoints) { - index++; - if (request.equals(endpoint)) { - break; - } - } - if (type.equals("finish")) { - // called when finishing the action - callback.onProgress(PROGRESS_STEP); - index++; - } - if (index > targetEndpoints.size()) { - finish(); - return; - } - d(TAG, "Called r("+type+", "+endpoint+"). Getting "+targetEndpoints.get(index)); - switch (targetEndpoints.get(index)) { - case "Me": - getMe(); - break; - case "Schools": - getSchools(); - break; - case "Classes": - getClasses(); - break; - case "VirtualClasses": - getVirtualClasses(); - break; - case "Units": - getUnits(); - break; - case "Users": - getUsers(); - break; - case "Subjects": - getSubjects(); - break; - case "Classrooms": - getClassrooms(); - break; - case "Timetables": - getTimetables(); - break; - case "Substitutions": - getSubstitutions(); - break; - case "Colors": - getColors(); - break; - case "SavedGradeCategories": - getSavedGradeCategories(); - break; - case "GradesCategories": - getGradesCategories(); - break; - case "PointGradesCategories": - getPointGradesCategories(); - break; - case "DescriptiveGradesCategories": - getDescriptiveGradesSkills(); - break; - case "TextGradesCategories": - getTextGradesCategories(); - break; - case "BehaviourGradesCategories": - getBehaviourGradesCategories(); - break; - case "SaveGradeCategories": - saveGradeCategories(); - break; - case "Grades": - getGrades(); - break; - case "PointGrades": - getPointGrades(); - break; - case "DescriptiveGrades": - getDescriptiveGrades(); - break; - case "TextGrades": - getTextGrades(); - break; - case "GradesComments": - getGradesComments(); - break; - case "BehaviourGrades": - getBehaviourGrades(); - break; - case "Events": - getEvents(); - break; - case "CustomTypes": - getCustomTypes(); - break; - case "Homework": - getHomework(); - break; - case "LuckyNumbers": - getLuckyNumbers(); - break; - case "Notices": - getNotices(); - break; - case "AttendanceTypes": - getAttendanceTypes(); - break; - case "Attendance": - getAttendance(); - break; - case "Announcements": - getAnnouncements(); - break; - case "PtMeetings": - getPtMeetings(); - break; - case "TeacherFreeDaysTypes": - getTeacherFreeDaysTypes(); - break; - case "TeacherFreeDays": - getTeacherFreeDays(); - break; - case "SchoolFreeDays": - getSchoolFreeDays(); - break; - case "MessagesLogin": - getMessagesLogin(); - break; - case "MessagesInbox": - getMessagesInbox(); - break; - case "MessagesOutbox": - getMessagesOutbox(); - break; - case "Finish": - finish(); - break; - } - } - private void saveData() { - if (teamList.size() > 0) { - //app.db.teamDao().clear(profileId); - app.db.teamDao().addAll(teamList); - } - if (teacherList.size() > 0 && teacherListChanged) - app.db.teacherDao().addAllIgnore(teacherList); - if (subjectList.size() > 0) - app.db.subjectDao().addAll(subjectList); - if (lessonList.size() > 0) { - app.db.lessonDao().clear(profileId); - app.db.lessonDao().addAll(lessonList); - } - if (lessonChangeList.size() > 0) - app.db.lessonChangeDao().addAll(lessonChangeList); - if (gradeCategoryList.size() > 0 && gradeCategoryListChanged) - app.db.gradeCategoryDao().addAll(gradeCategoryList); - if (gradeList.size() > 0) { - app.db.gradeDao().clear(profileId); - app.db.gradeDao().addAll(gradeList); - } - if (eventList.size() > 0) { - app.db.eventDao().removeFuture(profileId, Date.getToday()); - app.db.eventDao().addAll(eventList); - } - if (eventTypeList.size() > 0) - app.db.eventTypeDao().addAll(eventTypeList); - if (teacherAbsenceList.size() > 0) - app.db.teacherAbsenceDao().addAll(teacherAbsenceList); - if (noticeList.size() > 0) { - app.db.noticeDao().clear(profileId); - app.db.noticeDao().addAll(noticeList); - } - if (attendanceList.size() > 0) - app.db.attendanceDao().addAll(attendanceList); - if (announcementList.size() > 0) - app.db.announcementDao().addAll(announcementList); - if (messageList.size() > 0) - app.db.messageDao().addAllIgnore(messageList); - if (messageRecipientList.size() > 0) - app.db.messageRecipientDao().addAll(messageRecipientList); - if (messageRecipientIgnoreList.size() > 0) - app.db.messageRecipientDao().addAllIgnore(messageRecipientIgnoreList); - if (metadataList.size() > 0) - app.db.metadataDao().addAllIgnore(metadataList); - if (messageMetadataList.size() > 0) - app.db.metadataDao().setSeen(messageMetadataList); - } - private void finish() { - try { - saveData(); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 480, CODE_OTHER, app.getString(R.string.sync_error_saving_data), null, null, e, null)); - } - if (fullSync) { - profile.setLastFullSync(System.currentTimeMillis()); - fullSync = false; - } - profile.setEmpty(false); - callback.onSuccess(activityContext, new ProfileFull(profile, loginStore)); - } - private void finishWithError(AppError error) { - try { - saveData(); - } - catch (Exception e) { - Crashlytics.logException(e); - } - callback.onError(activityContext, error); - } - - /* _ _ - | | (_) - | | ___ __ _ _ _ __ - | | / _ \ / _` | | '_ \ - | |___| (_) | (_| | | | | | - |______\___/ \__, |_|_| |_| - __/ | - |__*/ - public void login(@NonNull LoginCallback loginCallback) - { - - authorizeCallback = new AuthorizeCallback() { - @Override - public void onCsrfToken(String csrfToken) { - d(TAG, "Found CSRF token: "+csrfToken); - login(csrfToken, librusEmail, librusPassword, librusLoginCallback); - } - - @Override - public void onAuthorizationCode(String code) { - d(TAG, "Found auth code: "+code); - accessToken(code, null, accessTokenCallback); - } - }; - - librusLoginCallback = redirectUrl -> { - fakeAuthorize = "authorize"; - authorize(AUTHORIZE_URL, authorizeCallback); - }; - - accessTokenCallback = new AccessTokenCallback() { - @Override - public void onSuccess(String tokenType, String accessToken, String refreshToken, int expiresIn) { - d(TAG, "Got tokens: "+tokenType+" "+accessToken); - d(TAG, "Got tokens: "+refreshToken); - loginStore.putLoginData("tokenType", tokenType); - loginStore.putLoginData("accessToken", accessToken); - loginStore.putLoginData("refreshToken", refreshToken); - loginStore.putLoginData("tokenExpiryTime", System.currentTimeMillis()/1000 + expiresIn); - getSynergiaToken(tokenType, accessToken, refreshToken, System.currentTimeMillis()/1000 + expiresIn); - } - - @Override - public void onError() { - d(TAG, "Beginning login (authorize)"); - authorize(AUTHORIZE_URL, authorizeCallback); - } - }; - - synergiaAccountsCallback = data -> { - d(TAG, "Accounts: "+data.toString()); - JsonArray accounts = data.getAsJsonArray("accounts"); - if (accounts.size() == 0) { - finishWithError(new AppError(TAG, 1237, CODE_OTHER, app.getString(R.string.sync_error_register_no_students), data)); - return; - } - long accountDataTime; - List accountIds = new ArrayList<>(); - List accountLogins = new ArrayList<>(); - List accountTokens = new ArrayList<>(); - List accountNamesLong = new ArrayList<>(); - List accountNamesShort = new ArrayList<>(); - accountIds.clear(); - accountLogins.clear(); - accountTokens.clear(); - accountNamesLong.clear(); - accountNamesShort.clear(); - accountDataTime = data.get("lastModification").getAsLong(); - for (JsonElement accountEl: accounts) { - JsonObject account = accountEl.getAsJsonObject(); - - JsonElement state = account.get("state"); - if (state != null && !(state instanceof JsonNull)) { - if (state.getAsString().equals("requiring_an_action")) { - finishWithError(new AppError(TAG, 694, CODE_LIBRUS_DISCONNECTED, data)); - return; - } - if (state.getAsString().equals("need-activation")) { - finishWithError(new AppError(TAG, 701, CODE_SYNERGIA_NOT_ACTIVATED, data)); - return; - } - } - - accountIds.add(account.get("id").getAsInt()); - accountLogins.add(account.get("login").getAsString()); - accountTokens.add(account.get("accessToken").getAsString()); - accountNamesLong.add(account.get("studentName").getAsString()); - String[] nameParts = account.get("studentName").getAsString().split(" "); - accountNamesShort.add(nameParts[0]+" "+nameParts[1].charAt(0)+"."); - } - - List profileList = new ArrayList<>(); - for (int index = 0; index < accountIds.size(); index++) { - Profile newProfile = new Profile(); - newProfile.setStudentNameLong(accountNamesLong.get(index)); - newProfile.setStudentNameShort(accountNamesShort.get(index)); - newProfile.setName(newProfile.getStudentNameLong()); - newProfile.setSubname(librusEmail); - newProfile.setEmpty(true); - newProfile.putStudentData("accountId", accountIds.get(index)); - newProfile.putStudentData("accountLogin", accountLogins.get(index)); - newProfile.putStudentData("accountToken", accountTokens.get(index)); - newProfile.putStudentData("accountTokenTime", accountDataTime); - profileList.add(newProfile); - } - - callback.onLoginFirst(profileList, loginStore); - }; - - synergiaAccountCallback = data -> { - if (data == null) { - // 410 Gone - app.cookieJar.clearForDomain("portal.librus.pl"); - authorize(AUTHORIZE_URL, authorizeCallback); - return; - } - if (profile == null) { - // this cannot be run on a fresh login - finishWithError(new AppError(TAG, 1290, CODE_PROFILE_NOT_FOUND, "Profile == null", data)); - return; - } - d(TAG, "Account: "+data.toString()); - // synergiaAccount is executed when a synergia token needs a refresh - JsonElement id = data.get("id"); - JsonElement login = data.get("login"); - JsonElement accessToken = data.get("accessToken"); - if (id == null || login == null || accessToken == null) { - finishWithError(new AppError(TAG, 1284, CODE_OTHER, data)); - return; - } - profile.putStudentData("accountId", id.getAsInt()); - profile.putStudentData("accountLogin", login.getAsString()); - profile.putStudentData("accountToken", accessToken.getAsString()); - profile.putStudentData("accountTokenTime", System.currentTimeMillis() / 1000); - profile.setStudentNameLong(data.get("studentName").getAsString()); - String[] nameParts = data.get("studentName").getAsString().split(" "); - profile.setStudentNameShort(nameParts[0] + " " + nameParts[1].charAt(0) + "."); - loginCallback.onSuccess(); - }; - - String tokenType = loginStore.getLoginData("tokenType", "Bearer"); - String accessToken = loginStore.getLoginData("accessToken", null); - String refreshToken = loginStore.getLoginData("refreshToken", null); - long tokenExpiryTime = loginStore.getLoginData("tokenExpiryTime", (long)0); - String accountToken; - - if (profile != null - && (accountToken = profile.getStudentData("accountToken", null)) != null - && !accountToken.equals("") - && (System.currentTimeMillis() / 1000) - profile.getStudentData("accountTokenTime", (long)0) < 3 * 60 * 60) { - c(TAG, "synergia token should be valid"); - loginCallback.onSuccess(); - } - else { - getSynergiaToken(tokenType, accessToken, refreshToken, tokenExpiryTime); - } - } - private String synergiaAccessToken = ""; - private void getSynergiaToken(String tokenType, String accessToken, String refreshToken, long tokenExpiryTime) { - c(TAG, "we have no synergia token or it expired"); - if (!tokenType.equals("") - && refreshToken != null - && accessToken != null - && tokenExpiryTime-30 > System.currentTimeMillis() / 1000) { - c(TAG, "we have a valid librus token, so we can use the API"); - // we have to decide whether we can already proceed getting the synergiaToken - // or a list of students - if (profile != null) { - app.cookieJar.clearForDomain("portal.librus.pl"); - c(TAG, "user is logged in, refreshing synergia token"); - d(TAG, "Librus token: "+accessToken); - synergiaAccount(tokenType, accessToken, profile.getStudentData("accountLogin", null), synergiaAccountCallback); - } - else { - // this *should* be executed only once. ever. - c(TAG, "user is not logged in, getting all the accounts"); - synergiaAccounts(tokenType, accessToken, synergiaAccountsCallback); - } - } else if (refreshToken != null) { - c(TAG, "we don't have a valid token or it expired"); - c(TAG, "but we have a refresh token"); - d(TAG, "Token expired at " + tokenExpiryTime + ", " + (System.currentTimeMillis() / 1000 - tokenExpiryTime) + " seconds ago"); - app.cookieJar.clearForDomain("portal.librus.pl"); - accessToken(null, refreshToken, accessTokenCallback); - } else { - c(TAG, "we don't have any of the needed librus tokens"); - c(TAG, "we need to log in and generate"); - app.cookieJar.clearForDomain("portal.librus.pl"); - authorize(AUTHORIZE_URL, authorizeCallback); - } - } - public boolean loginSynergia(@NonNull LoginCallback loginCallback) - { - if (profile == null) { - return false; - } - if (synergiaLogin == null || synergiaPassword == null || synergiaLogin.equals("") || synergiaPassword.equals("")) { - finishWithError(new AppError(TAG, 1152, CODE_INVALID_LOGIN, "Login field is empty")); - return false; - } - if (System.currentTimeMillis() - synergiaLastLoginTime < 10 * 60 * 1000 && synergiaLogin.equals(synergiaLastLogin)) {// 10 minutes - loginCallback.onSuccess(); - return true; - } - - String escapedPassword = synergiaPassword.replace("&", "&");// TODO: 2019-05-07 check other chars to escape - - String body = - "\n" + - "

\n" + - " \n" + - " "+synergiaLogin+"\n" + - " "+escapedPassword+"\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - ""; - synergiaRequest("Login", body, data -> { - if (data == null) { - finishWithError(new AppError(TAG, 1176, AppError.CODE_MAINTENANCE, "data == null (975)")); - return; - } - String error = data.select("response Login status").text(); - if (error.equals("ok")) { - synergiaLastLoginTime = System.currentTimeMillis(); - synergiaLastLogin = synergiaLogin; - loginCallback.onSuccess(); - } - else { - finishWithError(new AppError(TAG, 1186, CODE_INVALID_LOGIN, (Response) null, data.outerHtml())); - } - }); - return true; - } - - /* _ _ _ _ _ _ _ - | | | | | | ___ | | | | | | - | |__| | ___| |_ __ ___ _ __ ___ ( _ ) ___ __ _| | | |__ __ _ ___| | _____ - | __ |/ _ \ | '_ \ / _ \ '__/ __| / _ \/\ / __/ _` | | | '_ \ / _` |/ __| |/ / __| - | | | | __/ | |_) | __/ | \__ \ | (_> < | (_| (_| | | | |_) | (_| | (__| <\__ \ - |_| |_|\___|_| .__/ \___|_| |___/ \___/\/ \___\__,_|_|_|_.__/ \__,_|\___|_|\_\___/ - | | - |*/ - private String fakeAuthorize = "authorize"; - private void authorize(String url, AuthorizeCallback authorizeCallback) { - callback.onActionStarted(R.string.sync_action_authorizing); - Request.builder() - .url(fakeLogin ? "http://szkolny.eu/librus/"+fakeAuthorize+".php" : url) - .userAgent(userAgent) - .withClient(app.httpLazy) - .callback(new TextCallbackHandler() { - @Override - public void onSuccess(String data, Response response) { - //d("headers "+response.headers().toString()); - String location = response.headers().get("Location"); - if (location != null) { - Matcher authMatcher = Pattern.compile(REDIRECT_URL+"\\?code=([A-z0-9]+?)$", Pattern.DOTALL | Pattern.MULTILINE).matcher(location); - if (authMatcher.find()) { - authorizeCallback.onAuthorizationCode(authMatcher.group(1)); - } - else { - //callback.onError(activityContext, Edziennik.CODE_OTHER, "Auth code not found: "+location); - authorize(location, authorizeCallback); - } - } - else { - Matcher csrfMatcher = Pattern.compile("name=\"csrf-token\" content=\"([A-z0-9=+/\\-_]+?)\"", Pattern.DOTALL).matcher(data); - if (csrfMatcher.find()) { - authorizeCallback.onCsrfToken(csrfMatcher.group(1)); - } - else { - finishWithError(new AppError(TAG, 463, CODE_OTHER, "CSRF token not found.", response, data)); - } - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 207, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private void login(String csrfToken, String email, String password, LibrusLoginCallback librusLoginCallback) { - callback.onActionStarted(R.string.sync_action_logging_in); - Request.builder() - .url(fakeLogin ? "http://szkolny.eu/librus/login_action.php" : LOGIN_URL) - .userAgent(userAgent) - .addParameter("email", email) - .addParameter("password", password) - .addHeader("X-CSRF-TOKEN", csrfToken) - .contentType(MediaTypeUtils.APPLICATION_JSON) - .post() - .callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - if (response.parserErrorBody != null && response.parserErrorBody.contains("link aktywacyjny")) { - finishWithError(new AppError(TAG, 487, CODE_LIBRUS_NOT_ACTIVATED, response)); - return; - } - finishWithError(new AppError(TAG, 489, CODE_MAINTENANCE, response)); - return; - } - if (data.get("errors") != null) { - finishWithError(new AppError(TAG, 490, CODE_OTHER, data.get("errors").getAsJsonArray().get(0).getAsString(), response, data)); - return; - } - librusLoginCallback.onLogin(data.get("redirect") != null ? data.get("redirect").getAsString() : ""); - } - - @Override - public void onFailure(Response response, Throwable throwable) { - if (response.code() == 403 - || response.code() == 401) { - finishWithError(new AppError(TAG, 248, AppError.CODE_INVALID_LOGIN, response, throwable)); - return; - } - finishWithError(new AppError(TAG, 251, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private boolean refreshTokenFailed = false; - private void accessToken(String code, String refreshToken, AccessTokenCallback accessTokenCallback) { - callback.onActionStarted(R.string.sync_action_getting_token); - List> params = new ArrayList<>(); - params.add(new Pair<>("client_id", CLIENT_ID)); - if (code != null) { - params.add(new Pair<>("grant_type", "authorization_code")); - params.add(new Pair<>("code", code)); - params.add(new Pair<>("redirect_uri", REDIRECT_URL)); - } - else if (refreshToken != null) { - params.add(new Pair<>("grant_type", "refresh_token")); - params.add(new Pair<>("refresh_token", refreshToken)); - } - Request.builder() - .url(fakeLogin ? "http://szkolny.eu/librus/access_token.php" : TOKEN_URL) - .userAgent(userAgent) - .addParams(params) - .allowErrorCode(HTTP_UNAUTHORIZED) - .post() - .callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - finishWithError(new AppError(TAG, 539, CODE_MAINTENANCE, response)); - return; - } - if (data.get("error") != null) { - JsonElement message = data.get("message"); - JsonElement hint = data.get("hint"); - if (!refreshTokenFailed && refreshToken != null && hint != null && (hint.getAsString().equals("Token has been revoked") || hint.getAsString().equals("Token has expired"))) { - c(TAG, "refreshing the token failed. Trying to log in again."); - refreshTokenFailed = true; - accessTokenCallback.onError(); - return; - } - String errorText = data.get("error").getAsString()+" "+(message == null ? "" : message.getAsString())+" "+(hint == null ? "" : hint.getAsString()); - finishWithError(new AppError(TAG, 552, CODE_OTHER, errorText, response, data)); - return; - } - try { - accessTokenCallback.onSuccess( - data.get("token_type").getAsString(), - data.get("access_token").getAsString(), - data.get("refresh_token").getAsString(), - data.get("expires_in").getAsInt()); - } - catch (NullPointerException e) { - finishWithError(new AppError(TAG, 311, CODE_OTHER, response, e, data)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 317, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private void synergiaAccounts(String tokenType, String accessToken, SynergiaAccountsCallback synergiaAccountsCallback) { - callback.onActionStarted(R.string.sync_action_getting_accounts); - Request.builder() - .url(fakeLogin ? "http://szkolny.eu/librus/synergia_accounts.php" : ACCOUNTS_URL) - .userAgent(userAgent) - .addHeader("Authorization", tokenType+" "+accessToken) - .get() - .allowErrorCode(HTTP_FORBIDDEN) - .allowErrorCode(HTTP_UNAUTHORIZED) - .allowErrorCode(HTTP_BAD_REQUEST) - .callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - finishWithError(new AppError(TAG, 590, CODE_MAINTENANCE, response)); - return; - } - if (data.get("error") != null) { - JsonElement message = data.get("message"); - JsonElement hint = data.get("hint"); - String errorText = data.get("error").getAsString()+" "+(message == null ? "" : message.getAsString())+" "+(hint == null ? "" : hint.getAsString()); - finishWithError(new AppError(TAG, 597, CODE_OTHER, errorText, response, data)); - return; - } - try { - synergiaAccountsCallback.onSuccess(data); - } - catch (NullPointerException e) { - e.printStackTrace(); - finishWithError(new AppError(TAG, 358, CODE_OTHER, response, e, data)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 364, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private boolean error410 = false; - - private void synergiaAccount(String tokenType, String accessToken, String accountLogin, SynergiaAccountCallback synergiaAccountCallback) { - callback.onActionStarted(R.string.sync_action_getting_account); - d(TAG, "Requesting "+(fakeLogin ? "http://szkolny.eu/librus/synergia_accounts_fresh.php?login="+accountLogin : ACCOUNT_URL+accountLogin)); - if (accountLogin == null) { // just for safety - synergiaAccounts(tokenType, accessToken, synergiaAccountsCallback); - return; - } - Request.builder() - .url(fakeLogin ? "http://szkolny.eu/librus/synergia_accounts_fresh.php?login="+accountLogin : ACCOUNT_URL+accountLogin) - .userAgent(userAgent) - .addHeader("Authorization", tokenType+" "+accessToken) - .get() - .allowErrorCode(HTTP_NOT_FOUND) - .allowErrorCode(HTTP_FORBIDDEN) - .allowErrorCode(HTTP_UNAUTHORIZED) - .allowErrorCode(HTTP_BAD_REQUEST) - .allowErrorCode(HTTP_GONE) - .callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - finishWithError(new AppError(TAG, 641, CODE_MAINTENANCE, response)); - return; - } - if (response.code() == 410 && !error410) { - JsonElement reason = data.get("reason"); - if (reason != null && !(reason instanceof JsonNull) && reason.getAsString().equals("requires_an_action")) { - finishWithError(new AppError(TAG, 1078, CODE_LIBRUS_DISCONNECTED, response, data)); - return; - } - error410 = true; - synergiaAccountCallback.onSuccess(null); - return; - } - if (data.get("message") != null) { - String message = data.get("message").getAsString(); - if (message.equals("Account not found")) { - finishWithError(new AppError(TAG, 651, CODE_OTHER, app.getString(R.string.sync_error_register_student_not_associated_format, profile.getStudentNameLong(), accountLogin), response, data)); - return; - } - finishWithError(new AppError(TAG, 654, CODE_OTHER, message+"\n\n"+accountLogin, response, data)); - return; - } - if (response.code() == HTTP_OK) { - try { - synergiaAccountCallback.onSuccess(data); - } catch (NullPointerException e) { - e.printStackTrace(); - finishWithError(new AppError(TAG, 662, CODE_OTHER, response, e, data)); - } - } - else { - finishWithError(new AppError(TAG, 425, CODE_OTHER, response, data)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 432, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private int failed = 0; - - private void apiRequest(String endpoint, ApiRequestCallback apiRequestCallback) { - d(TAG, "Requesting "+API_URL+endpoint); - Request.builder() - .url(fakeLogin ? "http://szkolny.eu/librus/api/"+endpoint : API_URL+endpoint) - .userAgent(userAgent) - .addHeader("Authorization", "Bearer "+synergiaAccessToken) - .get() - .allowErrorCode(HTTP_FORBIDDEN) - .allowErrorCode(HTTP_UNAUTHORIZED) - .allowErrorCode(HTTP_BAD_REQUEST) - .callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - if (response.parserErrorBody != null && response.parserErrorBody.equals("Nieprawidłowy węzeł.")) { - apiRequestCallback.onSuccess(null); - return; - } - finishWithError(new AppError(TAG, 453, CODE_MAINTENANCE, response)); - return; - } - if (data.get("Status") != null) { - JsonElement message = data.get("Message"); - JsonElement code = data.get("Code"); - d(TAG, "apiRequest Error "+data.get("Status").getAsString()+" "+(message == null ? "" : message.getAsString())+" "+(code == null ? "" : code.getAsString())+"\n\n"+response.request().url().toString()); - if (message != null && !(message instanceof JsonNull) && message.getAsString().equals("Student timetable is not public")) { - try { - apiRequestCallback.onSuccess(null); - } - catch (NullPointerException e) { - e.printStackTrace(); - d(TAG, "apiRequest exception "+e.getMessage()); - finishWithError(new AppError(TAG, 503, CODE_OTHER, response, e, data)); - } - return; - } - if (code != null - && !(code instanceof JsonNull) - && (code.getAsString().equals("LuckyNumberIsNotActive") - || code.getAsString().equals("NotesIsNotActive") - || code.getAsString().equals("AccessDeny")) - ) { - try { - apiRequestCallback.onSuccess(null); - } - catch (NullPointerException e) { - e.printStackTrace(); - d(TAG, "apiRequest exception "+e.getMessage()); - finishWithError(new AppError(TAG, 504, CODE_OTHER, response, e, data)); - } - return; - } - String errorText = data.get("Status").getAsString()+" "+(message == null ? "" : message.getAsString())+" "+(code == null ? "" : code.getAsString()); - if (code != null && !(code instanceof JsonNull) && code.getAsString().equals("TokenIsExpired")) { - failed++; - d(TAG, "Trying to refresh synergia token, api request failed "+failed+" times now"); - if (failed > 1) { - d(TAG, "Giving up, failed "+failed+" times"); - finishWithError(new AppError(TAG, 485, CODE_OTHER, errorText, response, data)); - return; - } - String tokenType = loginStore.getLoginData("tokenType", "Bearer"); - String accessToken = loginStore.getLoginData("accessToken", null); - String refreshToken = loginStore.getLoginData("refreshToken", null); - long tokenExpiryTime = loginStore.getLoginData("tokenExpiryTime", (long)0); - getSynergiaToken(tokenType, accessToken, refreshToken, tokenExpiryTime); - return; - } - finishWithError(new AppError(TAG, 497, CODE_OTHER, errorText, response, data)); - return; - } - try { - apiRequestCallback.onSuccess(data); - } - catch (NullPointerException e) { - e.printStackTrace(); - d(TAG, "apiRequest exception "+e.getMessage()); - finishWithError(new AppError(TAG, 505, CODE_OTHER, response, e, data)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - if (response.code() == 405) { - // method not allowed - finishWithError(new AppError(TAG, 511, CODE_OTHER, response, throwable)); - return; - } - if (response.code() == 500) { - // TODO: 2019-09-10 dirty hotfix - if ("Classrooms".equals(endpoint)) { - apiRequestCallback.onSuccess(null); - return; - } - finishWithError(new AppError(TAG, 516, CODE_MAINTENANCE, response, throwable)); - return; - } - finishWithError(new AppError(TAG, 520, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private void synergiaRequest(String endpoint, String body, SynergiaRequestCallback synergiaRequestCallback) { - d(TAG, "Requesting "+SYNERGIA_URL+endpoint); - Request.builder() - .url(SYNERGIA_URL+endpoint) - .userAgent(synergiaUserAgent) - .setTextBody(body, MediaTypeUtils.APPLICATION_XML) - .callback(new TextCallbackHandler() { - @Override - public void onSuccess(String data, Response response) { - if ((data.contains("error") || data.contains("")) && !data.contains("Niepoprawny")) { - finishWithError(new AppError(TAG, 541, AppError.CODE_MAINTENANCE, response, data)); - return; - } - synergiaRequestCallback.onSuccess(Jsoup.parse(data, "", Parser.xmlParser())); - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 556, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - // CALLBACKS & INTERFACES - private interface AuthorizeCallback { - void onCsrfToken(String csrfToken); - void onAuthorizationCode(String code); - } - private interface LibrusLoginCallback { - void onLogin(String redirectUrl); - } - private interface AccessTokenCallback { - void onSuccess(String tokenType, String accessToken, String refreshToken, int expiresIn); - void onError(); - } - private interface SynergiaAccountsCallback { - void onSuccess(JsonObject data); - } - private interface SynergiaAccountCallback { - void onSuccess(JsonObject data); - } - private interface ApiRequestCallback { - void onSuccess(JsonObject data); - } - private interface SynergiaRequestCallback { - void onSuccess(Document data); - } - - private AuthorizeCallback authorizeCallback; - private LibrusLoginCallback librusLoginCallback; - private AccessTokenCallback accessTokenCallback; - private SynergiaAccountsCallback synergiaAccountsCallback; - private SynergiaAccountCallback synergiaAccountCallback; - - /* _____ _ _____ _ - | __ \ | | | __ \ | | - | | | | __ _| |_ __ _ | |__) |___ __ _ _ _ ___ ___| |_ ___ - | | | |/ _` | __/ _` | | _ // _ \/ _` | | | |/ _ \/ __| __/ __| - | |__| | (_| | || (_| | | | \ \ __/ (_| | |_| | __/\__ \ |_\__ \ - |_____/ \__,_|\__\__,_| |_| \_\___|\__, |\__,_|\___||___/\__|___/ - | | - |*/ - private void getMe() { - if (!fullSync) { - r("finish", "Me"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_account_info); - apiRequest("Me", data -> { - JsonObject me = data.get("Me").getAsJsonObject(); - me = me.get("Account").getAsJsonObject(); - //d("Got School: "+school.toString()); - try { - boolean premium = me.get("IsPremium").getAsBoolean(); - boolean premiumDemo = me.get("IsPremiumDemo").getAsBoolean(); - this.premium = premium || premiumDemo; - profile.putStudentData("isPremium", premium || premiumDemo); - r("finish", "Me"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1316, CODE_OTHER, e, data)); - } - }); - } - - private Map> lessonRanges = new HashMap<>(); - private String schoolName = ""; - private void getSchools() { - if (!fullSync) { - try { - lessonRanges = app.gson.fromJson(profile.getStudentData("lessonRanges", "{}"), new TypeToken>>() { - }.getType()); - if (lessonRanges != null && lessonRanges.size() > 0) { - r("finish", "Schools"); - return; - } - } - catch (Exception e) { - e.printStackTrace(); - } - } - lessonRanges = new HashMap<>(); - callback.onActionStarted(R.string.sync_action_syncing_school_info); - apiRequest("Schools", data -> { - //d("Got School: "+school.toString()); - try { - JsonObject school = data.get("School").getAsJsonObject(); - int schoolId = school.get("Id").getAsInt(); - String schoolNameLong = school.get("Name").getAsString(); - StringBuilder schoolNameShort = new StringBuilder(); - for (String schoolNamePart: schoolNameLong.split(" ")) { - if (schoolNamePart.isEmpty()) - continue; - schoolNameShort.append(Character.toLowerCase(schoolNamePart.charAt(0))); - } - String schoolTown = school.get("Town").getAsString(); - schoolName = schoolId+schoolNameShort.toString()+"_"+schoolTown.toLowerCase(); - profile.putStudentData("schoolName", schoolName); - - lessonRanges.clear(); - int index = 0; - for (JsonElement lessonRangeEl: school.get("LessonsRange").getAsJsonArray()) { - JsonObject lr = lessonRangeEl.getAsJsonObject(); - JsonElement from = lr.get("From"); - JsonElement to = lr.get("To"); - if (from != null && to != null && !(from instanceof JsonNull) && !(to instanceof JsonNull)) { - lessonRanges.put(index, new Pair<>(Time.fromH_m(from.getAsString()), Time.fromH_m(to.getAsString()))); - } - index++; - } - profile.putStudentData("lessonRanges", app.gson.toJson(lessonRanges)); - r("finish", "Schools"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1364, CODE_OTHER, e, data)); - } - }); - } - - private long teamClassId = -1; - private void getClasses() { - if (!fullSync) { - r("finish", "Classes"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_class); - apiRequest("Classes", data -> { - //d("Got Class: "+myClass.toString()); - try { - JsonObject myClass = data.get("Class").getAsJsonObject(); - String teamName = myClass.get("Number").getAsString() - + myClass.get("Symbol").getAsString(); - teamClassId = myClass.get("Id").getAsLong(); - teamList.add(new Team( - profileId, - teamClassId, - teamName, - 1, - schoolName+":"+teamName, - myClass.get("ClassTutor").getAsJsonObject().get("Id").getAsLong())); - JsonElement semester1Begin = myClass.get("BeginSchoolYear"); - JsonElement semester2Begin = myClass.get("EndFirstSemester"); - JsonElement yearEnd = myClass.get("EndSchoolYear"); - if (semester1Begin != null - && semester2Begin != null - && yearEnd != null - && !(semester1Begin instanceof JsonNull) - && !(semester2Begin instanceof JsonNull) - && !(yearEnd instanceof JsonNull)) { - profile.setDateSemester1Start(Date.fromY_m_d(semester1Begin.getAsString())); - profile.setDateSemester2Start(Date.fromY_m_d(semester2Begin.getAsString())); - profile.setDateYearEnd(Date.fromY_m_d(yearEnd.getAsString())); - } - JsonElement unit = myClass.get("Unit"); - if (unit != null && !(unit instanceof JsonNull)) { - unitId = unit.getAsJsonObject().get("Id").getAsLong(); - profile.putStudentData("unitId", unitId); - } - r("finish", "Classes"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1411, CODE_OTHER, e, data)); - } - }); - } - - private void getVirtualClasses() { - if (!fullSync) { - r("finish", "VirtualClasses"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_teams); - apiRequest("VirtualClasses", data -> { - if (data == null) { - r("finish", "VirtualClasses"); - return; - } - try { - JsonArray classes = data.get("VirtualClasses").getAsJsonArray(); - for (JsonElement myClassEl: classes) { - JsonObject myClass = myClassEl.getAsJsonObject(); - String teamName = myClass.get("Name").getAsString(); - long teamId = myClass.get("Id").getAsLong(); - long teacherId = -1; - JsonElement el; - if ((el = myClass.get("Teacher")) != null) { - teacherId = el.getAsJsonObject().get("Id").getAsLong(); - } - teamList.add(new Team( - profileId, - teamId, - teamName, - 2, - schoolName + ":" + teamName, - teacherId)); - } - r("finish", "VirtualClasses"); - } catch (Exception e) { - finishWithError(new AppError(TAG, 1449, CODE_OTHER, e, data)); - } - }); - } - - private void getUnits() { - if (!fullSync) { - enableStandardGrades = profile.getStudentData("enableStandardGrades", true); - enablePointGrades = profile.getStudentData("enablePointGrades", false); - enableDescriptiveGrades = profile.getStudentData("enableDescriptiveGrades", false); - enableTextGrades = profile.getStudentData("enableTextGrades", false); - enableBehaviourGrades = profile.getStudentData("enableBehaviourGrades", true); - startPointsSemester1 = profile.getStudentData("startPointsSemester1", 0); - startPointsSemester2 = profile.getStudentData("startPointsSemester2", 0); - r("finish", "Units"); - return; - } - d(TAG, "Grades settings: "+enableStandardGrades+", "+enablePointGrades+", "+enableDescriptiveGrades); - callback.onActionStarted(R.string.sync_action_syncing_school_info); - apiRequest("Units", data -> { - if (data == null) { - r("finish", "Units"); - return; - } - JsonArray units = data.getAsJsonArray("Units"); - try { - long unitId = profile.getStudentData("unitId", (long)-1); - enableStandardGrades = true; // once a week or two (during a full sync) force getting the standard grade list. If there aren't any, disable it again later. - enableBehaviourGrades = true; - enableTextGrades = true; // TODO: 2019-05-13 if "DescriptiveGradesEnabled" are also TextGrades - profile.putStudentData("enableStandardGrades", true); - profile.putStudentData("enableBehaviourGrades", true); - profile.putStudentData("enableTextGrades", true); - for (JsonElement unitEl: units) { - JsonObject unit = unitEl.getAsJsonObject(); - - if (unit.get("Id").getAsLong() == unitId) { - JsonObject gradesSettings = unit.getAsJsonObject("GradesSettings"); - enablePointGrades = gradesSettings.get("PointGradesEnabled").getAsBoolean(); - enableDescriptiveGrades = gradesSettings.get("DescriptiveGradesEnabled").getAsBoolean(); - - JsonObject behaviourGradesSettings = unit.getAsJsonObject("BehaviourGradesSettings"); - JsonObject startPoints = behaviourGradesSettings.getAsJsonObject("StartPoints"); - - startPointsSemester1 = startPoints.get("Semester1").getAsInt(); - JsonElement startPointsSemester2El; - if ((startPointsSemester2El = startPoints.get("Semester2")) != null) { - startPointsSemester2 = startPointsSemester2El.getAsInt(); - } - else { - startPointsSemester2 = startPointsSemester1; - } - - profile.putStudentData("enablePointGrades", enablePointGrades); - profile.putStudentData("enableDescriptiveGrades", enableDescriptiveGrades); - profile.putStudentData("startPointsSemester1", startPointsSemester1); - profile.putStudentData("startPointsSemester2", startPointsSemester2); - break; - } - } - d(TAG, "Grades settings: "+enableStandardGrades+", "+enablePointGrades+", "+enableDescriptiveGrades); - r("finish", "Units"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1506, CODE_OTHER, e, data)); - } - }); - } - - private boolean teacherListChanged = false; - private void getUsers() { - if (!fullSync) { - r("finish", "Users"); - teacherList = app.db.teacherDao().getAllNow(profileId); - teacherListChanged = false; - return; - } - callback.onActionStarted(R.string.sync_action_syncing_users); - apiRequest("Users", data -> { - if (data == null) { - r("finish", "Users"); - return; - } - JsonArray users = data.get("Users").getAsJsonArray(); - //d("Got Users: "+users.toString()); - try { - teacherListChanged = true; - for (JsonElement userEl : users) { - JsonObject user = userEl.getAsJsonObject(); - JsonElement firstName = user.get("FirstName"); - JsonElement lastName = user.get("LastName"); - teacherList.add(new Teacher( - profileId, - user.get("Id").getAsLong(), - firstName instanceof JsonNull ? "" : firstName.getAsString(), - lastName instanceof JsonNull ? "" : lastName.getAsString() - )); - } - r("finish", "Users"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1544, CODE_OTHER, e, data)); - } - }); - } - - private void getSubjects() { - if (!fullSync) { - r("finish", "Subjects"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_subjects); - apiRequest("Subjects", data -> { - if (data == null) { - r("finish", "Subjects"); - return; - } - JsonArray subjects = data.get("Subjects").getAsJsonArray(); - //d("Got Subjects: "+subjects.toString()); - try { - for (JsonElement subjectEl : subjects) { - JsonObject subject = subjectEl.getAsJsonObject(); - JsonElement longName = subject.get("Name"); - JsonElement shortName = subject.get("Short"); - subjectList.add(new Subject( - profileId, - subject.get("Id").getAsLong(), - longName instanceof JsonNull ? "" : longName.getAsString(), - shortName instanceof JsonNull ? "" : shortName.getAsString() - )); - } - subjectList.add(new Subject( - profileId, - 1, - "Zachowanie", - "zach" - )); - r("finish", "Subjects"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1588, CODE_OTHER, e, data)); - } - }); - } - - private SparseArray classrooms = new SparseArray<>(); - private void getClassrooms() { - //if (!fullSync) - // r("finish", "Classrooms"); - callback.onActionStarted(R.string.sync_action_syncing_classrooms); - apiRequest("Classrooms", data -> { - if (data == null) { - r("finish", "Classrooms"); - return; - } - if (data.get("Classrooms") == null) { - r("finish", "Classrooms"); - return; - } - JsonArray jClassrooms = data.get("Classrooms").getAsJsonArray(); - //d("Got Classrooms: "+jClassrooms.toString()); - classrooms.clear(); - try { - for (JsonElement classroomEl : jClassrooms) { - JsonObject classroom = classroomEl.getAsJsonObject(); - classrooms.put(classroom.get("Id").getAsInt(), classroom.get("Name").getAsString()); - } - r("finish", "Classrooms"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1617, CODE_OTHER, e, data)); - } - }); - } - - private void getTimetables() { - callback.onActionStarted(R.string.sync_action_syncing_timetable); - Date weekStart = Week.getWeekStart(); - if (Date.getToday().getWeekDay() > 4) { - weekStart.stepForward(0, 0, 7); - } - apiRequest("Timetables?weekStart="+weekStart.getStringY_m_d(), data -> { - if (data == null) { - r("finish", "Timetables"); - return; - } - JsonObject timetables = data.get("Timetable").getAsJsonObject(); - try { - for (Map.Entry dayEl: timetables.entrySet()) { - JsonArray day = dayEl.getValue().getAsJsonArray(); - Date lessonDate = Date.fromY_m_d(dayEl.getKey()); - for (JsonElement lessonGroupEl: day) { - if ((lessonGroupEl instanceof JsonArray && ((JsonArray) lessonGroupEl).size() == 0) || lessonGroupEl instanceof JsonNull || lessonGroupEl == null) { - continue; - } - JsonArray lessonGroup = lessonGroupEl.getAsJsonArray(); - for (JsonElement lessonEl: lessonGroup) { - if ((lessonEl instanceof JsonArray && ((JsonArray) lessonEl).size() == 0) || lessonEl instanceof JsonNull || lessonEl == null) { - continue; - } - JsonObject lesson = lessonEl.getAsJsonObject(); - - boolean substitution = false; - boolean cancelled = false; - JsonElement isSubstitutionClass; - if ((isSubstitutionClass = lesson.get("IsSubstitutionClass")) != null) { - substitution = isSubstitutionClass.getAsBoolean(); - } - JsonElement isCanceled; - if ((isCanceled = lesson.get("IsCanceled")) != null) { - cancelled = isCanceled.getAsBoolean(); - } - - if (substitution && cancelled) { - // the lesson is probably shifted. Skip this one - continue; - } - - Time startTime = null; - Time endTime = null; - try { - startTime = Time.fromH_m(lesson.get(substitution && !cancelled ? "OrgHourFrom" : "HourFrom").getAsString()); - endTime = Time.fromH_m(lesson.get(substitution && !cancelled ? "OrgHourTo" : "HourTo").getAsString()); - } - catch (Exception ignore) { - try { - JsonElement lessonNo; - if (!((lessonNo = lesson.get("LessonNo")) instanceof JsonNull)) { - Pair timePair = lessonRanges.get(strToInt(lessonNo.getAsString())); - if (timePair != null) { - startTime = timePair.first; - endTime = timePair.second; - } - } - } - catch (Exception ignore2) { } - } - - - Lesson lessonObject = new Lesson( - profileId, - lesson.get("DayNo").getAsInt() - 1, - startTime, - endTime - ); - - JsonElement subject; - if ((subject = lesson.get(substitution && !cancelled ? "OrgSubject" : "Subject")) != null) { - lessonObject.subjectId = subject.getAsJsonObject().get("Id").getAsLong(); - } - - JsonElement teacher; - if ((teacher = lesson.get(substitution && !cancelled ? "OrgTeacher" : "Teacher")) != null) { - lessonObject.teacherId = teacher.getAsJsonObject().get("Id").getAsLong(); - } - - JsonElement myClass; - if ((myClass = lesson.get("Class")) != null) { - lessonObject.teamId = myClass.getAsJsonObject().get("Id").getAsLong(); - } - if (myClass == null && (myClass = lesson.get("VirtualClass")) != null) { - lessonObject.teamId = myClass.getAsJsonObject().get("Id").getAsLong(); - } - - JsonElement classroom; - JsonElement substitutionClassroom; - if (substitution && !cancelled) { - classroom = lesson.get("OrgClassroom"); - substitutionClassroom = lesson.get("Classroom"); - - if (classroom != null) - lessonObject.classroomName = classrooms.get(classroom.getAsJsonObject().get("Id").getAsInt()); - - if (substitutionClassroom != null) { - for (LessonChange lessonChange : lessonChangeList) { - if(lessonChange.lessonDate.compareTo(lessonDate) == 0) { - lessonChange.classroomName - = classrooms.get(substitutionClassroom.getAsJsonObject().get("Id").getAsInt()); - break; - } - } - } - } else { - classroom = lesson.get("Classroom"); - if (classroom != null) - lessonObject.classroomName = classrooms.get(classroom.getAsJsonObject().get("Id").getAsInt()); - } - - lessonList.add(lessonObject); - } - } - } - r("finish", "Timetables"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1704, CODE_OTHER, e, data)); - } - }); - } - - private void getSubstitutions() { - callback.onActionStarted(R.string.sync_action_syncing_timetable_changes); - apiRequest("Calendars/Substitutions", data -> { - if (data == null) { - r("finish", "Substitutions"); - return; - } - - JsonArray substitutions = data.get("Substitutions").getAsJsonArray(); - try { - List ignoreList = new ArrayList<>(); - for (JsonElement substitutionEl : substitutions) { - JsonObject substitution = substitutionEl.getAsJsonObject(); - - String str_date = substitution.get("OrgDate").getAsString(); - Date lessonDate = Date.fromY_m_d(str_date); - - Time startTime = Time.getNow(); - JsonElement lessonNo; - if (!((lessonNo = substitution.get("OrgLessonNo")) instanceof JsonNull)) { - Pair timePair = lessonRanges.get(lessonNo.getAsInt()); - if (timePair != null) - startTime = timePair.first; - } - - JsonElement isShifted; - JsonElement isCancelled; - if ((isShifted = substitution.get("IsShifted")) != null && isShifted.getAsBoolean()) { - // a lesson is shifted - // add a TYPE_CANCELLED for the source lesson and a TYPE_CHANGE for the destination lesson - - // source lesson: cancel - LessonChange lessonCancelled = new LessonChange(profileId, lessonDate, startTime, startTime.clone().stepForward(0, 45, 0)); - lessonCancelled.type = TYPE_CANCELLED; - lessonChangeList.add(lessonCancelled); - metadataList.add(new Metadata(profileId, Metadata.TYPE_LESSON_CHANGE, lessonCancelled.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - - // target lesson: change - startTime = Time.getNow(); - if (!((lessonNo = substitution.get("LessonNo")) instanceof JsonNull)) { - Pair timePair = lessonRanges.get(lessonNo.getAsInt()); - if (timePair != null) - startTime = timePair.first; - } - - LessonChange lessonChanged = new LessonChange(profileId, lessonDate, startTime, startTime.clone().stepForward(0, 45, 0)); - lessonChanged.type = TYPE_CHANGE; - JsonElement subject; - if ((subject = substitution.get("Subject")) != null) { - lessonChanged.subjectId = subject.getAsJsonObject().get("Id").getAsLong(); - } - JsonElement teacher; - if ((teacher = substitution.get("Teacher")) != null) { - lessonChanged.teacherId = teacher.getAsJsonObject().get("Id").getAsLong(); - } - JsonElement myClass; - if ((myClass = substitution.get("Class")) != null) { - lessonChanged.teamId = myClass.getAsJsonObject().get("Id").getAsLong(); - } - if (myClass == null && (myClass = substitution.get("VirtualClass")) != null) { - lessonChanged.teamId = myClass.getAsJsonObject().get("Id").getAsLong(); - } - lessonChangeList.add(lessonChanged); - metadataList.add(new Metadata(profileId, Metadata.TYPE_LESSON_CHANGE, lessonChanged.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - - // ignore the target lesson in further array elements - it's already changed - ignoreList.add(lessonChanged.lessonDate.combineWith(lessonChanged.startTime)); - } - else if ((isCancelled = substitution.get("IsCancelled")) != null && isCancelled.getAsBoolean()) { - LessonChange lessonChange = new LessonChange(profileId, lessonDate, startTime, startTime.clone().stepForward(0, 45, 0)); - // if it's actually a lesson shift - ignore the target lesson cancellation - if (ignoreList.size() > 0 && ignoreList.contains(lessonChange.lessonDate.combineWith(lessonChange.startTime))) - continue; - lessonChange.type = TYPE_CANCELLED; - lessonChangeList.add(lessonChange); - metadataList.add(new Metadata(profileId, Metadata.TYPE_LESSON_CHANGE, lessonChange.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - else { - LessonChange lessonChange = new LessonChange(profileId, lessonDate, startTime, startTime.clone().stepForward(0, 45, 0)); - - lessonChange.type = TYPE_CHANGE; - - JsonElement subject; - if ((subject = substitution.get("Subject")) != null) { - lessonChange.subjectId = subject.getAsJsonObject().get("Id").getAsLong(); - } - JsonElement teacher; - if ((teacher = substitution.get("Teacher")) != null) { - lessonChange.teacherId = teacher.getAsJsonObject().get("Id").getAsLong(); - } - - JsonElement myClass; - if ((myClass = substitution.get("Class")) != null) { - lessonChange.teamId = myClass.getAsJsonObject().get("Id").getAsLong(); - } - if (myClass == null && (myClass = substitution.get("VirtualClass")) != null) { - lessonChange.teamId = myClass.getAsJsonObject().get("Id").getAsLong(); - } - - lessonChangeList.add(lessonChange); - metadataList.add(new Metadata(profileId, Metadata.TYPE_LESSON_CHANGE, lessonChange.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - - } - r("finish", "Substitutions"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1822, CODE_OTHER, e, data)); - } - }); - } - - private SparseIntArray colors = new SparseIntArray(); - private void getColors() { - colors.put( 1, 0xFFF0E68C); - colors.put( 2, 0xFF87CEFA); - colors.put( 3, 0xFFB0C4DE); - colors.put( 4, 0xFFF0F8FF); - colors.put( 5, 0xFFF0FFFF); - colors.put( 6, 0xFFF5F5DC); - colors.put( 7, 0xFFFFEBCD); - colors.put( 8, 0xFFFFF8DC); - colors.put( 9, 0xFFA9A9A9); - colors.put(10, 0xFFBDB76B); - colors.put(11, 0xFF8FBC8F); - colors.put(12, 0xFFDCDCDC); - colors.put(13, 0xFFDAA520); - colors.put(14, 0xFFE6E6FA); - colors.put(15, 0xFFFFA07A); - colors.put(16, 0xFF32CD32); - colors.put(17, 0xFF66CDAA); - colors.put(18, 0xFF66CDAA); - colors.put(19, 0xFFC0C0C0); - colors.put(20, 0xFFD2B48C); - colors.put(21, 0xFF3333FF); - colors.put(22, 0xFF7B68EE); - colors.put(23, 0xFFBA55D3); - colors.put(24, 0xFFFFB6C1); - colors.put(25, 0xFFFF1493); - colors.put(26, 0xFFDC143C); - colors.put(27, 0xFFFF0000); - colors.put(28, 0xFFFF8C00); - colors.put(29, 0xFFFFD700); - colors.put(30, 0xFFADFF2F); - colors.put(31, 0xFF7CFC00); - r("finish", "Colors"); - /* - apiRequest("Colors", data -> { - JsonArray jColors = data.get("Colors").getAsJsonArray(); - d("Got Colors: "+jColors.toString()); - colors.clear(); - try { - for (JsonElement colorEl : jColors) { - JsonObject color = colorEl.getAsJsonObject(); - colors.put(color.get("Id").getAsInt(), Color.parseColor("#"+color.get("RGB").getAsString())); - } - } - catch (Exception e) { - e.printStackTrace(); - } - getGrades(); - });*/ - } - - private boolean gradeCategoryListChanged = false; - private void getSavedGradeCategories() { - gradeCategoryList = app.db.gradeCategoryDao().getAllNow(profileId); - gradeCategoryListChanged = false; - r("finish", "SavedGradeCategories"); - } - - private void saveGradeCategories() { - r("finish", "SaveGradeCategories"); - } - - private void getGradesCategories() { - if (!fullSync && false) { - // cancel every not-full sync; no need to download categories again - // every full sync it'll be enabled to make sure there are no grades - by getUnits - r("finish", "GradesCategories"); - return; - } - // not a full sync. Will get all grade categories. Clear the current list. - //gradeCategoryList.clear(); - - callback.onActionStarted(R.string.sync_action_syncing_grade_categories); - apiRequest("Grades/Categories", data -> { - if (data == null) { - r("finish", "GradesCategories"); - return; - } - JsonArray categories = data.get("Categories").getAsJsonArray(); - enableStandardGrades = categories.size() > 0; - profile.putStudentData("enableStandardGrades", enableStandardGrades); - if (!enableStandardGrades) { - r("finish", "GradesCategories"); - return; - } - gradeCategoryListChanged = true; - //d("Got Grades/Categories: "+categories.toString()); - try { - for (JsonElement categoryEl : categories) { - JsonObject category = categoryEl.getAsJsonObject(); - JsonElement name = category.get("Name"); - JsonElement weight = category.get("Weight"); - JsonElement color = category.get("Color"); - JsonElement countToTheAverage = category.get("CountToTheAverage"); - int colorInt = Color.BLUE; - if (!(color instanceof JsonNull) && color != null) { - colorInt = colors.get(color.getAsJsonObject().get("Id").getAsInt()); - } - boolean countToTheAverageBool = !(countToTheAverage instanceof JsonNull) && countToTheAverage != null && countToTheAverage.getAsBoolean(); - int weightInt = weight instanceof JsonNull || weight == null || !countToTheAverageBool ? 0 : weight.getAsInt(); - int categoryId = category.get("Id").getAsInt(); - gradeCategoryList.add(new GradeCategory( - profileId, - categoryId, - weightInt, - colorInt, - name instanceof JsonNull || name == null ? "" : name.getAsString() - )); - } - r("finish", "GradesCategories"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1954, CODE_OTHER, e, data)); - } - }); - } - - private void getGradesComments() { - callback.onActionStarted(R.string.sync_action_syncing_grade_comments); - apiRequest("Grades/Comments", data -> { - if (data == null) { - r("finish", "GradesComments"); - return; - } - - JsonArray comments = data.get("Comments").getAsJsonArray(); - for (JsonElement commentEl : comments) { - JsonObject comment = commentEl.getAsJsonObject(); - long gradeId = comment.get("Grade").getAsJsonObject().get("Id").getAsLong(); - String text = comment.get("Text").getAsString(); - - for (Grade grade : gradeList) { - if (grade.id == gradeId) { - grade.description = text; - break; - } - } - } - - r("finish", "GradesComments"); - }); - } - - private void getPointGradesCategories() { - if (!fullSync || !enablePointGrades) { - // cancel every not-full sync; no need to download categories again - // or - // if it's a full sync, point grades may have already been disabled in getUnits - r("finish", "PointGradesCategories"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_point_grade_categories); - apiRequest("PointGrades/Categories", data -> { - if (data == null) { - r("finish", "PointGradesCategories"); - return; - } - JsonArray categories = data.get("Categories").getAsJsonArray(); - enablePointGrades = categories.size() > 0; - profile.putStudentData("enablePointGrades", enablePointGrades); - if (!enablePointGrades) { - r("finish", "PointGradesCategories"); - return; - } - gradeCategoryListChanged = true; - //d("Got Grades/Categories: "+categories.toString()); - for (JsonElement categoryEl : categories) { - JsonObject category = categoryEl.getAsJsonObject(); - JsonElement name = category.get("Name"); - JsonElement weight = category.get("Weight"); - JsonElement color = category.get("Color"); - JsonElement countToTheAverage = category.get("CountToTheAverage"); - JsonElement valueFrom = category.get("ValueFrom"); - JsonElement valueTo = category.get("ValueTo"); - int colorInt = Color.BLUE; - if (!(color instanceof JsonNull) && color != null) { - colorInt = colors.get(color.getAsJsonObject().get("Id").getAsInt()); - } - boolean countToTheAverageBool = !(countToTheAverage instanceof JsonNull) && countToTheAverage != null && countToTheAverage.getAsBoolean(); - int weightInt = weight instanceof JsonNull || weight == null || !countToTheAverageBool ? 0 : weight.getAsInt(); - int categoryId = category.get("Id").getAsInt(); - float valueFromFloat = valueFrom.getAsFloat(); - float valueToFloat = valueTo.getAsFloat(); - gradeCategoryList.add( - new GradeCategory( - profileId, - categoryId, - weightInt, - colorInt, - name instanceof JsonNull || name == null ? "" : name.getAsString() - ).setValueRange(valueFromFloat, valueToFloat) - ); - } - r("finish", "PointGradesCategories"); - }); - } - - private void getDescriptiveGradesSkills() { - if (!fullSync || !enableDescriptiveGrades) { - // cancel every not-full sync; no need to download categories again - // or - // if it's a full sync, descriptive grades may have already been disabled in getUnits - r("finish", "DescriptiveGradesCategories"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_descriptive_grade_categories); - apiRequest("DescriptiveTextGrades/Skills", data -> { - if (data == null) { - r("finish", "DescriptiveGradesCategories"); - return; - } - JsonArray categories = data.get("Skills").getAsJsonArray(); - enableDescriptiveGrades = categories.size() > 0; - profile.putStudentData("enableDescriptiveGrades", enableDescriptiveGrades); - if (!enableDescriptiveGrades) { - r("finish", "DescriptiveGradesCategories"); - return; - } - gradeCategoryListChanged = true; - //d("Got Grades/Categories: "+categories.toString()); - for (JsonElement categoryEl : categories) { - JsonObject category = categoryEl.getAsJsonObject(); - JsonElement name = category.get("Name"); - JsonElement color = category.get("Color"); - int colorInt = Color.BLUE; - if (!(color instanceof JsonNull) && color != null) { - colorInt = colors.get(color.getAsJsonObject().get("Id").getAsInt()); - } - int weightInt = -1; - int categoryId = category.get("Id").getAsInt(); - gradeCategoryList.add(new GradeCategory( - profileId, - categoryId, - weightInt, - colorInt, - name instanceof JsonNull || name == null ? "" : name.getAsString() - )); - } - r("finish", "DescriptiveGradesCategories"); - }); - } - - private void getTextGradesCategories() { - if (!fullSync || !enableTextGrades) { - // cancel every not-full sync; no need to download categories again - // or - // if it's a full sync, text grades may have already been disabled in getUnits - r("finish", "TextGradesCategories"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_descriptive_grade_categories); - apiRequest("TextGrades/Categories", data -> { - if (data == null) { - r("finish", "TextGradesCategories"); - return; - } - JsonArray categories = data.get("Categories").getAsJsonArray(); - enableTextGrades = categories.size() > 0; - profile.putStudentData("enableTextGrades", enableTextGrades); - if (!enableTextGrades) { - r("finish", "TextGradesCategories"); - return; - } - gradeCategoryListChanged = true; - //d("Got Grades/Categories: "+categories.toString()); - for (JsonElement categoryEl : categories) { - JsonObject category = categoryEl.getAsJsonObject(); - JsonElement name = category.get("Name"); - JsonElement color = category.get("Color"); - int colorInt = Color.BLUE; - if (!(color instanceof JsonNull) && color != null) { - colorInt = colors.get(color.getAsJsonObject().get("Id").getAsInt()); - } - int weightInt = -1; - int categoryId = category.get("Id").getAsInt(); - gradeCategoryList.add(new GradeCategory( - profileId, - categoryId, - weightInt, - colorInt, - name instanceof JsonNull || name == null ? "" : name.getAsString() - )); - } - r("finish", "TextGradesCategories"); - }); - } - - private void getBehaviourGradesCategories() { - if (!fullSync || !enableBehaviourGrades) { - // cancel every not-full sync; no need to download categories again - // or - // if it's a full sync, descriptive grades may have already been disabled in getUnits - r("finish", "BehaviourGradesCategories"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_behaviour_grade_categories); - apiRequest("BehaviourGrades/Points/Categories", data -> { - if (data == null) { - r("finish", "BehaviourGradesCategories"); - return; - } - JsonArray categories = data.get("Categories").getAsJsonArray(); - enableBehaviourGrades = categories.size() > 0; - profile.putStudentData("enableBehaviourGrades", enableBehaviourGrades); - if (!enableBehaviourGrades) { - r("finish", "BehaviourGradesCategories"); - return; - } - gradeCategoryListChanged = true; - //d("Got Grades/Categories: "+categories.toString()); - for (JsonElement categoryEl : categories) { - JsonObject category = categoryEl.getAsJsonObject(); - JsonElement name = category.get("Name"); - JsonElement valueFrom = category.get("ValueFrom"); - JsonElement valueTo = category.get("ValueTo"); - int colorInt = Color.BLUE; - int categoryId = category.get("Id").getAsInt(); - float valueFromFloat = valueFrom.getAsFloat(); - float valueToFloat = valueTo.getAsFloat(); - gradeCategoryList.add( - new GradeCategory( - profileId, - categoryId, - -1, - colorInt, - name instanceof JsonNull || name == null ? "" : name.getAsString() - ).setValueRange(valueFromFloat, valueToFloat) - ); - } - r("finish", "BehaviourGradesCategories"); - }); - } - - private void getGrades() { - d(TAG, "Grades settings: "+enableStandardGrades+", "+enablePointGrades+", "+enableDescriptiveGrades); - if (!enableStandardGrades && false) { - // cancel only if grades have been disabled before - // TODO do not cancel. this does not show any grades until a full sync happens. wtf - // in KOTLIN api, maybe this will be forced when user synchronises this feature exclusively - r("finish", "Grades"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_grades); - apiRequest("Grades", data -> { - if (data == null) { - r("finish", "Grades"); - return; - } - JsonArray grades = data.get("Grades").getAsJsonArray(); - enableStandardGrades = grades.size() > 0; - profile.putStudentData("enableStandardGrades", enableStandardGrades); - if (!enableStandardGrades) { - r("finish", "Grades"); - return; - } - //d("Got Grades: "+grades.toString()); - for (JsonElement gradeEl : grades) { - JsonObject grade = gradeEl.getAsJsonObject(); - long id = grade.get("Id").getAsLong(); - long teacherId = grade.get("AddedBy").getAsJsonObject().get("Id").getAsLong(); - int semester = grade.get("Semester").getAsInt(); - long categoryId = grade.get("Category").getAsJsonObject().get("Id").getAsLong(); - long subjectId = grade.get("Subject").getAsJsonObject().get("Id").getAsLong(); - String name = grade.get("Grade").getAsString(); - float value = getGradeValue(name); - - String str_date = grade.get("AddDate").getAsString(); - long addedDate = Date.fromIso(str_date); - - float weight = 0.0f; - String category = ""; - int color = -1; - GradeCategory gradeCategory = GradeCategory.search(gradeCategoryList, categoryId); - if (gradeCategory != null) { - weight = gradeCategory.weight; - category = gradeCategory.text; - color = gradeCategory.color; - } - - if (name.equals("-") || name.equals("+") || name.equalsIgnoreCase("np") || name.equalsIgnoreCase("bz")) { - // fix for + and - grades that lower the average - weight = 0; - } - - Grade gradeObject = new Grade( - profileId, - id, - category, - color, - "", - name, - value, - weight, - semester, - teacherId, - subjectId - ); - - if (grade.get("IsConstituent").getAsBoolean()) { - // normal grade - gradeObject.type = TYPE_NORMAL; - } - if (grade.get("IsSemester").getAsBoolean()) { - // semester final - gradeObject.type = (gradeObject.semester == 1 ? TYPE_SEMESTER1_FINAL : TYPE_SEMESTER2_FINAL); - } - else if (grade.get("IsSemesterProposition").getAsBoolean()) { - // semester proposed - gradeObject.type = (gradeObject.semester == 1 ? TYPE_SEMESTER1_PROPOSED : TYPE_SEMESTER2_PROPOSED); - } - else if (grade.get("IsFinal").getAsBoolean()) { - // year final - gradeObject.type = TYPE_YEAR_FINAL; - } - else if (grade.get("IsFinalProposition").getAsBoolean()) { - // year final - gradeObject.type = TYPE_YEAR_PROPOSED; - } - - JsonElement historyEl = grade.get("Improvement"); - if (historyEl != null) { - JsonObject history = historyEl.getAsJsonObject(); - long historicalId = history.get("Id").getAsLong(); - for (Grade historicalGrade: gradeList) { - if (historicalGrade.id != historicalId) - continue; - // historicalGrade - historicalGrade.parentId = gradeObject.id; - if (historicalGrade.name.equals("nb")) { - historicalGrade.weight = 0; - } - break; - } - gradeObject.isImprovement = true; - } - - /*if (RegisterGradeCategory.getById(app.register, registerGrade.categoryId, -1) == null) { - getGradesCategories = true; - }*/ - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - r("finish", "Grades"); - }); - } - - private void getPointGrades() { - d(TAG, "Grades settings: "+enableStandardGrades+", "+enablePointGrades+", "+enableDescriptiveGrades); - if (!enablePointGrades) { - // cancel only if grades have been disabled before - r("finish", "PointGrades"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_point_grades); - apiRequest("PointGrades", data -> { - if (data == null) { - r("finish", "PointGrades"); - return; - } - JsonArray grades = data.get("Grades").getAsJsonArray(); - enablePointGrades = grades.size() > 0; - profile.putStudentData("enablePointGrades", enablePointGrades); - if (!enablePointGrades) { - r("finish", "PointGrades"); - return; - } - //d("Got Grades: "+grades.toString()); - for (JsonElement gradeEl : grades) { - JsonObject grade = gradeEl.getAsJsonObject(); - long id = grade.get("Id").getAsLong(); - long teacherId = grade.get("AddedBy").getAsJsonObject().get("Id").getAsLong(); - int semester = grade.get("Semester").getAsInt(); - long categoryId = grade.get("Category").getAsJsonObject().get("Id").getAsLong(); - long subjectId = grade.get("Subject").getAsJsonObject().get("Id").getAsLong(); - String name = grade.get("Grade").getAsString(); - float originalValue = grade.get("GradeValue").getAsFloat(); - - String str_date = grade.get("AddDate").getAsString(); - long addedDate = Date.fromIso(str_date); - - float weight = 0.0f; - String category = ""; - int color = -1; - float value = 0.0f; - float maxPoints = 0.0f; - GradeCategory gradeCategory = GradeCategory.search(gradeCategoryList, categoryId); - if (gradeCategory != null) { - weight = gradeCategory.weight; - category = gradeCategory.text; - color = gradeCategory.color; - maxPoints = gradeCategory.valueTo; - value = originalValue; - } - - Grade gradeObject = new Grade( - profileId, - id, - category, - color, - "", - name, - value, - weight, - semester, - teacherId, - subjectId - ); - gradeObject.type = Grade.TYPE_POINT; - gradeObject.valueMax = maxPoints; - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - r("finish", "PointGrades"); - }); - } - - private void getDescriptiveGrades() { - d(TAG, "Grades settings: "+enableStandardGrades+", "+enablePointGrades+", "+enableDescriptiveGrades); - if (!enableDescriptiveGrades && !enableTextGrades) { - // cancel only if grades have been disabled before - r("finish", "DescriptiveGrades"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_descriptive_grades); - apiRequest("BaseTextGrades", data -> { - if (data == null) { - r("finish", "DescriptiveGrades"); - return; - } - JsonArray grades = data.get("Grades").getAsJsonArray(); - int descriptiveGradesCount = 0; - int textGradesCount = 0; - //d("Got Grades: "+grades.toString()); - for (JsonElement gradeEl : grades) { - JsonObject grade = gradeEl.getAsJsonObject(); - long id = grade.get("Id").getAsLong(); - long teacherId = grade.get("AddedBy").getAsJsonObject().get("Id").getAsLong(); - int semester = grade.get("Semester").getAsInt(); - long subjectId = grade.get("Subject").getAsJsonObject().get("Id").getAsLong(); - String description = grade.get("Grade").getAsString(); - - long categoryId = -1; - JsonElement categoryEl = grade.get("Category"); - JsonElement skillEl = grade.get("Skill"); - if (categoryEl != null) { - categoryId = categoryEl.getAsJsonObject().get("Id").getAsLong(); - textGradesCount++; - } - if (skillEl != null) { - categoryId = skillEl.getAsJsonObject().get("Id").getAsLong(); - descriptiveGradesCount++; - } - - String str_date = grade.get("AddDate").getAsString(); - long addedDate = Date.fromIso(str_date); - - String category = ""; - int color = -1; - GradeCategory gradeCategory = GradeCategory.search(gradeCategoryList, categoryId); - if (gradeCategory != null) { - category = gradeCategory.text; - color = gradeCategory.color; - } - - Grade gradeObject = new Grade( - profileId, - id, - category, - color, - description, - " ", - 0.0f, - 0, - semester, - teacherId, - subjectId - ); - gradeObject.type = Grade.TYPE_DESCRIPTIVE; - if (categoryEl != null) { - gradeObject.type = Grade.TYPE_TEXT; - } - if (skillEl != null) { - gradeObject.type = Grade.TYPE_DESCRIPTIVE; - } - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - enableDescriptiveGrades = descriptiveGradesCount > 0; - enableTextGrades = textGradesCount > 0; - profile.putStudentData("enableDescriptiveGrades", enableDescriptiveGrades); - profile.putStudentData("enableTextGrades", enableTextGrades); - r("finish", "DescriptiveGrades"); - }); - } - - private void getTextGrades() { - callback.onActionStarted(R.string.sync_action_syncing_descriptive_grades); - apiRequest("DescriptiveGrades", data -> { - if (data == null) { - r("finish", "TextGrades"); - return; - } - JsonArray grades = data.get("Grades").getAsJsonArray(); - //d("Got Grades: "+grades.toString()); - for (JsonElement gradeEl : grades) { - JsonObject grade = gradeEl.getAsJsonObject(); - long id = grade.get("Id").getAsLong(); - long teacherId = grade.get("AddedBy").getAsJsonObject().get("Id").getAsLong(); - int semester = grade.get("Semester").getAsInt(); - long subjectId = grade.get("Subject").getAsJsonObject().get("Id").getAsLong(); - JsonElement map = grade.get("Map"); - JsonElement realGrade = grade.get("RealGradeValue"); - String description = ""; - if (map != null) { - description = map.getAsString(); - } - else if (realGrade != null) { - description = realGrade.getAsString(); - } - - long categoryId = -1; - JsonElement skillEl = grade.get("Skill"); - if (skillEl != null) { - categoryId = skillEl.getAsJsonObject().get("Id").getAsLong(); - } - - String str_date = grade.get("AddDate").getAsString(); - long addedDate = Date.fromIso(str_date); - - String category = ""; - int color = -1; - GradeCategory gradeCategory = GradeCategory.search(gradeCategoryList, categoryId); - if (gradeCategory != null) { - category = gradeCategory.text; - color = gradeCategory.color; - } - - Grade gradeObject = new Grade( - profileId, - id, - category, - color, - "", - description, - 0.0f, - 0, - semester, - teacherId, - subjectId - ); - gradeObject.type = Grade.TYPE_DESCRIPTIVE; - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - r("finish", "TextGrades"); - }); - } - - private void getBehaviourGrades() { - d(TAG, "Grades settings: "+enableStandardGrades+", "+enablePointGrades+", "+enableDescriptiveGrades); - if (!enableBehaviourGrades) { - // cancel only if grades have been disabled before - r("finish", "BehaviourGrades"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_behaviour_grades); - apiRequest("BehaviourGrades/Points", data -> { - if (data == null) { - r("finish", "BehaviourGrades"); - return; - } - JsonArray grades = data.get("Grades").getAsJsonArray(); - enableBehaviourGrades = grades.size() > 0; - profile.putStudentData("enableBehaviourGrades", enableBehaviourGrades); - if (!enableBehaviourGrades) { - r("finish", "BehaviourGrades"); - return; - } - //d("Got Grades: "+grades.toString()); - DecimalFormat nameFormat = new DecimalFormat("#.##"); - - Grade gradeStartSemester1 = new Grade( - profileId, - -1, - app.getString(R.string.grade_start_points), - 0xffbdbdbd, - app.getString(R.string.grade_start_points_format, 1), - nameFormat.format(startPointsSemester1), - startPointsSemester1, - -1, - 1, - -1, - 1 - ); - gradeStartSemester1.type = Grade.TYPE_BEHAVIOUR; - Grade gradeStartSemester2 = new Grade( - profileId, - -2, - app.getString(R.string.grade_start_points), - 0xffbdbdbd, - app.getString(R.string.grade_start_points_format, 2), - nameFormat.format(startPointsSemester2), - startPointsSemester2, - -1, - 2, - -1, - 1 - ); - gradeStartSemester2.type = Grade.TYPE_BEHAVIOUR; - - gradeList.add(gradeStartSemester1); - gradeList.add(gradeStartSemester2); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, -1, true, true, profile.getSemesterStart(1).getInMillis())); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, -2, true, true, profile.getSemesterStart(2).getInMillis())); - - for (JsonElement gradeEl : grades) { - JsonObject grade = gradeEl.getAsJsonObject(); - long id = grade.get("Id").getAsLong(); - long teacherId = grade.get("AddedBy").getAsJsonObject().get("Id").getAsLong(); - int semester = grade.get("Semester").getAsInt(); - long categoryId = grade.get("Category").getAsJsonObject().get("Id").getAsLong(); - long subjectId = 1; - - float value = 0.0f; - String name = "?"; - JsonElement nameValue; - if ((nameValue = grade.get("Value")) != null) { - value = nameValue.getAsFloat(); - name = value < 0 ? nameFormat.format(value) : "+"+nameFormat.format(value); - } - else if ((nameValue = grade.get("ShortName")) != null) { - name = nameValue.getAsString(); - } - - String str_date = grade.get("AddDate").getAsString(); - long addedDate = Date.fromIso(str_date); - - int color = value > 0 ? 16 : value < 0 ? 26 : 12; - color = colors.get(color); - - String category = ""; - float maxPoints = 0.0f; - GradeCategory gradeCategory = GradeCategory.search(gradeCategoryList, categoryId); - if (gradeCategory != null) { - category = gradeCategory.text; - maxPoints = gradeCategory.valueTo; - } - - Grade gradeObject = new Grade( - profileId, - id, - category, - color, - "", - name, - value, - -1, - semester, - teacherId, - subjectId - ); - gradeObject.type = Grade.TYPE_BEHAVIOUR; - gradeObject.valueMax = maxPoints; - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - r("finish", "BehaviourGrades"); - }); - } - - //private boolean eventTypeListChanged = false; - private void getEvents() { - //eventTypeList = app.db.eventTypeDao().getAllNow(profileId); - // eventTypeListChanged = false; - callback.onActionStarted(R.string.sync_action_syncing_events); - apiRequest("HomeWorks", data -> { - if (data == null) { - r("finish", "Events"); - return; - } - JsonArray events = data.get("HomeWorks").getAsJsonArray(); - //d("Got Grades: "+events.toString()); - boolean getCustomTypes = false; - try { - for (JsonElement eventEl : events) { - JsonObject event = eventEl.getAsJsonObject(); - - JsonElement el; - JsonObject obj; - - long id = event.get("Id").getAsLong(); - long teacherId = -1; - long subjectId = -1; - int type = -1; - - if ((el = event.get("CreatedBy")) != null - && (obj = el.getAsJsonObject()) != null - && (el = obj.get("Id")) != null) { - teacherId = el.getAsLong(); - } - if ((el = event.get("Subject")) != null - && (obj = el.getAsJsonObject()) != null - && (el = obj.get("Id")) != null) { - subjectId = el.getAsLong(); - } - String topic = event.get("Content").getAsString(); - - if ((el = event.get("Category")) != null - && (obj = el.getAsJsonObject()) != null - && (el = obj.get("Id")) != null) { - type = el.getAsInt(); - } - /*EventType typeObject = app.db.eventTypeDao().getByIdNow(profileId, type); - if (typeObject == null) { - getCustomTypes = true; - }*/ - - JsonElement myClass = event.get("Class"); - long teamId = myClass == null ? -1 : myClass.getAsJsonObject().get("Id").getAsLong(); - - String str_date = event.get("AddDate").getAsString(); - long addedDate = Date.fromIso(str_date); - - str_date = event.get("Date").getAsString(); - Date eventDate = Date.fromY_m_d(str_date); - - - Time startTime = null; - JsonElement lessonNo; - JsonElement timeFrom; - if (!((lessonNo = event.get("LessonNo")) instanceof JsonNull)) { - Pair timePair = lessonRanges.get(lessonNo.getAsInt()); - if(timePair != null) - startTime = timePair.first; - } - if (startTime == null && !((timeFrom = event.get("TimeFrom")) instanceof JsonNull)) { - startTime = Time.fromH_m(timeFrom.getAsString()); - } - - Event eventObject = new Event( - profileId, - id, - eventDate, - startTime, - topic, - -1, - type, - false, - teacherId, - subjectId, - teamId - ); - - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_EVENT, eventObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - r("finish", "Events"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 2541, CODE_OTHER, e, data)); - } - }); - } - - private void getCustomTypes() { - if (!fullSync) { - r("finish", "CustomTypes"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_event_categories); - apiRequest("HomeWorks/Categories", data -> { - if (data == null) { - r("finish", "CustomTypes"); - return; - } - JsonArray jCategories = data.get("Categories").getAsJsonArray(); - //d("Got Classrooms: "+jClassrooms.toString()); - try { - for (JsonElement categoryEl : jCategories) { - JsonObject category = categoryEl.getAsJsonObject(); - eventTypeList.add(new EventType(profileId, category.get("Id").getAsInt(), category.get("Name").getAsString(), colors.get(category.get("Color").getAsJsonObject().get("Id").getAsInt()))); - } - r("finish", "CustomTypes"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 2573, CODE_OTHER, e, data)); - } - }); - } - - private void getHomework() { - if (!premium) { - r("finish", "Homework"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_homework); - apiRequest("HomeWorkAssignments", data -> { - if (data == null) { - r("finish", "Homework"); - return; - } - JsonArray homeworkList = data.get("HomeWorkAssignments").getAsJsonArray(); - //d("Got Grades: "+events.toString()); - try { - for (JsonElement homeworkEl : homeworkList) { - JsonObject homework = homeworkEl.getAsJsonObject(); - - JsonElement el; - JsonObject obj; - - long id = homework.get("Id").getAsLong(); - long teacherId = -1; - long subjectId = -1; - - if ((el = homework.get("Teacher")) != null - && (obj = el.getAsJsonObject()) != null - && (el = obj.get("Id")) != null) { - teacherId = el.getAsLong(); - } - - String topic = ""; - try { - topic = homework.get("Topic").getAsString() + "\n"; - topic += homework.get("Text").getAsString(); - } - catch (Exception e) { - e.printStackTrace(); - } - - String str_date = homework.get("Date").getAsString(); - Date addedDate = Date.fromY_m_d(str_date); - - str_date = homework.get("DueDate").getAsString(); - Date eventDate = Date.fromY_m_d(str_date); - - - Time startTime = null; - - Event eventObject = new Event( - profileId, - id, - eventDate, - startTime, - topic, - -1, - -1, - false, - teacherId, - subjectId, - -1 - ); - - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_HOMEWORK, eventObject.id, profile.getEmpty(), profile.getEmpty(), addedDate.getInMillis())); - } - r("finish", "Homework"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 2648, CODE_OTHER, e, data)); - } - }); - } - - private void getLuckyNumbers() { - if (!profile.getLuckyNumberEnabled() || (profile.getLuckyNumberDate() != null && profile.getLuckyNumberDate().getValue() == Date.getToday().getValue())) { - r("finish", "LuckyNumbers"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_lucky_number); - apiRequest("LuckyNumbers", data -> { - if (data == null) { - profile.setLuckyNumberEnabled(false); - } - else { - profile.setLuckyNumber(-1); - profile.setLuckyNumberDate(Date.getToday()); - try { - JsonElement luckyNumberEl = data.get("LuckyNumber"); - if (luckyNumberEl != null) { - JsonObject luckyNumber = luckyNumberEl.getAsJsonObject(); - profile.setLuckyNumber(luckyNumber.get("LuckyNumber").getAsInt()); - profile.setLuckyNumberDate(Date.fromY_m_d(luckyNumber.get("LuckyNumberDay").getAsString())); - } - } catch (Exception e) { - finishWithError(new AppError(TAG, 2678, CODE_OTHER, e, data)); - } - finally { - app.db.luckyNumberDao().add(new LuckyNumber(profileId, profile.getLuckyNumberDate(), profile.getLuckyNumber())); - } - } - r("finish", "LuckyNumbers"); - }); - } - - private void getNotices() { - callback.onActionStarted(R.string.sync_action_syncing_notices); - apiRequest("Notes", data -> { - if (data == null) { - r("finish", "Notices"); - return; - } - try { - JsonArray jNotices = data.get("Notes").getAsJsonArray(); - - for (JsonElement noticeEl : jNotices) { - JsonObject notice = noticeEl.getAsJsonObject(); - - int type = notice.get("Positive").getAsInt(); - switch (type) { - case 0: - type = TYPE_NEGATIVE; - break; - case 1: - type = TYPE_POSITIVE; - break; - case 2: - type = TYPE_NEUTRAL; - break; - } - - long id = notice.get("Id").getAsLong(); - - Date addedDate = Date.fromY_m_d(notice.get("Date").getAsString()); - - int semester = profile.dateToSemester(addedDate); - - JsonElement el; - JsonObject obj; - long teacherId = -1; - if ((el = notice.get("Teacher")) != null - && (obj = el.getAsJsonObject()) != null - && (el = obj.get("Id")) != null) { - teacherId = el.getAsLong(); - } - - Notice noticeObject = new Notice( - profileId, - id, - notice.get("Text").getAsString(), - semester, - type, - teacherId - ); - - noticeList.add(noticeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_NOTICE, noticeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate.getInMillis())); - } - r("finish", "Notices"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 2750, CODE_OTHER, e, data)); - } - }); - } - - private SparseArray> attendanceTypes = new SparseArray<>(); - private void getAttendanceTypes() { - callback.onActionStarted(R.string.sync_action_syncing_attendance_types); - apiRequest("Attendances/Types", data -> { - if (data == null) { - r("finish", "AttendanceTypes"); - return; - } - try { - JsonArray jTypes = data.get("Types").getAsJsonArray(); - for (JsonElement typeEl : jTypes) { - JsonObject type = typeEl.getAsJsonObject(); - int id = type.get("Id").getAsInt(); - attendanceTypes.put(id, - new Pair<>( - type.get("Standard").getAsBoolean() ? id : type.getAsJsonObject("StandardType").get("Id").getAsInt(), - type.get("Name").getAsString() - ) - ); - } - r("finish", "AttendanceTypes"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 2782, CODE_OTHER, e, data)); - } - }); - } - - private void getAttendance() { - callback.onActionStarted(R.string.sync_action_syncing_attendance); - apiRequest("Attendances"+(fullSync ? "" : "?dateFrom="+ Date.getToday().stepForward(0, -1, 0).getStringY_m_d()), data -> { - if (data == null) { - r("finish", "Attendance"); - return; - } - - try { - JsonArray jAttendance = data.get("Attendances").getAsJsonArray(); - - for (JsonElement attendanceEl : jAttendance) { - JsonObject attendance = attendanceEl.getAsJsonObject(); - - int type = attendance.getAsJsonObject("Type").get("Id").getAsInt(); - Pair attendanceType; - if ((attendanceType = attendanceTypes.get(type)) != null) { - type = attendanceType.first; - } - switch (type) { - case 1: - type = TYPE_ABSENT; - break; - case 2: - type = TYPE_BELATED; - break; - case 3: - type = TYPE_ABSENT_EXCUSED; - break; - case 4: - type = TYPE_RELEASED; - break; - default: - case 100: - type = TYPE_PRESENT; - break; - } - - String idStr = attendance.get("Id").getAsString(); - int id = strToInt(idStr.replaceAll("[^\\d.]", "")); - - long addedDate = Date.fromIso(attendance.get("AddDate").getAsString()); - - Time startTime = Time.getNow(); - Pair timePair = lessonRanges.get(attendance.get("LessonNo").getAsInt()); - if (timePair != null) - startTime = timePair.first; - Date lessonDate = Date.fromY_m_d(attendance.get("Date").getAsString()); - int lessonWeekDay = lessonDate.getWeekDay(); - long subjectId = -1; - String topic = ""; - if (attendanceType != null) { - topic = attendanceType.second; - } - - for (Lesson lesson: lessonList) { - if (lesson.weekDay == lessonWeekDay && lesson.startTime.getValue() == startTime.getValue()) { - subjectId = lesson.subjectId; - } - } - - Attendance attendanceObject = new Attendance( - profileId, - id, - attendance.getAsJsonObject("AddedBy").get("Id").getAsLong(), - subjectId, - attendance.get("Semester").getAsInt(), - topic, - lessonDate, - startTime, - type); - - attendanceList.add(attendanceObject); - if (attendanceObject.type != TYPE_PRESENT) { - metadataList.add(new Metadata(profileId, Metadata.TYPE_ATTENDANCE, attendanceObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - } - r("finish", "Attendance"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 2872, CODE_OTHER, e, data)); - } - }); - } - - private void getAnnouncements() { - callback.onActionStarted(R.string.sync_action_syncing_announcements); - apiRequest("SchoolNotices", data -> { - if (data == null) { - r("finish", "Announcements"); - return; - } - try { - JsonArray jAnnouncements = data.get("SchoolNotices").getAsJsonArray(); - - for (JsonElement announcementEl : jAnnouncements) { - JsonObject announcement = announcementEl.getAsJsonObject(); - - String idStr = announcement.get("Id").getAsString(); - long id = crc16(idStr.getBytes()); - - long addedDate = Date.fromIso(announcement.get("CreationDate").getAsString()); - - boolean read = announcement.get("WasRead").getAsBoolean(); - - String subject = ""; - String text = ""; - Date startDate = null; - Date endDate = null; - try { - subject = announcement.get("Subject").getAsString(); - text = announcement.get("Content").getAsString(); - startDate = Date.fromY_m_d(announcement.get("StartDate").getAsString()); - endDate = Date.fromY_m_d(announcement.get("EndDate").getAsString()); - } - catch (Exception e) { - e.printStackTrace(); - } - - JsonElement el; - JsonObject obj; - long teacherId = -1; - if ((el = announcement.get("AddedBy")) != null - && (obj = el.getAsJsonObject()) != null - && (el = obj.get("Id")) != null) { - teacherId = el.getAsLong(); - } - - Announcement announcementObject = new Announcement( - profileId, - id, - subject, - text, - startDate, - endDate, - teacherId - ); - - announcementList.add(announcementObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_ANNOUNCEMENT, announcementObject.id, read, read, addedDate)); - } - r("finish", "Announcements"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 2944, CODE_OTHER, e, data)); - } - }); - } - - private void getPtMeetings() { - if (!fullSync) { - r("finish", "PtMeetings"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_pt_meetings); - apiRequest("ParentTeacherConferences", data -> { - if (data == null) { - r("finish", "PtMeetings"); - return; - } - try { - JsonArray jMeetings = data.get("ParentTeacherConferences").getAsJsonArray(); - for (JsonElement meetingEl: jMeetings) { - JsonObject meeting = meetingEl.getAsJsonObject(); - - long id = meeting.get("Id").getAsLong(); - Event eventObject = new Event( - profileId, - id, - Date.fromY_m_d(meeting.get("Date").getAsString()), - Time.fromH_m(meeting.get("Time").getAsString()), - meeting.get("Topic").getAsString(), - -1, - TYPE_PT_MEETING, - false, - meeting.getAsJsonObject("Teacher").get("Id").getAsLong(), - -1, - teamClassId - ); - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_EVENT, eventObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - r("finish", "PtMeetings"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 2996, CODE_OTHER, e, data)); - } - }); - } - - private SparseArray teacherFreeDaysTypes = new SparseArray<>(); - private void getTeacherFreeDaysTypes() { - if (!fullSync) { - r("finish", "TeacherFreeDaysTypes"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_teacher_free_days_types); - apiRequest("TeacherFreeDays/Types", data -> { - if (data == null) { - r("finish", "TeacherFreeDays"); - return; - } - try { - JsonArray jTypes = data.get("Types").getAsJsonArray(); - for (JsonElement typeEl : jTypes) { - JsonObject type = typeEl.getAsJsonObject(); - int id = type.get("Id").getAsInt(); - teacherFreeDaysTypes.put(id, type.get("Name").getAsString()); - } - r("finish", "TeacherFreeDaysTypes"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 3019, CODE_OTHER, e, data)); - } - }); - } - - private void getTeacherFreeDays() { - callback.onActionStarted(R.string.sync_action_syncing_teacher_free_days); - apiRequest("TeacherFreeDays", data -> { - if (data == null) { - r("finish", "TeacherFreeDays"); - return; - } - try { - JsonArray jFreeDays = data.get("TeacherFreeDays").getAsJsonArray(); - for (JsonElement freeDayEl: jFreeDays) { - JsonObject freeDay = freeDayEl.getAsJsonObject(); - - long id = freeDay.get("Id").getAsLong(); - long teacherId = freeDay.getAsJsonObject("Teacher").get("Id").getAsLong(); - - Date dateFrom = Date.fromY_m_d(freeDay.get("DateFrom").getAsString()); - Date dateTo = Date.fromY_m_d(freeDay.get("DateTo").getAsString()); - - Time timeFrom = null; - Time timeTo = null; - - if (freeDay.get("TimeFrom") != null && freeDay.get("TimeTo") != null) { - timeFrom = Time.fromH_m_s(freeDay.get("TimeFrom").getAsString()); - timeTo = Time.fromH_m_s(freeDay.get("TimeTo").getAsString()); - } - - long type = freeDay.getAsJsonObject("Type").get("Id").getAsLong(); - - //String topic = teacherFreeDaysTypes.get(type)+"\n"+(dateFrom.getValue() != dateTo.getValue() ? dateFrom.getFormattedString()+" - "+dateTo.getFormattedString() : ""); - - TeacherAbsence teacherAbsence = new TeacherAbsence( - profileId, - id, - teacherId, - type, - null, - dateFrom, - dateTo, - timeFrom, - timeTo - ); - - teacherAbsenceList.add(teacherAbsence); - metadataList.add( - new Metadata( - profileId, - Metadata.TYPE_TEACHER_ABSENCE, - teacherAbsence.getId(), - true, - true, - System.currentTimeMillis()) - ); - - } - r("finish", "TeacherFreeDays"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 3069, CODE_OTHER, e, data)); - } - }); - } - - private void getSchoolFreeDays() { - callback.onActionStarted(R.string.sync_action_syncing_school_free_days); - apiRequest("SchoolFreeDays" + (unitId != -1 ? "?unit=" + unitId : ""), data -> { - if (data == null) { - r("finish", "SchoolFreeDays"); - return; - } - try { - JsonArray jFreeDays = data.get("SchoolFreeDays").getAsJsonArray(); - - for (JsonElement freeDayEl: jFreeDays) { - continue; - } - r("finish", "SchoolFreeDays"); - } catch (Exception e) { - finishWithError(new AppError(TAG, 3069, CODE_OTHER, e, data)); - } - }); - } - - private void getMessagesLogin() { - if (synergiaPassword == null) { - // skip messages - r("finish", "MessagesOutbox"); - return; - } - loginSynergia(() -> { - r("finish", "MessagesLogin"); - }); - } - - private void getMessagesInbox() { - String body = - "\n" + - "
\n" + - " \n" + - " 0\n" + - " \n" + - ""; - synergiaRequest("Inbox/action/GetList", body, data -> { - try { - long startTime = System.currentTimeMillis(); - - for (Element e: data.select("response GetList data ArrayItem")) { - - long id = Long.parseLong(e.select("messageId").text()); - - String subject = e.select("topic").text(); - - String senderFirstName = e.select("senderFirstName").text(); - String senderLastName = e.select("senderLastName").text(); - - long senderId = -1; - - for (Teacher teacher: teacherList) { - if (teacher.name.equalsIgnoreCase(senderFirstName) && teacher.surname.equalsIgnoreCase(senderLastName)) { - senderId = teacher.id; - break; - } - } - if (senderId == -1) { - Teacher teacher = new Teacher(profileId, -1 * Utils.crc16((senderFirstName+" "+senderLastName).getBytes()), senderFirstName, senderLastName); - senderId = teacher.id; - teacherList.add(teacher); - teacherListChanged = true; - } - - long readDate = 0; - long sentDate; - - String readDateStr = e.select("readDate").text(); - String sentDateStr = e.select("sendDate").text(); - - DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); - if (!readDateStr.isEmpty()) { - readDate = formatter.parse(readDateStr).getTime(); - } - sentDate = formatter.parse(sentDateStr).getTime(); - - Message message = new Message( - profileId, - id, - subject, - null, - TYPE_RECEIVED, - senderId, - -1 - ); - - MessageRecipient messageRecipient = new MessageRecipient( - profileId, - -1 /* me */, - -1, - readDate, - /*messageId*/ id - ); - - if (!e.select("isAnyFileAttached").text().equals("0")) - message.setHasAttachments(); - - messageList.add(message); - messageRecipientList.add(messageRecipient); - messageMetadataList.add(new Metadata(profileId, Metadata.TYPE_MESSAGE, message.id, readDate > 0, readDate > 0 || profile.getEmpty(), sentDate)); - } - - } catch (Exception e3) { - finishWithError(new AppError(TAG, 3164, CODE_OTHER, e3, data.outerHtml())); - return; - } - - r("finish", "MessagesInbox"); - }); - } - - private void getMessagesOutbox() { - if (!fullSync && onlyFeature != FEATURE_MESSAGES_OUTBOX && !profile.getEmpty()) { - // a quick sync and the profile is already synced at least once - r("finish", "MessagesOutbox"); - return; - } - String body = - "\n" + - "
\n" + - " \n" + - " 0\n" + - " \n" + - ""; - synergiaRequest("Outbox/action/GetList", body, data -> { - try { - long startTime = System.currentTimeMillis(); - - for (Element e: data.select("response GetList data ArrayItem")) { - - long id = Long.parseLong(e.select("messageId").text()); - - String subject = e.select("topic").text(); - - String receiverFirstName = e.select("receiverFirstName").text(); - String receiverLastName = e.select("receiverLastName").text(); - - long receiverId = -1; - - for (Teacher teacher: teacherList) { - if (teacher.name.equalsIgnoreCase(receiverFirstName) && teacher.surname.equalsIgnoreCase(receiverLastName)) { - receiverId = teacher.id; - break; - } - } - if (receiverId == -1) { - Teacher teacher = new Teacher(profileId, -1 * Utils.crc16((receiverFirstName+" "+receiverLastName).getBytes()), receiverFirstName, receiverLastName); - receiverId = teacher.id; - teacherList.add(teacher); - teacherListChanged = true; - } - - long sentDate; - - String sentDateStr = e.select("sendDate").text(); - - DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); - sentDate = formatter.parse(sentDateStr).getTime(); - - Message message = new Message( - profileId, - id, - subject, - null, - TYPE_SENT, - -1, - -1 - ); - - MessageRecipient messageRecipient = new MessageRecipient( - profileId, - receiverId, - -1, - -1, - /*messageId*/ id - ); - - if (!e.select("isAnyFileAttached").text().equals("0")) - message.setHasAttachments(); - - messageList.add(message); - messageRecipientIgnoreList.add(messageRecipient); - metadataList.add(new Metadata(profileId, Metadata.TYPE_MESSAGE, message.id, true, true, sentDate)); - } - - } catch (Exception e3) { - finishWithError(new AppError(TAG, 3270, CODE_OTHER, e3, data.outerHtml())); - return; - } - - r("finish", "MessagesOutbox"); - }); - } - - @Override - public Map getConfigurableEndpoints(Profile profile) { - Map configurableEndpoints = new LinkedHashMap<>(); - configurableEndpoints.put("Classrooms", new Endpoint("Classrooms",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("Timetables", new Endpoint("Timetables",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("Substitutions", new Endpoint("Substitutions",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("Grades", new Endpoint("Grades",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("PointGrades", new Endpoint("PointGrades",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("Events", new Endpoint("Events",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("Homework", new Endpoint("Homework",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("LuckyNumbers", new Endpoint("LuckyNumbers",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("Notices", new Endpoint("Notices",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("Attendance", new Endpoint("Attendance",true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("Announcements", new Endpoint("Announcements",true, true, profile.getChangedEndpoints())); - configurableEndpoints.put("PtMeetings", new Endpoint("PtMeetings",true, true, profile.getChangedEndpoints())); - configurableEndpoints.put("TeacherFreeDays", new Endpoint("TeacherFreeDays",false, false, profile.getChangedEndpoints())); - //configurableEndpoints.put("SchoolFreeDays", new Endpoint("SchoolFreeDays",true, true, profile.changedEndpoints)); - //configurableEndpoints.put("ClassFreeDays", new Endpoint("ClassFreeDays",true, true, profile.changedEndpoints)); - configurableEndpoints.put("MessagesInbox", new Endpoint("MessagesInbox", true, false, profile.getChangedEndpoints())); - configurableEndpoints.put("MessagesOutbox", new Endpoint("MessagesOutbox", true, true, profile.getChangedEndpoints())); - return configurableEndpoints; - } - - @Override - public boolean isEndpointEnabled(Profile profile, boolean defaultActive, String name) { - return defaultActive ^ contains(profile.getChangedEndpoints(), name); - } - - @Override - public void syncMessages(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile) - { - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - login(() -> { - Librus.this.profile.setEmpty(true); - targetEndpoints = new ArrayList<>(); - targetEndpoints.add("Users"); - targetEndpoints.add("MessagesLogin"); - targetEndpoints.add("MessagesInbox"); - targetEndpoints.add("MessagesOutbox"); - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - - /* __ __ - | \/ | - | \ / | ___ ___ ___ __ _ __ _ ___ ___ - | |\/| |/ _ \/ __/ __|/ _` |/ _` |/ _ \/ __| - | | | | __/\__ \__ \ (_| | (_| | __/\__ \ - |_| |_|\___||___/___/\__,_|\__, |\___||___/ - __/ | - |__*/ - @Override - public void getMessage(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull MessageFull message, @NonNull MessageGetCallback messageCallback) - { - if (message.body != null) { - boolean readByAll = true; - // load this message's recipient(s) data - message.recipients = app.db.messageRecipientDao().getAllByMessageId(profile.getId(), message.id); - for (MessageRecipientFull recipient: message.recipients) { - if (recipient.id == -1) - recipient.fullName = profile.getStudentNameLong(); - if (message.type == TYPE_SENT && recipient.readDate < 1) - readByAll = false; - } - if (!message.seen) { - app.db.metadataDao().setSeen(profile.getId(), message, true); - } - if (readByAll) { - // if a sent msg is not read by everyone, download it again to check the read status - new Handler(activityContext.getMainLooper()).post(() -> { - messageCallback.onSuccess(message); - }); - return; - } - } - - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - loginSynergia(() -> { - String requestBody = - "\n" + - "
\n" + - " \n" + - " "+message.id+"\n" + - " 0\n" + - " \n" + - ""; - synergiaRequest("GetMessage", requestBody, data -> { - - List messageRecipientList = new ArrayList<>(); - - try { - Element e = data.select("response GetMessage data").first(); - - String body = e.select("Message").text(); - body = new String(Base64.decode(body, Base64.DEFAULT)); - body = body.replaceAll("\n", "
"); - body = body.replaceAll("", ""); - - message.clearAttachments(); - Elements attachments = e.select("attachments ArrayItem"); - if (attachments != null) { - for (Element attachment: attachments) { - message.addAttachment(Long.parseLong(attachment.select("id").text()), attachment.select("filename").text(), -1); - } - } - - message.body = body; - - if (message.type == TYPE_RECEIVED) { - app.db.teacherDao().updateLoginId(profileId, message.senderId, e.select("senderId").text()); - - MessageRecipientFull recipient = new MessageRecipientFull(profileId, -1, message.id); - - long readDate = 0; - String readDateStr = e.select("readDate").text(); - if (!readDateStr.isEmpty()) { - DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); - readDate = formatter.parse(readDateStr).getTime(); - } - recipient.readDate = readDate; - - recipient.fullName = profile.getStudentNameLong(); - messageRecipientList.add(recipient); - - } - else if (message.type == TYPE_SENT) { - List teacherList = app.db.teacherDao().getAllNow(profileId); - for (Element receiver: e.select("receivers ArrayItem")) { - String receiverFirstName = e.select("firstName").text(); - String receiverLastName = e.select("lastName").text(); - - long receiverId = -1; - - for (Teacher teacher: teacherList) { - if (teacher.name.equalsIgnoreCase(receiverFirstName) && teacher.surname.equalsIgnoreCase(receiverLastName)) { - receiverId = teacher.id; - break; - } - } - - app.db.teacherDao().updateLoginId(profileId, receiverId, receiver.select("receiverId").text()); - - MessageRecipientFull recipient = new MessageRecipientFull(profileId, receiverId, message.id); - - long readDate = 0; - String readDateStr = e.select("readed").text(); - if (!readDateStr.isEmpty()) { - DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); - readDate = formatter.parse(readDateStr).getTime(); - } - recipient.readDate = readDate; - - recipient.fullName = receiverFirstName+" "+receiverLastName; - messageRecipientList.add(recipient); - } - } - - } - catch (Exception e) { - finishWithError(new AppError(TAG, 795, CODE_OTHER, e, data.outerHtml())); - return; - } - - if (!message.seen) { - app.db.metadataDao().setSeen(profileId, message, true); - } - app.db.messageDao().add(message); - app.db.messageRecipientDao().addAll((List)(List) messageRecipientList); // not addAllIgnore - - message.recipients = messageRecipientList; - - new Handler(activityContext.getMainLooper()).post(() -> { - messageCallback.onSuccess(message); - }); - }); - }); - } - - @Override - public void getAttachment(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull MessageFull message, long attachmentId, @NonNull AttachmentGetCallback attachmentCallback) { - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - loginSynergia(() -> { - String requestBody = - "\n" + - "
\n" + - " \n" + - " "+attachmentId+"\n" + - " "+message.id+"\n" + - " 0\n" + - " \n" + - ""; - synergiaRequest("GetFileDownloadLink", requestBody, data -> { - String downloadLink = data.select("response GetFileDownloadLink downloadLink").text(); - Matcher keyMatcher = Pattern.compile("singleUseKey=([0-9A-f_]+)").matcher(downloadLink); - if (keyMatcher.find()) { - getAttachmentCheckKeyTries = 0; - getAttachmentCheckKey(keyMatcher.group(1), attachmentCallback); - } - else { - finishWithError(new AppError(TAG, 629, CODE_OTHER, "Błąd pobierania tokenu. Skontaktuj się z twórcą aplikacji.", data.outerHtml())); - } - }); - }); - } - private int getAttachmentCheckKeyTries = 0; - private void getAttachmentCheckKey(String attachmentKey, AttachmentGetCallback attachmentCallback) { - Request.builder() - .url(SYNERGIA_SANDBOX_URL+"CSCheckKey") - .userAgent(synergiaUserAgent) - .addParameter("singleUseKey", attachmentKey) - .post() - .callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - finishWithError(new AppError(TAG, 645, AppError.CODE_MAINTENANCE, response)); - return; - } - try { - String status = data.get("status").getAsString(); - if (status.equals("not_downloaded_yet")) { - if (getAttachmentCheckKeyTries++ > 5) { - finishWithError(new AppError(TAG, 658, CODE_OTHER, "Załącznik niedostępny. Przekroczono czas oczekiwania.", response, data)); - return; - } - new Handler(activityContext.getMainLooper()).postDelayed(() -> { - getAttachmentCheckKey(attachmentKey, attachmentCallback); - }, 2000); - } - else if (status.equals("ready")) { - Request.Builder builder = Request.builder() - .url(SYNERGIA_SANDBOX_URL+"CSDownload&singleUseKey="+attachmentKey); - new Handler(activityContext.getMainLooper()).post(() -> { - attachmentCallback.onSuccess(builder); - }); - } - else { - finishWithError(new AppError(TAG, 667, AppError.CODE_ATTACHMENT_NOT_AVAILABLE, response, data)); - } - } - catch (Exception e) { - finishWithError(new AppError(TAG, 671, AppError.CODE_OTHER, response, e, data)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 677, CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - @Override - public void getRecipientList(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull RecipientListGetCallback recipientListGetCallback) { - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - if (System.currentTimeMillis() - profile.getLastReceiversSync() < 24 * 60 * 60 * 1000) { - AsyncTask.execute(() -> { - List teacherList = app.db.teacherDao().getAllNow(profileId); - new Handler(activityContext.getMainLooper()).post(() -> recipientListGetCallback.onSuccess(teacherList)); - }); - return; - } - - loginSynergia(() -> { - String requestBody = - "\n" + - "
\n" + - " \n" + - " 1\n" + - " \n" + - ""; - synergiaRequest("Receivers/action/GetTypes", requestBody, data -> { - - teacherList = app.db.teacherDao().getAllNow(profileId); - - for (Teacher teacher: teacherList) { - teacher.typeDescription = null; // TODO: 2019-06-13 it better - } - - Elements categories = data.select("response GetTypes data list ArrayItem"); - for (Element category: categories) { - String categoryId = category.select("id").text(); - String categoryName = category.select("name").text(); - Elements categoryList = getRecipientCategory(categoryId); - if (categoryList == null) - return; // the error callback is already executed - for (Element item: categoryList) { - if (item.select("list").size() == 1) { - String className = item.select("label").text(); - Elements list = item.select("list ArrayItem"); - for (Element teacher: list) { - updateTeacher(categoryId, Long.parseLong(teacher.select("id").text()), teacher.select("label").text(), categoryName, className); - } - } - else { - updateTeacher(categoryId, Long.parseLong(item.select("id").text()), item.select("label").text(), categoryName, null); - } - } - } - - app.db.teacherDao().addAll(teacherList); - - profile.setLastReceiversSync(System.currentTimeMillis()); - app.db.profileDao().add(profile); - - new Handler(activityContext.getMainLooper()).post(() -> recipientListGetCallback.onSuccess(new ArrayList<>(teacherList))); - }); - }); - } - private Elements getRecipientCategory(String categoryId) { - Response response = null; - try { - String endpoint = "Receivers/action/GetListForType"; - d(TAG, "Requesting "+SYNERGIA_URL+endpoint); - String body = - "\n" + - "
\n" + - " \n" + - " "+categoryId+"\n" + - " \n" + - ""; - response = Request.builder() - .url(SYNERGIA_URL+endpoint) - .userAgent(synergiaUserAgent) - .setTextBody(body, MediaTypeUtils.APPLICATION_XML) - .build().execute(); - if (response.code() != 200) { - finishWithError(new AppError(TAG, 3569, CODE_OTHER, response)); - return null; - } - String data = new TextCallbackHandler().backgroundParser(response); - if (data.contains("error") || data.contains("")) { - finishWithError(new AppError(TAG, 3556, AppError.CODE_MAINTENANCE, response, data)); - return null; - } - Document doc = Jsoup.parse(data, "", Parser.xmlParser()); - return doc.select("response GetListForType data ArrayItem"); - } catch (Exception e) { - finishWithError(new AppError(TAG, 3562, CODE_OTHER, response, e)); - return null; - } - } - private void updateTeacher(String category, long loginId, String nameLastFirst, String typeDescription, String className) { - nameLastFirst = nameLastFirst.replaceAll("\\s+", " "); - int type = TYPE_OTHER; - String position; - switch (category) { - case "tutors": - type = TYPE_EDUCATOR; - break; - case "teachers": - type = TYPE_TEACHER; - break; - case "pedagogue": - type = TYPE_PEDAGOGUE; - break; - case "librarian": - type = TYPE_LIBRARIAN; - break; - case "admin": - type = TYPE_SCHOOL_ADMIN; - break; - case "secretary": - type = TYPE_SECRETARIAT; - break; - case "sadmin": - type = TYPE_SUPER_ADMIN; - break; - case "parentsCouncil": - type = TYPE_PARENTS_COUNCIL; - int index = nameLastFirst.indexOf(" - "); - position = index == -1 ? "" : nameLastFirst.substring(index+3); - nameLastFirst = index == -1 ? nameLastFirst : nameLastFirst.substring(0, index); - typeDescription = bs(className)+bs(": ", position); - break; - case "schoolParentsCouncil": - type = TYPE_SCHOOL_PARENTS_COUNCIL; - index = nameLastFirst.indexOf(" - "); - position = index == -1 ? "" : nameLastFirst.substring(index+3); - nameLastFirst = index == -1 ? nameLastFirst : nameLastFirst.substring(0, index); - typeDescription = bs(position); - break; - case "contactsGroups": - return; - } - Teacher teacher = Teacher.getByFullNameLastFirst(teacherList, nameLastFirst); - if (teacher == null) { - String[] nameParts = nameLastFirst.split(" ", Integer.MAX_VALUE); - teacher = new Teacher(profileId, -1 * Utils.crc16((nameParts.length > 1 ? nameParts[1]+" "+nameParts[0] : nameParts[0]).getBytes()), nameParts.length > 1 ? nameParts[1] : "", nameParts[0]); - teacherList.add(teacher); - } - teacher.loginId = String.valueOf(loginId); - teacher.type = 0; - teacher.setType(type); - if (type == TYPE_OTHER) { - teacher.typeDescription = typeDescription+bs(" ", teacher.typeDescription); - } - else { - teacher.typeDescription = typeDescription; - } - } - - @Override - public MessagesComposeInfo getComposeInfo(@NonNull ProfileFull profile) { - return new MessagesComposeInfo(0, 0, 150, 20000); - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Mobidziennik.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Mobidziennik.java deleted file mode 100644 index 23fed516..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Mobidziennik.java +++ /dev/null @@ -1,2425 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api; - -import android.content.Context; -import android.graphics.Color; -import android.os.AsyncTask; -import android.os.Handler; -import android.text.Html; -import android.util.LongSparseArray; -import android.util.Pair; -import android.util.SparseArray; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.crashlytics.android.Crashlytics; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; -import org.jsoup.select.Elements; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import im.wangchao.mhttp.Request; -import im.wangchao.mhttp.Response; -import im.wangchao.mhttp.callback.TextCallbackHandler; -import pl.szczodrzynski.edziennik.App; -import pl.szczodrzynski.edziennik.BuildConfig; -import pl.szczodrzynski.edziennik.R; -import pl.szczodrzynski.edziennik.data.api.interfaces.AttachmentGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface; -import pl.szczodrzynski.edziennik.data.api.interfaces.LoginCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.MessageGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.RecipientListGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.SyncCallback; -import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance; -import pl.szczodrzynski.edziennik.data.db.modules.events.Event; -import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade; -import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange; -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore; -import pl.szczodrzynski.edziennik.data.db.modules.luckynumber.LuckyNumber; -import pl.szczodrzynski.edziennik.data.db.modules.messages.Message; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipientFull; -import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata; -import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileFull; -import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject; -import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher; -import pl.szczodrzynski.edziennik.data.db.modules.teams.Team; -import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesComposeInfo; -import pl.szczodrzynski.edziennik.utils.models.Date; -import pl.szczodrzynski.edziennik.utils.models.Endpoint; -import pl.szczodrzynski.edziennik.utils.models.Time; -import pl.szczodrzynski.edziennik.utils.models.Week; - -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_INVALID_LOGIN; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_MAINTENANCE; -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_OTHER; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_ABSENT; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_ABSENT_EXCUSED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_BELATED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_CUSTOM; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_PRESENT; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_RELEASED; -import static pl.szczodrzynski.edziennik.data.db.modules.events.Event.TYPE_DEFAULT; -import static pl.szczodrzynski.edziennik.data.db.modules.events.Event.TYPE_EXAM; -import static pl.szczodrzynski.edziennik.data.db.modules.events.Event.TYPE_SHORT_QUIZ; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER2_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER2_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_YEAR_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_YEAR_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore.LOGIN_TYPE_MOBIDZIENNIK; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_DELETED; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_RECEIVED; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT; -import static pl.szczodrzynski.edziennik.utils.Utils.bs; -import static pl.szczodrzynski.edziennik.utils.Utils.crc16; -import static pl.szczodrzynski.edziennik.utils.Utils.d; -import static pl.szczodrzynski.edziennik.utils.Utils.monthFromName; -import static pl.szczodrzynski.edziennik.utils.Utils.strToInt; - -public class Mobidziennik implements EdziennikInterface { - public Mobidziennik(App app) { - this.app = app; - } - - private static final String TAG = "api.Mobidziennik"; - - private App app; - private Context activityContext = null; - private SyncCallback callback = null; - private int profileId = -1; - private Profile profile = null; - private LoginStore loginStore = null; - private boolean fullSync = true; - private Date today = Date.getToday(); - private List targetEndpoints = new ArrayList<>(); - - // PROGRESS - private static final int PROGRESS_LOGIN = 10; - private int PROGRESS_COUNT = 1; - private int PROGRESS_STEP = (90/PROGRESS_COUNT); - - private int onlyFeature = FEATURE_ALL; - - private List teamList; - private List teacherList; - private List subjectList; - private List lessonList; - private List lessonChangeList; - private LongSparseArray gradeAddedDates; - private LongSparseArray gradeAverages; - private LongSparseArray gradeColors; - private List gradeList; - private List eventList; - private List noticeList; - private List attendanceList; - private List messageList; - private List messageRecipientList; - private List messageRecipientIgnoreList; - private List metadataList; - private List messageMetadataList; - - private static boolean fakeLogin = false && !(BuildConfig.BUILD_TYPE.equals("release")); - private String lastLogin = null; - private long lastLoginTime = 0; - private String lastResponse = null; - private String loginServerName = null; - private String loginUsername = null; - private String loginPassword = null; - private int studentId = -1; - - private long attendanceLastSync = 0; - - private boolean prepare(@NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable Profile profile, @NonNull LoginStore loginStore) { - this.activityContext = activityContext; - this.callback = callback; - this.profileId = profileId; - // here we must have a login store: either with a correct ID or -1 - // there may be no profile and that's when onLoginFirst happens - this.profile = profile; - this.loginStore = loginStore; - this.fullSync = profile == null || profile.getEmpty() || profile.shouldFullSync(activityContext); - this.today = Date.getToday(); - - this.loginServerName = loginStore.getLoginData("serverName", ""); - this.loginUsername = loginStore.getLoginData("username", ""); - this.loginPassword = loginStore.getLoginData("password", ""); - if (loginServerName.equals("") || loginUsername.equals("") || loginPassword.equals("")) { - finishWithError(new AppError(TAG, 157, AppError.CODE_INVALID_LOGIN, "Login field is empty")); - return false; - } - this.studentId = profile == null ? -1 : profile.getStudentData("studentId", -1); - this.attendanceLastSync = profile == null ? 0 : profile.getStudentData("attendanceLastSync", (long)0); - fakeLogin = BuildConfig.DEBUG && loginUsername.toLowerCase().startsWith("fake"); - - teamList = profileId == -1 ? new ArrayList<>() : app.db.teamDao().getAllNow(profileId); - teacherList = profileId == -1 ? new ArrayList<>() : app.db.teacherDao().getAllNow(profileId); - subjectList = new ArrayList<>(); - lessonList = new ArrayList<>(); - lessonChangeList = new ArrayList<>(); - gradeAddedDates = new LongSparseArray<>(); - gradeAverages = new LongSparseArray<>(); - gradeColors = new LongSparseArray<>(); - gradeList = new ArrayList<>(); - eventList = new ArrayList<>(); - noticeList = new ArrayList<>(); - attendanceList = new ArrayList<>(); - messageList = new ArrayList<>(); - messageRecipientList = new ArrayList<>(); - messageRecipientIgnoreList = new ArrayList<>(); - metadataList = new ArrayList<>(); - messageMetadataList = new ArrayList<>(); - - return true; - } - - @Override - public void sync(@NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable Profile profile, @NonNull LoginStore loginStore) { - if (!prepare(activityContext, callback, profileId, profile, loginStore)) - return; - - login(() -> { - targetEndpoints = new ArrayList<>(); - targetEndpoints.add("GetData"); - targetEndpoints.add("ProcessData"); - targetEndpoints.add("Attendance"); - targetEndpoints.add("ClassCalendar"); - targetEndpoints.add("GradeDetails"); - targetEndpoints.add("NoticeDetails"); - targetEndpoints.add("Messages"); - targetEndpoints.add("MessagesInbox"); - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - @Override - public void syncFeature(@NonNull Context activityContext, @NonNull SyncCallback callback, @NonNull ProfileFull profile, @Nullable int ... featureList) { - if (featureList == null) { - sync(activityContext, callback, profile.getId(), profile, LoginStore.fromProfileFull(profile)); - return; - } - if (!prepare(activityContext, callback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - login(() -> { - targetEndpoints = new ArrayList<>(); - if (featureList.length == 1) - onlyFeature = featureList[0]; - if (featureList.length == 1 && (featureList[0] == FEATURE_MESSAGES_INBOX || featureList[0] == FEATURE_MESSAGES_OUTBOX)) { - teacherList = app.db.teacherDao().getAllNow(profileId); - for (Teacher teacher: teacherList) { - teachersMap.put((int) teacher.id, teacher.getFullNameLastFirst()); - } - if (featureList[0] == FEATURE_MESSAGES_INBOX) { - targetEndpoints.add("MessagesInbox"); - } - else { - targetEndpoints.add("Messages"); - } - } - else { - // this is needed for all features except messages - targetEndpoints.add("GetData"); - targetEndpoints.add("ProcessData"); - for (int feature: featureList) { - switch (feature) { - case FEATURE_AGENDA: - targetEndpoints.add("ClassCalendar"); - break; - case FEATURE_GRADES: - targetEndpoints.add("GradeDetails"); - break; - case FEATURE_NOTICES: - targetEndpoints.add("NoticeDetails"); - break; - case FEATURE_ATTENDANCE: - targetEndpoints.add("Attendance"); - break; - case FEATURE_MESSAGES_INBOX: - targetEndpoints.add("MessagesInbox"); - break; - case FEATURE_MESSAGES_OUTBOX: - targetEndpoints.add("Messages"); - break; - } - } - } - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - - private void begin() { - callback.onProgress(PROGRESS_LOGIN); - - r("get", null); - } - - private void r(String type, String endpoint) { - // endpoint == null when beginning - if (endpoint == null) - endpoint = targetEndpoints.get(0); - int index = -1; - for (String request: targetEndpoints) { - index++; - if (request.equals(endpoint)) { - break; - } - } - if (type.equals("finish")) { - // called when finishing the action - callback.onProgress(PROGRESS_STEP); - index++; - } - d(TAG, "Called r("+type+", "+endpoint+"). Getting "+targetEndpoints.get(index)); - switch (targetEndpoints.get(index)) { - case "GetData": - getData(); - break; - case "ProcessData": - processData(); - break; - case "ClassCalendar": - getClassCalendar(); - break; - case "GradeDetails": - getGradeDetails(); - break; - case "NoticeDetails": - getNoticeDetails(); - break; - case "Attendance": - getAttendance(); - break; - case "Messages": - getAllMessages(); - break; - case "MessagesInbox": - getMessagesInbox(); - break; - case "Finish": - finish(); - break; - } - } - private void saveData() { - if (teamList.size() > 0) { - app.db.teamDao().clear(profileId); - app.db.teamDao().addAll(teamList); - } - if (teacherList.size() > 0) - app.db.teacherDao().addAllIgnore(teacherList); - if (subjectList.size() > 0) - app.db.subjectDao().addAll(subjectList); - if (lessonList.size() > 0) { - app.db.lessonDao().clear(profileId); - app.db.lessonDao().addAll(lessonList); - } - if (lessonChangeList.size() > 0) - app.db.lessonChangeDao().addAll(lessonChangeList); - if (gradeList.size() > 0) { - app.db.gradeDao().clearForSemester(profileId, profile.getCurrentSemester()); - app.db.gradeDao().addAll(gradeList); - app.db.gradeDao().updateDetails(profileId, gradeAverages, gradeAddedDates, gradeColors); - } - if (eventList.size() > 0) { - app.db.eventDao().removeFuture(profileId, today); - app.db.eventDao().addAll(eventList); - } - if (noticeList.size() > 0) { - app.db.noticeDao().clear(profileId); - app.db.noticeDao().addAll(noticeList); - } - if (attendanceList.size() > 0) { - // clear only last two weeks - //app.db.attendanceDao().clearAfterDate(profileId, Date.getToday().stepForward(0, 0, -14)); - app.db.attendanceDao().addAll(attendanceList); - } - if (messageList.size() > 0) - app.db.messageDao().addAllIgnore(messageList); - if (messageRecipientList.size() > 0) - app.db.messageRecipientDao().addAll(messageRecipientList); - if (messageRecipientIgnoreList.size() > 0) - app.db.messageRecipientDao().addAllIgnore(messageRecipientIgnoreList); - if (metadataList.size() > 0) - app.db.metadataDao().addAllIgnore(metadataList); - if (messageMetadataList.size() > 0) - app.db.metadataDao().setSeen(messageMetadataList); - } - private void finish() { - try { - saveData(); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 303, CODE_OTHER, app.getString(R.string.sync_error_saving_data), null, null, e, null)); - } - if (fullSync) { - profile.setLastFullSync(System.currentTimeMillis()); - fullSync = false; - } - profile.setEmpty(false); - callback.onSuccess(activityContext, new ProfileFull(profile, loginStore)); - } - private void finishWithError(AppError error) { - try { - saveData(); - } - catch (Exception e) { - Crashlytics.logException(e); - } - callback.onError(activityContext, error); - } - - /* _ _ - | | (_) - | | ___ __ _ _ _ __ - | | / _ \ / _` | | '_ \ - | |___| (_) | (_| | | | | | - |______\___/ \__, |_|_| |_| - __/ | - |__*/ - private void login(@NonNull LoginCallback loginCallback) { - if (System.currentTimeMillis() - lastLoginTime < 10*60*1000 - && lastLogin.equals(loginServerName+":"+loginUsername)) { - loginCallback.onSuccess(); - return; - } - app.cookieJar.clearForDomain(loginServerName+".mobidziennik.pl"); - - Request.builder() - .url(fakeLogin ? "https://szkolny.eu/mobimobi/mobi_mod_log.php" : "https://"+loginServerName+".mobidziennik.pl/api/") - .userAgent(System.getProperty( "http.agent" )) - .contentType("application/x-www-form-urlencoded; charset=UTF-8") - .addParameter("wersja", "20") - .addParameter("ip", app.deviceId) - .addParameter("login", loginUsername) - .addParameter("haslo", loginPassword) - .addParameter("token", app.appConfig.fcmTokens.get(LOGIN_TYPE_MOBIDZIENNIK).first) - .post() - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 333, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - - //app.profile.loggedIn = (data != null && data.equals("ok")); - if (data == null || data.equals("")) { // for safety - finishWithError(new AppError(TAG, 339, CODE_MAINTENANCE, response)); - } - else if (data.equals("Nie jestes zalogowany")) { - finishWithError(new AppError(TAG, 343, AppError.CODE_INVALID_LOGIN, response, data)); - } - else if (data.equals("ifun")) { - finishWithError(new AppError(TAG, 346, AppError.CODE_INVALID_DEVICE, response, data)); - } - else if (data.equals("stare haslo")) { - finishWithError(new AppError(TAG, 349, AppError.CODE_OLD_PASSWORD, response, data)); - } - else if (data.equals("Archiwum")) { - finishWithError(new AppError(TAG, 352, AppError.CODE_ARCHIVED, response, data)); - } - else if (data.equals("Trwają prace techniczne lub pojawił się jakiś problem")) { - finishWithError(new AppError(TAG, 355, CODE_MAINTENANCE, data, response, data)); - } - else if (data.equals("ok")) - { - // a successful login - if (profile != null && studentId != -1) { - lastLogin = loginServerName+":"+loginUsername; - lastLoginTime = System.currentTimeMillis(); - loginCallback.onSuccess(); - return; - } - getData((data1 -> { - String[] tables = data1.split("T@B#LA"); - - List studentIds = new ArrayList<>(); - List studentNamesLong = new ArrayList<>(); - List studentNamesShort = new ArrayList<>(); - String[] student = tables[8].split("\n"); - for (String aStudent : student) { - if (aStudent.isEmpty()) { - continue; - } - String[] student1 = aStudent.split("\\|", Integer.MAX_VALUE); - if (student1.length == 2) - continue; - studentIds.add(strToInt(student1[0])); - studentNamesLong.add(student1[2] + " " + student1[4]); - studentNamesShort.add(student1[2] + " " + student1[4].charAt(0) + "."); - } - - List profileList = new ArrayList<>(); - - for (int index = 0; index < studentIds.size(); index++) { - Profile profile = new Profile(); - profile.setStudentNameLong(studentNamesLong.get(index)); - profile.setStudentNameShort(studentNamesShort.get(index)); - profile.setName(profile.getStudentNameLong()); - profile.setSubname(loginUsername); - profile.setEmpty(true); - profile.putStudentData("studentId", studentIds.get(index)); - profileList.add(profile); - } - - callback.onLoginFirst(profileList, loginStore); - })); - } - else { - if (data.contains("Uuuups... nieprawidłowy adres")) { - finishWithError(new AppError(TAG, 404, AppError.CODE_INVALID_SERVER_ADDRESS, response, data)); - return; - } - finishWithError(new AppError(TAG, 407, AppError.CODE_MAINTENANCE, response, data)); - } - } - }) - .build() - .enqueue(); - } - - /* _ _ _ _ _ _ _ - | | | | | | ___ | | | | | | - | |__| | ___| |_ __ ___ _ __ ___ ( _ ) ___ __ _| | | |__ __ _ ___| | _____ - | __ |/ _ \ | '_ \ / _ \ '__/ __| / _ \/\ / __/ _` | | | '_ \ / _` |/ __| |/ / __| - | | | | __/ | |_) | __/ | \__ \ | (_> < | (_| (_| | | | |_) | (_| | (__| <\__ \ - |_| |_|\___|_| .__/ \___|_| |___/ \___/\/ \___\__,_|_|_|_.__/ \__,_|\___|_|\_\___/ - | | - |*/ - private interface GetDataCallback { - void onSuccess(String data); - } - private void getData(GetDataCallback getDataCallback) { - callback.onActionStarted(R.string.sync_action_syncing); - Request.builder() - .url(fakeLogin ? "https://szkolny.eu/mobimobi/mobi_mod.php?"+System.currentTimeMillis() : "https://"+ loginServerName +".mobidziennik.pl/api/zrzutbazy") - .userAgent(System.getProperty( "http.agent" )) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 427, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - if (data == null || data.equals("")) { - finishWithError(new AppError(TAG, 432, CODE_MAINTENANCE, response)); - return; - } - if (data.equals("Nie jestes zalogowany")) { - finishWithError(new AppError(TAG, 437, CODE_INVALID_LOGIN, response, data)); - lastLoginTime = 0; - return; - } - - lastResponse = data; - - getDataCallback.onSuccess(data); - } - }) - .build() - .enqueue(); - } - private void getData() { - getData((data -> r("finish", "GetData"))); - } - - /* _____ _ _____ _ - | __ \ | | | __ \ | | - | | | | __ _| |_ __ _ | |__) |___ __ _ _ _ ___ ___| |_ ___ - | | | |/ _` | __/ _` | | _ // _ \/ _` | | | |/ _ \/ __| __/ __| - | |__| | (_| | || (_| | | | \ \ __/ (_| | |_| | __/\__ \ |_\__ \ - |_____/ \__,_|\__\__,_| |_| \_\___|\__, |\__,_|\___||___/\__|___/ - | | - |*/ - private void processData() { - callback.onActionStarted(R.string.sync_action_processing_data); - String[] tables = lastResponse.split("T@B#LA"); - - if (studentId == -1) { - return; - } - try { - int i = 0; - for (String table : tables) { - if (i == 0) { - processUsers(table); - } - if (i == 3) { - processDates(table); - } - if (i == 4) { - processSubjects(table); - } - if (i == 7) { - processTeams(table, null); - } - if (i == 8) { - processStudent(table, loginUsername); - } - if (i == 9) { - processTeams(null, table); - } - if (i == 14) { - processGradeCategories(table); - } - if (i == 15) { - processLessons(table); - } - if (i == 16) { - processAttendance(table); - } - if (i == 17) { - processNotices(table); - } - if (i == 18) { - processGrades(table); - } - if (i == 21) { - processEvents(table); - } - if (i == 23) { - processHomework(table); - } - if (i == 24) { - processTimetable(table); - } - i++; - } - r("finish", "ProcessData"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 511, CODE_OTHER, e, lastResponse)); - } - } - - private void getClassCalendar() { - callback.onActionStarted(R.string.sync_action_syncing_calendar); - Request.builder() - .url(fakeLogin ? "https://szkolny.eu/mobimobi/mobi_mod_kalendarzklasowy.php" : "https://" + loginServerName + ".mobidziennik.pl/dziennik/kalendarzklasowy") - .userAgent(System.getProperty("http.agent")) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 523, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - // just skip any failures here - if (data == null || data.equals("")) { - r("finish", "ClassCalendar"); - return; - } - if (data.contains("nie-pamietam-hasla")) { - r("finish", "ClassCalendar"); - return; - } - - if (profile.getLuckyNumberEnabled() - && (profile.getLuckyNumberDate() == null || profile.getLuckyNumberDate().getValue() != Date.getToday().getValue() || profile.getLuckyNumber() == -1)) { - // set the current date as we checked the lucky number today - profile.setLuckyNumber(-1); - profile.setLuckyNumberDate(Date.getToday()); - - Matcher matcher = Pattern.compile("class=\"szczesliwy_numerek\".*>0*([0-9]+)(?:/0*[0-9]+)*", Pattern.DOTALL).matcher(data); - if (matcher.find()) { - try { - profile.setLuckyNumber(Integer.parseInt(matcher.group(1))); - } catch (Exception e3) { - e3.printStackTrace(); - } - } - app.db.luckyNumberDao().add(new LuckyNumber(profileId, profile.getLuckyNumberDate(), profile.getLuckyNumber())); - } - - Matcher matcher = Pattern.compile("events: (.+),$", Pattern.MULTILINE).matcher(data); - if (matcher.find()) { - try { - //d(TAG, matcher.group(1)); - JsonArray events = new JsonParser().parse(matcher.group(1)).getAsJsonArray(); - //d(TAG, "Events size "+events.size()); - for (JsonElement eventEl: events) { - JsonObject event = eventEl.getAsJsonObject(); - - //d(TAG, "Event "+event.toString()); - - JsonElement idEl = event.get("id"); - String idStr; - if (idEl != null && (idStr = idEl.getAsString()).startsWith("kalendarz;")) { - String[] idParts = idStr.split(";", Integer.MAX_VALUE); - if (idParts.length > 2) { - long id = idParts[2].isEmpty() ? -1 : strToInt(idParts[2]); - long targetStudentId = strToInt(idParts[1]); - if (targetStudentId != studentId) - continue; - - // TODO null-safe getAs..() - Date eventDate = Date.fromY_m_d(event.get("start").getAsString()); - //if (eventDate.getValue() < today.getValue()) - // continue; - - String eventColor = event.get("color").getAsString(); - - int eventType = Event.TYPE_INFORMATION; - - switch (eventColor) { - case "#C54449": - case "#c54449": - eventType = Event.TYPE_SHORT_QUIZ; - break; - case "#AB0001": - case "#ab0001": - eventType = Event.TYPE_EXAM; - break; - case "#008928": - eventType = Event.TYPE_CLASS_EVENT; - break; - case "#b66000": - case "#B66000": - eventType = Event.TYPE_EXCURSION; - break; - } - - String title = event.get("title").getAsString(); - String comment = event.get("comment").getAsString(); - - String topic = title; - if (!title.equals(comment)) { - topic += "\n"+comment; - } - - if (id == -1) { - id = crc16(topic.getBytes()); - } - - Event eventObject = new Event( - profileId, - id, - eventDate, - null, - topic, - -1, - eventType, - false, - -1, - -1, - teamClass != null ? teamClass.id : -1 - ); - - - //d(TAG, "Event "+eventObject); - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_EVENT, eventObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis() /* no addedDate here though */)); - } - } - } - r("finish", "ClassCalendar"); - } catch (Exception e3) { - finishWithError(new AppError(TAG, 638, CODE_OTHER, response, e3, data)); - } - } - else { - r("finish", "ClassCalendar"); - } - } - }) - .build() - .enqueue(); - } - - private void getGradeDetails() { - callback.onActionStarted(R.string.sync_action_syncing_grade_details); - Request.builder() - .url(fakeLogin ? "https://szkolny.eu/mobimobi/mobi_mod_oceny.php" : "https://" + loginServerName + ".mobidziennik.pl/dziennik/oceny?semestr="+ profile.getCurrentSemester()) - .userAgent(System.getProperty("http.agent")) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 658, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - // just skip any failures here - if (data == null || data.equals("")) { - r("finish", "GradeDetails"); - return; - } - if (data.contains("nie-pamietam-hasla")) { - r("finish", "GradeDetails"); - return; - } - - try { - Document doc = Jsoup.parse(data); - - Elements grades = doc.select("table.spis a, table.spis span, table.spis div"); - - String gradeCategory = ""; - int gradeColor = -1; - String subjectName = ""; - - for (Element e: grades) { - switch (e.tagName()) { - case "div": { - //d(TAG, "Outer HTML "+e.outerHtml()); - Matcher matcher = Pattern.compile("\\n*\\s*(.+?)\\s*\\n*(?:<.*?)??", Pattern.DOTALL).matcher(e.outerHtml()); - if (matcher.find()) { - subjectName = matcher.group(1); - } - break; - } - case "span": { - String css = e.attr("style"); - Matcher matcher = Pattern.compile("background-color:([#A-Fa-f0-9]+);", Pattern.DOTALL).matcher(css); - if (matcher.find()) { - gradeColor = Color.parseColor(matcher.group(1)); - } - matcher = Pattern.compile("> (.+?):", Pattern.DOTALL).matcher(e.outerHtml()); - if (matcher.find()) { - gradeCategory = matcher.group(1); - } - break; - } - case "a": { - int gradeId = strToInt(e.attr("rel")); - float gradeClassAverage = -1; - Date gradeAddedDate = null; - long gradeAddedDateMillis = -1; - - String html = e.html(); - Matcher matcher = Pattern.compile("Średnia ocen:.*([0-9]*\\.?[0-9]*)", Pattern.DOTALL).matcher(html); - if (matcher.find()) { - gradeClassAverage = Float.parseFloat(matcher.group(1)); - } - - matcher = Pattern.compile("Wpisano:.*.+?,\\s([0-9]+)\\s(.+?)\\s([0-9]{4}),\\sgodzina\\s([0-9:]+)", Pattern.DOTALL).matcher(html); - int month = 1; - if (matcher.find()) { - switch (matcher.group(2)) { - case "stycznia": - month = 1; - break; - case "lutego": - month = 2; - break; - case "marca": - month = 3; - break; - case "kwietnia": - month = 4; - break; - case "maja": - month = 5; - break; - case "czerwca": - month = 6; - break; - case "lipca": - month = 7; - break; - case "sierpnia": - month = 8; - break; - case "września": - month = 9; - break; - case "października": - month = 10; - break; - case "listopada": - month = 11; - break; - case "grudnia": - month = 12; - break; - } - gradeAddedDate = new Date( - strToInt(matcher.group(3)), - month, - strToInt(matcher.group(1)) - ); - Time time = Time.fromH_m_s( - matcher.group(4) - ); - gradeAddedDateMillis = gradeAddedDate.combineWith(time); - } - - matcher = Pattern.compile("Liczona do średniej:.*?nie
", Pattern.DOTALL).matcher(html); - if (matcher.find()) { - matcher = Pattern.compile("(.+?).*?.+?.*?\\((.+?)\\).*?.*?Wartość oceny:.*?([0-9.]+).*?Wpisał\\(a\\):.*?(.+?)", Pattern.DOTALL).matcher(html); - if (matcher.find()) { - String gradeName = matcher.group(1); - String gradeDescription = matcher.group(2); - float gradeValue = Float.parseFloat(matcher.group(3)); - String teacherName = matcher.group(4); - - long teacherId = -1; - long subjectId = -1; - - //d(TAG, "Grade "+gradeName+" "+gradeCategory+" subject "+subjectName+" teacher "+teacherName); - - if (!subjectName.equals("")) { - int subjectIndex = -1; - for (int i = 0; i < subjectsMap.size(); i++) { - if (subjectsMap.valueAt(i).equals(subjectName)) { - subjectIndex = i; - } - } - //d(TAG, "subjectIndex "+subjectIndex); - if (subjectIndex >= 0 && subjectIndex < subjectsMap.size()) { - subjectId = subjectsMap.keyAt(subjectIndex); - } - } - - if (!teacherName.equals("")) { - //d(TAG, "teacherName "+teacherName); - int teacherIndex = -1; - for (int i = 0; i < teachersMap.size(); i++) { - if (teachersMap.valueAt(i).equals(teacherName)) { - teacherIndex = i; - } - } - //d(TAG, "teacherIndex "+teacherIndex); - if (teacherIndex >= 0 && teacherIndex < teachersMap.size()) { - teacherId = teachersMap.keyAt(teacherIndex); - } - } - - int semester = profile.dateToSemester(gradeAddedDate); - - Grade gradeObject = new Grade( - profileId, - gradeId, - gradeCategory, - gradeColor, - "NLDŚR, "+gradeDescription, - gradeName, - gradeValue, - 0, - semester, - teacherId, - subjectId - ); - - gradeObject.classAverage = gradeClassAverage; - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), gradeAddedDateMillis)); - } - } else { - gradeAverages.put(gradeId, gradeClassAverage); - gradeAddedDates.put(gradeId, gradeAddedDateMillis); - gradeColors.put(gradeId, gradeColor); - } - break; - } - } - } - - r("finish", "GradeDetails"); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 852, CODE_OTHER, response, e, data)); - } - } - }) - .build() - .enqueue(); - } - - // TODO: 2019-06-04 punkty z zachowania znikają po synchronizacji - private void getNoticeDetails() { - if (noticeList.size() == 0) { - r("finish", "NoticeDetails"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_notice_details); - Request.builder() - .url(fakeLogin ? "https://szkolny.eu/mobimobi/mobi_mod_zachowanie.php" : "https://" + loginServerName + ".mobidziennik.pl/mobile/zachowanie") - .userAgent(System.getProperty("http.agent")) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 873, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - // just skip any failures here - if (data == null || data.equals("")) { - r("finish", "NoticeDetails"); - return; - } - if (data.contains("nie-pamietam-hasla")) { - r("finish", "NoticeDetails"); - return; - } - - Matcher matcher = Pattern.compile("(?:([0-9-.,]+).+?)?
.+?Treść:.+?(?:Kategoria: (.+?).+?)?Czas", Pattern.DOTALL).matcher(data); - while (matcher.find()) { - try { - String pointsStr = matcher.group(1); - String idStr = matcher.group(2); - String categoryStr = matcher.groupCount() == 3 ? matcher.group(3) : null; - float points = pointsStr == null ? 0 : Float.parseFloat(pointsStr); - long id = Long.parseLong(idStr); - for (Notice notice: noticeList) { - if (notice.id != id) - continue; - notice.points = points; - notice.category = categoryStr; - } - } catch (Exception e) { - Crashlytics.logException(e); - e.printStackTrace(); - if (onlyFeature == FEATURE_NOTICES) - finishWithError(new AppError(TAG, 880, CODE_OTHER, response, e, data)); - else - r("finish", "NoticeDetails"); - return; - } - } - r("finish", "NoticeDetails"); - } - }) - .build() - .enqueue(); - } - - public static class AttendanceLessonRange { - int weekDay; - int lessonNumber; - Time startTime; - Time endTime; - List lessons; - - public AttendanceLessonRange(int section, int number, int lessonNumber, Time startTime, Time endTime) { - this.weekDay = section == 1 ? number + 4 : number; - this.lessonNumber = lessonNumber; - this.startTime = startTime; - this.endTime = endTime; - this.lessons = new ArrayList<>(); - } - public void addLesson(AttendanceLesson lesson) { - lessons.add(lesson); - } - } - public static class AttendanceLesson { - String subjectName; - String topic; - String teamName; - String teacherName; - long lessonId; - - public AttendanceLesson(String subjectName, String topic, String teamName, String teacherName, long lessonId) { - this.subjectName = subjectName; - this.topic = topic; - this.teamName = teamName; - this.teacherName = teacherName; - this.lessonId = lessonId; - } - } - private Date attendanceCheckDate = Week.getWeekStart(); - private void getAttendance() { - r("finish", "Attendance"); - // TODO: 2019-09-10 please download attendance from /dziennik/frekwencja. /mobile does not work above v13.0 - if (true) { - return; - } - callback.onActionStarted(R.string.sync_action_syncing_attendance); - d(TAG, "Get attendance for week "+ attendanceCheckDate.getStringY_m_d()); - Request.builder() - .url(fakeLogin ? "https://szkolny.eu/mobimobi/mobi_mod_frekwencja.php" : "https://" + loginServerName + ".mobidziennik.pl/mobile/frekwencja") - .userAgent(System.getProperty("http.agent")) - .addParameter("uczen", studentId) - .addParameter("data_poniedzialek", attendanceCheckDate.getStringY_m_d()) - .post() - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 944, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - // just skip any failures here - if (data == null || data.equals("")) { - r("finish", "Attendance"); - return; - } - if (data.contains("nie-pamietam-hasla")) { - r("finish", "Attendance"); - return; - } - - List ranges = new ArrayList<>(); - - try { - Matcher matcher = Pattern.compile("
.*?

([0-9]{2}):([0-9]{2}) - ([0-9]{2}):([0-9]{2}) (.+?)\\s*?

.*?

", Pattern.DOTALL).matcher(data); - while (matcher.find()) { - int section = strToInt(matcher.group(1)); - int number = strToInt(matcher.group(2)); - int lessonNumber = strToInt(matcher.group(3)); - int startHour = strToInt(matcher.group(4)); - int startMinute = strToInt(matcher.group(5)); - int endHour = strToInt(matcher.group(6)); - int endMinute = strToInt(matcher.group(7)); - String lessonsString = matcher.group(8); - AttendanceLessonRange range = new AttendanceLessonRange(section, number, lessonNumber, new Time(startHour, startMinute, 0), new Time(endHour, endMinute, 0)); - Matcher matcher2 = Pattern.compile("(.+?) - (.+?)??.+?.+?\\(([0-9]{1,2}) (.+?),\\s+(.+?), id lekcji: ([0-9]+?)\\).+?", Pattern.DOTALL).matcher(lessonsString); - while (matcher2.find()) { - String topic = matcher2.group(2); - topic = topic == null ? "" : Html.fromHtml(topic).toString(); - if (topic.startsWith("Lekcja odwołana:")) - continue; - String teamName = matcher2.group(3)+matcher2.group(4); - boolean notFound = true; - for (Team team: teamList) { - if (teamName.equals(team.name)) { - notFound = false; - break; - } - } - if (notFound) - continue; - range.addLesson( - new AttendanceLesson( - matcher2.group(1), - topic, - teamName, - matcher2.group(5), - strToInt(matcher2.group(6)) - ) - ); - } - ranges.add(range); - } - matcher = Pattern.compile("([.|sz+]+)?", Pattern.DOTALL).matcher(data); - int index = 0; - while (matcher.find()) { - int currentIndex = index++; - String markers = matcher.groupCount() == 1 ? matcher.group(1) : null; - if (markers == null) - continue; - AttendanceLessonRange range = ranges.get(currentIndex); - - Date date = attendanceCheckDate.clone().stepForward(0, 0, range.weekDay); - long addedDate = date.combineWith(range.startTime); - - int markerIndex = 0; - for (char marker: markers.toCharArray()) { - int type = TYPE_CUSTOM; - switch (marker) { - case '.': - type = TYPE_PRESENT; - break; - case '|': - type = TYPE_ABSENT; - break; - case 's': - type = TYPE_BELATED; - break; - case 'z': - type = TYPE_RELEASED; - break; - case '+': - type = TYPE_ABSENT_EXCUSED; - } - AttendanceLesson lesson; - if (markerIndex >= range.lessons.size()) { - lesson = new AttendanceLesson("", "Nieznana lekcja", "???", "", addedDate); - } - else { - lesson = range.lessons.get(markerIndex); - } - - long teacherId = -1; - long subjectId = -1; - - int teacherIndex = -1; - for (int i = 0; i < teachersMap.size(); i++) { - if (teachersMap.valueAt(i).equals(lesson.teacherName)) { - teacherIndex = i; - } - } - if (teacherIndex >= 0 && teacherIndex < teachersMap.size()) { - teacherId = teachersMap.keyAt(teacherIndex); - } - - int subjectIndex = -1; - for (int i = 0; i < subjectsMap.size(); i++) { - if (subjectsMap.valueAt(i).equals(lesson.subjectName)) { - subjectIndex = i; - } - } - if (subjectIndex >= 0 && subjectIndex < subjectsMap.size()) { - subjectId = subjectsMap.keyAt(subjectIndex); - } - - Attendance attendanceObject = new Attendance( - profileId, - lesson.lessonId, - teacherId, - subjectId, - profile.dateToSemester(date), - lesson.topic, - date, - range.startTime, - type); - markerIndex++; - attendanceList.add(attendanceObject); - if (attendanceObject.type != TYPE_PRESENT) { - boolean markAsRead = onlyFeature == FEATURE_ATTENDANCE && attendanceLastSync == 0; - metadataList.add(new Metadata(profileId, Metadata.TYPE_ATTENDANCE, attendanceObject.id, profile.getEmpty() || markAsRead, profile.getEmpty() || markAsRead, addedDate)); - } - } - } - } catch (Exception e) { - Crashlytics.logException(e); - e.printStackTrace(); - if (onlyFeature == FEATURE_ATTENDANCE) - finishWithError(new AppError(TAG, 955, CODE_OTHER, response, e, data)); - else - r("finish", "Attendance"); - return; - } - - if (onlyFeature == FEATURE_ATTENDANCE) { - // syncing attendance exclusively - if (attendanceLastSync == 0) { - // first sync - get attendance until it's start of the school year - attendanceLastSync = profile.getSemesterStart(1).getInMillis(); - } - Date lastSyncDate = Date.fromMillis(attendanceLastSync); - lastSyncDate.stepForward(0, 0, -7); - if (lastSyncDate.getValue() < attendanceCheckDate.getValue()) { - attendanceCheckDate.stepForward(0, 0, -7); - r("get", "Attendance"); - } - else { - profile.putStudentData("attendanceLastSync", System.currentTimeMillis()); - r("finish", "Attendance"); - } - } - else { - if (attendanceLastSync != 0) { - // not a first sync - Date lastSyncDate = Date.fromMillis(attendanceLastSync); - lastSyncDate.stepForward(0, 0, 2); - if (lastSyncDate.getValue() >= attendanceCheckDate.getValue()) { - profile.putStudentData("attendanceLastSync", System.currentTimeMillis()); - } - } - r("finish", "Attendance"); - } - } - }) - .build() - .enqueue(); - } - - private void getAllMessages() { - if (!fullSync && onlyFeature != FEATURE_MESSAGES_OUTBOX) { - r("finish", "Messages"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_messages); - Request.builder() - .url(fakeLogin ? "https://szkolny.eu/mobimobi/mobi_mod_wiadomosci_szukaj.php" : "https://" + loginServerName + ".mobidziennik.pl/dziennik/wyszukiwarkawiadomosci?q=+") - .userAgent(System.getProperty("http.agent")) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 1012, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - // just skip any failures here - if (data == null || data.equals("")) { - r("finish", "GradeDetails"); - return; - } - if (data.contains("nie-pamietam-hasla")) { - r("finish", "GradeDetails"); - return; - } - - try { - long startTime = System.currentTimeMillis(); - Document doc = Jsoup.parse(data); - - Element listElement = doc.getElementsByClass("spis").first(); - if (listElement == null) { - r("finish", "Messages"); - return; - } - Elements list = listElement.getElementsByClass("podswietl"); - for (Element item: list) { - long id = Long.parseLong(item.attr("rel").replaceAll("[^\\d]", "" )); - - Element subjectEl = item.select("td:eq(0) div").first(); - String subject = subjectEl.text(); - - Element addedDateEl = item.select("td:eq(1)").first(); - String addedDateStr = addedDateEl.text(); - long addedDate = Date.fromIsoHm(addedDateStr); - - Element typeEl = item.select("td:eq(2) img").first(); - int type = TYPE_RECEIVED; - if (typeEl.outerHtml().contains("mail_send.png")) - type = TYPE_SENT; - - Element senderEl = item.select("td:eq(3) div").first(); - long senderId = -1; - if (type == TYPE_RECEIVED) { - String senderName = senderEl.text(); - for (int i = 0; i < teachersMap.size(); i++) { - if (senderName.equals(teachersMap.valueAt(i))) { - senderId = teachersMap.keyAt(i); - break; - } - } - messageRecipientList.add(new MessageRecipient(profileId, -1, id)); - } - else { - // TYPE_SENT, so multiple recipients possible - String[] recipientNames = senderEl.text().split(", "); - for (String recipientName: recipientNames) { - long recipientId = -1; - for (int i = 0; i < teachersMap.size(); i++) { - if (recipientName.equals(teachersMap.valueAt(i))) { - recipientId = teachersMap.keyAt(i); - break; - } - } - messageRecipientIgnoreList.add(new MessageRecipient(profileId, recipientId, id)); - } - } - - Message message = new Message( - profileId, - id, - subject, - null, - type, - senderId, - -1 - ); - - messageList.add(message); - metadataList.add(new Metadata(profileId, Metadata.TYPE_MESSAGE, message.id, true, true, addedDate)); - } - - } catch (Exception e3) { - finishWithError(new AppError(TAG, 949, CODE_OTHER, response, e3, data)); - return; - } - - r("finish", "Messages"); - } - }) - .build() - .enqueue(); - } - - private void getMessagesInbox() { - callback.onActionStarted(R.string.sync_action_syncing_messages); - Request.builder() - .url(fakeLogin ? "https://szkolny.eu/mobimobi/mobi_mod_wiadomosci.php" : "https://" + loginServerName + ".mobidziennik.pl/dziennik/wiadomosci") - .userAgent(System.getProperty("http.agent")) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 972, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - // just skip any failures here - if (data == null || data.equals("")) { - r("finish", "GradeDetails"); - return; - } - if (data.contains("nie-pamietam-hasla")) { - r("finish", "GradeDetails"); - return; - } - - try { - if (data.contains("Brak wiadomości odebranych.")) { - r("finish", "MessagesInbox"); - return; - } - Document doc = Jsoup.parse(data); - - Elements list = doc.getElementsByClass("spis").first().getElementsByClass("podswietl"); - for (Element item: list) { - long id = Long.parseLong(item.attr("rel")); - - Element subjectEl = item.select("td:eq(0)").first(); - //d(TAG, "subjectEl "+subjectEl.outerHtml()); - boolean hasAttachments = false; - if (subjectEl.getElementsByTag("a").size() != 0) { - hasAttachments = true; - } - String subject = subjectEl.ownText(); - - Element addedDateEl = item.select("td:eq(1) small").first(); - //d(TAG, "addedDateEl "+addedDateEl.outerHtml()); - String addedDateStr = addedDateEl.text(); - long addedDate = Date.fromIsoHm(addedDateStr); - - Element senderEl = item.select("td:eq(2)").first(); - //d(TAG, "senderEl "+senderEl.outerHtml()); - String senderName = senderEl.ownText(); - long senderId = -1; - for (int i = 0; i < teachersMap.size(); i++) { - if (senderName.equals(teachersMap.valueAt(i))) { - senderId = teachersMap.keyAt(i); - break; - } - } - messageRecipientIgnoreList.add(new MessageRecipient(profileId, -1, id)); - - boolean isRead = item.select("td:eq(3) span").first().hasClass("wiadomosc_przeczytana"); - - Message message = new Message( - profileId, - id, - subject, - null, - TYPE_RECEIVED, - senderId, - -1 - ); - - if (hasAttachments) - message.setHasAttachments(); - - - messageList.add(message); - messageMetadataList.add(new Metadata(profileId, Metadata.TYPE_MESSAGE, message.id, isRead, isRead || profile.getEmpty(), addedDate)); - } - r("finish", "MessagesInbox"); - } catch (Exception e3) { - finishWithError(new AppError(TAG, 1044, CODE_OTHER, response, e3, data)); - } - } - }) - .build() - .enqueue(); - } - - private SparseArray teachersMap = new SparseArray<>(); - private void processUsers(String table) - { - String[] users = table.split("\n"); - - //app.db.teacherDao().clear(profileId); - for (String userStr: users) - { - if (userStr.isEmpty()) { - continue; - } - String[] user = userStr.split("\\|", Integer.MAX_VALUE); - - teachersMap.put(strToInt(user[0]), user[5].trim()+" "+user[4].trim()); - teacherList.add(new Teacher(profileId, strToInt(user[0]), user[4].trim(), user[5].trim())); - } - } - - private void processDates(String table) - { - String[] dates = table.split("\n"); - for (String dateStr: dates) - { - if (dateStr.isEmpty()) { - continue; - } - String[] date = dateStr.split("\\|", Integer.MAX_VALUE); - switch (date[1]) { - case "semestr1_poczatek": - profile.setDateSemester1Start(Date.fromYmd(date[3])); - break; - case "semestr2_poczatek": - profile.setDateSemester2Start(Date.fromYmd(date[3])); - break; - case "koniec_roku_szkolnego": - profile.setDateYearEnd(Date.fromYmd(date[3])); - break; - } - } - } - - private SparseArray subjectsMap = new SparseArray<>(); - private void processSubjects(String table) - { - String[] subjects = table.split("\n"); - - // because a student may no longer have a subject - // which he had before - // therefore, this subject's grades do not get deleted - // so we should keep the subject for correct naming - //app.db.subjectDao().clear(profileId); - for (String subjectStr: subjects) - { - if (subjectStr.isEmpty()) { - continue; - } - String[] subject = subjectStr.split("\\|", Integer.MAX_VALUE); - - subjectsMap.put(strToInt(subject[0]), subject[1]); - subjectList.add(new Subject(profileId, strToInt(subject[0]), subject[1], subject[2])); - } - } - - private Team teamClass = null; - private SparseArray teamsMap = new SparseArray<>(); - private List allTeams = new ArrayList<>(); - private void processTeams(String tableTeams, String tableRelations) - { - if (tableTeams != null) - { - allTeams.clear(); - String[] teams = tableTeams.split("\n"); - for (String teamStr: teams) { - if (teamStr.isEmpty()) { - continue; - } - String[] team = teamStr.split("\\|", Integer.MAX_VALUE); - Team teamObject = new Team( - profileId, - strToInt(team[0]), - team[1]+team[2], - strToInt(team[3]), - loginServerName+":"+team[1]+team[2],// TODO zrobiłem to na szybko - strToInt(team[4], -1)); - allTeams.add(teamObject); - } - } - if (tableRelations != null) - { - app.db.teamDao().clear(profileId); - String[] teams = tableRelations.split("\n"); - for (String teamStr: teams) { - if (teamStr.isEmpty()) { - continue; - } - String[] team = teamStr.split("\\|", Integer.MAX_VALUE); - if (strToInt(team[1]) != studentId) - continue; - Team teamObject = Team.getById(allTeams, strToInt(team[2])); - if (teamObject != null) { - if (teamObject.type == 1) { - profile.setStudentNumber(strToInt(team[4])); - teamClass = teamObject; - } - teamsMap.put((int)teamObject.id, teamObject.name); - teamList.add(teamObject); - } - } - } - } - - private SparseArray> students = new SparseArray<>(); - private boolean processStudent(String table, String loginUsername) - { - students.clear(); - String[] student = table.split("\n"); - for (int i = 0; i < student.length; i++) { - if (student[i].isEmpty()) { - continue; - } - String[] student1 = student[i].split("\\|", Integer.MAX_VALUE); - String[] student2 = student[++i].split("\\|", Integer.MAX_VALUE); - students.put(strToInt(student1[0]), new Pair<>(student1, student2)); - } - Pair studentData = students.get(studentId); - try { - profile.setAttendancePercentage(Float.parseFloat(studentData.second[1])); - } - catch (Exception e) { - e.printStackTrace(); - } - return true; - } - - private SparseArray gradeCategoryList = new SparseArray<>(); - private void processGradeCategories(String table) - { - String[] gradeCategories = table.split("\n"); - - for (String gradeCategoryStr: gradeCategories) - { - if (gradeCategoryStr.isEmpty()) { - continue; - } - String[] gradeCategory = gradeCategoryStr.split("\\|", Integer.MAX_VALUE); - List columns = new ArrayList<>(); - Collections.addAll(columns, gradeCategory[7].split(";")); - - if (teamsMap.indexOfKey(strToInt(gradeCategory[1])) >= 0) { - gradeCategoryList.put( - Integer.parseInt(gradeCategory[0]), - new GradeCategory( - profileId, - Integer.parseInt(gradeCategory[0]), - Float.parseFloat(gradeCategory[3]), - Color.parseColor("#"+gradeCategory[6]), - gradeCategory[4] - ).addColumns(columns) - ); - } - } - } - - private class MobiLesson { - long id; - int subjectId; - int teacherId; - int teamId; - String topic; - Date date; - Time startTime; - Time endTime; - int presentCount; - int absentCount; - int lessonNumber; - String signed; - - MobiLesson(long id, int subjectId, int teacherId, int teamId, String topic, Date date, Time startTime, Time endTime, int presentCount, int absentCount, int lessonNumber, String signed) { - this.id = id; - this.subjectId = subjectId; - this.teacherId = teacherId; - this.teamId = teamId; - this.topic = topic; - this.date = date; - this.startTime = startTime; - this.endTime = endTime; - this.presentCount = presentCount; - this.absentCount = absentCount; - this.lessonNumber = lessonNumber; - this.signed = signed; - } - } - - private List mobiLessons = new ArrayList<>(); - - private MobiLesson getLesson(long id) { - for (MobiLesson mobiLesson : mobiLessons) { - if (mobiLesson.id == id) - return mobiLesson; - } - return null; - } - - private void processLessons(String table) - { - mobiLessons.clear(); - - String[] lessons2 = table.split("\n"); - for (String lessonStr: lessons2) - { - if (lessonStr.isEmpty()) { - continue; - } - String[] lesson = lessonStr.split("\\|", Integer.MAX_VALUE); - - MobiLesson newMobiLesson = new MobiLesson( - Long.parseLong(lesson[0]), - strToInt(lesson[1]), - strToInt(lesson[2]), - strToInt(lesson[3]), - lesson[4], - Date.fromYmd(lesson[5]), - Time.fromYmdHm(lesson[6]), - Time.fromYmdHm(lesson[7]), - strToInt(lesson[8]), - strToInt(lesson[9]), - strToInt(lesson[10]), - lesson[11]); - mobiLessons.add(newMobiLesson); - } - } - - private void processAttendance(String table) - { - if (true) - return; - String[] attendanceList = table.split("\n"); - for (String attendanceStr: attendanceList) - { - if (attendanceStr.isEmpty()) { - continue; - } - String[] attendance = attendanceStr.split("\\|", Integer.MAX_VALUE); - - if (strToInt(attendance[2]) != studentId) - continue; - //RegisterAttendance oldAttendance = RegisterAttendance.getById(oldAttendances, Long.parseLong(attendance[0])); - - MobiLesson mobiLesson = getLesson(Long.parseLong(attendance[1])); - if (mobiLesson != null) { - int type = TYPE_PRESENT; - switch (attendance[4]) { - case "2": - type = TYPE_ABSENT; - break; - case "5": - type = TYPE_ABSENT_EXCUSED; - break; - case "4": - type = TYPE_RELEASED; - break; - } - - int semester = profile.dateToSemester(mobiLesson.date); - - Attendance attendanceObject = new Attendance( - profileId, - strToInt(attendance[0]), - mobiLesson.teacherId, - mobiLesson.subjectId, - semester, - mobiLesson.topic, - mobiLesson.date, - mobiLesson.startTime, - type); - this.attendanceList.add(attendanceObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_ATTENDANCE, attendanceObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - } - - } - - private void processNotices(String table) - { - String[] notices = table.split("\n"); - - for (String noticeStr: notices) - { - if (noticeStr.isEmpty()) { - continue; - } - String[] notice = noticeStr.split("\\|", Integer.MAX_VALUE); - - if (strToInt(notice[2]) != studentId) - continue; - - Notice noticeObject = new Notice( - profileId, - strToInt(notice[0]), - notice[4], - strToInt(notice[6]), - (notice[3] != null ? (notice[3].equals("1") ? Notice.TYPE_POSITIVE : Notice.TYPE_NEGATIVE) : Notice.TYPE_NEUTRAL), - strToInt(notice[5]) - ); - - Metadata metadata = new Metadata(profileId, Metadata.TYPE_NOTICE, noticeObject.id, profile.getEmpty(), profile.getEmpty(), new Date().parseFromYmd(notice[7]).getInMillis()); - - noticeList.add(noticeObject); - metadataList.add(metadata); - } - } - - private void processGrades(String table) - { - app.db.gradeDao().getDetails(profileId, gradeAddedDates, gradeAverages, gradeColors); - - String[] grades = table.split("\n"); - - long addedDate = Date.getNowInMillis(); - for (String gradeStr: grades) - { - if (gradeStr.isEmpty()) { - continue; - } - String[] grade = gradeStr.split("\\|", Integer.MAX_VALUE); - - if (strToInt(grade[1]) != studentId) - continue; - - if (grade[6].equals("")) { - grade[6] = "-1"; - } - if (grade[10].equals("")) { - grade[10] = "1"; - } - - float weight = 0.0f; - String category = ""; - String description = ""; - int color = -1; - int categoryId = strToInt(grade[6]); - GradeCategory gradeCategory = gradeCategoryList.get(categoryId); - if (gradeCategory != null) { - weight = gradeCategory.weight; - category = gradeCategory.text; - description = gradeCategory.columns.get(strToInt(grade[10]) - 1); - color = gradeCategory.color; - } - - Grade gradeObject = new Grade( - profileId, - strToInt(grade[0]), - category, - color, - description, - grade[7], - Float.parseFloat(grade[11]), - weight, - strToInt(grade[5]), - strToInt(grade[2]), - strToInt(grade[3]) - ); - - - // fix for "0" value grades, so they're not counted in the average - if (gradeObject.value == 0.0f) { - gradeObject.weight = 0; - } - - switch (grade[8]) { - case "3": - // semester proposed - gradeObject.type = (gradeObject.semester == 1 ? TYPE_SEMESTER1_PROPOSED : TYPE_SEMESTER2_PROPOSED); - break; - case "1": - // semester final - gradeObject.type = (gradeObject.semester == 1 ? TYPE_SEMESTER1_FINAL : TYPE_SEMESTER2_FINAL); - break; - case "4": - // year proposed - gradeObject.type = TYPE_YEAR_PROPOSED; - break; - case "2": - // year final - gradeObject.type = TYPE_YEAR_FINAL; - break; - } - - Metadata metadata = new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate); - gradeList.add(gradeObject); - metadataList.add(metadata); - addedDate++; // increase the added date to sort grades as they are in the school profile - } - } - - private void processEvents(String table) - { - String[] events = table.split("\n"); - Date today = Date.getToday(); - for (String eventStr: events) - { - if (eventStr.isEmpty()) { - continue; - } - String[] event = eventStr.split("\\|", Integer.MAX_VALUE); - - Date eventDate = new Date().parseFromYmd(event[4]); - if (eventDate.getValue() < today.getValue()) - continue; - - String examType = ""; - Pattern pattern = Pattern.compile("\\(([0-9A-ząęóżźńśłć]*?)\\)$"); - Matcher matcher = pattern.matcher(event[5]); - if (matcher.find()) { - examType = matcher.group(1); - } - - long addedDate = 0; - - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US); - try { - addedDate = simpleDateFormat.parse(event[7]).getTime(); - } catch (ParseException e) { - e.printStackTrace(); - } - - int eventType = examType.equals("sprawdzian") ? TYPE_EXAM : examType.equals("kartkówka") ? TYPE_SHORT_QUIZ : TYPE_DEFAULT; - Event eventObject = new Event( - profileId, - strToInt(event[0]), - eventDate, - new Time().parseFromYmdHm(event[6]), - event[5].replace("("+examType+")", ""), - -1, - eventType, - false, - strToInt(event[1]), - strToInt(event[3]), - strToInt(event[2]) - ); - - if (teamsMap.indexOfKey((int)eventObject.teamId) >= 0) { - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_EVENT, eventObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - } - } - - private void processHomework(String table) - { - String[] homeworkList = table.split("\n"); - Date today = Date.getToday(); - for (String homeworkStr: homeworkList) - { - if (homeworkStr.isEmpty()) { - continue; - } - String[] homework = homeworkStr.split("\\|", Integer.MAX_VALUE); - - Date eventDate = new Date().parseFromYmd(homework[2]); - if (eventDate.getValue() < today.getValue()) - continue; - - Event eventObject = new Event( - profileId, - strToInt(homework[0]), - new Date().parseFromYmd(homework[2]), - new Time().parseFromYmdHm(homework[3]), - homework[1], - -1, - Event.TYPE_HOMEWORK, - false, - strToInt(homework[7]), - strToInt(homework[6]), - strToInt(homework[5]) - ); - - if (teamsMap.indexOfKey((int)eventObject.teamId) >= 0) { - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_HOMEWORK, eventObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - } - } - - private void processTimetable(String table) - { - String[] lessons = table.split("\n"); - List list = Arrays.asList(lessons); - //Collections.reverse(list); - - // searching for all planned lessons - for (String lessonStr: (String[])list.toArray()) - { - //Log.d(TAG, lessonStr); - if (!lessonStr.isEmpty()) - { - String[] lesson = lessonStr.split("\\|", Integer.MAX_VALUE); - - if (strToInt(lesson[0]) != studentId) - continue; - - if (lesson[1].equals("plan_lekcji") || lesson[1].equals("lekcja")) - { - Lesson lessonObject = new Lesson(profileId, lesson[2], lesson[3], lesson[4]); - - for(int i = 0; i < subjectsMap.size(); i++) { - int key = subjectsMap.keyAt(i); - String str = subjectsMap.valueAt(i); - if (lesson[5].equalsIgnoreCase(str)) { - lessonObject.subjectId = key; - } - } - for(int i = 0; i < teachersMap.size(); i++) { - int key = teachersMap.keyAt(i); - String str = teachersMap.valueAt(i); - if ((lesson[7].trim() + " " + lesson[6].trim()).equalsIgnoreCase(str)) { - lessonObject.teacherId = key; - } - } - for(int i = 0; i < teamsMap.size(); i++) { - int key = teamsMap.keyAt(i); - String str = teamsMap.valueAt(i); - if ((lesson[8] + lesson[9]).equalsIgnoreCase(str)) { - lessonObject.teamId = key; - } - } - lessonObject.classroomName = lesson[11]; - lessonList.add(lessonObject); - } - } - } - - // searching for all changes - for (String lessonStr: (String[])list.toArray()) - { - if (!lessonStr.isEmpty()) - { - String[] lesson = lessonStr.split("\\|", Integer.MAX_VALUE); - - if (strToInt(lesson[0]) != studentId) - continue; - - if (lesson[1].equals("zastepstwo") || lesson[1].equals("lekcja_odwolana")) - { - LessonChange lessonChange = new LessonChange(profileId, lesson[2], lesson[3], lesson[4]); - - //Log.d(TAG, "Lekcja "+lessonStr); - - for(int i = 0; i < subjectsMap.size(); i++) { - int key = subjectsMap.keyAt(i); - String str = subjectsMap.valueAt(i); - if (lesson[5].equalsIgnoreCase(str)) { - lessonChange.subjectId = key; - } - } - for(int i = 0; i < teachersMap.size(); i++) { - int key = teachersMap.keyAt(i); - String str = teachersMap.valueAt(i); - if ((lesson[7].trim() + " " + lesson[6].trim()).equalsIgnoreCase(str)) { - lessonChange.teacherId = key; - } - } - for(int i = 0; i < teamsMap.size(); i++) { - int key = teamsMap.keyAt(i); - String str = teamsMap.valueAt(i); - if ((lesson[8] + lesson[9]).equalsIgnoreCase(str)) { - lessonChange.teamId = key; - } - } - - if (lesson[1].equals("zastepstwo")) { - lessonChange.type = LessonChange.TYPE_CHANGE; - } - if (lesson[1].equals("lekcja_odwolana")) { - lessonChange.type = LessonChange.TYPE_CANCELLED; - } - if (lesson[1].equals("lekcja")) { - lessonChange.type = LessonChange.TYPE_ADDED; - } - lessonChange.classroomName = lesson[11]; - - Lesson originalLesson = lessonChange.getOriginalLesson(lessonList); - - if (lessonChange.type == LessonChange.TYPE_ADDED) { - if (originalLesson == null) { - // original lesson doesn't exist, save a new addition - // TODO - /*if (!RegisterLessonChange.existsAddition(app.profile, registerLessonChange)) { - app.profile.timetable.addLessonAddition(registerLessonChange); - }*/ - } - else { - // original lesson exists, so we need to compare them - if (!lessonChange.matches(originalLesson)) { - // the lessons are different, so it's probably a lesson change - // ahhh this damn API - lessonChange.type = LessonChange.TYPE_CHANGE; - } - } - - } - if (lessonChange.type != LessonChange.TYPE_ADDED) { - // it's not a lesson addition - lessonChangeList.add(lessonChange); - metadataList.add(new Metadata(profileId, Metadata.TYPE_LESSON_CHANGE, lessonChange.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - if (originalLesson == null) { - // there is no original lesson, so we have to add one in order to change it - lessonList.add(Lesson.fromLessonChange(lessonChange)); - } - } - } - } - } - } - - @Override - public Map getConfigurableEndpoints(Profile profile) { - return null; - } - - @Override - public boolean isEndpointEnabled(Profile profile, boolean defaultActive, String name) { - return defaultActive; - } - - @Override - public void syncMessages(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile) { - - } - - /* __ __ - | \/ | - | \ / | ___ ___ ___ __ _ __ _ ___ ___ - | |\/| |/ _ \/ __/ __|/ _` |/ _` |/ _ \/ __| - | | | | __/\__ \__ \ (_| | (_| | __/\__ \ - |_| |_|\___||___/___/\__,_|\__, |\___||___/ - __/ | - |__*/ - @Override - public void getMessage(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull MessageFull message, @NonNull MessageGetCallback messageCallback) { - if (message.body != null) { - boolean readByAll = true; - message.recipients = app.db.messageRecipientDao().getAllByMessageId(profile.getId(), message.id); - for (MessageRecipientFull recipient: message.recipients) { - if (recipient.id == -1) - recipient.fullName = profile.getStudentNameLong(); - if (message.type == TYPE_SENT && recipient.readDate < 1) - readByAll = false; - } - if (!message.seen) { - app.db.metadataDao().setSeen(profile.getId(), message, true); - } - if (readByAll) { - // if a sent msg is not read by everyone, download it again to check the read status - new Handler(activityContext.getMainLooper()).post(() -> { - messageCallback.onSuccess(message); - }); - return; - } - } - - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - login(() -> Request.builder() - .url("https://" + loginServerName + ".mobidziennik.pl/dziennik/"+(message.type == TYPE_RECEIVED || message.type == TYPE_DELETED ? "wiadodebrana" : "wiadwyslana")+"/?id="+message.id) - .userAgent(System.getProperty("http.agent")) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 1720, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - if (data == null || data.equals("")) { - finishWithError(new AppError(TAG, 1727, CODE_MAINTENANCE, response)); - return; - } - if (data.contains("nie-pamietam-hasla")) { - lastLoginTime = 0; - finishWithError(new AppError(TAG, 1732, CODE_INVALID_LOGIN, response, data)); - return; - } - - List messageRecipientList = new ArrayList<>(); - - try { - Document doc = Jsoup.parse(data); - - Element content = doc.select("#content").first(); - - Element body = content.select(".wiadomosc_tresc").first(); - - if (message.type == TYPE_RECEIVED) { - MessageRecipientFull recipient = new MessageRecipientFull(profileId, -1, message.id); - long readDate = System.currentTimeMillis(); - Matcher matcher = Pattern.compile("czas przeczytania:.+?,\\s([0-9]+)\\s(.+?)\\s([0-9]{4}),\\sgodzina\\s([0-9:]+)", Pattern.DOTALL).matcher(body.html()); - if (matcher.find()) { - Date date = new Date( - strToInt(matcher.group(3)), - monthFromName(matcher.group(2)), - strToInt(matcher.group(1)) - ); - Time time = Time.fromH_m_s( - matcher.group(4) - ); - readDate = date.combineWith(time); - } - recipient.readDate = readDate; - recipient.fullName = profile.getStudentNameLong(); - messageRecipientList.add(recipient); - } - else { - message.senderId = -1; - message.senderReplyId = -1; - - List teacherList = app.db.teacherDao().getAllNow(profileId); - - Elements recipients = content.select("table.spis tr:has(td)"); - for (Element recipientEl: recipients) { - - Element senderEl = recipientEl.select("td:eq(0)").first(); - Teacher teacher = null; - String senderName = senderEl.text(); - for (Teacher aTeacher: teacherList) { - if (aTeacher.getFullNameLastFirst().equals(senderName)) { - teacher = aTeacher; - break; - } - } - - long readDate = 0; - Element isReadEl = recipientEl.select("td:eq(2)").first(); - if (!isReadEl.ownText().equals("NIE")) { - Element readDateEl = recipientEl.select("td:eq(3) small").first(); - Matcher matcher = Pattern.compile(".+?,\\s([0-9]+)\\s(.+?)\\s([0-9]{4}),\\sgodzina\\s([0-9:]+)", Pattern.DOTALL).matcher(readDateEl.ownText()); - if (matcher.find()) { - Date date = new Date( - strToInt(matcher.group(3)), - monthFromName(matcher.group(2)), - strToInt(matcher.group(1)) - ); - Time time = Time.fromH_m_s( - matcher.group(4) - ); - readDate = date.combineWith(time); - } - } - - MessageRecipientFull recipient = new MessageRecipientFull(profileId, teacher == null ? -1 : teacher.id, message.id); - recipient.readDate = readDate; - recipient.fullName = teacher == null ? "?" : teacher.getFullName(); - messageRecipientList.add(recipient); - } - } - - body.select("div").remove(); - - message.body = body.html(); - - message.clearAttachments(); - Elements attachments = content.select("ul li"); - if (attachments != null) { - //d(TAG, "attachments "+attachments.outerHtml()); - for (Element attachment: attachments) { - attachment = attachment.select("a").first(); - //d(TAG, "attachment "+attachment.outerHtml()); - String attachmentName = attachment.ownText(); - long attachmentId = -1; - Matcher matcher = Pattern.compile("href=\"https://.+?\\.mobidziennik.pl/.+?&(?:amp;)?zalacznik=([0-9]+)\"(?:.+?)(List) messageRecipientList); - - message.recipients = messageRecipientList; - - new Handler(activityContext.getMainLooper()).post(() -> { - messageCallback.onSuccess(message); - }); - } - }) - .build() - .enqueue()); - } - - @Override - public void getAttachment(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull MessageFull message, long attachmentId, @NonNull AttachmentGetCallback attachmentCallback) { - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - login(() -> { - Request.Builder builder = Request.builder() - .url("https://"+loginServerName+".mobidziennik.pl/dziennik/"+(message.type == TYPE_SENT ? "wiadwyslana" : "wiadodebrana")+"/?id="+message.id+"&zalacznik="+attachmentId); - new Handler(activityContext.getMainLooper()).post(() -> { - attachmentCallback.onSuccess(builder); - }); - }); - } - - @Override - public void getRecipientList(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull RecipientListGetCallback recipientListGetCallback) { - if (!prepare(activityContext, errorCallback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - if (System.currentTimeMillis() - profile.getLastReceiversSync() < 24 * 60 * 60 * 1000) { - AsyncTask.execute(() -> { - List teacherList = app.db.teacherDao().getAllNow(profileId); - new Handler(activityContext.getMainLooper()).post(() -> recipientListGetCallback.onSuccess(teacherList)); - }); - return; - } - - login(() -> { - Request.builder() - .url("https://" + loginServerName + ".mobidziennik.pl/mobile/dodajwiadomosc") - .userAgent(System.getProperty("http.agent")) - .callback(new TextCallbackHandler() { - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 2289, CODE_OTHER, response, throwable)); - } - - @Override - public void onSuccess(String data, Response response) { - if (data == null || data.equals("")) { - finishWithError(new AppError(TAG, 2295, CODE_MAINTENANCE, response)); - return; - } - if (data.contains("nie-pamietam-hasla")) { - lastLoginTime = 0; - finishWithError(new AppError(TAG, 2300, CODE_INVALID_LOGIN, response, data)); - return; - } - - teacherList = app.db.teacherDao().getAllNow(profileId); - - for (Teacher teacher: teacherList) { - teacher.typeDescription = null; // TODO: 2019-06-13 it better - } - - Matcher categoryMatcher = Pattern.compile("").matcher(data); - while (categoryMatcher.find()) { - String categoryId = categoryMatcher.group(1); - String categoryName = categoryMatcher.group(2); - String categoryHtml = getRecipientCategory(categoryId); - if (categoryHtml == null) - return; // the error callback is already executed - Matcher teacherMatcher = Pattern.compile("(.+?)|").matcher(categoryHtml); - while (teacherMatcher.find()) { - if (teacherMatcher.group(1) != null) { - String className = teacherMatcher.group(1); - String listHtml = teacherMatcher.group(2); - Matcher listMatcher = Pattern.compile("").matcher(listHtml); - while (listMatcher.find()) { - updateTeacher(categoryId, Long.parseLong(listMatcher.group(1)), listMatcher.group(2), categoryName, className); - } - } - else { - updateTeacher(categoryId, Long.parseLong(teacherMatcher.group(3)), teacherMatcher.group(4), categoryName, null); - } - } - } - app.db.teacherDao().addAll(teacherList); - - profile.setLastReceiversSync(System.currentTimeMillis()); - app.db.profileDao().add(profile); - - new Handler(activityContext.getMainLooper()).post(() -> recipientListGetCallback.onSuccess(new ArrayList<>(teacherList))); - } - }) - .build() - .enqueue(); - }); - } - private String getRecipientCategory(String categoryId) { - Response response = null; - try { - response = Request.builder() - .url("https://" + loginServerName + ".mobidziennik.pl/mobile/odbiorcyWiadomosci") - .userAgent(System.getProperty("http.agent")) - .addParameter("typ", categoryId) - .post() - .build().execute(); - if (response.code() != 200) { - finishWithError(new AppError(TAG, 2349, CODE_OTHER, response)); - return null; - } - String data = new TextCallbackHandler().backgroundParser(response); - if (data == null || data.equals("")) { - return ""; - } - if (data.contains("nie-pamietam-hasla")) { - lastLoginTime = 0; - finishWithError(new AppError(TAG, 2300, CODE_INVALID_LOGIN, response, data)); - return null; - } - return data; - } catch (Exception e) { - finishWithError(new AppError(TAG, 2355, CODE_OTHER, response, e)); - return null; - } - } - private void updateTeacher(String category, long id, String nameLastFirst, String typeDescription, String className) { - Teacher teacher = Teacher.getById(teacherList, id); - if (teacher == null) { - String[] nameParts = nameLastFirst.split(" ", Integer.MAX_VALUE); - teacher = new Teacher(profileId, id, nameParts[1], nameParts[0]); - teacherList.add(teacher); - } - teacher.loginId = String.valueOf(id); - teacher.type = 0; - if (className != null) { - teacher.typeDescription = bs("", teacher.typeDescription, ", ")+className; - } - switch (category) { - case "1": - teacher.setType(Teacher.TYPE_PRINCIPAL); - break; - case "2": - teacher.setType(Teacher.TYPE_TEACHER); - break; - case "8": - teacher.setType(Teacher.TYPE_EDUCATOR); - break; - case "9": - teacher.setType(Teacher.TYPE_PEDAGOGUE); - break; - case "10": - teacher.setType(Teacher.TYPE_OTHER); - teacher.typeDescription = "Specjaliści"; - break; - case "Administracja WizjaNet": - teacher.setType(Teacher.TYPE_SUPER_ADMIN); - break; - case "Administratorzy": - teacher.setType(Teacher.TYPE_SCHOOL_ADMIN); - break; - case "Sekretarka": - teacher.setType(Teacher.TYPE_SECRETARIAT); - break; - default: - teacher.setType(Teacher.TYPE_OTHER); - teacher.typeDescription = typeDescription+bs(" ", teacher.typeDescription); - break; - } - } - - @Override - public MessagesComposeInfo getComposeInfo(@NonNull ProfileFull profile) { - return new MessagesComposeInfo(-1, profile.getStudentData("attachmentSizeLimit", 6291456L), 100, -1); - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Vulcan.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Vulcan.java deleted file mode 100644 index c8dc1a7a..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Vulcan.java +++ /dev/null @@ -1,1687 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api; - -import android.content.Context; -import android.os.Build; -import android.os.Handler; -import android.util.Base64; -import android.util.Pair; -import android.util.SparseArray; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.crashlytics.android.Crashlytics; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonNull; -import com.google.gson.JsonObject; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.security.GeneralSecurityException; -import java.text.DecimalFormat; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import im.wangchao.mhttp.Request; -import im.wangchao.mhttp.Response; -import im.wangchao.mhttp.callback.JsonCallbackHandler; -import okhttp3.OkHttpClient; -import okio.Buffer; -import pl.szczodrzynski.edziennik.App; -import pl.szczodrzynski.edziennik.R; -import pl.szczodrzynski.edziennik.data.api.interfaces.AttachmentGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.EdziennikInterface; -import pl.szczodrzynski.edziennik.data.api.interfaces.LoginCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.MessageGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.RecipientListGetCallback; -import pl.szczodrzynski.edziennik.data.api.interfaces.SyncCallback; -import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance; -import pl.szczodrzynski.edziennik.data.db.modules.events.Event; -import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade; -import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson; -import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange; -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore; -import pl.szczodrzynski.edziennik.data.db.modules.messages.Message; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageFull; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipient; -import pl.szczodrzynski.edziennik.data.db.modules.messages.MessageRecipientFull; -import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata; -import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile; -import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileFull; -import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject; -import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher; -import pl.szczodrzynski.edziennik.data.db.modules.teams.Team; -import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesComposeInfo; -import pl.szczodrzynski.edziennik.utils.Utils; -import pl.szczodrzynski.edziennik.utils.models.Date; -import pl.szczodrzynski.edziennik.utils.models.Endpoint; -import pl.szczodrzynski.edziennik.utils.models.Time; -import pl.szczodrzynski.edziennik.utils.models.Week; - -import static pl.szczodrzynski.edziennik.data.api.AppError.CODE_OTHER; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_ABSENT; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_ABSENT_EXCUSED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_BELATED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_BELATED_EXCUSED; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_PRESENT; -import static pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.TYPE_RELEASED; -import static pl.szczodrzynski.edziennik.data.db.modules.events.Event.TYPE_EXAM; -import static pl.szczodrzynski.edziennik.data.db.modules.events.Event.TYPE_SHORT_QUIZ; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER1_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER2_FINAL; -import static pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_SEMESTER2_PROPOSED; -import static pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange.TYPE_CANCELLED; -import static pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonChange.TYPE_CHANGE; -import static pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore.LOGIN_TYPE_VULCAN; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_RECEIVED; -import static pl.szczodrzynski.edziennik.data.db.modules.messages.Message.TYPE_SENT; -import static pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata.TYPE_MESSAGE; -import static pl.szczodrzynski.edziennik.data.db.modules.notices.Notice.TYPE_NEUTRAL; -import static pl.szczodrzynski.edziennik.utils.Utils.c; -import static pl.szczodrzynski.edziennik.utils.Utils.crc16; -import static pl.szczodrzynski.edziennik.utils.Utils.d; -import static pl.szczodrzynski.edziennik.utils.Utils.getGradeValue; -import static pl.szczodrzynski.edziennik.utils.Utils.getVulcanGradeColor; -import static pl.szczodrzynski.edziennik.utils.Utils.intToStr; - -public class Vulcan implements EdziennikInterface { - public Vulcan(App app) { - this.app = app; - } - - private static final String TAG = "api.Vulcan"; - private static final String MOBILE_APP_NAME = "VULCAN-Android-ModulUcznia"; - private static final String MOBILE_APP_VERSION = "19.4.1.436"; - private static final String ENDPOINT_CERTIFICATE = "mobile-api/Uczen.v3.UczenStart/Certyfikat"; - private static final String ENDPOINT_STUDENT_LIST = "mobile-api/Uczen.v3.UczenStart/ListaUczniow"; - private static final String ENDPOINT_DICTIONARIES = "mobile-api/Uczen.v3.Uczen/Slowniki"; - private static final String ENDPOINT_TIMETABLE = "mobile-api/Uczen.v3.Uczen/PlanLekcjiZeZmianami"; - private static final String ENDPOINT_GRADES = "mobile-api/Uczen.v3.Uczen/Oceny"; - private static final String ENDPOINT_GRADES_PROPOSITIONS = "mobile-api/Uczen.v3.Uczen/OcenyPodsumowanie"; - private static final String ENDPOINT_EVENTS = "mobile-api/Uczen.v3.Uczen/Sprawdziany"; - private static final String ENDPOINT_HOMEWORK = "mobile-api/Uczen.v3.Uczen/ZadaniaDomowe"; - private static final String ENDPOINT_NOTICES = "mobile-api/Uczen.v3.Uczen/UwagiUcznia"; - private static final String ENDPOINT_ATTENDANCE = "mobile-api/Uczen.v3.Uczen/Frekwencje"; - private static final String ENDPOINT_MESSAGES_RECEIVED = "mobile-api/Uczen.v3.Uczen/WiadomosciOdebrane"; - private static final String ENDPOINT_MESSAGES_SENT = "mobile-api/Uczen.v3.Uczen/WiadomosciWyslane"; - private static final String ENDPOINT_MESSAGES_CHANGE_STATUS = "mobile-api/Uczen.v3.Uczen/ZmienStatusWiadomosci"; - private static final String ENDPOINT_PUSH = "mobile-api/Uczen.v3.Uczen/UstawPushToken"; - private static final String userAgent = "MobileUserAgent"; - - private App app; - private Context activityContext = null; - private SyncCallback callback = null; - private int profileId = -1; - private Profile profile = null; - private LoginStore loginStore = null; - private boolean fullSync = true; - private Date today = Date.getToday(); - private List targetEndpoints = new ArrayList<>(); - - // PROGRESS - private static final int PROGRESS_LOGIN = 10; - private int PROGRESS_COUNT = 1; - private int PROGRESS_STEP = (90/PROGRESS_COUNT); - - private int onlyFeature = FEATURE_ALL; - - private List teamList; - private List teacherList; - private List subjectList; - private List lessonList; - private List lessonChangeList; - private List gradeCategoryList; - private List gradeList; - private List eventList; - private List noticeList; - private List attendanceList; - private List messageList; - private List messageRecipientList; - private List messageRecipientIgnoreList; - private List metadataList; - private List messageMetadataList; - - private String apiUrl = null; - private String certificateKey = null; - private String certificatePfx = null; - private String deviceToken = null; - private String deviceSymbol = null; - private String devicePin = null; - /** - * deviceSymbol_JednostkaSprawozdawczaSymbol - */private String schoolName = null; - /** - * JednostkaSprawozdawczaSymbol - */private String schoolSymbol = null; - /** - * Id - */private int studentId = -1; - /** - * UzytkownikLoginId - */private int studentLoginId = -1; - /** - * IdOddzial - */private int studentClassId = -1; - /** - * IdOkresKlasyfikacyjny - */private int studentSemesterId = -1; - /** - * OkresNumer - */private int studentSemesterNumber = -1; - private boolean syncingSemester1 = false; - private OkHttpClient signingHttp = null; - private Date oneMonthBack = today.clone().stepForward(0, -1, 0); - - /* _____ - | __ \ - | |__) | __ ___ _ __ __ _ _ __ ___ - | ___/ '__/ _ \ '_ \ / _` | '__/ _ \ - | | | | | __/ |_) | (_| | | | __/ - |_| |_| \___| .__/ \__,_|_| \___| - | | - |*/ - private boolean prepare(@NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable Profile profile, @NonNull LoginStore loginStore) { - this.activityContext = activityContext; - this.callback = callback; - this.profileId = profileId; - // here we must have a login store: either with a correct ID or -1 - // there may be no profile and that's when onLoginFirst happens - this.profile = profile; - this.loginStore = loginStore; - this.fullSync = profile == null || profile.getEmpty() || profile.shouldFullSync(activityContext); - this.today = Date.getToday(); - - if (this.signingHttp == null) { - this.signingHttp = this.app.http.newBuilder().addInterceptor(chain -> { - okhttp3.Request request = chain.request(); - Buffer buffer = new Buffer(); - assert request.body() != null; - request.body().writeTo(buffer); - String signature = ""; - // cannot use Utils.exception, because we are not in the main thread here! - // Utils.exception would show the dialog which has to be shown in the UI thread - try { - signature = Utils.VulcanRequestEncryptionUtils.signContent(buffer.readByteArray(), new ByteArrayInputStream(Base64.decode(this.certificatePfx, Base64.DEFAULT))); - } catch (IOException e) { - e.printStackTrace(); - } catch (GeneralSecurityException e) { - e.printStackTrace(); - } - request = request.newBuilder().addHeader("RequestSignatureValue", signature).addHeader("RequestCertificateKey", this.certificateKey).build(); - return chain.proceed(request); - }).build(); - } - - - if (certificateKey == null || certificatePfx == null || apiUrl == null) { - c(TAG, "first login, use TOKEN, SYMBOL, PIN"); - - } - - teamList = profileId == -1 ? new ArrayList<>() : app.db.teamDao().getAllNow(profileId); - teacherList = profileId == -1 ? new ArrayList<>() : app.db.teacherDao().getAllNow(profileId); - subjectList = new ArrayList<>(); - lessonList = new ArrayList<>(); - lessonChangeList = new ArrayList<>(); - gradeCategoryList = new ArrayList<>(); - gradeList = new ArrayList<>(); - eventList = new ArrayList<>(); - noticeList = new ArrayList<>(); - attendanceList = new ArrayList<>(); - messageList = new ArrayList<>(); - messageRecipientList = new ArrayList<>(); - messageRecipientIgnoreList = new ArrayList<>(); - metadataList = new ArrayList<>(); - messageMetadataList = new ArrayList<>(); - - return true; - } - - @Override - public void sync(@NonNull Context activityContext, @NonNull SyncCallback callback, int profileId, @Nullable Profile profile, @NonNull LoginStore loginStore) { - if (!prepare(activityContext, callback, profileId, profile, loginStore)) - return; - - login(() -> { - targetEndpoints = new ArrayList<>(); - targetEndpoints.add("SetPushToken"); - targetEndpoints.add("Dictionaries"); - targetEndpoints.add("Timetable"); - targetEndpoints.add("Grades"); - targetEndpoints.add("ProposedGrades"); - targetEndpoints.add("Events"); - targetEndpoints.add("Homework"); - targetEndpoints.add("Notices"); - targetEndpoints.add("Attendance"); - targetEndpoints.add("MessagesInbox"); - targetEndpoints.add("MessagesOutbox"); - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - @Override - public void syncFeature(@NonNull Context activityContext, @NonNull SyncCallback callback, @NonNull ProfileFull profile, int ... featureList) { - if (featureList == null) { - sync(activityContext, callback, profile.getId(), profile, LoginStore.fromProfileFull(profile)); - return; - } - if (!prepare(activityContext, callback, profile.getId(), profile, LoginStore.fromProfileFull(profile))) - return; - - login(() -> { - targetEndpoints = new ArrayList<>(); - if (featureList.length == 1) - onlyFeature = featureList[0]; - targetEndpoints.add("SetPushToken"); - targetEndpoints.add("Dictionaries"); - for (int feature: featureList) { - switch (feature) { - case FEATURE_TIMETABLE: - targetEndpoints.add("Timetable"); - break; - case FEATURE_AGENDA: - targetEndpoints.add("Events"); - break; - case FEATURE_GRADES: - targetEndpoints.add("Grades"); - targetEndpoints.add("ProposedGrades"); - break; - case FEATURE_HOMEWORK: - targetEndpoints.add("Homework"); - break; - case FEATURE_NOTICES: - targetEndpoints.add("Notices"); - break; - case FEATURE_ATTENDANCE: - targetEndpoints.add("Attendance"); - break; - case FEATURE_MESSAGES_INBOX: - targetEndpoints.add("MessagesInbox"); - break; - case FEATURE_MESSAGES_OUTBOX: - targetEndpoints.add("MessagesOutbox"); - break; - } - } - targetEndpoints.add("Finish"); - PROGRESS_COUNT = targetEndpoints.size()-1; - PROGRESS_STEP = (90/PROGRESS_COUNT); - begin(); - }); - } - - private void begin() { - schoolName = profile.getStudentData("schoolName", null); - schoolSymbol = profile.getStudentData("schoolSymbol", null); - studentId = profile.getStudentData("studentId", -1); - studentLoginId = profile.getStudentData("studentLoginId", -1); - studentClassId = profile.getStudentData("studentClassId", -1); - studentSemesterId = profile.getStudentData("studentSemesterId", -1); - studentSemesterNumber = profile.getStudentData("studentSemesterNumber", profile.getCurrentSemester()); - if (profile.getEmpty() && studentSemesterNumber == 2) { - syncingSemester1 = true; - studentSemesterId -= 1; - studentSemesterNumber -= 1; - } - else { - syncingSemester1 = false; - } - d(TAG, "Syncing student "+studentId+", class "+studentClassId+", semester "+studentSemesterId); - - callback.onProgress(PROGRESS_LOGIN); - - r("get", null); - } - - private void r(String type, String endpoint) { - // endpoint == null when beginning - if (endpoint == null) - endpoint = targetEndpoints.get(0); - int index = -1; - for (String request: targetEndpoints) { - index++; - if (request.equals(endpoint)) { - break; - } - } - if (type.equals("finish")) { - // called when finishing the action - callback.onProgress(PROGRESS_STEP); - index++; - } - d(TAG, "Called r("+type+", "+endpoint+"). Getting "+targetEndpoints.get(index)); - switch (targetEndpoints.get(index)) { - case "SetPushToken": - setPushToken(); - break; - case "Dictionaries": - getDictionaries(); - break; - case "Timetable": - getTimetable(); - break; - case "Grades": - getGrades(); - break; - case "ProposedGrades": - getProposedGrades(); - break; - case "Events": - getEvents(); - break; - case "Homework": - getHomework(); - break; - case "Notices": - getNotices(); - break; - case "Attendance": - getAttendance(); - break; - case "MessagesInbox": - getMessagesInbox(); - break; - case "MessagesOutbox": - getMessagesOutbox(); - break; - case "Finish": - finish(); - break; - } - } - private void saveData() { - if (teamList.size() > 0) { - app.db.teamDao().addAll(teamList); - } - if (teacherList.size() > 0) - app.db.teacherDao().addAll(teacherList); - if (subjectList.size() > 0) - app.db.subjectDao().addAll(subjectList); - if (lessonList.size() > 0) { - app.db.lessonDao().clear(profileId); - app.db.lessonDao().addAll(lessonList); - } - if (lessonChangeList.size() > 0) - app.db.lessonChangeDao().addAll(lessonChangeList); - if (gradeCategoryList.size() > 0) - app.db.gradeCategoryDao().addAll(gradeCategoryList); - if (gradeList.size() > 0) { - app.db.gradeDao().clearForSemester(profileId, studentSemesterNumber); - app.db.gradeDao().addAll(gradeList); - } - if (eventList.size() > 0) { - app.db.eventDao().removeFuture(profileId, Date.getToday()); - app.db.eventDao().addAll(eventList); - } - if (noticeList.size() > 0) { - app.db.noticeDao().clearForSemester(profileId, studentSemesterNumber); - app.db.noticeDao().addAll(noticeList); - } - if (attendanceList.size() > 0) { - app.db.attendanceDao().clearAfterDate(profileId, getCurrentSemesterStartDate()); - app.db.attendanceDao().addAll(attendanceList); - } - if (messageList.size() > 0) - app.db.messageDao().addAllIgnore(messageList); - if (messageRecipientList.size() > 0) - app.db.messageRecipientDao().addAll(messageRecipientList); - if (messageRecipientIgnoreList.size() > 0) - app.db.messageRecipientDao().addAllIgnore(messageRecipientIgnoreList); - if (metadataList.size() > 0) - app.db.metadataDao().addAllIgnore(metadataList); - if (messageMetadataList.size() > 0) - app.db.metadataDao().setSeen(messageMetadataList); - } - private void finish() { - if (syncingSemester1) { - syncingSemester1 = false; - studentSemesterId += 1; - studentSemesterNumber += 1; - // no need to download dictionaries again - r("get", null);// TODO: 2019-06-04 start with Timetables or first element (if partial sync) - return; - } - try { - saveData(); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 425, CODE_OTHER, app.getString(R.string.sync_error_saving_data), null, null, e, null)); - } - if (fullSync) { - profile.setLastFullSync(System.currentTimeMillis()); - fullSync = false; - } - profile.setEmpty(false); - callback.onSuccess(activityContext, new ProfileFull(profile, loginStore)); - } - private void finishWithError(AppError error) { - try { - saveData(); - } - catch (Exception e) { - Crashlytics.logException(e); - } - callback.onError(activityContext, error); - } - - /* _ _ - | | (_) - | | ___ __ _ _ _ __ - | | / _ \ / _` | | '_ \ - | |___| (_) | (_| | | | | | - |______\___/ \__, |_|_| |_| - __/ | - |__*/ - private void login(@NonNull LoginCallback loginCallback) { - if (profile == null) { - // FIRST LOGIN - this.deviceToken = loginStore.getLoginData("deviceToken", null); - this.deviceSymbol = loginStore.getLoginData("deviceSymbol", null); - this.devicePin = loginStore.getLoginData("devicePin", null); - if (deviceToken == null || deviceSymbol == null || devicePin == null) { - finishWithError(new AppError(TAG, 443, AppError.CODE_INVALID_LOGIN, "Login field is empty")); - return; - } - - setApiUrl(deviceToken, deviceSymbol); - getCertificate(deviceToken, devicePin, ((certificateKey, certificatePfx, apiUrl, userLogin, userName) -> { - loginStore.removeLoginData("deviceToken"); - loginStore.removeLoginData("devicePin"); - loginStore.putLoginData("certificateKey", certificateKey); - loginStore.putLoginData("certificatePfx", certificatePfx); - loginStore.putLoginData("apiUrl", apiUrl); - loginStore.putLoginData("userLogin", userLogin); - this.apiUrl = apiUrl; - this.certificateKey = certificateKey; - this.certificatePfx = certificatePfx; - this.deviceToken = null; - this.devicePin = null; - - c(TAG, "first login. get the list of students"); - getStudentList(usersMap -> { - List profileList = new ArrayList<>(); - for (int index = 0; index < usersMap.size(); index++) { - JsonObject account = new ArrayList<>(usersMap.values()).get(index); - - Profile newProfile = new Profile(); - newProfile.setEmpty(true); - saveStudentData(newProfile, account); - - profileList.add(newProfile); - } - callback.onLoginFirst(profileList, loginStore); - }, false); - - })); - } - else { - this.apiUrl = loginStore.getLoginData("apiUrl", null); - this.certificateKey = loginStore.getLoginData("certificateKey", null); - this.certificatePfx = loginStore.getLoginData("certificatePfx", null); - if (apiUrl == null || certificateKey == null || certificatePfx == null) { - finishWithError(new AppError(TAG, 489, AppError.CODE_INVALID_LOGIN, "Login field is empty")); - return; - } - if (profile.getStudentData("currentSemesterEndDate", System.currentTimeMillis()/*default always larger*/) < System.currentTimeMillis()/1000 - || (studentLoginId = profile.getStudentData("studentLoginId", -1)) == -1) { - // current semester is over, we need to get student data again - callback.onActionStarted(R.string.sync_action_getting_account); - getStudentList(usersMap -> { - studentId = profile.getStudentData("studentId", -1); - d(TAG, "Searching the student ID "+studentId); - JsonObject foundUser = null; - for (JsonObject user: usersMap.values()) { - if (user.get("Id").getAsInt() == studentId) { - foundUser = user; - } - } - if (foundUser == null || foundUser.get("IdOkresKlasyfikacyjny").getAsInt() == 0) { - d(TAG, "Not found"); - finishWithError(new AppError(TAG, 507, AppError.CODE_OTHER, app.getString(R.string.error_register_student_no_term), usersMap.toString())); - } - else { - d(TAG, "Saving updated student data"); - saveStudentData(profile, foundUser); - loginCallback.onSuccess(); - } - }, true); - } - else { - loginCallback.onSuccess(); - } - } - } - - /* _ _ _ _ _ _ _ - | | | | | | ___ | | | | | | - | |__| | ___| |_ __ ___ _ __ ___ ( _ ) ___ __ _| | | |__ __ _ ___| | _____ - | __ |/ _ \ | '_ \ / _ \ '__/ __| / _ \/\ / __/ _` | | | '_ \ / _` |/ __| |/ / __| - | | | | __/ | |_) | __/ | \__ \ | (_> < | (_| (_| | | | |_) | (_| | (__| <\__ \ - |_| |_|\___|_| .__/ \___|_| |___/ \___/\/ \___\__,_|_|_|_.__/ \__,_|\___|_|\_\___/ - | | - |*/ - private void getCertificate(String token, String pin, CertificateCallback certificateCallback) { - callback.onActionStarted(R.string.sync_action_getting_certificate); - d(TAG, "Post JSON "+apiUrl + ENDPOINT_CERTIFICATE); - if (apiUrl == null) { - finishWithError(new AppError(TAG, 1215, AppError.CODE_INVALID_TOKEN, "Empty apiUrl(1)")); - return; - } - Request.builder() - .url(apiUrl + ENDPOINT_CERTIFICATE) - .userAgent(userAgent) - .addParameter("PIN", pin) - .addParameter("TokenKey", token) - .addParameter("AppVersion", MOBILE_APP_VERSION) - .addParameter("DeviceId", UUID.randomUUID().toString()) - .addParameter("DeviceName", "Szkolny.eu "+Build.MODEL) - .addParameter("DeviceNameUser", "") - .addParameter("DeviceDescription", "") - .addParameter("DeviceSystemType", "Android") - .addParameter("DeviceSystemVersion", Build.VERSION.RELEASE) - .addParameter("RemoteMobileTimeKey", getUnixTime()) - .addParameter("TimeKey", getUnixTime() - 1) - .addParameter("RequestId", UUID.randomUUID().toString()) - .addParameter("RemoteMobileAppVersion", MOBILE_APP_VERSION) - .addParameter("RemoteMobileAppName", MOBILE_APP_NAME) - .postJson() - .addHeader("RequestMobileType", "RegisterDevice") - .callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - finishWithError(new AppError(TAG, 1241, AppError.CODE_MAINTENANCE, response)); - return; - } - d(TAG, "Certificate data "+data.toString()); - boolean isError = data.get("IsError").getAsBoolean(); - if (isError) { - JsonElement message = data.get("Message"); - JsonElement tokenStatus = data.get("TokenStatus"); - String msg = null; - String tokenStatusStr = null; - if (message != null) { - msg = message.getAsString(); - } - if (tokenStatus != null) { - tokenStatusStr = tokenStatus.getAsString(); - } - if ("TokenNotFound".equals(msg)) { - finishWithError(new AppError(TAG, 1258, AppError.CODE_INVALID_TOKEN, response, data)); - return; - } - if ("TokenDead".equals(msg)) { - finishWithError(new AppError(TAG, 1262, AppError.CODE_EXPIRED_TOKEN, response, data)); - return; - } - if ("WrongPIN".equals(tokenStatusStr)) { - Matcher matcher = Pattern.compile("Liczba pozostałych prób: ([0-9])", Pattern.DOTALL).matcher(tokenStatusStr); - finishWithError(new AppError(TAG, 1267, AppError.CODE_INVALID_PIN, matcher.matches() ? matcher.group(1) : "?", response, data)); - return; - } - if ("Broken".equals(tokenStatusStr)) { - finishWithError(new AppError(TAG, 1262, AppError.CODE_INVALID_PIN, "0", response, data)); - return; - } - finishWithError(new AppError(TAG, 1274, AppError.CODE_OTHER, "Sprawdź wprowadzone dane.\n\nBłąd certyfikatu "+(message == null ? "" : message.getAsString()), response, data)); - return; - } - JsonElement tokenCert = data.get("TokenCert"); - if (tokenCert == null || tokenCert instanceof JsonNull) { - finishWithError(new AppError(TAG, 1279, AppError.CODE_OTHER, "Sprawdź wprowadzone dane.\n\nCertificate error. TokenCert null", response, data)); - return; - } - data = tokenCert.getAsJsonObject(); - try { - certificateCallback.onSuccess( - data.get("CertyfikatKlucz").getAsString(), - data.get("CertyfikatPfx").getAsString(), - data.get("AdresBazowyRestApi").getAsString(), - data.get("UzytkownikLogin").getAsString(), - data.get("UzytkownikNazwa").getAsString() - ); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1293, AppError.CODE_OTHER, response, e, data)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - if (response.code() == 400) { - finishWithError(new AppError(TAG, 1300, AppError.CODE_INVALID_SYMBOL, response, throwable)); - return; - } - finishWithError(new AppError(TAG, 1303, AppError.CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private void apiRequest(String endpoint, JsonObject payload, ApiRequestCallback apiRequestCallback) { - d(TAG, "API request "+apiUrl + endpoint); - if (apiUrl == null) { - finishWithError(new AppError(TAG, 1313, AppError.CODE_OTHER, app.getString(R.string.sync_error_no_api_url), "Empty apiUrl(2)")); - return; - } - if (payload == null) - payload = new JsonObject(); - payload.addProperty("RemoteMobileTimeKey", getUnixTime()); - payload.addProperty("TimeKey", getUnixTime() - 1); - payload.addProperty("RequestId", UUID.randomUUID().toString()); - payload.addProperty("RemoteMobileAppVersion", MOBILE_APP_VERSION); - payload.addProperty("RemoteMobileAppName", MOBILE_APP_NAME); - Request.builder() - .url(apiUrl + endpoint) - .userAgent(userAgent) - .withClient(signingHttp) - .setJsonBody(payload) - .callback(new JsonCallbackHandler() { - @Override - public void onSuccess(JsonObject data, Response response) { - if (data == null) { - finishWithError(new AppError(TAG, 1332, AppError.CODE_MAINTENANCE, response)); - return; - } - try { - apiRequestCallback.onSuccess(data); - } - catch (Exception e) { - finishWithError(new AppError(TAG, 1339, AppError.CODE_OTHER, response, e, data)); - } - } - - @Override - public void onFailure(Response response, Throwable throwable) { - finishWithError(new AppError(TAG, 1345, AppError.CODE_OTHER, response, throwable)); - } - }) - .build() - .enqueue(); - } - - private void getStudentList(StudentListCallback studentListCallback, boolean reportStudentsWithNoSemester) { - apiRequest(ENDPOINT_STUDENT_LIST, null, data -> { - d(TAG, "Got: " + data.toString()); - JsonElement listEl = data.get("Data"); - JsonArray accountList; - if (listEl == null || listEl instanceof JsonNull || (accountList = listEl.getAsJsonArray()).size() == 0) { - finishWithError(new AppError(TAG, 1369, AppError.CODE_OTHER, app.getString(R.string.sync_error_register_no_students), data)); - return; - } - LinkedHashMap usersMap = new LinkedHashMap<>(); - for (JsonElement userEl : accountList) { - JsonObject user = userEl.getAsJsonObject(); - if (reportStudentsWithNoSemester || (user != null && user.get("IdOkresKlasyfikacyjny").getAsInt() != 0)) { - usersMap.put(user.get("Imie").getAsString() + " " + user.get("Nazwisko").getAsString() + " " + (user.get("OddzialKod") instanceof JsonNull ? "" : user.get("OddzialKod").getAsString()), user); - } else if (accountList.size() == 1) { - finishWithError(new AppError(TAG, 1377, AppError.CODE_OTHER, app.getString(R.string.error_register_student_no_term), data)); - return; - } - } - studentListCallback.onSuccess(usersMap); - }); - } - - private void saveStudentData(Profile targetProfile, JsonObject account) { - String firstName = account.get("Imie").getAsString(); - String lastName = account.get("Nazwisko").getAsString(); - schoolName = loginStore.getLoginData("deviceSymbol", getSymbolFromApiUrl())+"_"+account.get("JednostkaSprawozdawczaSymbol").getAsString(); - schoolSymbol = account.get("JednostkaSprawozdawczaSymbol").getAsString(); - studentId = account.get("Id").getAsInt(); - studentLoginId = account.get("UzytkownikLoginId").getAsInt(); - studentClassId = account.get("IdOddzial").getAsInt(); - studentSemesterId = account.get("IdOkresKlasyfikacyjny").getAsInt(); - String studentClassName = account.get("OkresPoziom").getAsInt()+account.get("OddzialSymbol").getAsString(); - targetProfile.putStudentData("userName", account.get("UzytkownikNazwa").getAsString()); - targetProfile.putStudentData("schoolName", schoolName); - targetProfile.putStudentData("schoolSymbol", schoolSymbol); - targetProfile.putStudentData("studentId", studentId); - targetProfile.putStudentData("studentLoginId", studentLoginId); - targetProfile.putStudentData("studentClassId", studentClassId); - targetProfile.putStudentData("studentClassName", studentClassName); - targetProfile.putStudentData("studentSemesterId", studentSemesterId); - targetProfile.putStudentData("currentSemesterEndDate", account.get("OkresDataDo").getAsInt()+86400); - targetProfile.putStudentData("studentSemesterNumber", account.get("OkresNumer").getAsInt()); - targetProfile.setCurrentSemester(account.get("OkresNumer").getAsInt()); - if (targetProfile.getCurrentSemester() == 1) { - targetProfile.setDateSemester1Start(Date.fromMillis((long) account.get("OkresDataOd").getAsInt() * 1000)); - targetProfile.setDateSemester2Start(Date.fromMillis(((long) account.get("OkresDataDo").getAsInt() + 86400) * 1000)); - } - else if (targetProfile.getCurrentSemester() == 2) { - targetProfile.setDateSemester2Start(Date.fromMillis((long) account.get("OkresDataOd").getAsInt() * 1000)); - targetProfile.setDateYearEnd(Date.fromMillis(((long) account.get("OkresDataDo").getAsInt() + 86400) * 1000)); - } - //db.teamDao().add(new Team(profileId, studentClassId, account.get("OddzialKod").getAsString(), 1, -1)); - targetProfile.setStudentNameLong(firstName + " " + lastName); - targetProfile.setStudentNameShort(firstName + " " + lastName.charAt(0) + "."); - if (targetProfile.getName() == null || targetProfile.getName().equals("")) { - targetProfile.setName(targetProfile.getStudentNameLong()); - } - targetProfile.setSubname(account.get("UzytkownikLogin").getAsString()); - } - - private Team searchTeam(String name, String code, long teacherId) { - Team team; - team = Team.getByName(teamList, name); - - if (team == null) { - team = new Team(profileId, crc16(name.getBytes()), name, 2, code, teacherId); - teamList.add(team); - } - else { - team.teacherId = teacherId; - } - return team; - } - - public interface CertificateCallback { - void onSuccess(String certificateKey, String certificatePfx, String apiUrl, String userLogin, String userName); - } - private interface StudentListCallback { - void onSuccess(LinkedHashMap usersMap); - } - private interface ApiRequestCallback { - void onSuccess(JsonObject data); - } - - private String getSymbolFromApiUrl() { - String[] parts = apiUrl.split("/"); - return parts[parts.length - 2]; - } - - private String getClassTeamName() { - if (teamList.size() == 0) - return ""; - if (teamList.get(0).type != 1) - return ""; - return teamList.get(0).name; - } - - private long getClassTeamId() { - if (teamList.size() == 0) - return -1; - if (teamList.get(0).type != 1) - return -1; - return teamList.get(0).id; - } - - private Date getCurrentSemesterStartDate() { - return profile.getSemesterStart(studentSemesterNumber); - } - - private Date getCurrentSemesterEndDate() { - return profile.getSemesterEnd(studentSemesterNumber); - } - - private long getUnixTime() { - return System.currentTimeMillis() / 1000; - } - - private void setApiUrl(String token, String symbol) { - String rule = token.substring(0, 3); - switch (rule) { - case "3S1": - if (!App.devMode) - apiUrl = "https://lekcjaplus.vulcan.net.pl/"+symbol+"/"; - else - apiUrl = "http://hack.szkolny.eu/"+symbol+"/"; - break; - case "TA1": - apiUrl = "https://uonetplus-komunikacja.umt.tarnow.pl/"+symbol+"/"; - break; - case "OP1": - apiUrl = "https://uonetplus-komunikacja.eszkola.opolskie.pl/"+symbol+"/"; - break; - case "RZ1": - apiUrl = "https://uonetplus-komunikacja.resman.pl/"+symbol+"/"; - break; - case "GD1": - apiUrl = "https://uonetplus-komunikacja.edu.gdansk.pl/"+symbol+"/"; - break; - case "KA1": - apiUrl = "https://uonetplus-komunikacja.mcuw.katowice.eu/"+symbol+"/"; - break; - case "KA2": - apiUrl = "https://uonetplus-komunikacja-test.mcuw.katowice.eu/"+symbol+"/"; - break; - case "P03": - apiUrl = "https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl/"+symbol+"/"; - break; - case "P01": - apiUrl = "http://efeb-komunikacja.pro-hudson.win.vulcan.pl/"+symbol+"/"; - break; - case "P02": - apiUrl = "http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl/"+symbol+"/"; - break; - case "P90": - apiUrl = "http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl/"+symbol+"/"; - break; - case "FK1": - apiUrl = "http://api.fakelog.cf/"+symbol+"/"; - break; - case "SZ9": - //apiUrl = "http://vulcan.szkolny.eu/"+symbol+"/"; - break; - default: - apiUrl = null; - break; - } - } - - /* _____ _ _____ _ - | __ \ | | | __ \ | | - | | | | __ _| |_ __ _ | |__) |___ __ _ _ _ ___ ___| |_ ___ - | | | |/ _` | __/ _` | | _ // _ \/ _` | | | |/ _ \/ __| __/ __| - | |__| | (_| | || (_| | | | \ \ __/ (_| | |_| | __/\__ \ |_\__ \ - |_____/ \__,_|\__\__,_| |_| \_\___|\__, |\__,_|\___||___/\__|___/ - | | - |*/ - private void setPushToken() { - String token; - Pair> pair = app.appConfig.fcmTokens.get(LOGIN_TYPE_VULCAN); - if (pair == null || (token = pair.first) == null || (pair.second != null && pair.second.contains(profileId))) { - r("finish", "SetPushToken"); - return; - } - callback.onActionStarted(R.string.sync_action_setting_push_token); - JsonObject json = new JsonObject(); - json.addProperty("IdUczen", studentId); - json.addProperty("Token", token); - json.addProperty("PushOcena", true); - json.addProperty("PushFrekwencja", true); - json.addProperty("PushUwaga", true); - json.addProperty("PushWiadomosc", true); - apiRequest(schoolSymbol+"/"+ENDPOINT_PUSH, json, result -> { - if (pair.second == null) { - List list = new ArrayList<>(); - list.add(profileId); - app.appConfig.fcmTokens.put(LOGIN_TYPE_VULCAN, new Pair<>(token, list)); - } - else { - pair.second.add(profileId); - app.appConfig.fcmTokens.put(LOGIN_TYPE_VULCAN, new Pair<>(token, pair.second)); - } - r("finish", "SetPushToken"); - }); - } - - private SparseArray> lessonRanges = new SparseArray<>(); - private SparseArray noticeCategories = new SparseArray<>(); - private SparseArray> attendanceCategories = new SparseArray<>(); - private void getDictionaries() { - callback.onActionStarted(R.string.sync_action_syncing_dictionaries); - if (teamList.size() == 0 || teamList.get(0).type != 1) { - String name = profile.getStudentData("studentClassName", null); - if (name != null) { - long id = crc16(name.getBytes()); - teamList.add(new Team(profileId, id, name, 1, schoolName + ":" + name, -1)); - } - } - apiRequest(schoolSymbol+"/"+ENDPOINT_DICTIONARIES, null, result -> { - JsonObject data = result.getAsJsonObject("Data"); - JsonArray teachers = data.getAsJsonArray("Nauczyciele"); - JsonArray subjects = data.getAsJsonArray("Przedmioty"); - JsonArray lessonRanges = data.getAsJsonArray("PoryLekcji"); - JsonArray gradeCategories = data.getAsJsonArray("KategorieOcen"); - JsonArray noticeCategories = data.getAsJsonArray("KategorieUwag"); - JsonArray attendanceCategories = data.getAsJsonArray("KategorieFrekwencji"); - - for (JsonElement teacherEl : teachers) { - JsonObject teacher = teacherEl.getAsJsonObject(); - JsonElement loginId = teacher.get("LoginId"); - teacherList.add( - new Teacher( - profileId, - teacher.get("Id").getAsInt(), - teacher.get("Imie").getAsString(), - teacher.get("Nazwisko").getAsString(), - loginId == null || loginId instanceof JsonNull ? "-1" : loginId.getAsString() - ) - ); - } - - for (JsonElement subjectEl : subjects) { - JsonObject subject = subjectEl.getAsJsonObject(); - subjectList.add( - new Subject( - profileId, - subject.get("Id").getAsInt(), - subject.get("Nazwa").getAsString(), - subject.get("Kod").getAsString() - ) - ); - } - - this.lessonRanges.clear(); - for (JsonElement lessonRangeEl : lessonRanges) { - JsonObject lessonRange = lessonRangeEl.getAsJsonObject(); - this.lessonRanges.put( - lessonRange.get("Id").getAsInt(), - new Pair<>( - Time.fromH_m(lessonRange.get("PoczatekTekst").getAsString()), - Time.fromH_m(lessonRange.get("KoniecTekst").getAsString()) - ) - ); - } - - for (JsonElement gradeCategoryEl : gradeCategories) { - JsonObject gradeCategory = gradeCategoryEl.getAsJsonObject(); - gradeCategoryList.add( - new GradeCategory( - profileId, - gradeCategory.get("Id").getAsInt(), - -1, - -1, - gradeCategory.get("Nazwa").getAsString() - ) - ); - } - - this.noticeCategories.clear(); - for (JsonElement noticeCategoryEl : noticeCategories) { - JsonObject noticeCategory = noticeCategoryEl.getAsJsonObject(); - this.noticeCategories.put( - noticeCategory.get("Id").getAsInt(), - noticeCategory.get("Nazwa").getAsString() - ); - } - - this.attendanceCategories.clear(); - for (JsonElement attendanceCategoryEl : attendanceCategories) { - JsonObject attendanceCategory = attendanceCategoryEl.getAsJsonObject(); - int type = -1; - boolean absent = attendanceCategory.get("Nieobecnosc").getAsBoolean(); - boolean excused = attendanceCategory.get("Usprawiedliwione").getAsBoolean(); - if (absent) { - type = excused ? TYPE_ABSENT_EXCUSED : TYPE_ABSENT; - } - else { - if (attendanceCategory.get("Spoznienie").getAsBoolean()) { - type = excused ? TYPE_BELATED_EXCUSED : TYPE_BELATED; - } - else if (attendanceCategory.get("Zwolnienie").getAsBoolean()) { - type = TYPE_RELEASED; - } - else if (attendanceCategory.get("Obecnosc").getAsBoolean()) { - type = TYPE_PRESENT; - } - } - this.attendanceCategories.put( - attendanceCategory.get("Id").getAsInt(), - new Pair<>( - type, - attendanceCategory.get("Nazwa").getAsString() - ) - ); - } - r("finish", "Dictionaries"); - }); - } - - private void getTimetable() { - if (syncingSemester1) { - // we are in semester 2. we're syncing semester 1 to show old data. skip the timetable. - r("finish", "Timetable"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_timetable); - JsonObject json = new JsonObject(); - json.addProperty("IdUczen", studentId); - - Date weekStart = Week.getWeekStart(); - if (Date.getToday().getWeekDay() > 4) { - weekStart.stepForward(0, 0, 7); - } - Date weekEnd = weekStart.clone().stepForward(0, 0, 6); - - json.addProperty("DataPoczatkowa", weekStart.getStringY_m_d()); - json.addProperty("DataKoncowa", weekEnd.getStringY_m_d()); - json.addProperty("IdOddzial", studentClassId); - json.addProperty("IdOkresKlasyfikacyjny", studentSemesterId); - apiRequest(schoolSymbol+"/"+ENDPOINT_TIMETABLE, json, result -> { - JsonArray lessons = result.getAsJsonArray("Data"); - - for (JsonElement lessonEl: lessons) { - JsonObject lesson = lessonEl.getAsJsonObject(); - - if (!lesson.get("PlanUcznia").getAsBoolean()) { - continue; - } - - Date lessonDate = Date.fromY_m_d(lesson.get("DzienTekst").getAsString()); - Pair lessonRange = lessonRanges.get(lesson.get("IdPoraLekcji").getAsInt()); - Lesson lessonObject = new Lesson( - profileId, - lessonDate.getWeekDay(), - lessonRange.first, - lessonRange.second - ); - - JsonElement subject = lesson.get("IdPrzedmiot"); - if (!(subject instanceof JsonNull)) { - lessonObject.subjectId = subject.getAsInt(); - } - JsonElement teacher = lesson.get("IdPracownik"); - if (!(teacher instanceof JsonNull)) { - lessonObject.teacherId = teacher.getAsInt(); - } - - lessonObject.teamId = getClassTeamId(); - JsonElement team = lesson.get("PodzialSkrot"); - if (team != null && !(team instanceof JsonNull)) { - String name = getClassTeamName()+" "+team.getAsString(); - Team teamObject = searchTeam(name, schoolName+":"+name, lessonObject.teacherId); - lessonObject.teamId = teamObject.id; - } - JsonElement classroom = lesson.get("Sala"); - if (classroom != null && !(classroom instanceof JsonNull)) { - lessonObject.classroomName = classroom.getAsString(); - } - - JsonElement isCancelled = lesson.get("PrzekreslonaNazwa"); - JsonElement oldTeacherId = lesson.get("IdPracownikOld"); - if (isCancelled != null && isCancelled.getAsBoolean()) { - LessonChange lessonChangeObject = new LessonChange( - profileId, - lessonDate, lessonObject.startTime, lessonObject.endTime - ); - lessonChangeObject.type = TYPE_CANCELLED; - lessonChangeObject.teacherId = lessonObject.teacherId; - lessonChangeObject.subjectId = lessonObject.subjectId; - lessonChangeObject.teamId = lessonObject.teamId; - lessonChangeObject.classroomName = lessonObject.classroomName; - - lessonChangeList.add(lessonChangeObject); - metadataList.add(new Metadata(profileId,Metadata.TYPE_LESSON_CHANGE, lessonChangeObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - else if (!(oldTeacherId instanceof JsonNull)) { - LessonChange lessonChangeObject = new LessonChange( - profileId, - lessonDate, lessonObject.startTime, lessonObject.endTime - ); - lessonChangeObject.type = TYPE_CHANGE; - lessonChangeObject.teacherId = lessonObject.teacherId; - lessonChangeObject.subjectId = lessonObject.subjectId; - lessonChangeObject.teamId = lessonObject.teamId; - lessonChangeObject.classroomName = lessonObject.classroomName; - lessonObject.teacherId = oldTeacherId.getAsInt(); - - lessonChangeList.add(lessonChangeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_LESSON_CHANGE, lessonChangeObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - lessonList.add(lessonObject); - } - r("finish", "Timetable"); - }); - } - - private void getGrades() { - callback.onActionStarted(R.string.sync_action_syncing_grades); - JsonObject json = new JsonObject(); - json.addProperty("IdUczen", studentId); - json.addProperty("IdOkresKlasyfikacyjny", studentSemesterId); - apiRequest(schoolSymbol+"/"+ENDPOINT_GRADES, json, result -> { - JsonArray grades = result.getAsJsonArray("Data"); - - for (JsonElement gradeEl: grades) { - JsonObject grade = gradeEl.getAsJsonObject(); - JsonElement name = grade.get("Wpis"); - JsonElement description = grade.get("Opis"); - JsonElement comment = grade.get("Komentarz"); - JsonElement value = grade.get("Wartosc"); - JsonElement modificatorValue = grade.get("WagaModyfikatora"); - JsonElement numerator = grade.get("Licznik"); - JsonElement denominator = grade.get("Mianownik"); - - int id = grade.get("Id").getAsInt(); - int weight = grade.get("WagaOceny").getAsInt(); - int subjectId = grade.get("IdPrzedmiot").getAsInt(); - int teacherId = grade.get("IdPracownikD").getAsInt(); - int categoryId = grade.get("IdKategoria").getAsInt(); - long addedDate = grade.get("DataModyfikacji").getAsLong() * 1000; - - float finalValue = 0.0f; - String finalName; - String finalDescription = ""; - - if (!(numerator instanceof JsonNull) && !(denominator instanceof JsonNull)) { - // calculate the grade's value and name: divide - float numeratorF = numerator.getAsFloat(); - float denominatorF = denominator.getAsFloat(); - finalValue = numeratorF / denominatorF; - weight = 0; - finalName = intToStr(Math.round(finalValue*100))+"%"; - finalDescription += new DecimalFormat("#.##").format(numeratorF)+"/"+new DecimalFormat("#.##").format(denominatorF); - } - else { - // "normal" grade. Get the name and value if set. Otherwise zero the weight. - finalName = name.getAsString(); - if (!(value instanceof JsonNull)) { - finalValue = value.getAsFloat(); - if (!(modificatorValue instanceof JsonNull)) { - finalValue += modificatorValue.getAsFloat(); - } - } - else { - weight = 0; - } - } - - if (!(comment instanceof JsonNull)) { - if (finalName.equals("")) { - finalName = comment.getAsString(); - } - else { - finalDescription += (finalDescription.equals("") ? "" : " ")+comment.getAsString(); - } - } - if (!(description instanceof JsonNull)) { - finalDescription += (finalDescription.equals("") ? "" : " - ")+description.getAsString(); - } - - int semester = studentSemesterNumber; - /*Date addedDateObj = Date.fromMillis(addedDate); - if (app.register.dateSemester2Start != null && addedDateObj.compareTo(app.register.dateSemester2Start) > 0) { - semester = 2; - }*/ - - String category = ""; - for (GradeCategory gradeCategory: gradeCategoryList) { - if (gradeCategory.categoryId == categoryId) { - category = gradeCategory.text; - } - } - - int color = 0xff3D5F9C; - switch (finalName) { - case "1-": - case "1": - case "1+": - color = 0xffd65757; - break; - case "2-": - case "2": - case "2+": - color = 0xff9071b3; - break; - case "3-": - case "3": - case "3+": - color = 0xffd2ab24; - break; - case "4-": - case "4": - case "4+": - color = 0xff50b6d6; - break; - case "5-": - case "5": - case "5+": - color = 0xff2cbd92; - break; - case "6-": - case "6": - case "6+": - color = 0xff91b43c; - break; - } - - Grade gradeObject = new Grade( - profileId, - id, - category, - color, - finalDescription, - finalName, - finalValue, - weight, - semester, - teacherId, - subjectId - ); - - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - r("finish", "Grades"); - }); - } - - private void processGradeList(JsonArray jsonGrades, List gradeList, List metadataList, boolean isFinal) { - for (JsonElement gradeEl: jsonGrades) { - JsonObject grade = gradeEl.getAsJsonObject(); - - String name = grade.get("Wpis").getAsString(); - float value = getGradeValue(name); - long subjectId = grade.get("IdPrzedmiot").getAsLong(); - - long id = subjectId * -100 - studentSemesterNumber; - - int color = getVulcanGradeColor(name); - - Grade gradeObject = new Grade( - profileId, - id, - "", - color, - "", - name, - value, - 0, - studentSemesterNumber, - -1, - subjectId - ); - if (studentSemesterNumber == 1) { - gradeObject.type = isFinal ? TYPE_SEMESTER1_FINAL : TYPE_SEMESTER1_PROPOSED; - } - else { - gradeObject.type = isFinal ? TYPE_SEMESTER2_FINAL : TYPE_SEMESTER2_PROPOSED; - } - gradeList.add(gradeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_GRADE, gradeObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - } - private void getProposedGrades() { - callback.onActionStarted(R.string.sync_action_syncing_proposition_grades); - JsonObject json = new JsonObject(); - json.addProperty("IdUczen", studentId); - json.addProperty("IdOkresKlasyfikacyjny", studentSemesterId); - apiRequest(schoolSymbol+"/"+ENDPOINT_GRADES_PROPOSITIONS, json, result -> { - JsonObject grades = result.getAsJsonObject("Data"); - JsonArray gradesProposed = grades.getAsJsonArray("OcenyPrzewidywane"); - JsonArray gradesFinal = grades.getAsJsonArray("OcenyKlasyfikacyjne"); - - processGradeList(gradesProposed, gradeList, metadataList, false); - processGradeList(gradesFinal, gradeList, metadataList, true); - r("finish", "ProposedGrades"); - }); - } - - private void getEvents() { - callback.onActionStarted(R.string.sync_action_syncing_exams); - JsonObject json = new JsonObject(); - // from today to the end of the current semester - // or if empty: from the beginning of the semester - json.addProperty("DataPoczatkowa", profile.getEmpty() ? getCurrentSemesterStartDate().getStringY_m_d() : oneMonthBack.getStringY_m_d()); - json.addProperty("DataKoncowa", getCurrentSemesterEndDate().getStringY_m_d()); - json.addProperty("IdOddzial", studentClassId); - json.addProperty("IdUczen", studentId); - json.addProperty("IdOkresKlasyfikacyjny", studentSemesterId); - apiRequest(schoolSymbol+"/"+ENDPOINT_EVENTS, json, result -> { - JsonArray events = result.getAsJsonArray("Data"); - - for (JsonElement eventEl: events) { - JsonObject event = eventEl.getAsJsonObject(); - - int id = event.get("Id").getAsInt(); - - int eventType = event.get("Rodzaj").getAsBoolean() ? TYPE_EXAM : TYPE_SHORT_QUIZ; - - int subjectId = event.get("IdPrzedmiot").getAsInt(); - - Date lessonDate = Date.fromY_m_d(event.get("DataTekst").getAsString()); - Time startTime = null; - - int weekDay = lessonDate.getWeekDay(); - for (Lesson lesson: lessonList) { - if (lesson.weekDay == weekDay && lesson.subjectId == subjectId) { - startTime = lesson.startTime; - } - } - - long teacherId = event.get("IdPracownik").getAsInt(); - - Event eventObject = new Event( - profileId, - id, - lessonDate, - startTime, - event.get("Opis").getAsString(), - -1, - eventType, - false, - teacherId, - subjectId, - getClassTeamId() - ); - - JsonElement team = event.get("PodzialSkrot"); - if (team != null && !(team instanceof JsonNull)) { - String name = getClassTeamName()+" "+team.getAsString(); - Team teamObject = searchTeam(name, schoolName+":"+name, teacherId); - eventObject.teamId = teamObject.id; - } - - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_EVENT, eventObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - r("finish", "Events"); - }); - } - - private void getHomework() { - callback.onActionStarted(R.string.sync_action_syncing_homework); - JsonObject json = new JsonObject(); - json.addProperty("DataPoczatkowa", profile.getEmpty() ? getCurrentSemesterStartDate().getStringY_m_d() : oneMonthBack.getStringY_m_d()); - json.addProperty("DataKoncowa", getCurrentSemesterEndDate().getStringY_m_d()); - json.addProperty("IdOddzial", studentClassId); - json.addProperty("IdUczen", studentId); - json.addProperty("IdOkresKlasyfikacyjny", studentSemesterId); - apiRequest(schoolSymbol+"/"+ ENDPOINT_HOMEWORK, json, result -> { - JsonArray homeworkList = result.getAsJsonArray("Data"); - - for (JsonElement homeworkEl: homeworkList) { - JsonObject homework = homeworkEl.getAsJsonObject(); - - int id = homework.get("Id").getAsInt(); - - int subjectId = homework.get("IdPrzedmiot").getAsInt(); - - Date lessonDate = Date.fromY_m_d(homework.get("DataTekst").getAsString()); - Time startTime = null; - - int weekDay = lessonDate.getWeekDay(); - for (Lesson lesson: lessonList) { - if (lesson.weekDay == weekDay && lesson.subjectId == subjectId) { - startTime = lesson.startTime; - } - } - - Event eventObject = new Event( - profileId, - id, - lessonDate, - startTime, - homework.get("Opis").getAsString(), - -1, - Event.TYPE_HOMEWORK, - false, - homework.get("IdPracownik").getAsInt(), - subjectId, - getClassTeamId() - ); - - eventList.add(eventObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_HOMEWORK, eventObject.id, profile.getEmpty(), profile.getEmpty(), System.currentTimeMillis())); - } - r("finish", "Homework"); - }); - } - - private void getNotices() { - callback.onActionStarted(R.string.sync_action_syncing_notices); - JsonObject json = new JsonObject(); - json.addProperty("IdUczen", studentId); - json.addProperty("IdOkresKlasyfikacyjny", studentSemesterId); - apiRequest(schoolSymbol+"/"+ENDPOINT_NOTICES, json, result -> { - JsonArray notices = result.getAsJsonArray("Data"); - - for (JsonElement noticeEl: notices) { - JsonObject notice = noticeEl.getAsJsonObject(); - - int id = notice.get("Id").getAsInt(); - long addedDate = Date.fromY_m_d(notice.get("DataWpisuTekst").getAsString()).getInMillis(); - - int semester = studentSemesterNumber; - /*Date addedDateObj = Date.fromMillis(addedDate); - if (profile.dateSemester2Start != null && addedDateObj.compareTo(profile.dateSemester2Start) > 0) { - semester = 2; - }*/ - - Notice noticeObject = new Notice( - profileId, - id, - (notice.get("IdKategoriaUwag") instanceof JsonNull ? "" : noticeCategories.get(notice.get("IdKategoriaUwag").getAsInt())) +"\n"+notice.get("TrescUwagi").getAsString(), - semester, - TYPE_NEUTRAL, - notice.get("IdPracownik").getAsInt() - ); - - noticeList.add(noticeObject); - metadataList.add(new Metadata(profileId, Metadata.TYPE_NOTICE, noticeObject.id, profile.getEmpty(), profile.getEmpty(), addedDate)); - } - r("finish", "Notices"); - }); - } - - private void getAttendance() { - callback.onActionStarted(R.string.sync_action_syncing_attendance); - JsonObject json = new JsonObject(); - json.addProperty("DataPoczatkowa", true ? getCurrentSemesterStartDate().getStringY_m_d() : oneMonthBack.getStringY_m_d()); - json.addProperty("DataKoncowa", getCurrentSemesterEndDate().getStringY_m_d()); - json.addProperty("IdOddzial", studentClassId); - json.addProperty("IdUczen", studentId); - json.addProperty("IdOkresKlasyfikacyjny", studentSemesterId); - apiRequest(schoolSymbol+"/"+ ENDPOINT_ATTENDANCE, json, result -> { - JsonArray attendanceList = result.getAsJsonObject("Data").getAsJsonArray("Frekwencje"); - - for (JsonElement attendanceEl: attendanceList) { - JsonObject attendance = attendanceEl.getAsJsonObject(); - - Pair attendanceCategory = attendanceCategories.get(attendance.get("IdKategoria").getAsInt()); - if (attendanceCategory == null) - continue; - - int type = attendanceCategory.first; - - int id = attendance.get("Dzien").getAsInt() + attendance.get("Numer").getAsInt(); - - long lessonDateMillis = Date.fromY_m_d(attendance.get("DzienTekst").getAsString()).getInMillis(); - Date lessonDate = Date.fromMillis(lessonDateMillis); - - int lessonSemester = profile.dateToSemester(lessonDate); - - Attendance attendanceObject = new Attendance( - profileId, - id, - 0, - attendance.get("IdPrzedmiot").getAsInt(), - lessonSemester, - attendance.get("PrzedmiotNazwa").getAsString()+" - "+attendanceCategory.second, - lessonDate, - lessonRanges.get(attendance.get("IdPoraLekcji").getAsInt()).first, - type); - - this.attendanceList.add(attendanceObject); - if (attendanceObject.type != TYPE_PRESENT) { - metadataList.add(new Metadata(profileId, Metadata.TYPE_ATTENDANCE, attendanceObject.id, profile.getEmpty(), profile.getEmpty(), attendanceObject.lessonDate.combineWith(attendanceObject.startTime))); - } - } - r("finish", "Attendance"); - }); - } - - private void getMessagesInbox() { - callback.onActionStarted(R.string.sync_action_syncing_messages_inbox); - JsonObject json = new JsonObject(); - json.addProperty("DataPoczatkowa", true ? getCurrentSemesterStartDate().getInUnix() : oneMonthBack.getInUnix()); - json.addProperty("DataKoncowa", getCurrentSemesterEndDate().getInUnix()); - json.addProperty("LoginId", studentLoginId); - json.addProperty("IdUczen", studentId); - apiRequest(schoolSymbol+"/"+ENDPOINT_MESSAGES_RECEIVED, json, result -> { - JsonArray messages = result.getAsJsonArray("Data"); - - for (JsonElement messageEl: messages) { - JsonObject message = messageEl.getAsJsonObject(); - - long id = message.get("WiadomoscId").getAsLong(); - - long senderId = -1; - int senderLoginId = message.get("NadawcaId").getAsInt(); - String senderLoginIdStr = String.valueOf(senderLoginId); - - for (Teacher teacher: teacherList) { - if (senderLoginIdStr.equals(teacher.loginId)) { - senderId = teacher.id; - } - } - - String subject = message.get("Tytul").getAsString(); - String body = message.get("Tresc").getAsString(); - body = body.replaceAll("\n", "
"); - - long addedDate = message.get("DataWyslaniaUnixEpoch").getAsLong() * 1000; - long readDate = 0; - JsonElement readDateUnix; - if (!((readDateUnix = message.get("DataPrzeczytaniaUnixEpoch")) instanceof JsonNull)) { - readDate = readDateUnix.getAsLong() * 1000L; - } - - Message messageObject = new Message(profileId, id, subject, body, TYPE_RECEIVED, senderId, -1); - MessageRecipient messageRecipientObject = new MessageRecipient(profileId, -1, -1, readDate, id); - - messageList.add(messageObject); - messageRecipientList.add(messageRecipientObject); - messageMetadataList.add(new Metadata(profileId, TYPE_MESSAGE, id, readDate > 0, readDate > 0 || profile.getEmpty(), addedDate)); - } - r("finish", "MessagesInbox"); - }); - } - - private void getMessagesOutbox() { - if (!fullSync && onlyFeature != FEATURE_MESSAGES_OUTBOX) { - r("finish", "MessagesOutbox"); - return; - } - callback.onActionStarted(R.string.sync_action_syncing_messages_outbox); - JsonObject json = new JsonObject(); - json.addProperty("DataPoczatkowa", true ? getCurrentSemesterStartDate().getInUnix() : oneMonthBack.getInUnix()); - json.addProperty("DataKoncowa", getCurrentSemesterEndDate().getInUnix()); - json.addProperty("LoginId", studentLoginId); - json.addProperty("IdUczen", studentId); - apiRequest(schoolSymbol+"/"+ENDPOINT_MESSAGES_SENT, json, result -> { - JsonArray messages = result.getAsJsonArray("Data"); - - for (JsonElement jMessageEl: messages) { - JsonObject jMessage = jMessageEl.getAsJsonObject(); - - long messageId = jMessage.get("WiadomoscId").getAsLong(); - - String subject = jMessage.get("Tytul").getAsString(); - - String body = jMessage.get("Tresc").getAsString(); - body = body.replaceAll("\n", "
"); - - long sentDate = jMessage.get("DataWyslaniaUnixEpoch").getAsLong() * 1000; - - Message message = new Message( - profileId, - messageId, - subject, - body, - TYPE_SENT, - -1, - -1 - ); - - int readBy = jMessage.get("Przeczytane").getAsInt(); - int unreadBy = jMessage.get("Nieprzeczytane").getAsInt(); - for (JsonElement recipientEl: jMessage.getAsJsonArray("Adresaci")) { - JsonObject recipient = recipientEl.getAsJsonObject(); - long recipientId = -1; - int recipientLoginId = recipient.get("LoginId").getAsInt(); - String recipientLoginIdStr = String.valueOf(recipientLoginId); - for (Teacher teacher: teacherList) { - if (recipientLoginIdStr.equals(teacher.loginId)) { - recipientId = teacher.id; - } - } - MessageRecipient messageRecipient = new MessageRecipient( - profileId, - recipientId, - -1, - readBy == 0 ? 0 : unreadBy == 0 ? 1 : -1, - /*messageId*/ messageId - ); - messageRecipientIgnoreList.add(messageRecipient); - } - - messageList.add(message); - metadataList.add(new Metadata(profileId, Metadata.TYPE_MESSAGE, messageId, true, true, sentDate)); - } - r("finish", "MessagesOutbox"); - }); - } - - /* __ __ - | \/ | - | \ / | ___ ___ ___ __ _ __ _ ___ ___ - | |\/| |/ _ \/ __/ __|/ _` |/ _` |/ _ \/ __| - | | | | __/\__ \__ \ (_| | (_| | __/\__ \ - |_| |_|\___||___/___/\__,_|\__, |\___||___/ - __/ | - |__*/ - @Override - public void getMessage(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull MessageFull message, @NonNull MessageGetCallback messageCallback) { - if (message.body != null) { - message.recipients = app.db.messageRecipientDao().getAllByMessageId(profile.getId(), message.id); - for (MessageRecipientFull recipient: message.recipients) { - if (recipient.id == -1) - recipient.fullName = profile.getStudentNameLong(); - } - if (!message.seen) { - studentId = profile.getStudentData("studentId", -1); - studentLoginId = profile.getStudentData("studentLoginId", -1); - JsonObject json = new JsonObject(); - json.addProperty("WiadomoscId", message.id); - json.addProperty("FolderWiadomosci", "Odebrane"); - json.addProperty("Status", "Widoczna"); - json.addProperty("LoginId", studentLoginId); - json.addProperty("IdUczen", studentId); - apiRequest(schoolSymbol+"/"+ENDPOINT_MESSAGES_CHANGE_STATUS, json, result -> { }); - app.db.metadataDao().setSeen(profile.getId(), message, true); - if (message.type != TYPE_SENT) { - app.db.messageRecipientDao().add(new MessageRecipient(profile.getId(), -1, -1, System.currentTimeMillis(), message.id)); - } - } - new Handler(activityContext.getMainLooper()).post(() -> { - messageCallback.onSuccess(message); - }); - return; - } - } - - @Override - public void getAttachment(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull MessageFull message, long attachmentId, @NonNull AttachmentGetCallback attachmentCallback) { - - } - - @Override - public void getRecipientList(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile, @NonNull RecipientListGetCallback recipientListGetCallback) { - - } - - @Override - public MessagesComposeInfo getComposeInfo(@NonNull ProfileFull profile) { - return new MessagesComposeInfo(0, 0, -1, -1); - } - - @Override - public void syncMessages(@NonNull Context activityContext, @NonNull SyncCallback errorCallback, @NonNull ProfileFull profile) { - - } - - @Override - public Map getConfigurableEndpoints(Profile profile) { - return null; - } - - @Override - public boolean isEndpointEnabled(Profile profile, boolean defaultActive, String name) { - return defaultActive; - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/ApiLoginResult.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/ApiLoginResult.kt deleted file mode 100644 index 8dc3ac89..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/ApiLoginResult.kt +++ /dev/null @@ -1,6 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api.v2 - -import pl.szczodrzynski.edziennik.data.api.AppError -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore - -data class ApiLoginResult(val loginStore: LoginStore, val error: AppError?) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/FirstLoginResult.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/FirstLoginResult.kt deleted file mode 100644 index 099c2771..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/FirstLoginResult.kt +++ /dev/null @@ -1,6 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api.v2 - -import pl.szczodrzynski.edziennik.data.api.AppError -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile - -data class FirstLoginResult(val profileList: ArrayList, val error: AppError?) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/librus/firstlogin/FirstLoginLibrus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/librus/firstlogin/FirstLoginLibrus.kt deleted file mode 100644 index 0e8e02b6..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/librus/firstlogin/FirstLoginLibrus.kt +++ /dev/null @@ -1,12 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api.v2.librus.firstlogin - -import pl.szczodrzynski.edziennik.App -import pl.szczodrzynski.edziennik.data.api.interfaces.ProgressCallback -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile - -class FirstLoginLibrus(val app: App, val loginStore: LoginStore, val progressCallback: ProgressCallback, val onSuccess: (profileList: List) -> Unit) { - init { - - } -} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/librus/firstlogin/FirstLoginSynergia.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/librus/firstlogin/FirstLoginSynergia.kt deleted file mode 100644 index 79587cd8..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/v2/librus/firstlogin/FirstLoginSynergia.kt +++ /dev/null @@ -1,12 +0,0 @@ -package pl.szczodrzynski.edziennik.data.api.v2.librus.firstlogin - -import pl.szczodrzynski.edziennik.App -import pl.szczodrzynski.edziennik.data.api.interfaces.ProgressCallback -import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore -import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile - -class FirstLoginSynergia(val app: App, val loginStore: LoginStore, val progressCallback: ProgressCallback, val onSuccess: (profileList: List) -> Unit) { - init { - - } -}