[API] Pass parameters to user action required errors.

This commit is contained in:
kuba2k2 2022-10-13 11:45:17 +02:00
parent 4b64277948
commit 7935d0f097
No known key found for this signature in database
GPG Key ID: 7919598F25F38819
6 changed files with 55 additions and 18 deletions

View File

@ -853,7 +853,7 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
fun onUserActionRequiredEvent(event: UserActionRequiredEvent) { fun onUserActionRequiredEvent(event: UserActionRequiredEvent) {
app.userActionManager.execute(this, event.profileId, event.type) app.userActionManager.execute(this, event.profileId, event.type, event.params)
} }
private fun fragmentToSyncName(currentFragment: Int): Int { private fun fragmentToSyncName(currentFragment: Int): Int {
@ -914,7 +914,8 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
app.userActionManager.execute( app.userActionManager.execute(
this, this,
extras.getInt("profileId"), extras.getInt("profileId"),
extras.getInt("type") extras.getInt("type"),
extras.getBundle("params"),
) )
true true
} }

View File

@ -4,11 +4,14 @@
package pl.szczodrzynski.edziennik.data.api.events package pl.szczodrzynski.edziennik.data.api.events
data class UserActionRequiredEvent(val profileId: Int, val type: Int) { import android.os.Bundle
data class UserActionRequiredEvent(val profileId: Int, val type: Int, val params: Bundle?) {
companion object { companion object {
const val LOGIN_DATA_MOBIDZIENNIK = 101 const val LOGIN_DATA_MOBIDZIENNIK = 101
const val LOGIN_DATA_LIBRUS = 102 const val LOGIN_DATA_LIBRUS = 102
const val LOGIN_DATA_VULCAN = 104 const val LOGIN_DATA_VULCAN = 104
const val CAPTCHA_LIBRUS = 202 const val CAPTCHA_LIBRUS = 202
const val OAUTH_USOS = 701
} }
} }

View File

@ -5,6 +5,7 @@
package pl.szczodrzynski.edziennik.data.api.models package pl.szczodrzynski.edziennik.data.api.models
import android.content.Context import android.content.Context
import android.os.Bundle
import com.google.gson.JsonObject import com.google.gson.JsonObject
import im.wangchao.mhttp.Request import im.wangchao.mhttp.Request
import im.wangchao.mhttp.Response import im.wangchao.mhttp.Response
@ -30,6 +31,7 @@ class ApiError(val tag: String, var errorCode: Int) {
var request: Request? = null var request: Request? = null
var response: Response? = null var response: Response? = null
var isCritical = true var isCritical = true
var params: Bundle? = null
fun withThrowable(throwable: Throwable?): ApiError { fun withThrowable(throwable: Throwable?): ApiError {
this.throwable = throwable this.throwable = throwable
@ -58,6 +60,11 @@ class ApiError(val tag: String, var errorCode: Int) {
return this return this
} }
fun withParams(bundle: Bundle): ApiError {
this.params = bundle
return this
}
fun getStringText(context: Context): String { fun getStringText(context: Context): String {
return context.resources.getIdentifier("error_${errorCode}", "string", context.packageName).let { return context.resources.getIdentifier("error_${errorCode}", "string", context.packageName).let {
if (it != 0) if (it != 0)

View File

@ -137,9 +137,8 @@ class LoginProgressFragment : Fragment(), CoroutineScope {
return return
} }
app.userActionManager.execute(activity, event.profileId, event.type, onSuccess = { code -> app.userActionManager.execute(activity, event.profileId, event.type, event.params, onSuccess = { params ->
args.putString("recaptchaCode", code) args.putAll(params)
args.putLong("recaptchaTime", System.currentTimeMillis())
doFirstLogin(args) doFirstLogin(args)
}, onFailure = { }, onFailure = {
activity.error(ApiError(TAG, ERROR_CAPTCHA_NEEDED)) activity.error(ApiError(TAG, ERROR_CAPTCHA_NEEDED))

View File

@ -7,6 +7,7 @@ package pl.szczodrzynski.edziennik.utils.managers
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
@ -14,9 +15,11 @@ import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.MainActivity import pl.szczodrzynski.edziennik.MainActivity
import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.api.ERROR_CAPTCHA_LIBRUS_PORTAL import pl.szczodrzynski.edziennik.data.api.ERROR_CAPTCHA_LIBRUS_PORTAL
import pl.szczodrzynski.edziennik.data.api.ERROR_USOS_OAUTH_LOGIN_REQUEST
import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask
import pl.szczodrzynski.edziennik.data.api.events.UserActionRequiredEvent import pl.szczodrzynski.edziennik.data.api.events.UserActionRequiredEvent
import pl.szczodrzynski.edziennik.data.api.models.ApiError import pl.szczodrzynski.edziennik.data.api.models.ApiError
import pl.szczodrzynski.edziennik.ext.Bundle
import pl.szczodrzynski.edziennik.ext.Intent import pl.szczodrzynski.edziennik.ext.Intent
import pl.szczodrzynski.edziennik.ext.JsonObject import pl.szczodrzynski.edziennik.ext.JsonObject
import pl.szczodrzynski.edziennik.ext.pendingIntentFlag import pl.szczodrzynski.edziennik.ext.pendingIntentFlag
@ -27,18 +30,21 @@ class UserActionManager(val app: App) {
private const val TAG = "UserActionManager" private const val TAG = "UserActionManager"
} }
fun requiresUserAction(apiError: ApiError): Boolean { fun requiresUserAction(apiError: ApiError) = when (apiError.errorCode) {
return apiError.errorCode == ERROR_CAPTCHA_LIBRUS_PORTAL ERROR_CAPTCHA_LIBRUS_PORTAL -> true
ERROR_USOS_OAUTH_LOGIN_REQUEST -> true
else -> false
} }
fun sendToUser(apiError: ApiError) { fun sendToUser(apiError: ApiError) {
val type = when (apiError.errorCode) { val type = when (apiError.errorCode) {
ERROR_CAPTCHA_LIBRUS_PORTAL -> UserActionRequiredEvent.CAPTCHA_LIBRUS ERROR_CAPTCHA_LIBRUS_PORTAL -> UserActionRequiredEvent.CAPTCHA_LIBRUS
ERROR_USOS_OAUTH_LOGIN_REQUEST -> UserActionRequiredEvent.OAUTH_USOS
else -> 0 else -> 0
} }
if (EventBus.getDefault().hasSubscriberForEvent(UserActionRequiredEvent::class.java)) { if (EventBus.getDefault().hasSubscriberForEvent(UserActionRequiredEvent::class.java)) {
EventBus.getDefault().post(UserActionRequiredEvent(apiError.profileId ?: -1, type)) EventBus.getDefault().post(UserActionRequiredEvent(apiError.profileId ?: -1, type, apiError.params))
return return
} }
@ -46,6 +52,7 @@ class UserActionManager(val app: App) {
val text = app.getString(when (type) { val text = app.getString(when (type) {
UserActionRequiredEvent.CAPTCHA_LIBRUS -> R.string.notification_user_action_required_captcha_librus UserActionRequiredEvent.CAPTCHA_LIBRUS -> R.string.notification_user_action_required_captcha_librus
UserActionRequiredEvent.OAUTH_USOS -> R.string.notification_user_action_required_oauth_usos
else -> R.string.notification_user_action_required_text else -> R.string.notification_user_action_required_text
}, apiError.profileId) }, apiError.profileId)
@ -54,7 +61,8 @@ class UserActionManager(val app: App) {
MainActivity::class.java, MainActivity::class.java,
"action" to "userActionRequired", "action" to "userActionRequired",
"profileId" to (apiError.profileId ?: -1), "profileId" to (apiError.profileId ?: -1),
"type" to type "type" to type,
"params" to apiError.params,
) )
val pendingIntent = PendingIntent.getActivity(app, System.currentTimeMillis().toInt(), intent, PendingIntent.FLAG_ONE_SHOT or pendingIntentFlag()) val pendingIntent = PendingIntent.getActivity(app, System.currentTimeMillis().toInt(), intent, PendingIntent.FLAG_ONE_SHOT or pendingIntentFlag())
@ -78,23 +86,41 @@ class UserActionManager(val app: App) {
activity: AppCompatActivity, activity: AppCompatActivity,
profileId: Int?, profileId: Int?,
type: Int, type: Int,
onSuccess: ((code: String) -> Unit)? = null, params: Bundle? = null,
onSuccess: ((params: Bundle) -> Unit)? = null,
onFailure: (() -> Unit)? = null onFailure: (() -> Unit)? = null
) { ) {
if (type != UserActionRequiredEvent.CAPTCHA_LIBRUS) when (type) {
return UserActionRequiredEvent.CAPTCHA_LIBRUS -> executeLibrus(activity, profileId, onSuccess, onFailure)
UserActionRequiredEvent.OAUTH_USOS -> executeOauth(activity, profileId, params, onSuccess, onFailure)
}
}
private fun executeLibrus(
activity: AppCompatActivity,
profileId: Int?,
onSuccess: ((params: Bundle) -> Unit)? = null,
onFailure: (() -> Unit)? = null,
) {
if (profileId == null) if (profileId == null)
return return
// show captcha dialog // show captcha dialog
// use passed onSuccess listener, else sync profile // use passed onSuccess listener, else sync profile
LibrusCaptchaDialog( LibrusCaptchaDialog(
activity = activity, activity = activity,
onSuccess = onSuccess ?: { code -> onSuccess = { code ->
EdziennikTask.syncProfile(profileId, arguments = JsonObject( if (onSuccess != null) {
"recaptchaCode" to code, val params = Bundle(
"recaptchaTime" to System.currentTimeMillis() "recaptchaCode" to code,
)).enqueue(activity) "recaptchaTime" to System.currentTimeMillis(),
)
onSuccess(params)
} else {
EdziennikTask.syncProfile(profileId, arguments = JsonObject(
"recaptchaCode" to code,
"recaptchaTime" to System.currentTimeMillis(),
)).enqueue(activity)
}
}, },
onFailure = onFailure onFailure = onFailure
).show() ).show()

View File

@ -1541,4 +1541,5 @@
<string name="login_type_usos">USOS</string> <string name="login_type_usos">USOS</string>
<string name="login_mode_usos_oauth">Logowanie z użyciem przeglądarki</string> <string name="login_mode_usos_oauth">Logowanie z użyciem przeglądarki</string>
<string name="login_mode_usos_oauth_guide">TODO</string> <string name="login_mode_usos_oauth_guide">TODO</string>
<string name="notification_user_action_required_oauth_usos">USOS - wymagane logowanie z użyciem przeglądarki</string>
</resources> </resources>