diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt index e90eed55..280b5c64 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/ApiService.kt @@ -22,8 +22,10 @@ import pl.szczodrzynski.edziennik.api.v2.mobidziennik.Mobidziennik import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.api.v2.models.ApiTask import pl.szczodrzynski.edziennik.api.v2.template.Template +import pl.szczodrzynski.edziennik.api.v2.vulcan.Vulcan import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile +import pl.szczodrzynski.edziennik.utils.Utils.d import kotlin.math.min class ApiService : Service() { @@ -40,6 +42,7 @@ class ApiService : Service() { private var serviceClosed = false private var taskCancelled = false + private var taskRunningObject: ApiTask? = null // for debug purposes private var taskRunning = false private var taskRunningId = -1 private var taskMaximumId = 0 @@ -65,6 +68,7 @@ class ApiService : Service() { EventBus.getDefault().post(SyncProfileFinishedEvent(taskProfileId)) } notification.setIdle().post() + taskRunningObject = null taskRunning = false taskRunningId = -1 sync() @@ -77,6 +81,7 @@ class ApiService : Service() { taskId = ++taskMaximumId } } + apiError.profileId = taskProfileId EventBus.getDefault().post(SyncErrorEvent(apiError)) errorList.add(apiError) if (apiError.isCritical) { @@ -123,12 +128,22 @@ class ApiService : Service() { taskCancelled = false taskRunning = true taskRunningId = task.taskId + taskRunningObject = task if (task is ErrorReportTask) { notification .setCurrentTask(taskRunningId, null) .setProgressRes(R.string.edziennik_notification_api_error_report_title) .post() + errorList.forEach { error -> + d(TAG, "Error ${error.tag} profile ${error.profileId}: code ${error.errorCode}") + } + errorList.clear() + notification.setIdle().post() + taskRunningObject = null + taskRunning = false + taskRunningId = -1 + sync() return } @@ -153,6 +168,7 @@ class ApiService : Service() { edziennikInterface = when (loginStore.type) { LOGIN_TYPE_LIBRUS -> Librus(app, profile, loginStore, taskCallback) LOGIN_TYPE_MOBIDZIENNIK -> Mobidziennik(app, profile, loginStore, taskCallback) + LOGIN_TYPE_VULCAN -> Vulcan(app, profile, loginStore, taskCallback) LOGIN_TYPE_TEMPLATE -> Template(app, profile, loginStore, taskCallback) else -> null } @@ -161,7 +177,9 @@ class ApiService : Service() { } when (task) { - is SyncProfileRequest -> edziennikInterface?.sync(task.viewIds?.flatMap { Features.getIdsByView(it.first, it.second) } ?: Features.getAllIds()) + is SyncProfileRequest -> edziennikInterface?.sync( + featureIds = task.viewIds?.flatMap { Features.getIdsByView(it.first, it.second) } ?: Features.getAllIds(), + viewId = task.viewIds?.get(0)?.first) is MessageGetRequest -> edziennikInterface?.getMessage(task.messageId) } } @@ -196,15 +214,6 @@ class ApiService : Service() { sync() } - @Subscribe(threadMode = ThreadMode.ASYNC) - fun onSyncViewRequest(syncViewRequest: SyncViewRequest) { - Log.d(TAG, syncViewRequest.toString()) - taskQueue += syncViewRequest.apply { - taskId = ++taskMaximumId - } - sync() - } - @Subscribe(threadMode = ThreadMode.ASYNC) fun onMessageGetRequest(messageGetRequest: MessageGetRequest) { Log.d(TAG, messageGetRequest.toString()) @@ -235,11 +244,11 @@ class ApiService : Service() { |_____/ \___|_| \_/ |_|\___\___| \___/ \_/ \___|_| |_| |_|\__,_|\___||__*/ override fun onCreate() { EventBus.getDefault().register(this) + notification.setIdle().setCloseAction() } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { startForeground(EdziennikNotification.NOTIFICATION_ID, notification.notification) - notification.setIdle().setCloseAction().post() return START_NOT_STICKY } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt index b74e9b77..fecfecbd 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Constants.kt @@ -4,6 +4,8 @@ package pl.szczodrzynski.edziennik.api.v2 +import android.os.Build + const val GET = 0 const val POST = 1 @@ -42,4 +44,24 @@ const val LIBRUS_SANDBOX_URL = "https://sandbox.librus.pl/index.php?action=" -val MOBIDZIENNIK_USER_AGENT = SYSTEM_USER_AGENT \ No newline at end of file +val MOBIDZIENNIK_USER_AGENT = SYSTEM_USER_AGENT + +const val VULCAN_API_USER_AGENT = "MobileUserAgent" +const val VULCAN_API_APP_NAME = "VULCAN-Android-ModulUcznia" +const val VULCAN_API_APP_VERSION = "19.4.1.436" +val VULCAN_API_DEVICE_NAME = "Szkolny.eu ${Build.MODEL}" + +const val VULCAN_API_ENDPOINT_CERTIFICATE = "mobile-api/Uczen.v3.UczenStart/Certyfikat"; +const val VULCAN_API_ENDPOINT_STUDENT_LIST = "mobile-api/Uczen.v3.UczenStart/ListaUczniow"; +const val VULCAN_API_ENDPOINT_DICTIONARIES = "mobile-api/Uczen.v3.Uczen/Slowniki"; +const val VULCAN_API_ENDPOINT_TIMETABLE = "mobile-api/Uczen.v3.Uczen/PlanLekcjiZeZmianami"; +const val VULCAN_API_ENDPOINT_GRADES = "mobile-api/Uczen.v3.Uczen/Oceny"; +const val VULCAN_API_ENDPOINT_GRADES_PROPOSITIONS = "mobile-api/Uczen.v3.Uczen/OcenyPodsumowanie"; +const val VULCAN_API_ENDPOINT_EVENTS = "mobile-api/Uczen.v3.Uczen/Sprawdziany"; +const val VULCAN_API_ENDPOINT_HOMEWORK = "mobile-api/Uczen.v3.Uczen/ZadaniaDomowe"; +const val VULCAN_API_ENDPOINT_NOTICES = "mobile-api/Uczen.v3.Uczen/UwagiUcznia"; +const val VULCAN_API_ENDPOINT_ATTENDANCE = "mobile-api/Uczen.v3.Uczen/Frekwencje"; +const val VULCAN_API_ENDPOINT_MESSAGES_RECEIVED = "mobile-api/Uczen.v3.Uczen/WiadomosciOdebrane"; +const val VULCAN_API_ENDPOINT_MESSAGES_SENT = "mobile-api/Uczen.v3.Uczen/WiadomosciWyslane"; +const val VULCAN_API_ENDPOINT_MESSAGES_CHANGE_STATUS = "mobile-api/Uczen.v3.Uczen/ZmienStatusWiadomosci"; +const val VULCAN_API_ENDPOINT_PUSH = "mobile-api/Uczen.v3.Uczen/UstawPushToken"; \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Errors.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Errors.kt index 7780cb92..458a79bf 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Errors.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/Errors.kt @@ -92,6 +92,8 @@ const val ERROR_LOGIN_LIBRUS_PORTAL_NO_REFRESH = 168 const val ERROR_LOGIN_LIBRUS_PORTAL_NO_REDIRECT = 169 const val ERROR_LOGIN_LIBRUS_PORTAL_UNSUPPORTED_GRANT = 170 const val ERROR_LOGIN_LIBRUS_PORTAL_INVALID_CLIENT_ID = 171 +const val ERROR_LOGIN_LIBRUS_PORTAL_REFRESH_INVALID = 172 +const val ERROR_LOGIN_LIBRUS_PORTAL_REFRESH_REVOKED = 173 const val ERROR_LOGIN_MOBIDZIENNIK_WEB_INVALID_LOGIN = 201 const val ERROR_LOGIN_MOBIDZIENNIK_WEB_OLD_PASSWORD = 202 @@ -105,7 +107,16 @@ const val ERROR_MOBIDZIENNIK_WEB_NO_SESSION_KEY = 212 const val ERROR_MOBIDZIENNIK_WEB_NO_SESSION_VALUE = 212 const val ERROR_MOBIDZIENNIK_WEB_NO_SERVER_ID = 213 -const val ERROR_TEMPLATE_WEB_OTHER = 301 +const val ERROR_LOGIN_VULCAN_INVALID_SYMBOL = 301 +const val ERROR_LOGIN_VULCAN_INVALID_TOKEN = 302 +const val ERROR_LOGIN_VULCAN_INVALID_PIN = 309 +const val ERROR_LOGIN_VULCAN_INVALID_PIN_0_REMAINING = 310 +const val ERROR_LOGIN_VULCAN_INVALID_PIN_1_REMAINING = 311 +const val ERROR_LOGIN_VULCAN_INVALID_PIN_2_REMAINING = 312 +const val ERROR_LOGIN_VULCAN_EXPIRED_TOKEN = 321 +const val ERROR_LOGIN_VULCAN_OTHER = 322 + +const val ERROR_TEMPLATE_WEB_OTHER = 801 const val EXCEPTION_LOGIN_LIBRUS_API_TOKEN = 901 const val EXCEPTION_LOGIN_LIBRUS_PORTAL_TOKEN = 902 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 8d94e388..5d08354c 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 @@ -12,6 +12,7 @@ import pl.szczodrzynski.edziennik.api.v2.mobidziennik.login.MobidziennikLoginWeb import pl.szczodrzynski.edziennik.api.v2.models.LoginMethod import pl.szczodrzynski.edziennik.api.v2.template.login.TemplateLoginApi import pl.szczodrzynski.edziennik.api.v2.template.login.TemplateLoginWeb +import pl.szczodrzynski.edziennik.api.v2.vulcan.login.VulcanLoginApi // librus // mobidziennik @@ -21,36 +22,32 @@ import pl.szczodrzynski.edziennik.api.v2.template.login.TemplateLoginWeb const val SYNERGIA_API_ENABLED = true -const val LOGIN_TYPE_LIBRUS = 2 -const val LOGIN_TYPE_MOBIDZIENNIK = 1 + + const val LOGIN_TYPE_IDZIENNIK = 3 -const val LOGIN_TYPE_VULCAN = 4 + const val LOGIN_TYPE_TEMPLATE = 21 // LOGIN MODES -const val LOGIN_MODE_LIBRUS_EMAIL = 0 -const val LOGIN_MODE_LIBRUS_SYNERGIA = 1 -const val LOGIN_MODE_LIBRUS_JST = 2 -const val LOGIN_MODE_MOBIDZIENNIK_WEB = 0 const val LOGIN_MODE_IDZIENNIK_WEB = 0 -const val LOGIN_MODE_VULCAN_WEB = 0 + const val LOGIN_MODE_TEMPLATE_WEB = 0 // LOGIN METHODS const val LOGIN_METHOD_NOT_NEEDED = -1 +const val LOGIN_METHOD_IDZIENNIK_WEB = 100 +const val LOGIN_METHOD_IDZIENNIK_API = 200 +const val LOGIN_METHOD_TEMPLATE_WEB = 100 +const val LOGIN_METHOD_TEMPLATE_API = 200 + +const val LOGIN_TYPE_LIBRUS = 2 +const val LOGIN_MODE_LIBRUS_EMAIL = 0 +const val LOGIN_MODE_LIBRUS_SYNERGIA = 1 +const val LOGIN_MODE_LIBRUS_JST = 2 const val LOGIN_METHOD_LIBRUS_PORTAL = 100 const val LOGIN_METHOD_LIBRUS_API = 200 const val LOGIN_METHOD_LIBRUS_SYNERGIA = 300 const val LOGIN_METHOD_LIBRUS_MESSAGES = 400 -const val LOGIN_METHOD_MOBIDZIENNIK_WEB = 100 -const val LOGIN_METHOD_MOBIDZIENNIK_API2 = 300 -const val LOGIN_METHOD_IDZIENNIK_WEB = 100 -const val LOGIN_METHOD_IDZIENNIK_API = 200 -const val LOGIN_METHOD_VULCAN_WEB = 100 -const val LOGIN_METHOD_VULCAN_API = 200 -const val LOGIN_METHOD_TEMPLATE_WEB = 100 -const val LOGIN_METHOD_TEMPLATE_API = 200 - val librusLoginMethods = listOf( LoginMethod(LOGIN_TYPE_LIBRUS, LOGIN_METHOD_LIBRUS_PORTAL, LibrusLoginPortal::class.java) .withIsPossible { _, loginStore -> @@ -79,12 +76,44 @@ val librusLoginMethods = listOf( } ) +const val LOGIN_TYPE_MOBIDZIENNIK = 1 +const val LOGIN_MODE_MOBIDZIENNIK_WEB = 0 +const val LOGIN_METHOD_MOBIDZIENNIK_WEB = 100 +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 } ) +const val LOGIN_TYPE_VULCAN = 4 +const val LOGIN_MODE_VULCAN_API = 0 +const val LOGIN_MODE_VULCAN_WEB = 1 +const val LOGIN_METHOD_VULCAN_WEB_MAIN = 100 +const val LOGIN_METHOD_VULCAN_WEB_NEW = 200 +const val LOGIN_METHOD_VULCAN_WEB_OLD = 300 +const val LOGIN_METHOD_VULCAN_WEB_MESSAGES = 400 +const val LOGIN_METHOD_VULCAN_API = 500 +val vulcanLoginMethods = listOf( + /*LoginMethod(LOGIN_TYPE_VULCAN, LOGIN_METHOD_VULCAN_WEB_MAIN, VulcanLoginWebMain::class.java) + .withIsPossible { _, _ -> false } + .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_NOT_NEEDED }, + + LoginMethod(LOGIN_TYPE_VULCAN, LOGIN_METHOD_VULCAN_WEB_NEW, VulcanLoginWebNew::class.java) + .withIsPossible { _, _ -> false } + .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_VULCAN_WEB_MAIN }, + + LoginMethod(LOGIN_TYPE_VULCAN, LOGIN_METHOD_VULCAN_WEB_OLD, VulcanLoginWebOld::class.java) + .withIsPossible { _, _ -> false } + .withRequiredLoginMethod { _, _ -> LOGIN_METHOD_VULCAN_WEB_MAIN },*/ + + LoginMethod(LOGIN_TYPE_VULCAN, LOGIN_METHOD_VULCAN_API, VulcanLoginApi::class.java) + .withIsPossible { _, _ -> true } + .withRequiredLoginMethod { _, loginStore -> + if (loginStore.mode == LOGIN_MODE_VULCAN_WEB) LOGIN_METHOD_VULCAN_WEB_NEW else LOGIN_METHOD_NOT_NEEDED + } +) + val templateLoginMethods = listOf( LoginMethod(LOGIN_TYPE_TEMPLATE, LOGIN_METHOD_TEMPLATE_WEB, TemplateLoginWeb::class.java) .withIsPossible { _, _ -> true } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/SyncViewRequest.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/SyncViewRequest.kt deleted file mode 100644 index fe494236..00000000 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/events/requests/SyncViewRequest.kt +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright (c) Kuba Szczodrzyński 2019-9-29. - */ - -package pl.szczodrzynski.edziennik.api.v2.events.requests - -import pl.szczodrzynski.edziennik.api.v2.models.ApiTask - -class SyncViewRequest(override val profileId: Int, val targetId: Int, val targetType: Int) : ApiTask(profileId) { - override fun toString(): String { - return "SyncViewRequest(profileId=$profileId, targetId=$targetId, targetType=$targetType)" - } -} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/DataLibrus.kt similarity index 99% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/DataLibrus.kt index 950ca815..602884eb 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/DataLibrus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/DataLibrus.kt @@ -2,7 +2,7 @@ * Copyright (c) Kuba Szczodrzyński 2019-9-21. */ -package pl.szczodrzynski.edziennik.api.v2.librus.data +package pl.szczodrzynski.edziennik.api.v2.librus import okhttp3.Cookie import pl.szczodrzynski.edziennik.App diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt index 7ad98803..66594fd7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/Librus.kt @@ -11,7 +11,6 @@ import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_NOT_NEEDED import pl.szczodrzynski.edziennik.api.v2.endpoints import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusData import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLogin import pl.szczodrzynski.edziennik.api.v2.librusLoginMethods @@ -91,7 +90,7 @@ class Librus(val app: App, val profile: Profile?, val loginStore: LoginStore, va .singleOrNull { it.endpointId == endpoint.first } ?: EndpointTimer(data.profile?.id ?: -1, endpoint.first)) .let { timer -> if (timer.nextSync == SYNC_ALWAYS || - (timer.viewId == viewId) || + (viewId != null && timer.viewId == viewId) || (timer.nextSync != SYNC_NEVER && timer.nextSync < timestamp)) { data.targetEndpointIds.add(endpoint.first) requiredLoginMethods.add(endpoint.second) 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 eabb3263..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 @@ -9,8 +9,10 @@ import im.wangchao.mhttp.Request import im.wangchao.mhttp.Response import im.wangchao.mhttp.callback.JsonCallbackHandler import pl.szczodrzynski.edziennik.api.v2.* +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.Utils.d import java.net.HttpURLConnection.* open class LibrusApi(open val data: DataLibrus) { @@ -24,7 +26,9 @@ open class LibrusApi(open val data: DataLibrus) { val profile get() = data.profile - fun apiGet(tag: String, endpoint: String, method: Int = GET, payload: JsonObject? = null, onSuccess: (json: JsonObject?) -> Unit) { + fun apiGet(tag: String, endpoint: String, method: Int = GET, payload: JsonObject? = null, onSuccess: (json: JsonObject) -> Unit) { + + d(tag, "Request: Librus/Api - $LIBRUS_API_URL/$endpoint") val callback = object : JsonCallbackHandler() { override fun onSuccess(json: JsonObject?, response: Response?) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt index 6cfbd377..3b488a02 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt @@ -7,6 +7,7 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_SCHOOLS +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiMe import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiSchools import pl.szczodrzynski.edziennik.utils.Utils diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt index 2badd084..7beeaace 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt @@ -4,11 +4,11 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi class LibrusApiEvents(override val data: DataLibrus, - val onSuccess: () -> Unit) : LibrusApi(data) { + val onSuccess: () -> Unit) : LibrusApi(data) { companion object { const val TAG = "LibrusApiEvents" } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiMe.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiMe.kt index 956bd683..2e408b0b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiMe.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiMe.kt @@ -6,7 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi class LibrusApiMe(override val data: DataLibrus, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt index 53bea29a..582462f1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiSchools.kt @@ -4,24 +4,22 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api -import android.util.Pair -import com.google.gson.JsonNull import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_SCHOOLS -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi import pl.szczodrzynski.edziennik.data.db.modules.lessons.LessonRange import pl.szczodrzynski.edziennik.utils.models.Time import java.util.* class LibrusApiSchools(override val data: DataLibrus, - val onSuccess: () -> Unit) : LibrusApi(data) { + val onSuccess: () -> Unit) : LibrusApi(data) { companion object { const val TAG = "LibrusApiSchools" } init { - apiGet(LibrusApiMe.TAG, "") { json -> + apiGet(TAG, "Schools") { json -> val school = json?.getJsonObject("School") val schoolId = school?.getInt("Id") val schoolNameLong = school?.getString("Name") diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt index cacd1215..e09456f2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTemplate.kt @@ -4,8 +4,7 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api -import pl.szczodrzynski.edziennik.DAY -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi class LibrusApiTemplate(override val data: DataLibrus, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt index 725c716d..b0dea3d4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLogin.kt @@ -9,7 +9,7 @@ import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_API import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_MESSAGES import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_PORTAL import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_SYNERGIA -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.utils.Utils class LibrusLogin(val data: DataLibrus, val onSuccess: () -> Unit) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginApi.kt index 102db2d1..22309ac6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginApi.kt @@ -11,8 +11,11 @@ import im.wangchao.mhttp.body.MediaTypeUtils import im.wangchao.mhttp.callback.JsonCallbackHandler import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.login.MobidziennikLoginWeb import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.Utils.d import java.net.HttpURLConnection.* class LibrusLoginApi { @@ -157,6 +160,8 @@ class LibrusLoginApi { } private fun synergiaGetToken() { + d(TAG, "Request: Librus/Login/Api - $LIBRUS_API_TOKEN_URL") + Request.builder() .url(LIBRUS_API_TOKEN_URL) .userAgent(LIBRUS_USER_AGENT) @@ -175,6 +180,8 @@ class LibrusLoginApi { .enqueue() } private fun synergiaRefreshToken() { + d(TAG, "Request: Librus/Login/Api - $LIBRUS_API_TOKEN_URL") + Request.builder() .url(LIBRUS_API_TOKEN_URL) .userAgent(LIBRUS_USER_AGENT) @@ -192,6 +199,8 @@ class LibrusLoginApi { .enqueue() } private fun jstGetToken() { + d(TAG, "Request: Librus/Login/Api - $LIBRUS_API_TOKEN_JST_URL") + Request.builder() .url(LIBRUS_API_TOKEN_JST_URL) .userAgent(LIBRUS_USER_AGENT) @@ -212,6 +221,8 @@ class LibrusLoginApi { .enqueue() } private fun jstRefreshToken() { + d(TAG, "Request: Librus/Login/Api - $LIBRUS_API_TOKEN_JST_URL") + Request.builder() .url(LIBRUS_API_TOKEN_JST_URL) .userAgent(LIBRUS_USER_AGENT) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginMessages.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginMessages.kt index d6adf3b1..95503944 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginMessages.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginMessages.kt @@ -8,13 +8,12 @@ import im.wangchao.mhttp.Request import im.wangchao.mhttp.Response import im.wangchao.mhttp.callback.TextCallbackHandler import okhttp3.Cookie -import okhttp3.HttpUrl -import okhttp3.internal.http.HttpDate import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.models.ApiError -import pl.szczodrzynski.edziennik.currentTimeUnix import pl.szczodrzynski.edziennik.getUnixDate +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.Utils.d class LibrusLoginMessages(val data: DataLibrus, val onSuccess: () -> Unit) { companion object { @@ -62,6 +61,8 @@ class LibrusLoginMessages(val data: DataLibrus, val onSuccess: () -> Unit) { * A login method using the Synergia website (/wiadomosci2 Auto Login). */ private fun loginWithSynergia(url: String = "https://synergia.librus.pl/wiadomosci2") { + d(TAG, "Request: Librus/Login/Messages - $url") + val callback = object : TextCallbackHandler() { override fun onSuccess(text: String?, response: Response?) { val location = response?.headers()?.get("Location") diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginPortal.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginPortal.kt index 5aa864ec..67730e01 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginPortal.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginPortal.kt @@ -9,8 +9,10 @@ import im.wangchao.mhttp.callback.JsonCallbackHandler import im.wangchao.mhttp.callback.TextCallbackHandler import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.Utils.d import java.net.HttpURLConnection.HTTP_UNAUTHORIZED import java.util.ArrayList import java.util.regex.Pattern @@ -45,6 +47,8 @@ class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) { }} private fun authorize(url: String?) { + d(TAG, "Request: Librus/Login/Portal - $url") + Request.builder() .url(url) .userAgent(LIBRUS_USER_AGENT) @@ -82,6 +86,8 @@ class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) { } private fun login(csrfToken: String) { + d(TAG, "Request: Librus/Login/Portal - $LIBRUS_LOGIN_URL") + Request.builder() .url(LIBRUS_LOGIN_URL) .userAgent(LIBRUS_USER_AGENT) @@ -129,6 +135,8 @@ class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) { private var refreshTokenFailed = false private fun accessToken(code: String?, refreshToken: String?) { + d(TAG, "Request: Librus/Login/Portal - $LIBRUS_TOKEN_URL") + val onSuccess = { json: JsonObject, response: Response? -> data.portalAccessToken = json.getString("access_token") data.portalRefreshToken = json.getString("refresh_token") @@ -148,6 +156,8 @@ class LibrusLoginPortal(val data: DataLibrus, val onSuccess: () -> Unit) { when (code) { "Authorization code has expired" -> ERROR_LOGIN_LIBRUS_PORTAL_CODE_EXPIRED "Authorization code has been revoked" -> ERROR_LOGIN_LIBRUS_PORTAL_CODE_REVOKED + "Cannot decrypt the refresh token" -> ERROR_LOGIN_LIBRUS_PORTAL_REFRESH_INVALID + "Token has been revoked" -> ERROR_LOGIN_LIBRUS_PORTAL_REFRESH_REVOKED "Check the `client_id` parameter" -> ERROR_LOGIN_LIBRUS_PORTAL_NO_CLIENT_ID "Check the `code` parameter" -> ERROR_LOGIN_LIBRUS_PORTAL_NO_CODE "Check the `refresh_token` parameter" -> ERROR_LOGIN_LIBRUS_PORTAL_NO_REFRESH diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginSynergia.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginSynergia.kt index 23a0b361..46fbfe2d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginSynergia.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/LibrusLoginSynergia.kt @@ -11,13 +11,16 @@ import im.wangchao.mhttp.callback.JsonCallbackHandler import im.wangchao.mhttp.callback.TextCallbackHandler import okhttp3.Cookie import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.getString import pl.szczodrzynski.edziennik.getUnixDate +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.Utils.d import java.net.HttpURLConnection -class LibrusLoginSynergia(val data: DataLibrus, val onSuccess: () -> Unit) { +class LibrusLoginSynergia(override val data: DataLibrus, val onSuccess: () -> Unit) : LibrusApi(data) { companion object { private const val TAG = "LoginLibrusSynergia" } @@ -63,80 +66,13 @@ class LibrusLoginSynergia(val data: DataLibrus, val onSuccess: () -> Unit) { * A login method using the Synergia API (AutoLoginToken endpoint). */ private fun loginWithApi() { + d(TAG, "Request: Librus/Login/Synergia - $LIBRUS_API_URL/AutoLoginToken") + val onSuccess = { json: JsonObject -> loginWithToken(json.getString("Token")) } - val callback = object : JsonCallbackHandler() { - override fun onSuccess(json: JsonObject?, response: Response?) { - if (json == null && response?.parserErrorBody == null) { - data.error(ApiError(TAG, ERROR_RESPONSE_EMPTY) - .withResponse(response)) - return - } - val error = if (response?.code() == 200) null else - json.getString("Code") ?: - json.getString("Message") ?: - response?.parserErrorBody - error?.let { code -> - when (code) { - "TokenIsExpired" -> ERROR_LIBRUS_API_TOKEN_EXPIRED - "Insufficient scopes" -> ERROR_LIBRUS_API_INSUFFICIENT_SCOPES - "Request is denied" -> ERROR_LIBRUS_API_ACCESS_DENIED - "Resource not found" -> ERROR_LIBRUS_API_RESOURCE_NOT_FOUND - "NotFound" -> ERROR_LIBRUS_API_DATA_NOT_FOUND - "AccessDeny" -> when (json.getString("Message")) { - "Student timetable is not public" -> ERROR_LIBRUS_API_TIMETABLE_NOT_PUBLIC - else -> ERROR_LIBRUS_API_RESOURCE_ACCESS_DENIED - } - "LuckyNumberIsNotActive" -> ERROR_LIBRUS_API_LUCKY_NUMBER_NOT_ACTIVE - "NotesIsNotActive" -> ERROR_LIBRUS_API_NOTES_NOT_ACTIVE - "InvalidRequest" -> ERROR_LIBRUS_API_INVALID_REQUEST_PARAMS - "Nieprawidłowy węzeł." -> ERROR_LIBRUS_API_INCORRECT_ENDPOINT - else -> ERROR_LIBRUS_API_OTHER - }.let { errorCode -> - data.error(ApiError(TAG, errorCode) - .withApiResponse(json) - .withResponse(response)) - return - } - } - - if (json == null) { - data.error(ApiError(TAG, ERROR_RESPONSE_EMPTY) - .withResponse(response)) - return - } - - try { - onSuccess(json) - } catch (e: Exception) { - data.error(ApiError(TAG, EXCEPTION_LIBRUS_API_REQUEST) - .withResponse(response) - .withThrowable(e) - .withApiResponse(json)) - } - } - - override fun onFailure(response: Response?, throwable: Throwable?) { - // TODO add hotfix for Classrooms 500 - data.error(ApiError(TAG, ERROR_REQUEST_FAILURE) - .withResponse(response) - .withThrowable(throwable)) - } - } - - Request.builder() - .url("$LIBRUS_API_URL/AutoLoginToken") - .userAgent(LIBRUS_USER_AGENT) - .addHeader("Authorization", "Bearer ${data.apiAccessToken}") - .post() - .allowErrorCode(HttpURLConnection.HTTP_BAD_REQUEST) - .allowErrorCode(HttpURLConnection.HTTP_FORBIDDEN) - .allowErrorCode(HttpURLConnection.HTTP_UNAUTHORIZED) - .callback(callback) - .build() - .enqueue() + apiGet(TAG, "AutoLoginToken", POST, null, onSuccess) } private fun loginWithToken(token: String?) { @@ -145,6 +81,8 @@ class LibrusLoginSynergia(val data: DataLibrus, val onSuccess: () -> Unit) { return } + d(TAG, "Request: Librus/Login/Synergia - " + LIBRUS_SYNERGIA_TOKEN_LOGIN_URL.replace("TOKEN", token) + "/uczen/widok/centrum_powiadomien") + val callback = object : TextCallbackHandler() { override fun onSuccess(json: String?, response: Response?) { val location = response?.headers()?.get("Location") diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/SynergiaTokenExtractor.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/SynergiaTokenExtractor.kt index 091ccbb2..be0fd126 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/SynergiaTokenExtractor.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/login/SynergiaTokenExtractor.kt @@ -1,14 +1,14 @@ package pl.szczodrzynski.edziennik.api.v2.librus.login -import com.google.gson.JsonNull import com.google.gson.JsonObject import im.wangchao.mhttp.Request import im.wangchao.mhttp.Response import im.wangchao.mhttp.callback.JsonCallbackHandler import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.utils.Utils import pl.szczodrzynski.edziennik.utils.Utils.d import java.net.HttpURLConnection.* @@ -42,9 +42,12 @@ class SynergiaTokenExtractor(val data: DataLibrus, val onSuccess: () -> Unit) { * If necessary, refreshes the token. */ private fun synergiaAccount(): Boolean { + val accountLogin = data.apiLogin ?: return false val accessToken = data.portalAccessToken ?: return false + d(TAG, "Request: Librus/SynergiaTokenExtractor - $LIBRUS_ACCOUNT_URL$accountLogin") + val onSuccess = { json: JsonObject, response: Response? -> // synergiaAccount is executed when a synergia token needs a refresh val accountId = json.getInt("id") diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/DataMobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt similarity index 98% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/DataMobidziennik.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt index c0c6c49c..770be112 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/DataMobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt @@ -2,7 +2,7 @@ * Copyright (c) Kuba Szczodrzyński 2019-10-5. */ -package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data +package pl.szczodrzynski.edziennik.api.v2.mobidziennik import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_MOBIDZIENNIK_WEB diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt index 938ed410..123eba43 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/Mobidziennik.kt @@ -9,14 +9,10 @@ import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.api.v2.* import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface -import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.DataMobidziennik import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.MobidziennikData import pl.szczodrzynski.edziennik.api.v2.mobidziennik.login.MobidziennikLogin import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.api.v2.models.Feature -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate -import pl.szczodrzynski.edziennik.api.v2.template.data.TemplateData -import pl.szczodrzynski.edziennik.api.v2.template.login.TemplateLogin import pl.szczodrzynski.edziennik.data.db.modules.api.EndpointTimer import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_NEVER @@ -93,7 +89,7 @@ class Mobidziennik(val app: App, val profile: Profile?, val loginStore: LoginSto .singleOrNull { it.endpointId == endpoint.first } ?: EndpointTimer(data.profile?.id ?: -1, endpoint.first)) .let { timer -> if (timer.nextSync == SYNC_ALWAYS || - (timer.viewId == viewId) || + (viewId != null && timer.viewId == viewId) || (timer.nextSync != SYNC_NEVER && timer.nextSync < timestamp)) { data.targetEndpointIds.add(endpoint.first) requiredLoginMethods.add(endpoint.second) 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 46efbd00..1f952aeb 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 @@ -4,15 +4,7 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data -import pl.szczodrzynski.edziennik.R -import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_API_SAMPLE -import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_WEB_SAMPLE -import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_WEB_SAMPLE_2 import pl.szczodrzynski.edziennik.api.v2.mobidziennik.* -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate -import pl.szczodrzynski.edziennik.api.v2.template.data.api.TemplateApiSample -import pl.szczodrzynski.edziennik.api.v2.template.data.web.TemplateWebSample -import pl.szczodrzynski.edziennik.api.v2.template.data.web.TemplateWebSample2 import pl.szczodrzynski.edziennik.utils.Utils class MobidziennikData(val data: DataMobidziennik, val onSuccess: () -> Unit) { 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 a5c14df7..c5728346 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 @@ -4,18 +4,13 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data -import com.google.gson.JsonObject import im.wangchao.mhttp.Request import im.wangchao.mhttp.Response import im.wangchao.mhttp.callback.TextCallbackHandler import okhttp3.Cookie import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginSynergia +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik import pl.szczodrzynski.edziennik.api.v2.models.ApiError -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate -import pl.szczodrzynski.edziennik.currentTimeUnix -import pl.szczodrzynski.edziennik.data.api.AppError -import pl.szczodrzynski.edziennik.data.api.AppError.* import pl.szczodrzynski.edziennik.utils.Utils.d open class MobidziennikWeb(open val data: DataMobidziennik) { @@ -32,7 +27,7 @@ open class MobidziennikWeb(open val data: DataMobidziennik) { fun webGet(tag: String, endpoint: String, method: Int = GET, payload: List>? = null, onSuccess: (text: String) -> Unit) { val url = "https://${data.loginServerName}.mobidziennik.pl$endpoint" - d(tag, "Requesting $url") + d(tag, "Request: Mobidziennik/Web - $url") if (data.webSessionKey == null) { data.error(TAG, ERROR_MOBIDZIENNIK_WEB_NO_SESSION_KEY) 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 efbcd6c8..58f94e36 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 @@ -6,7 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.login import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_MOBIDZIENNIK_WEB -import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.DataMobidziennik +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik import pl.szczodrzynski.edziennik.utils.Utils class MobidziennikLogin(val data: DataMobidziennik, val onSuccess: () -> Unit) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLoginWeb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLoginWeb.kt index 0cf3bd7c..1a40d786 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLoginWeb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/login/MobidziennikLoginWeb.kt @@ -8,10 +8,12 @@ import im.wangchao.mhttp.Request import im.wangchao.mhttp.Response import im.wangchao.mhttp.callback.TextCallbackHandler import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.DataMobidziennik +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.getUnixDate import pl.szczodrzynski.edziennik.isNotNullNorEmpty +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.Utils.d class MobidziennikLoginWeb(val data: DataMobidziennik, val onSuccess: () -> Unit) { companion object { @@ -39,6 +41,8 @@ class MobidziennikLoginWeb(val data: DataMobidziennik, val onSuccess: () -> Unit }} private fun loginWithCredentials() { + d(TAG, "Request: Mobidziennik/Login/Web - https://${data.loginServerName}.mobidziennik.pl/api/") + val callback = object : TextCallbackHandler() { override fun onSuccess(text: String?, response: Response?) { if (text != "ok") { 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 a6b5bbe9..23cc19fb 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 @@ -90,7 +90,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) val teacherList = LongSparseArray() val subjectList = LongSparseArray() - val teamList = mutableListOf() + var teamList = mutableListOf() var lessonRanges = mutableListOf() var lessonsToRemove: DataRemoveModel? = null @@ -139,6 +139,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) db.subjectDao().getAllNow(profile.id).forEach { subject -> subjectList.put(subject.id, subject) } + teamList = db.teamDao().getAllNow(profile.id).toMutableList() } /*val teacher = teachers.byNameFirstLast("Jan Kowalski") ?: Teacher(1, 1, "", "").let { @@ -180,6 +181,8 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) db.endpointTimerDao().addAll(endpointTimers) + db.lessonRangeDao().addAll(lessonRanges) + if (teacherList.isNotEmpty()) { val tempList: ArrayList = ArrayList() teacherList.forEach { _, teacher -> diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/DataTemplate.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/DataTemplate.kt similarity index 98% rename from app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/DataTemplate.kt rename to app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/DataTemplate.kt index 36a973f5..8715d09f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/DataTemplate.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/DataTemplate.kt @@ -2,7 +2,7 @@ * Copyright (c) Kuba Szczodrzyński 2019-10-5. */ -package pl.szczodrzynski.edziennik.api.v2.template.data +package pl.szczodrzynski.edziennik.api.v2.template import okhttp3.Cookie import pl.szczodrzynski.edziennik.App diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt index f12ce12e..6e39087c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/Template.kt @@ -12,7 +12,6 @@ import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface import pl.szczodrzynski.edziennik.api.v2.models.ApiError import pl.szczodrzynski.edziennik.api.v2.models.Feature -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate import pl.szczodrzynski.edziennik.api.v2.template.data.TemplateData import pl.szczodrzynski.edziennik.api.v2.template.login.TemplateLogin import pl.szczodrzynski.edziennik.api.v2.templateEndpoints @@ -93,7 +92,7 @@ class Template(val app: App, val profile: Profile?, val loginStore: LoginStore, .singleOrNull { it.endpointId == endpoint.first } ?: EndpointTimer(data.profile?.id ?: -1, endpoint.first)) .let { timer -> if (timer.nextSync == SYNC_ALWAYS || - (timer.viewId == viewId) || + (viewId != null && timer.viewId == viewId) || (timer.nextSync != SYNC_NEVER && timer.nextSync < timestamp)) { data.targetEndpointIds.add(endpoint.first) requiredLoginMethods.add(endpoint.second) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateApi.kt index cff0c42a..9021b2db 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateApi.kt @@ -8,6 +8,7 @@ import com.google.gson.JsonObject import pl.szczodrzynski.edziennik.api.v2.ERROR_TEMPLATE_WEB_OTHER import pl.szczodrzynski.edziennik.api.v2.GET import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.currentTimeUnix open class TemplateApi(open val data: DataTemplate) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateData.kt index 16601d2c..dfdc7813 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateData.kt @@ -8,6 +8,7 @@ import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_API_SAMPLE import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_WEB_SAMPLE import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_WEB_SAMPLE_2 +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.api.v2.template.data.api.TemplateApiSample import pl.szczodrzynski.edziennik.api.v2.template.data.web.TemplateWebSample import pl.szczodrzynski.edziennik.api.v2.template.data.web.TemplateWebSample2 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateWeb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateWeb.kt index 9cbfe0f5..8e7f79c7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateWeb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/TemplateWeb.kt @@ -5,12 +5,10 @@ package pl.szczodrzynski.edziennik.api.v2.template.data import com.google.gson.JsonObject -import pl.szczodrzynski.edziennik.api.v2.ERROR_REQUEST_FAILURE -import pl.szczodrzynski.edziennik.api.v2.ERROR_RESPONSE_EMPTY import pl.szczodrzynski.edziennik.api.v2.ERROR_TEMPLATE_WEB_OTHER import pl.szczodrzynski.edziennik.api.v2.GET -import pl.szczodrzynski.edziennik.api.v2.librus.data.DataLibrus import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.currentTimeUnix open class TemplateWeb(open val data: DataTemplate) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/api/TemplateApiSample.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/api/TemplateApiSample.kt index 7a5875d2..ef0ac8e6 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/api/TemplateApiSample.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/api/TemplateApiSample.kt @@ -7,10 +7,8 @@ package pl.szczodrzynski.edziennik.api.v2.template.data.api import pl.szczodrzynski.edziennik.DAY import pl.szczodrzynski.edziennik.MainActivity import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_API_SAMPLE -import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_WEB_SAMPLE -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.api.v2.template.data.TemplateApi -import pl.szczodrzynski.edziennik.api.v2.template.data.TemplateWeb import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS class TemplateApiSample(override val data: DataTemplate, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/web/TemplateWebSample.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/web/TemplateWebSample.kt index 2aacdc7f..6e5340e1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/web/TemplateWebSample.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/web/TemplateWebSample.kt @@ -8,7 +8,7 @@ import pl.szczodrzynski.edziennik.DAY import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_GRADES import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_HOME import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_WEB_SAMPLE -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.api.v2.template.data.TemplateWeb import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/web/TemplateWebSample2.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/web/TemplateWebSample2.kt index 80f5b1c4..94e2235d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/web/TemplateWebSample2.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/data/web/TemplateWebSample2.kt @@ -6,14 +6,13 @@ package pl.szczodrzynski.edziennik.api.v2.template.data.web import pl.szczodrzynski.edziennik.DAY import pl.szczodrzynski.edziennik.MainActivity -import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_WEB_SAMPLE import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_TEMPLATE_WEB_SAMPLE_2 -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.api.v2.template.data.TemplateWeb import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS class TemplateWebSample2(override val data: DataTemplate, - val onSuccess: () -> Unit) : TemplateWeb(data) { + val onSuccess: () -> Unit) : TemplateWeb(data) { companion object { private const val TAG = "TemplateWebSample2" } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLogin.kt index 283ea9b7..111f0fd1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLogin.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLogin.kt @@ -6,11 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.template.login import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.api.v2.* -import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginApi -import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginMessages -import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginPortal -import pl.szczodrzynski.edziennik.api.v2.librus.login.LibrusLoginSynergia -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.utils.Utils class TemplateLogin(val data: DataTemplate, val onSuccess: () -> Unit) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLoginApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLoginApi.kt index 777c0157..ab044e5f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLoginApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLoginApi.kt @@ -4,13 +4,11 @@ package pl.szczodrzynski.edziennik.api.v2.template.login -import okhttp3.Cookie -import pl.szczodrzynski.edziennik.DAY import pl.szczodrzynski.edziennik.HOUR import pl.szczodrzynski.edziennik.api.v2.ERROR_LOGIN_DATA_MISSING import pl.szczodrzynski.edziennik.api.v2.ERROR_PROFILE_MISSING import pl.szczodrzynski.edziennik.api.v2.models.ApiError -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.currentTimeUnix class TemplateLoginApi(val data: DataTemplate, val onSuccess: () -> Unit) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLoginWeb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLoginWeb.kt index 14837bbc..1ab34895 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLoginWeb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/template/login/TemplateLoginWeb.kt @@ -7,11 +7,9 @@ package pl.szczodrzynski.edziennik.api.v2.template.login import okhttp3.Cookie import pl.szczodrzynski.edziennik.api.v2.ERROR_LOGIN_DATA_MISSING import pl.szczodrzynski.edziennik.api.v2.ERROR_PROFILE_MISSING -import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_LIBRUS_API import pl.szczodrzynski.edziennik.api.v2.models.ApiError -import pl.szczodrzynski.edziennik.api.v2.template.data.DataTemplate +import pl.szczodrzynski.edziennik.api.v2.template.DataTemplate import pl.szczodrzynski.edziennik.currentTimeUnix -import pl.szczodrzynski.edziennik.getUnixDate class TemplateLoginWeb(val data: DataTemplate, val onSuccess: () -> Unit) { companion object { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/DataVulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/DataVulcan.kt new file mode 100644 index 00000000..d19a95af --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/DataVulcan.kt @@ -0,0 +1,172 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-6. + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan + +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_VULCAN_API +import pl.szczodrzynski.edziennik.api.v2.models.Data +import pl.szczodrzynski.edziennik.currentTimeUnix +import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore +import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile +import pl.szczodrzynski.edziennik.isNotNullNorEmpty + +class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) { + + fun isApiLoginValid() = apiCertificateExpiryTime-30 > currentTimeUnix() + && apiCertificateKey.isNotNullNorEmpty() + && apiCertificatePfx.isNotNullNorEmpty() + && symbol.isNotNullNorEmpty() + + override fun satisfyLoginMethods() { + loginMethods.clear() + if (isApiLoginValid()) { + loginMethods += LOGIN_METHOD_VULCAN_API + } + } + + /** + * A UONET+ client symbol. + * + * Present in the URL: https://uonetplus-uczen.vulcan.net.pl/[symbol]/[schoolSymbol]/ + * + * e.g. "poznan" + */ + private var mSymbol: String? = null + var symbol: String? + get() { mSymbol = mSymbol ?: loginStore.getLoginData("deviceSymbol", null); return mSymbol } + set(value) { loginStore.putLoginData("deviceSymbol", value); mSymbol = value } + + /** + * Group symbol/number of the student's school. + * + * Present in the URL: https://uonetplus-uczen.vulcan.net.pl/[symbol]/[schoolSymbol]/ + * + * ListaUczniow/JednostkaSprawozdawczaSymbol, e.g. "000088" + */ + private var mSchoolSymbol: String? = null + var schoolSymbol: String? + get() { mSchoolSymbol = mSchoolSymbol ?: profile?.getStudentData("schoolSymbol", null); return mSchoolSymbol } + set(value) { profile?.putStudentData("schoolSymbol", value) ?: return; mSchoolSymbol = value } + + /** + * A school ID consisting of the [symbol] and [schoolSymbol]. + * + * [symbol]_[schoolSymbol] + * + * e.g. "poznan_000088" + */ + private var mSchoolName: String? = null + var schoolName: String? + get() { mSchoolName = mSchoolName ?: profile?.getStudentData("schoolName", null); return mSchoolName } + set(value) { profile?.putStudentData("schoolName", value) ?: return; mSchoolName = value } + + /** + * ID of the student. + * + * ListaUczniow/Id, e.g. 42632 + */ + private var mStudentId: Int? = null + var studentId: Int + get() { mStudentId = mStudentId ?: profile?.getStudentData("studentId", 0); return mStudentId ?: 0 } + set(value) { profile?.putStudentData("studentId", value) ?: return; mStudentId = value } + + /** + * ID of the student's account. + * + * ListaUczniow/UzytkownikLoginId, e.g. 1709 + */ + private var mStudentLoginId: Int? = null + var studentLoginId: Int + get() { mStudentLoginId = mStudentLoginId ?: profile?.getStudentData("studentLoginId", 0); return mStudentLoginId ?: 0 } + set(value) { profile?.putStudentData("studentLoginId", value) ?: return; mStudentLoginId = value } + + /** + * ID of the student's class. + * + * ListaUczniow/IdOddzial, e.g. 35 + */ + private var mStudentClassId: Int? = null + var studentClassId: Int + get() { mStudentClassId = mStudentClassId ?: profile?.getStudentData("studentClassId", 0); return mStudentClassId ?: 0 } + set(value) { profile?.putStudentData("studentClassId", value) ?: return; mStudentClassId = value } + + /** + * ListaUczniow/IdOkresKlasyfikacyjny, e.g. 321 + */ + private var mStudentSemesterId: Int? = null + var studentSemesterId: Int + get() { mStudentSemesterId = mStudentSemesterId ?: profile?.getStudentData("studentSemesterId", 0); return mStudentSemesterId ?: 0 } + set(value) { profile?.putStudentData("studentSemesterId", value) ?: return; mStudentSemesterId = value } + + /** + * ListaUczniow/OkresNumer, e.g. 1 or 2 + */ + private var mStudentSemesterNumber: Int? = null + var studentSemesterNumber: Int + get() { mStudentSemesterNumber = mStudentSemesterNumber ?: profile?.getStudentData("studentSemesterNumber", 0); return mStudentSemesterNumber ?: 0 } + set(value) { profile?.putStudentData("studentSemesterNumber", value) ?: return; mStudentSemesterNumber = value } + + /* _____ _____ ____ + /\ | __ \_ _| |___ \ + / \ | |__) || | __ ____) | + / /\ \ | ___/ | | \ \ / /__ < + / ____ \| | _| |_ \ V /___) | + /_/ \_\_| |_____| \_/|___*/ + /** + * A mobile API registration token. + * + * After first login only 3 first characters are stored here. + * This is later used to determine the API URL address. + */ + private var mApiToken: String? = null + var apiToken: String? + get() { mApiToken = mApiToken ?: loginStore.getLoginData("deviceToken", null); return mApiToken } + set(value) { loginStore.putLoginData("deviceToken", value); mApiToken = value } + + /** + * A mobile API registration PIN. + * + * After first login, this is removed and/or set to null. + */ + private var mApiPin: String? = null + var apiPin: String? + get() { mApiPin = mApiPin ?: loginStore.getLoginData("devicePin", null); return mApiPin } + set(value) { loginStore.putLoginData("devicePin", value); mApiPin = value } + + private var mApiCertificateKey: String? = null + var apiCertificateKey: String? + get() { mApiCertificateKey = mApiCertificateKey ?: loginStore.getLoginData("certificateKey", null); return mApiCertificateKey } + set(value) { loginStore.putLoginData("certificateKey", value); mApiCertificateKey = value } + + private var mApiCertificatePfx: String? = null + var apiCertificatePfx: String? + get() { mApiCertificatePfx = mApiCertificatePfx ?: loginStore.getLoginData("certificatePfx", null); return mApiCertificatePfx } + set(value) { loginStore.putLoginData("certificatePfx", value); mApiCertificatePfx = value } + + private var mApiCertificateExpiryTime: Int? = null + var apiCertificateExpiryTime: Int + get() { mApiCertificateExpiryTime = mApiCertificateExpiryTime ?: loginStore.getLoginData("certificateExpiryTime", 0); return mApiCertificateExpiryTime ?: 0 } + set(value) { loginStore.putLoginData("certificateExpiryTime", value); mApiCertificateExpiryTime = value } + + val apiUrl: String? + get() { + return when (apiToken?.substring(0, 3)) { + "3S1" -> "https://lekcjaplus.vulcan.net.pl/$symbol/" + "TA1" -> "https://uonetplus-komunikacja.umt.tarnow.pl/$symbol/" + "OP1" -> "https://uonetplus-komunikacja.eszkola.opolskie.pl/$symbol/" + "RZ1" -> "https://uonetplus-komunikacja.resman.pl/$symbol/" + "GD1" -> "https://uonetplus-komunikacja.edu.gdansk.pl/$symbol/" + "KA1" -> "https://uonetplus-komunikacja.mcuw.katowice.eu/$symbol/" + "KA2" -> "https://uonetplus-komunikacja-test.mcuw.katowice.eu/$symbol/" + "P03" -> "https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl/$symbol/" + "P01" -> "http://efeb-komunikacja.pro-hudson.win.vulcan.pl/$symbol/" + "P02" -> "http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl/$symbol/" + "P90" -> "http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl/$symbol/" + "FK1", "FS1" -> "http://api.fakelog.cf/$symbol/" + "SZ9" -> "http://vulcan.szkolny.eu/$symbol/" + else -> null + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt new file mode 100644 index 00000000..c88023e5 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/Vulcan.kt @@ -0,0 +1,171 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-6. + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan + +import android.util.Log +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.api.v2.CODE_INTERNAL_LIBRUS_ACCOUNT_410 +import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_NOT_NEEDED +import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikCallback +import pl.szczodrzynski.edziennik.api.v2.interfaces.EdziennikInterface +import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.api.v2.models.Feature +import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanData +import pl.szczodrzynski.edziennik.api.v2.vulcan.login.VulcanLogin +import pl.szczodrzynski.edziennik.api.v2.vulcanLoginMethods +import pl.szczodrzynski.edziennik.data.db.modules.api.EndpointTimer +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_NEVER +import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore +import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile +import pl.szczodrzynski.edziennik.utils.Utils + +class Vulcan(val app: App, val profile: Profile?, val loginStore: LoginStore, val callback: EdziennikCallback) : EdziennikInterface { + companion object { + private const val TAG = "Vulcan" + } + + val internalErrorList = mutableListOf() + val data: DataVulcan + private var cancelled = false + + init { + data = DataVulcan(app, profile, loginStore).apply { + callback = wrapCallback(this@Vulcan.callback) + } + data.satisfyLoginMethods() + } + + private fun completed() { + data.saveData() + callback.onCompleted() + } + + /* _______ _ _ _ _ _ + |__ __| | /\ | | (_) | | | + | | | |__ ___ / \ | | __ _ ___ _ __ _| |_| |__ _ __ ___ + | | | '_ \ / _ \ / /\ \ | |/ _` |/ _ \| '__| | __| '_ \| '_ ` _ \ + | | | | | | __/ / ____ \| | (_| | (_) | | | | |_| | | | | | | | | + |_| |_| |_|\___| /_/ \_\_|\__, |\___/|_| |_|\__|_| |_|_| |_| |_| + __/ | + |__*/ + override fun sync(featureIds: List, viewId: Int?) { + val possibleLoginMethods = data.loginMethods.toMutableList() + + for (loginMethod in vulcanLoginMethods) { + if (loginMethod.isPossible(profile, loginStore)) + possibleLoginMethods += loginMethod.loginMethodId + } + + //var highestLoginMethod = 0 + var endpointList = mutableListOf() + val requiredLoginMethods = mutableListOf() + + data.targetEndpointIds.clear() + data.targetLoginMethodIds.clear() + + // get all endpoints for every feature, only if possible to login + for (featureId in featureIds) { + VulcanFeatures.filter { + it.featureId == featureId && possibleLoginMethods.containsAll(it.requiredLoginMethods) + } + .let { + endpointList.addAll(it) + } + } + + val timestamp = System.currentTimeMillis() + + endpointList = endpointList + // sort the endpoint list by feature ID and priority + .sortedWith(compareBy(Feature::featureId, Feature::priority)) + // select only the most important endpoint for each feature + .distinctBy { it.featureId } + .toMutableList() + // add all endpoint IDs and required login methods, filtering using timers + .onEach { feature -> + feature.endpointIds.forEach { endpoint -> + (data.endpointTimers + .singleOrNull { it.endpointId == endpoint.first } ?: EndpointTimer(data.profile?.id ?: -1, endpoint.first)) + .let { timer -> + if (timer.nextSync == SYNC_ALWAYS || + (viewId != null && timer.viewId == viewId) || + (timer.nextSync != SYNC_NEVER && timer.nextSync < timestamp)) { + data.targetEndpointIds.add(endpoint.first) + requiredLoginMethods.add(endpoint.second) + } + } + } + } + + // check every login method for any dependencies + for (loginMethodId in requiredLoginMethods) { + var requiredLoginMethod: Int? = loginMethodId + while (requiredLoginMethod != LOGIN_METHOD_NOT_NEEDED) { + vulcanLoginMethods.singleOrNull { it.loginMethodId == requiredLoginMethod }?.let { loginMethod -> + if (requiredLoginMethod != null) + data.targetLoginMethodIds.add(requiredLoginMethod!!) + requiredLoginMethod = loginMethod.requiredLoginMethod(data.profile, data.loginStore) + } + } + } + + // sort and distinct every login method and endpoint + data.targetLoginMethodIds = data.targetLoginMethodIds.toHashSet().toMutableList() + data.targetLoginMethodIds.sort() + + data.targetEndpointIds = data.targetEndpointIds.toHashSet().toMutableList() + data.targetEndpointIds.sort() + + Log.d(TAG, "LoginMethod IDs: ${data.targetLoginMethodIds}") + Log.d(TAG, "Endpoint IDs: ${data.targetEndpointIds}") + + VulcanLogin(data) { + VulcanData(data) { + completed() + } + } + } + + override fun getMessage(messageId: Int) { + + } + + override fun cancel() { + Utils.d(TAG, "Cancelled") + cancelled = true + } + + private fun wrapCallback(callback: EdziennikCallback): EdziennikCallback { + return object : EdziennikCallback { + override fun onCompleted() { + callback.onCompleted() + } + + override fun onProgress(step: Int) { + callback.onProgress(step) + } + + override fun onStartProgress(stringRes: Int) { + callback.onStartProgress(stringRes) + } + + override fun onError(apiError: ApiError) { + when (apiError.errorCode) { + in internalErrorList -> { + // finish immediately if the same error occurs twice during the same sync + callback.onError(apiError) + } + CODE_INTERNAL_LIBRUS_ACCOUNT_410 -> { + internalErrorList.add(apiError.errorCode) + loginStore.removeLoginData("refreshToken") // force a clean login + //loginLibrus() + } + else -> callback.onError(apiError) + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/VulcanFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/VulcanFeatures.kt new file mode 100644 index 00000000..ab65467e --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/VulcanFeatures.kt @@ -0,0 +1,85 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-6. + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan + +import pl.szczodrzynski.edziennik.api.v2.* +import pl.szczodrzynski.edziennik.api.v2.models.Feature + +const val ENDPOINT_VULCAN_API_STUDENT_LIST = 1000 +const val ENDPOINT_VULCAN_API_DICTIONARIES = 1010 +const val ENDPOINT_VULCAN_API_TIMETABLE = 1020 +const val ENDPOINT_VULCAN_API_EXAMS = 1030 +const val ENDPOINT_VULCAN_API_GRADES = 1040 +const val ENDPOINT_VULCAN_API_GRADES_SUMMARY = 1050 +const val ENDPOINT_VULCAN_API_HOMEWORK = 1060 +const val ENDPOINT_VULCAN_API_NOTICES = 1070 +const val ENDPOINT_VULCAN_API_ATTENDANCE = 1080 +const val ENDPOINT_VULCAN_API_MESSAGES_INBOX = 1090 +const val ENDPOINT_VULCAN_API_MESSAGES_SENT = 1100 + +val VulcanFeatures = listOf( + // timetable + Feature(LOGIN_TYPE_VULCAN, FEATURE_TIMETABLE, listOf( + ENDPOINT_VULCAN_API_TIMETABLE to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)), + // agenda + Feature(LOGIN_TYPE_VULCAN, FEATURE_AGENDA, listOf( + ENDPOINT_VULCAN_API_EXAMS to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)), + // grades + Feature(LOGIN_TYPE_VULCAN, FEATURE_GRADES, listOf( + ENDPOINT_VULCAN_API_GRADES to LOGIN_METHOD_VULCAN_API, + ENDPOINT_VULCAN_API_GRADES_SUMMARY to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)), + // homework + Feature(LOGIN_TYPE_VULCAN, FEATURE_HOMEWORK, listOf( + ENDPOINT_VULCAN_API_HOMEWORK to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)), + // behaviour + Feature(LOGIN_TYPE_VULCAN, FEATURE_BEHAVIOUR, listOf( + ENDPOINT_VULCAN_API_NOTICES to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)), + // attendance + Feature(LOGIN_TYPE_VULCAN, FEATURE_ATTENDANCE, listOf( + ENDPOINT_VULCAN_API_ATTENDANCE to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)), + // messages + Feature(LOGIN_TYPE_VULCAN, FEATURE_MESSAGES_INBOX, listOf( + ENDPOINT_VULCAN_API_MESSAGES_INBOX to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_MESSAGES_SENT, listOf( + ENDPOINT_VULCAN_API_MESSAGES_SENT to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)), + + Feature(LOGIN_TYPE_VULCAN, FEATURE_ALWAYS_NEEDED, listOf( + ENDPOINT_VULCAN_API_STUDENT_LIST to LOGIN_METHOD_VULCAN_API, + ENDPOINT_VULCAN_API_DICTIONARIES to LOGIN_METHOD_VULCAN_API + ), listOf(LOGIN_METHOD_VULCAN_API)) + /*Feature(LOGIN_TYPE_VULCAN, FEATURE_STUDENT_INFO, listOf( + ENDPOINT_VULCAN_API to LOGIN_METHOD_VULCAN_WEB + ), listOf(LOGIN_METHOD_VULCAN_WEB)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_STUDENT_NUMBER, listOf( + ENDPOINT_VULCAN_API to LOGIN_METHOD_VULCAN_WEB + ), listOf(LOGIN_METHOD_VULCAN_WEB)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_SCHOOL_INFO, listOf( + ENDPOINT_VULCAN_API to LOGIN_METHOD_VULCAN_WEB + ), listOf(LOGIN_METHOD_VULCAN_WEB)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_CLASS_INFO, listOf( + ENDPOINT_VULCAN_API to LOGIN_METHOD_VULCAN_WEB + ), listOf(LOGIN_METHOD_VULCAN_WEB)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_TEAM_INFO, listOf( + ENDPOINT_VULCAN_API to LOGIN_METHOD_VULCAN_WEB + ), listOf(LOGIN_METHOD_VULCAN_WEB)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_TEACHERS, listOf( + ENDPOINT_VULCAN_API to LOGIN_METHOD_VULCAN_WEB + ), listOf(LOGIN_METHOD_VULCAN_WEB)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_SUBJECTS, listOf( + ENDPOINT_VULCAN_API to LOGIN_METHOD_VULCAN_WEB + ), listOf(LOGIN_METHOD_VULCAN_WEB)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_CLASSROOMS, listOf( + ENDPOINT_VULCAN_API to LOGIN_METHOD_VULCAN_WEB + ), listOf(LOGIN_METHOD_VULCAN_WEB)),*/ + +) \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt new file mode 100644 index 00000000..554c060a --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/VulcanData.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-6. + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan.data + +import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.utils.Utils + +class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) { + companion object { + private const val TAG = "VulcanData" + } + + private var cancelled = false + + init { + nextEndpoint(onSuccess) + } + + private fun nextEndpoint(onSuccess: () -> Unit) { + if (data.targetEndpointIds.isEmpty()) { + onSuccess() + return + } + useEndpoint(data.targetEndpointIds.removeAt(0)) { + if (cancelled) { + onSuccess() + return@useEndpoint + } + nextEndpoint(onSuccess) + } + } + + private fun useEndpoint(endpointId: Int, onSuccess: () -> Unit) { + Utils.d(TAG, "Using endpoint $endpointId") + when (endpointId) { + /*ENDPOINT_VULCAN_API -> { + data.startProgress(R.string.edziennik_progress_endpoint_data) + VulcanApi(data) { onSuccess() } + }*/ + else -> onSuccess() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLogin.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLogin.kt new file mode 100644 index 00000000..6ecfeb19 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLogin.kt @@ -0,0 +1,53 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-6. + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan.login + +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_VULCAN_API +import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.utils.Utils + +class VulcanLogin(val data: DataVulcan, val onSuccess: () -> Unit) { + companion object { + private const val TAG = "VulcanLogin" + } + + private var cancelled = false + + init { + nextLoginMethod(onSuccess) + } + + private fun nextLoginMethod(onSuccess: () -> Unit) { + if (data.targetLoginMethodIds.isEmpty()) { + onSuccess() + return + } + useLoginMethod(data.targetLoginMethodIds.removeAt(0)) { usedMethodId -> + if (usedMethodId != -1) + data.loginMethods.add(usedMethodId) + if (cancelled) { + onSuccess() + return@useLoginMethod + } + nextLoginMethod(onSuccess) + } + } + + private fun useLoginMethod(loginMethodId: Int, onSuccess: (usedMethodId: Int) -> Unit) { + // this should never be true + if (data.loginMethods.contains(loginMethodId)) { + onSuccess(-1) + return + } + Utils.d(TAG, "Using login method $loginMethodId") + when (loginMethodId) { + LOGIN_METHOD_VULCAN_API -> { + data.startProgress(R.string.edziennik_progress_login_vulcan_api) + VulcanLoginApi(data) { onSuccess(loginMethodId) } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLoginApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLoginApi.kt new file mode 100644 index 00000000..be8e9aa9 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/login/VulcanLoginApi.kt @@ -0,0 +1,134 @@ +/* + * Copyright (c) Kuba Szczodrzyński 2019-10-6. + */ + +package pl.szczodrzynski.edziennik.api.v2.vulcan.login + +import android.os.Build +import com.google.gson.JsonObject +import im.wangchao.mhttp.Request +import im.wangchao.mhttp.Response +import im.wangchao.mhttp.callback.JsonCallbackHandler +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.api.v2.* +import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.Utils.d +import java.net.HttpURLConnection.HTTP_BAD_REQUEST +import java.util.* +import java.util.regex.Pattern + +class VulcanLoginApi(val data: DataVulcan, val onSuccess: () -> Unit) { + companion object { + private const val TAG = "VulcanLoginApi" + } + + init { run { + if (data.profile == null) { + data.error(ApiError(TAG, ERROR_PROFILE_MISSING)) + return@run + } + + if (data.isApiLoginValid()) { + onSuccess() + } + else { + if (data.symbol.isNotNullNorEmpty() && data.apiToken.isNotNullNorEmpty() && data.apiPin.isNotNullNorEmpty()) { + loginWithToken() + } + else { + data.error(ApiError(TAG, ERROR_LOGIN_DATA_MISSING)) + } + } + }} + + private fun loginWithToken() { + d(TAG, "Request: Vulcan/Login/Api - ${data.apiUrl}$VULCAN_API_ENDPOINT_CERTIFICATE") + + val callback = object : JsonCallbackHandler() { + override fun onSuccess(json: JsonObject?, response: Response?) { + if (json == null) { + if (response?.code() == HTTP_BAD_REQUEST) { + data.error(TAG, ERROR_LOGIN_VULCAN_INVALID_SYMBOL, response) + return + } + data.error(ApiError(TAG, ERROR_RESPONSE_EMPTY) + .withResponse(response)) + return + } + + var tokenStatus = json.getString("TokenStatus") + if (tokenStatus == "Null" || tokenStatus == "CertGenerated") + tokenStatus = null + val error = tokenStatus ?: json.getString("Message") + error?.let { code -> + when (code) { + "TokenNotFound" -> ERROR_LOGIN_VULCAN_INVALID_TOKEN + "TokenDead" -> ERROR_LOGIN_VULCAN_EXPIRED_TOKEN + "WrongPIN" -> { + Pattern.compile("Liczba pozostałych prób: ([0-9])", Pattern.DOTALL).matcher(tokenStatus).let { matcher -> + if (matcher.matches()) + ERROR_LOGIN_VULCAN_INVALID_PIN + 1 + matcher.group(1).toInt() + else + ERROR_LOGIN_VULCAN_INVALID_PIN + } + } + "Broken" -> ERROR_LOGIN_VULCAN_INVALID_PIN_0_REMAINING + else -> ERROR_LOGIN_VULCAN_OTHER + }.let { errorCode -> + data.error(ApiError(TAG, errorCode) + .withApiResponse(json) + .withResponse(response)) + return + } + } + + val cert = json.getJsonObject("TokenCert") + if (cert == null) { + data.error(ApiError(TAG, ERROR_LOGIN_VULCAN_OTHER) + .withApiResponse(json) + .withResponse(response)) + return + } + + data.apiCertificateKey = cert.getString("CertyfikatKlucz") + data.apiCertificatePfx = cert.getString("CertyfikatPfx") + data.apiCertificateExpiryTime = 1598832000 + data.apiToken = data.apiToken?.substring(0, 3) + data.loginStore.removeLoginData("devicePin") + onSuccess() + } + + override fun onFailure(response: Response?, throwable: Throwable?) { + data.error(ApiError(TAG, ERROR_REQUEST_FAILURE) + .withResponse(response) + .withThrowable(throwable)) + } + } + + Request.builder() + .url(data.apiUrl + VULCAN_API_ENDPOINT_CERTIFICATE) + .userAgent(VULCAN_API_USER_AGENT) + .addHeader("RequestMobileType", "RegisterDevice") + .addParameter("PIN", data.apiPin) + .addParameter("TokenKey", data.apiToken) + .addParameter("DeviceId", UUID.randomUUID().toString()) + .addParameter("DeviceName", VULCAN_API_DEVICE_NAME) + .addParameter("DeviceNameUser", "") + .addParameter("DeviceDescription", "") + .addParameter("DeviceSystemType", "Android") + .addParameter("DeviceSystemVersion", Build.VERSION.RELEASE) + .addParameter("RemoteMobileTimeKey", currentTimeUnix()) + .addParameter("TimeKey", currentTimeUnix() - 1) + .addParameter("RequestId", UUID.randomUUID().toString()) + .addParameter("AppVersion", VULCAN_API_APP_VERSION) + .addParameter("RemoteMobileAppVersion", VULCAN_API_APP_VERSION) + .addParameter("RemoteMobileAppName", VULCAN_API_APP_NAME) + .postJson() + .allowErrorCode(HTTP_BAD_REQUEST) + .callback(callback) + .build() + .enqueue() + } +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.java index e18a3877..be045510 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.java @@ -45,7 +45,6 @@ import pl.szczodrzynski.edziennik.BuildConfig; import pl.szczodrzynski.edziennik.R; import pl.szczodrzynski.edziennik.MainActivity; import pl.szczodrzynski.edziennik.api.v2.events.requests.SyncProfileRequest; -import pl.szczodrzynski.edziennik.api.v2.events.requests.SyncViewRequest; import pl.szczodrzynski.edziennik.api.v2.librus.LibrusTest; import pl.szczodrzynski.edziennik.databinding.CardLuckyNumberBinding; import pl.szczodrzynski.edziennik.databinding.CardUpdateBinding; diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/Utils.java b/app/src/main/java/pl/szczodrzynski/edziennik/utils/Utils.java index f4b6b23c..f7bd5c0f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/Utils.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/Utils.java @@ -107,13 +107,13 @@ public class Utils { public static void d(String TAG, String message) { if (App.devMode) { Log.d(TAG, message); - debugLog.add(TAG+": "+message); + //debugLog.add(TAG+": "+message); } } public static void c(String TAG, String message) { if (App.devMode) { Log.d(TAG, "// " + message); - debugLog.add(TAG+": // "+message); + ///debugLog.add(TAG+": // "+message); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a55488e0..a713e520 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -937,4 +937,5 @@ Pobieram kalendarz... Pobieram uwagi ucznia... Pobieram frekwencję ucznia... + Logowanie do Vulcan API...