diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt index 5b6676d0..55beea7d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/MainActivity.kt @@ -36,11 +36,13 @@ import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode import pl.droidsonroids.gif.GifDrawable +import pl.szczodrzynski.edziennik.data.api.ERROR_API_INVALID_SIGNATURE import pl.szczodrzynski.edziennik.data.api.ERROR_VULCAN_API_DEPRECATED import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask import pl.szczodrzynski.edziennik.data.api.events.* import pl.szczodrzynski.edziennik.data.api.models.ApiError import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi +import pl.szczodrzynski.edziennik.data.api.szkolny.response.RegisterAvailabilityStatus import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.Metadata.* @@ -628,22 +630,40 @@ class MainActivity : AppCompatActivity(), CoroutineScope { app.profile.registerName?.let { registerName -> var status = app.config.sync.registerAvailability[registerName] if (status == null || status.nextCheckAt < currentTimeUnix()) { - withContext(Dispatchers.IO) { - val api = SzkolnyApi(app) - api.runCatching(this@MainActivity) { + val api = SzkolnyApi(app) + val result = withContext(Dispatchers.IO) { + return@withContext api.runCatching({ val availability = getRegisterAvailability() app.config.sync.registerAvailability = availability - status = availability[registerName] + availability[registerName] + }, onError = { + if (it.toErrorCode() == ERROR_API_INVALID_SIGNATURE) { + return@withContext false + } + return@withContext it + }) + } + + when (result) { + false -> { + Toast.makeText(this@MainActivity, R.string.error_no_api_access, Toast.LENGTH_SHORT).show() + return@let + } + is Throwable -> { + errorSnackbar.addError(result.toApiError(SzkolnyApi.TAG)).show() + return + } + is RegisterAvailabilityStatus -> { + status = result } } } - if (status?.available != true - || status?.minVersionCode ?: BuildConfig.VERSION_CODE > BuildConfig.VERSION_CODE) { + if (status?.available != true || status.minVersionCode > BuildConfig.VERSION_CODE) { swipeRefreshLayout.isRefreshing = false loadTarget(DRAWER_ITEM_HOME) if (status != null) - RegisterUnavailableDialog(this, status!!) + RegisterUnavailableDialog(this, status) return } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt index 7a578356..e3692c33 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/EdziennikTask.kt @@ -90,7 +90,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa return } - profile.registerName?.let { registerName -> + profile.registerName?.also { registerName -> var status = app.config.sync.registerAvailability[registerName] if (status == null || status.nextCheckAt < currentTimeUnix()) { val api = SzkolnyApi(app) @@ -99,7 +99,11 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa app.config.sync.registerAvailability = availability status = availability[registerName] }, onError = { - taskCallback.onError(it.toApiError(TAG)) + val apiError = it.toApiError(TAG) + if (apiError.errorCode == ERROR_API_INVALID_SIGNATURE) { + return@also + } + taskCallback.onError(apiError) return }) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt index e6d970b8..3a434001 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt @@ -5,6 +5,7 @@ package pl.szczodrzynski.edziennik.data.api.szkolny import android.os.Build +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.gson.GsonBuilder import kotlinx.coroutines.CoroutineScope @@ -14,6 +15,7 @@ import kotlinx.coroutines.withContext import okhttp3.OkHttpClient import org.greenrobot.eventbus.EventBus import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.data.api.ERROR_API_INVALID_SIGNATURE import pl.szczodrzynski.edziennik.data.api.szkolny.adapter.DateAdapter import pl.szczodrzynski.edziennik.data.api.szkolny.adapter.TimeAdapter import pl.szczodrzynski.edziennik.data.api.szkolny.interceptor.SignatureInterceptor @@ -89,11 +91,17 @@ class SzkolnyApi(val app: App) : CoroutineScope { } catch (e: Exception) { withContext(coroutineContext) { + val apiError = e.toApiError(TAG) + if (apiError.errorCode == ERROR_API_INVALID_SIGNATURE) { + Toast.makeText(activity, R.string.error_no_api_access, Toast.LENGTH_SHORT).show() + return@withContext null + } ErrorDetailsDialog( activity, - listOf(e.toApiError(TAG)), + listOf(apiError), R.string.error_occured ) + null } null } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/AppSync.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/AppSync.kt index c10bab3e..15482c53 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/AppSync.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/task/AppSync.kt @@ -5,10 +5,13 @@ package pl.szczodrzynski.edziennik.data.api.task import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.data.api.ERROR_API_INVALID_SIGNATURE import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi +import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApiException import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.entity.Notification import pl.szczodrzynski.edziennik.data.db.entity.Profile +import pl.szczodrzynski.edziennik.toErrorCode import pl.szczodrzynski.edziennik.utils.models.Date class AppSync(val app: App, val notifications: MutableList, val profiles: List, val api: SzkolnyApi) { @@ -27,7 +30,13 @@ class AppSync(val app: App, val notifications: MutableList, val pr */ fun run(lastSyncTime: Long, markAsSeen: Boolean = false): Int { val blacklistedIds = app.db.eventDao().blacklistedIds - val events = api.getEvents(profiles, notifications, blacklistedIds, lastSyncTime) + val events = try { + api.getEvents(profiles, notifications, blacklistedIds, lastSyncTime) + } catch (e: SzkolnyApiException) { + if (e.toErrorCode() == ERROR_API_INVALID_SIGNATURE) + return 0 + throw e + } app.config.sync.lastAppSync = System.currentTimeMillis() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3d24343d..d81e0ff5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1424,4 +1424,5 @@ Szczegóły wersji Informacje o kompilacji Sprawdź kod + Brak dostępu do API