From ed8ca00a85091ed1fa4406dcfdcd14011dc7a525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Fri, 11 Oct 2019 16:24:45 +0200 Subject: [PATCH] [APIv2] Add Grades. Update Firebase push receivers. --- app/src/main/AndroidManifest.xml | 8 + .../java/pl/szczodrzynski/edziennik/App.java | 15 +- .../pl/szczodrzynski/edziennik/Extensions.kt | 6 + .../edziennik/api/v2/Features.kt | 1 + .../edziennik/api/v2/LoginMethods.kt | 6 +- .../szczodrzynski/edziennik/api/v2/Regexes.kt | 27 ++- .../edziennik/api/v2/librus/data/LibrusApi.kt | 3 +- .../api/v2/mobidziennik/DataMobidziennik.kt | 19 ++- .../v2/mobidziennik/MobidziennikFeatures.kt | 12 +- .../v2/mobidziennik/data/MobidziennikData.kt | 5 +- .../v2/mobidziennik/data/MobidziennikWeb.kt | 3 +- .../mobidziennik/data/web/MobidziennikApi.kt | 5 + .../data/web/MobidziennikWebCalendar.kt | 5 + .../data/web/MobidziennikWebGrades.kt | 154 ++++++++++++++++++ .../data/web/apidata/MobidziennikApiGrades.kt | 6 +- .../web/apidata/MobidziennikApiNotices.kt | 1 + .../mobidziennik/login/MobidziennikLogin.kt | 5 + .../edziennik/api/v2/models/Data.kt | 2 +- .../edziennik/data/api/Mobidziennik.java | 13 +- .../data/db/modules/grades/GradeDao.java | 12 +- .../data/db/modules/login/LoginStore.java | 5 + .../sync/FirebaseBroadcastReceiver.kt | 46 ++++++ .../sync/MyFirebaseMessagingService.java | 2 +- app/src/main/res/values/strings.xml | 1 + 24 files changed, 322 insertions(+), 40 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikWebGrades.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/sync/FirebaseBroadcastReceiver.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 974eb812..9ffe0db4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -204,6 +204,14 @@ + + + + + diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/App.java b/app/src/main/java/pl/szczodrzynski/edziennik/App.java index 5596a664..374738e1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/App.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/App.java @@ -86,6 +86,7 @@ import pl.szczodrzynski.edziennik.utils.PermissionChecker; import pl.szczodrzynski.edziennik.utils.Themes; import pl.szczodrzynski.edziennik.utils.Utils; +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; @@ -409,20 +410,20 @@ public class App extends androidx.multidex.MultiDexApplication { FirebaseApp pushMobidziennikApp = FirebaseApp.initializeApp( this, new FirebaseOptions.Builder() - .setApplicationId("1:1029629079999:android:58bb378dab031f42") - .setGcmSenderId("1029629079999") + .setApiKey("AIzaSyCi5LmsZ5BBCQnGtrdvWnp1bWLCNP8OWQE") + .setApplicationId("1:747285019373:android:f6341bf7b158621d") .build(), - "Mobidziennik" + "Mobidziennik2" ); - /*FirebaseApp pushLibrusApp = FirebaseApp.initializeApp( + FirebaseApp pushLibrusApp = FirebaseApp.initializeApp( this, new FirebaseOptions.Builder() .setApiKey("AIzaSyDfTuEoYPKdv4aceEws1CO3n0-HvTndz-o") .setApplicationId("1:513056078587:android:1e29083b760af544") .build(), "Librus" - );*/ + ); FirebaseApp pushVulcanApp = FirebaseApp.initializeApp( this, @@ -443,10 +444,10 @@ public class App extends androidx.multidex.MultiDexApplication { Log.d(TAG, "Token for Mobidziennik is " + instanceIdResult.getToken() + ", ID is " + instanceIdResult.getId()); appConfig.fcmTokens.put(LOGIN_TYPE_MOBIDZIENNIK, new Pair<>(instanceIdResult.getToken(), new ArrayList<>())); }); - /*FirebaseInstanceId.getInstance(pushLibrusApp).getInstanceId().addOnSuccessListener(instanceIdResult -> { + FirebaseInstanceId.getInstance(pushLibrusApp).getInstanceId().addOnSuccessListener(instanceIdResult -> { Log.d(TAG, "Token for Librus is " + instanceIdResult.getToken() + ", ID is " + instanceIdResult.getId()); appConfig.fcmTokens.put(LOGIN_TYPE_LIBRUS, new Pair<>(instanceIdResult.getToken(), new ArrayList<>())); - });*/ + }); FirebaseInstanceId.getInstance(pushVulcanApp).getInstanceId().addOnSuccessListener(instanceIdResult -> { Log.d(TAG, "Token for Vulcan is " + instanceIdResult.getToken() + ", ID is " + instanceIdResult.getId()); Pair> pair = appConfig.fcmTokens.get(LOGIN_TYPE_VULCAN); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index fbddf815..96b1cf26 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -218,4 +218,10 @@ fun LongSparseArray.getById(id: Long): Team? { return value } return null +} + +operator fun MatchResult.get(group: Int): String { + if (group >= groupValues.size) + return "" + return groupValues[group] } \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Features.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Features.kt index 33bbe2f9..4d0c94c8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Features.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Features.kt @@ -37,6 +37,7 @@ const val FEATURE_LUCKY_NUMBER = 105 const val FEATURE_TEACHERS = 106 const val FEATURE_SUBJECTS = 107 const val FEATURE_CLASSROOMS = 108 +const val FEATURE_PUSH_CONFIG = 120 const val FEATURE_MESSAGE_GET = 201 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/LoginMethods.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/LoginMethods.kt index 5d08354c..a16c265e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/LoginMethods.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/LoginMethods.kt @@ -83,7 +83,11 @@ const val LOGIN_METHOD_MOBIDZIENNIK_API2 = 300 val mobidziennikLoginMethods = listOf( LoginMethod(LOGIN_TYPE_MOBIDZIENNIK, LOGIN_METHOD_MOBIDZIENNIK_WEB, MobidziennikLoginWeb::class.java) .withIsPossible { _, _ -> true } - .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED } + .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED }/*, + + LoginMethod(LOGIN_TYPE_MOBIDZIENNIK, LOGIN_METHOD_MOBIDZIENNIK_API2, MobidziennikLoginApi2::class.java) + .withIsPossible { _, loginStore -> loginStore.hasLoginData("email") } + .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED }*/ ) const val LOGIN_TYPE_VULCAN = 4 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Regexes.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Regexes.kt index 769b4787..ab0f4260 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Regexes.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Regexes.kt @@ -6,15 +6,34 @@ package pl.szczodrzynski.edziennik.api.v2 object Regexes { val MOBIDZIENNIK_GRADES_SUBJECT_NAME by lazy { - "\\n*\\s*(.+?)\\s*\\n*(?:<.*?)??".toRegex(RegexOption.DOT_MATCHES_ALL) + """\n*\s*(.+?)\s*\n*(?:<.*?)??""".toRegex(RegexOption.DOT_MATCHES_ALL) } + val MOBIDZIENNIK_GRADES_COLOR by lazy { + """background-color:([#A-Fa-f0-9]+);""".toRegex(RegexOption.DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_GRADES_CATEGORY by lazy { + """> (.+?):""".toRegex(RegexOption.DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_GRADES_CLASS_AVERAGE by lazy { + """Średnia ocen:.*([0-9]*\.?[0-9]*)""".toRegex(RegexOption.DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_GRADES_ADDED_DATE by lazy { + """Wpisano:.*.+?,\s([0-9]+)\s(.+?)\s([0-9]{4}),\sgodzina\s([0-9:]+)""".toRegex(RegexOption.DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_GRADES_COUNT_TO_AVG by lazy { + """Liczona do średniej:.*?nie
""".toRegex(RegexOption.DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_GRADES_DETAILS by lazy { + """(.+?).*?.+?.*?\((.+?)\).*?.*?Wartość oceny:.*?([0-9.]+).*?Wpisał\(a\):.*?(.+?)""".toRegex(RegexOption.DOT_MATCHES_ALL) + } + val MOBIDZIENNIK_EVENT_TYPE by lazy { - "\\(([0-9A-ząęóżźńśłć]*?)\\)$".toRegex(RegexOption.DOT_MATCHES_ALL) + """\(([0-9A-ząęóżźńśłć]*?)\)$""".toRegex(RegexOption.DOT_MATCHES_ALL) } val MOBIDZIENNIK_LUCKY_NUMBER by lazy { - "class=\"szczesliwy_numerek\".*>0*([0-9]+)(?:/0*[0-9]+)*".toRegex(RegexOption.DOT_MATCHES_ALL) + """class="szczesliwy_numerek".*>0*([0-9]+)(?:/0*[0-9]+)*""".toRegex(RegexOption.DOT_MATCHES_ALL) } val MOBIDZIENNIK_CLASS_CALENDAR by lazy { - "events: (.+),$".toRegex(RegexOption.DOT_MATCHES_ALL) + """events: (.+),$""".toRegex(RegexOption.MULTILINE) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt index 89ce94a7..497d4845 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusApi.kt @@ -77,8 +77,7 @@ open class LibrusApi(open val data: DataLibrus) { data.error(ApiError(tag, EXCEPTION_LIBRUS_API_REQUEST) .withResponse(response) .withThrowable(e) - .withApiResponse(json) - .setCritical(false)) + .withApiResponse(json)) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt index f4232e94..d012186f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt @@ -7,6 +7,7 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik import android.util.LongSparseArray import android.util.SparseArray import android.util.SparseIntArray +import androidx.core.util.isNotEmpty import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_MOBIDZIENNIK_WEB import pl.szczodrzynski.edziennik.api.v2.models.Data @@ -34,15 +35,20 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da val teachersMap = LongSparseArray() val subjectsMap = LongSparseArray() - val gradeAddedDates = SparseArray() - val gradeAverages = SparseArray() - val gradeColors = SparseIntArray() + val gradeAddedDates = LongSparseArray() + val gradeAverages = LongSparseArray() + val gradeColors = LongSparseArray() private var mLoginServerName: String? = null var loginServerName: String? get() { mLoginServerName = mLoginServerName ?: loginStore.getLoginData("serverName", null); return mLoginServerName } set(value) { loginStore.putLoginData("serverName", value); mLoginServerName = value } + private var mLoginEmail: String? = null + var loginEmail: String? + get() { mLoginEmail = mLoginEmail ?: loginStore.getLoginData("email", null); return mLoginEmail } + set(value) { loginStore.putLoginData("email", value); mLoginEmail = value } + private var mLoginUsername: String? = null var loginUsername: String? get() { mLoginUsername = mLoginUsername ?: loginStore.getLoginData("username", null); return mLoginUsername } @@ -85,6 +91,13 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da set(value) { loginStore.putLoginData("sessionIDTime", value); mWebSessionIdExpiryTime = value } + override fun saveData() { + super.saveData() + if (gradeAddedDates.isNotEmpty()) { + app.db.gradeDao().updateDetails(profileId, gradeAverages, gradeAddedDates, gradeColors) + } + } + val mobiLessons = mutableListOf() data class MobiLesson( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/MobidziennikFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/MobidziennikFeatures.kt index a7c31d48..660f846f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/MobidziennikFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/MobidziennikFeatures.kt @@ -7,7 +7,7 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik import pl.szczodrzynski.edziennik.api.v2.* import pl.szczodrzynski.edziennik.api.v2.models.Feature -const val ENDPOINT_MOBIDZIENNIK_API_MAIN = 1000 +const val ENDPOINT_MOBIDZIENNIK_API_MAIN = 1000 const val ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_INBOX = 2011 const val ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_SENT = 2012 const val ENDPOINT_MOBIDZIENNIK_WEB_MESSAGES_ALL = 2019 @@ -16,7 +16,8 @@ const val ENDPOINT_MOBIDZIENNIK_WEB_GRADES = 2030 const val ENDPOINT_MOBIDZIENNIK_WEB_NOTICES = 2040 const val ENDPOINT_MOBIDZIENNIK_WEB_ATTENDANCE = 2050 const val ENDPOINT_MOBIDZIENNIK_WEB_MANUALS = 2100 -const val ENDPOINT_MOBIDZIENNIK_API2 = 3000 +const val ENDPOINT_MOBIDZIENNIK_WEB_ACCOUNT_EMAIL = 2200 +const val ENDPOINT_MOBIDZIENNIK_API2_MAIN = 3000 val MobidziennikFeatures = listOf( // timetable @@ -55,8 +56,13 @@ val MobidziennikFeatures = listOf( ), listOf(LOGIN_METHOD_MOBIDZIENNIK_WEB)), Feature(LOGIN_TYPE_MOBIDZIENNIK, FEATURE_ALWAYS_NEEDED, listOf( - ENDPOINT_MOBIDZIENNIK_API_MAIN to LOGIN_METHOD_MOBIDZIENNIK_WEB + ENDPOINT_MOBIDZIENNIK_API_MAIN to LOGIN_METHOD_MOBIDZIENNIK_WEB, + ENDPOINT_MOBIDZIENNIK_WEB_ACCOUNT_EMAIL to LOGIN_METHOD_MOBIDZIENNIK_WEB ), listOf(LOGIN_METHOD_MOBIDZIENNIK_WEB)), + + Feature(LOGIN_TYPE_MOBIDZIENNIK, FEATURE_PUSH_CONFIG, listOf( + ENDPOINT_MOBIDZIENNIK_API2_MAIN to LOGIN_METHOD_MOBIDZIENNIK_API2 + ), listOf(LOGIN_METHOD_MOBIDZIENNIK_API2)), /*Feature(LOGIN_TYPE_MOBIDZIENNIK, FEATURE_STUDENT_INFO, listOf( ENDPOINT_MOBIDZIENNIK_API_MAIN to LOGIN_METHOD_MOBIDZIENNIK_WEB ), listOf(LOGIN_METHOD_MOBIDZIENNIK_WEB)), diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikData.kt index b9e2ac5a..72e0ae59 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikData.kt @@ -8,6 +8,7 @@ import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.mobidziennik.* import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.MobidziennikApi import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.MobidziennikWebCalendar +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.MobidziennikWebGrades import pl.szczodrzynski.edziennik.utils.Utils class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) { @@ -51,11 +52,11 @@ class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) { ENDPOINT_MOBIDZIENNIK_WEB_CALENDAR -> { data.startProgress(R.string.edziennik_progress_endpoint_calendar) MobidziennikWebCalendar(data) { onSuccess() } - }/* + } ENDPOINT_MOBIDZIENNIK_WEB_GRADES -> { data.startProgress(R.string.edziennik_progress_endpoint_grades) MobidziennikWebGrades(data) { onSuccess() } - } + }/* ENDPOINT_MOBIDZIENNIK_WEB_NOTICES -> { data.startProgress(R.string.edziennik_progress_endpoint_behaviour) MobidziennikWebNotices(data) { onSuccess() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikWeb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikWeb.kt index 28afc3cc..67828b73 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikWeb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/MobidziennikWeb.kt @@ -62,8 +62,7 @@ open class MobidziennikWeb(open val data: DataMobidziennik) { data.error(ApiError(tag, EXCEPTION_MOBIDZIENNIK_WEB_REQUEST) .withResponse(response) .withThrowable(e) - .withApiResponse(text) - .setCritical(false)) + .withApiResponse(text)) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikApi.kt index f9cbbd9b..c4eaf860 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikApi.kt @@ -4,11 +4,15 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web +import pl.szczodrzynski.edziennik.DAY +import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME import pl.szczodrzynski.edziennik.api.v2.ERROR_MOBIDZIENNIK_WEB_INVALID_RESPONSE import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.ENDPOINT_MOBIDZIENNIK_API_MAIN import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikWeb import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.apidata.* import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS class MobidziennikApi(override val data: DataMobidziennik, val onSuccess: () -> Unit) : MobidziennikWeb(data) { @@ -45,6 +49,7 @@ class MobidziennikApi(override val data: DataMobidziennik, } } + data.setSyncNext(ENDPOINT_MOBIDZIENNIK_API_MAIN, SYNC_ALWAYS) onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikWebCalendar.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikWebCalendar.kt index d871952f..94f0a5e4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikWebCalendar.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikWebCalendar.kt @@ -5,9 +5,12 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web import com.google.gson.JsonParser +import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME import pl.szczodrzynski.edziennik.api.v2.Regexes import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.ENDPOINT_MOBIDZIENNIK_WEB_CALENDAR import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikWeb +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.modules.events.Event import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.getString @@ -91,6 +94,8 @@ class MobidziennikWebCalendar(override val data: DataMobidziennik, )) } } + + data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_CALENDAR, SYNC_ALWAYS) onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikWebGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikWebGrades.kt new file mode 100644 index 00000000..2a24b436 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikWebGrades.kt @@ -0,0 +1,154 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-10. + */ + +package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web + +import android.graphics.Color +import com.google.gson.JsonParser +import org.jsoup.Jsoup +import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME +import pl.szczodrzynski.edziennik.api.v2.Regexes +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.ENDPOINT_MOBIDZIENNIK_WEB_GRADES +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikWeb +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +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.metadata.Metadata +import pl.szczodrzynski.edziennik.fixWhiteSpaces +import pl.szczodrzynski.edziennik.get +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.singleOrNull +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.Utils.strToInt +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time +import java.util.* +import java.util.regex.Pattern + +class MobidziennikWebGrades(override val data: DataMobidziennik, + val onSuccess: () -> Unit) : MobidziennikWeb(data) { + companion object { + private const val TAG = "MobidziennikWebGrades" + } + + init { + webGet(TAG, "/dziennik/oceny?semestr=${profile?.currentSemester ?: 1}") { text -> + MobidziennikLuckyNumberExtractor(data, text) + + val doc = Jsoup.parse(text) + + val grades = doc.select("table.spis a, table.spis span, table.spis div") + + var gradeCategory = "" + var gradeColor = -1 + var subjectName = "" + + for (e in grades) { + when (e.tagName()) { + "div" -> { + Regexes.MOBIDZIENNIK_GRADES_SUBJECT_NAME.find(e.outerHtml())?.let { + subjectName = it[1] + } + } + "span" -> { + val css = e.attr("style") + Regexes.MOBIDZIENNIK_GRADES_COLOR.find(css)?.let { + // (#2196f3) + gradeColor = Color.parseColor(it[1]) + } + Regexes.MOBIDZIENNIK_GRADES_CATEGORY.find(e.outerHtml())?.let { + // (category) + gradeCategory = it[1] + } + } + "a" -> { + val gradeId = e.attr("rel").toLong() + var gradeAddedDateMillis: Long = -1 + var gradeSemester = 1 + + val html = e.html() + val gradeClassAverage = Regexes.MOBIDZIENNIK_GRADES_CLASS_AVERAGE.find(html)?.let { + // (4.75) + it[1].toFloatOrNull() + } ?: -1f + + Regexes.MOBIDZIENNIK_GRADES_ADDED_DATE.find(html)?.let { + // (2) (stycznia) (2019), (12:34:56) + val month = when (it[2]) { + "stycznia" -> 1 + "lutego" -> 2 + "marca" -> 3 + "kwietnia" -> 4 + "maja" -> 5 + "czerwca" -> 6 + "lipca" -> 7 + "sierpnia" -> 8 + "września" -> 9 + "października" -> 10 + "listopada" -> 11 + "grudnia" -> 12 + else -> 1 + } + val gradeAddedDate = Date( + it[3].toInt(), + month, + it[1].toInt() + ) + val time = Time.fromH_m_s(it[4]) + gradeAddedDateMillis = gradeAddedDate.combineWith(time) + gradeSemester = profile?.dateToSemester(gradeAddedDate) ?: 1 + } + + if (Regexes.MOBIDZIENNIK_GRADES_COUNT_TO_AVG.containsMatchIn(html)) { + Regexes.MOBIDZIENNIK_GRADES_DETAILS.find(html)?.let { match -> + val gradeName = match[1] + val gradeDescription = match[2] + val gradeValue = match[3].toFloatOrNull() ?: 0.0f + val teacherName = match[4].fixWhiteSpaces() + + val teacherId = data.teacherList.singleOrNull { it.fullNameLastFirst == teacherName }?.id ?: -1 + val subjectId = data.subjectList.singleOrNull { it.longName == subjectName }?.id ?: -1 + + val gradeObject = Grade( + profileId, + gradeId, + gradeCategory, + gradeColor, + "NLDŚR, $gradeDescription", + gradeName, + gradeValue, + 0f, + gradeSemester, + teacherId, + subjectId + ) + + gradeObject.classAverage = gradeClassAverage + + data.gradeList.add(gradeObject) + data.metadataList.add( + Metadata( + profileId, + Metadata.TYPE_GRADE, + gradeObject.id, + profile?.empty ?: false, + profile?.empty ?: false, + gradeAddedDateMillis + )) + } + } else { + data.gradeAverages.put(gradeId, gradeClassAverage) + data.gradeAddedDates.put(gradeId, gradeAddedDateMillis) + data.gradeColors.put(gradeId, gradeColor) + } + } + } + } + + data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_GRADES, SYNC_ALWAYS) + onSuccess() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiGrades.kt index 05eb9663..056444d6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiGrades.kt @@ -23,7 +23,7 @@ class MobidziennikApiGrades(val data: DataMobidziennik, rows: List) { continue val cols = row.split("|") - val studentId = cols[2].toInt() + val studentId = cols[1].toInt() if (studentId != data.studentId) return@run @@ -35,7 +35,7 @@ class MobidziennikApiGrades(val data: DataMobidziennik, rows: List) { val semester = cols[5].toInt() val teacherId = cols[2].toLong() val subjectId = cols[3].toLong() - var type = when (cols[8]) { + val type = when (cols[8]) { "3" -> if (semester == 1) TYPE_SEMESTER1_PROPOSED else TYPE_SEMESTER2_PROPOSED "1" -> if (semester == 1) TYPE_SEMESTER1_FINAL else TYPE_SEMESTER2_FINAL "4" -> TYPE_YEAR_PROPOSED @@ -70,7 +70,7 @@ class MobidziennikApiGrades(val data: DataMobidziennik, rows: List) { weight, semester, teacherId, - subjectId) + subjectId);gradeObject.type = type data.gradeList.add(gradeObject) data.metadataList.add( diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiNotices.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiNotices.kt index 416d3f6d..87519aad 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiNotices.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiNotices.kt @@ -29,6 +29,7 @@ class MobidziennikApiNotices(val data: DataMobidziennik, rows: List) { val type = when (cols[3]) { "0" -> Notice.TYPE_NEGATIVE "1" -> Notice.TYPE_POSITIVE + "3" -> Notice.TYPE_NEUTRAL else -> Notice.TYPE_NEUTRAL } val teacherId = cols[5].toLong() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLogin.kt index 58f94e36..4baf451c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLogin.kt @@ -5,6 +5,7 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.login import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_MOBIDZIENNIK_API2 import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_MOBIDZIENNIK_WEB import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik import pl.szczodrzynski.edziennik.utils.Utils @@ -48,6 +49,10 @@ class MobidziennikLogin(val data: DataMobidziennik, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_login_mobidziennik_web) MobidziennikLoginWeb(data) { onSuccess(loginMethodId) } } + LOGIN_METHOD_MOBIDZIENNIK_API2 -> { + data.startProgress(R.string.edziennik_progress_login_mobidziennik_api2) + //MobidziennikLoginApi2(data) { onSuccess(loginMethodId) } + } } } } \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index b85fed4d..dd4e7fe9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -190,7 +190,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) messageMetadataList.clear() } - fun saveData() { + open fun saveData() { if (profile == null) return 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 index 4a3d191f..785ab5ff 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Mobidziennik.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Mobidziennik.java @@ -5,6 +5,7 @@ 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 android.util.SparseIntArray; @@ -128,9 +129,9 @@ public class Mobidziennik implements EdziennikInterface { private List subjectList; private List lessonList; private List lessonChangeList; - private SparseArray gradeAddedDates; - private SparseArray gradeAverages; - private SparseIntArray gradeColors; + private LongSparseArray gradeAddedDates; + private LongSparseArray gradeAverages; + private LongSparseArray gradeColors; private List gradeList; private List eventList; private List noticeList; @@ -179,9 +180,9 @@ public class Mobidziennik implements EdziennikInterface { subjectList = new ArrayList<>(); lessonList = new ArrayList<>(); lessonChangeList = new ArrayList<>(); - gradeAddedDates = new SparseArray<>(); - gradeAverages = new SparseArray<>(); - gradeColors = new SparseIntArray(); + gradeAddedDates = new LongSparseArray<>(); + gradeAverages = new LongSparseArray<>(); + gradeColors = new LongSparseArray<>(); gradeList = new ArrayList<>(); eventList = new ArrayList<>(); noticeList = new ArrayList<>(); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeDao.java index 4487317b..5765d235 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeDao.java @@ -9,6 +9,8 @@ import androidx.room.OnConflictStrategy; import androidx.room.Query; import androidx.room.RawQuery; import androidx.room.Transaction; + +import android.util.LongSparseArray; import android.util.SparseArray; import android.util.SparseIntArray; @@ -88,15 +90,15 @@ public abstract class GradeDao { } @Query("UPDATE grades SET gradeClassAverage = :classAverage, gradeColor = :color WHERE profileId = :profileId AND gradeId = :gradeId") - public abstract void updateDetailsById(int profileId, int gradeId, float classAverage, int color); + public abstract void updateDetailsById(int profileId, long gradeId, float classAverage, int color); @Query("UPDATE metadata SET addedDate = :addedDate WHERE profileId = :profileId AND thingType = "+TYPE_GRADE+" AND thingId = :gradeId") - public abstract void updateAddedDateById(int profileId, int gradeId, long addedDate); + public abstract void updateAddedDateById(int profileId, long gradeId, long addedDate); @Transaction - public void updateDetails(int profileId, SparseArray gradeAverages, SparseArray gradeAddedDates, SparseIntArray gradeColors) { + public void updateDetails(int profileId, LongSparseArray gradeAverages, LongSparseArray gradeAddedDates, LongSparseArray gradeColors) { for (int i = 0; i < gradeAverages.size(); i++) { - int gradeId = gradeAverages.keyAt(i); + long gradeId = gradeAverages.keyAt(i); float classAverage = gradeAverages.valueAt(i); long addedDate = gradeAddedDates.valueAt(i); int color = gradeColors.valueAt(i); @@ -114,7 +116,7 @@ public abstract class GradeDao { @Query("SELECT addedDate FROM metadata WHERE profileId = :profileId AND thingType = "+TYPE_GRADE+" ORDER BY thingId") public abstract List getAddedDates(int profileId); @Transaction - public void getDetails(int profileId, SparseArray gradeAddedDates, SparseArray gradeAverages, SparseIntArray gradeColors) { + public void getDetails(int profileId, LongSparseArray gradeAddedDates, LongSparseArray gradeAverages, LongSparseArray gradeColors) { List ids = getIds(profileId); List classAverages = getClassAverages(profileId); List colors = getColors(profileId); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/login/LoginStore.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/login/LoginStore.java index e32dc143..f3d1b585 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/login/LoginStore.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/login/LoginStore.java @@ -63,6 +63,11 @@ public class LoginStore { } } } + public boolean hasLoginData(String key) { + if (data == null) + return false; + return data.has(key); + } @Nullable public String getLoginData(String key, @Nullable String defaultValue) { if (data == null) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/sync/FirebaseBroadcastReceiver.kt b/app/src/main/java/pl/szczodrzynski/edziennik/sync/FirebaseBroadcastReceiver.kt new file mode 100644 index 00000000..cc437ad6 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/sync/FirebaseBroadcastReceiver.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-11. + */ + +package pl.szczodrzynski.edziennik.sync + +import android.content.Context +import android.content.Intent +import android.util.Log +import androidx.legacy.content.WakefulBroadcastReceiver +import com.google.firebase.messaging.RemoteMessage +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.App + +class FirebaseBroadcastReceiver : WakefulBroadcastReceiver() { + + val TAG: String = FirebaseBroadcastReceiver::class.java.simpleName + + override fun onReceive(context: Context, intent: Intent) { + + val json = JsonObject() + + val dataBundle = intent.extras + if (dataBundle != null) + for (key in dataBundle.keySet()) { + dataBundle.get(key)?.let { + when (it) { + is String -> json.addProperty(key, it) + is Int -> json.addProperty(key, it) + is Long -> json.addProperty(key, it) + is Float -> json.addProperty(key, it) + is Boolean -> json.addProperty(key, it) + else -> json.addProperty(key, it.toString()) + } + } + } + + Log.d(TAG, "Firebase got push from Librus Broadcast ${json}") + + val sharedPreferences = context.getSharedPreferences("pushtest_broadcast", Context.MODE_PRIVATE) + sharedPreferences.edit().putString( + System.currentTimeMillis().toString(), + json.toString() + ).apply() + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/sync/MyFirebaseMessagingService.java b/app/src/main/java/pl/szczodrzynski/edziennik/sync/MyFirebaseMessagingService.java index d8841ebf..feae360f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/sync/MyFirebaseMessagingService.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/sync/MyFirebaseMessagingService.java @@ -61,7 +61,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { //processAppPush processAppPush(app, remoteMessage); break; - case "1029629079999": + case "747285019373": app.debugLog("Firebase got push from Mobidziennik "+remoteMessage.getData().toString()); processMobidziennikPush(app, remoteMessage); break; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a713e520..4ffc89a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -938,4 +938,5 @@ Pobieram uwagi ucznia... Pobieram frekwencję ucznia... Logowanie do Vulcan API... + Logowanie do API MobiDziennika...