Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AND-8722 Fix security delay placeholder #410

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.tangem.common.card.Card
import com.tangem.common.card.EllipticCurve
import com.tangem.common.core.CompletionCallback
import com.tangem.common.core.Config
import com.tangem.common.core.ProductType
import com.tangem.common.core.TangemError
import com.tangem.common.core.TangemSdkError
import com.tangem.common.extensions.VoidCallback
Expand Down Expand Up @@ -180,10 +181,12 @@ class SdkTaskSpinnerFragment : BaseFragment() {
message: ViewDelegateMessage?,
enableHowTo: Boolean,
iconScanRes: Int?,
productType: ProductType,
) {}
override fun onSecurityDelay(ms: Int, totalDurationSeconds: Int) {}
override fun onDelay(total: Int, current: Int, step: Int) {}
override fun onTagLost() {}

override fun onSecurityDelay(ms: Int, totalDurationSeconds: Int, productType: ProductType) {}
override fun onDelay(total: Int, current: Int, step: Int, productType: ProductType) {}
override fun onTagLost(productType: ProductType) {}
override fun onTagConnected() {}
override fun onWrongCard(wrongValueType: WrongValueType) {}
override fun onSessionStopped(message: Message?) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.google.android.material.slider.Slider
import com.tangem.SessionViewDelegate
import com.tangem.WrongValueType
import com.tangem.common.UserCodeType
import com.tangem.common.core.ProductType
import com.tangem.common.core.TangemSdkError
import com.tangem.demo.inflate
import com.tangem.demo.ui.extension.withMainContext
Expand Down Expand Up @@ -87,19 +88,19 @@ class TagConnectTagLostError : BaseDelegateAction() {
}

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.onTagConnected() }
delay(timeMillis = 2000)
withMainContext { delegate.onTagLost() }
withMainContext { delegate.onTagLost(ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.onTagConnected() }
delay(timeMillis = 2000)
withMainContext { delegate.onTagLost() }
withMainContext { delegate.onTagLost(ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.onError(TangemSdkError.UnknownError()) }
delay(timeMillis = 2000)
withMainContext { delegate.onTagLost() }
withMainContext { delegate.onTagLost(ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.dismiss() }
}
Expand Down Expand Up @@ -129,7 +130,7 @@ class SecurityDelay : BaseDelegateAction() {
}

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.onTagConnected() }

Expand All @@ -139,7 +140,13 @@ class SecurityDelay : BaseDelegateAction() {
repeat(securityDelayCounts) {
delay(timeMillis = 1000)
currentMs = currentMs.minus(other = 100)
withMainContext { delegate.onSecurityDelay(currentMs, totalDurationMs) }
withMainContext {
delegate.onSecurityDelay(
ms = currentMs,
totalDurationSeconds = totalDurationMs,
productType = ProductType.ANY,
)
}
}

withMainContext { delegate.onSessionStopped(null) }
Expand Down Expand Up @@ -170,7 +177,7 @@ class SecurityDelayPinFails : BaseDelegateAction() {
}

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.onTagConnected() }
delay(timeMillis = 1000)
Expand All @@ -180,11 +187,17 @@ class SecurityDelayPinFails : BaseDelegateAction() {
repeat(repeatCounts) {
delay(timeMillis = 1000)
currentMs = currentMs.minus(other = 100)
withMainContext { delegate.onSecurityDelay(ms = currentMs, totalDurationSeconds = 0) }
withMainContext {
delegate.onSecurityDelay(
ms = currentMs,
totalDurationSeconds = 0,
productType = ProductType.ANY,
)
}
}

delay(timeMillis = 2000)
withMainContext { delegate.onTagLost() }
withMainContext { delegate.onTagLost(ProductType.ANY) }
delay(timeMillis = 1000)
withMainContext { delegate.dismiss() }
}
Expand All @@ -201,7 +214,7 @@ class WrongCard : BaseDelegateAction() {
}

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.onTagConnected() }
delay(timeMillis = 2000)
Expand All @@ -224,7 +237,7 @@ class OnError : BaseDelegateAction() {
}

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.onTagConnected() }
delay(timeMillis = 2000)
Expand All @@ -247,19 +260,19 @@ class RequestAccessCode : BaseDelegateAction() {
}

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 500)
withMainContext { delegate.requestUserCode(UserCodeType.AccessCode, true, false, null) {} }
delay(timeMillis = 2000)
withMainContext { delegate.dismiss() }
delay(timeMillis = 500)
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 500)
withMainContext { delegate.requestUserCode(UserCodeType.AccessCode, false, false, null) {} }
delay(timeMillis = 2000)
withMainContext { delegate.dismiss() }
delay(timeMillis = 500)
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 500)
withMainContext { delegate.requestUserCode(UserCodeType.AccessCode, true, false, null) {} }
delay(timeMillis = 2000)
Expand All @@ -276,7 +289,7 @@ class SingleRequestAccessCode(
override fun usedCommandsInfo(): String = "RequestUserCode(access,true), Dismiss"

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 500)
withMainContext { delegate.requestUserCode(UserCodeType.AccessCode, true, showForgotButton, null) {} }
}
Expand All @@ -295,19 +308,19 @@ class RequestPasscode : BaseDelegateAction() {
}

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 500)
withMainContext { delegate.requestUserCode(UserCodeType.Passcode, true, false, null) {} }
delay(timeMillis = 2000)
withMainContext { delegate.dismiss() }
delay(timeMillis = 500)
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 500)
withMainContext { delegate.requestUserCode(UserCodeType.Passcode, false, false, null) {} }
delay(timeMillis = 2000)
withMainContext { delegate.dismiss() }
delay(timeMillis = 500)
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 500)
withMainContext { delegate.requestUserCode(UserCodeType.Passcode, true, false, null) {} }
delay(timeMillis = 2000)
Expand Down Expand Up @@ -349,7 +362,7 @@ class RequestPinSetup : BaseDelegateAction() {
override fun usedCommandsInfo(): String = "SessionStarted, RequestPinSetup"

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 500)
withMainContext { delegate.requestUserCodeChange(UserCodeType.Passcode, null) {} }
}
Expand All @@ -364,7 +377,7 @@ class ErrorsWithDifferentFormats : BaseDelegateAction() {
""".trimIndent()

override fun getCommandsPool(): suspend () -> Unit = suspend {
withMainContext { delegate.onSessionStarted(null, null, true) }
withMainContext { delegate.onSessionStarted(null, null, true, null, ProductType.ANY) }
delay(timeMillis = 2000)
withMainContext { delegate.onError(TangemSdkError.BackupFailedIncompatibleBatch()) }
delay(timeMillis = 5000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.tangem.common.CardIdFormatter
import com.tangem.common.UserCodeType
import com.tangem.common.core.CompletionCallback
import com.tangem.common.core.Config
import com.tangem.common.core.ProductType
import com.tangem.common.core.TangemError
import com.tangem.common.extensions.VoidCallback
import com.tangem.operations.resetcode.ResetCodesViewDelegate
Expand Down Expand Up @@ -44,9 +45,15 @@ class DefaultSessionViewDelegate(
message: ViewDelegateMessage?,
enableHowTo: Boolean,
iconScanRes: Int?,
productType: ProductType,
) {
Log.view { "session started" }
createAndShowState(SessionViewDelegateState.Ready(formatCardId(cardId)), enableHowTo, message, iconScanRes)
createAndShowState(
state = SessionViewDelegateState.Ready(formatCardId(cardId), productType),
enableHowTo = enableHowTo,
message = message,
iconScanRes = iconScanRes,
)
checkNfcEnabled()
}

Expand All @@ -72,19 +79,19 @@ class DefaultSessionViewDelegate(
readingDialog?.show(SessionViewDelegateState.Success(message))
}

override fun onSecurityDelay(ms: Int, totalDurationSeconds: Int) {
override fun onSecurityDelay(ms: Int, totalDurationSeconds: Int, productType: ProductType) {
Log.view { "showing security delay: $ms, $totalDurationSeconds" }
readingDialog?.show(SessionViewDelegateState.SecurityDelay(ms, totalDurationSeconds))
readingDialog?.show(SessionViewDelegateState.SecurityDelay(ms, totalDurationSeconds, productType))
}

override fun onDelay(total: Int, current: Int, step: Int) {
override fun onDelay(total: Int, current: Int, step: Int, productType: ProductType) {
Log.view { "showing delay" }
readingDialog?.show(SessionViewDelegateState.Delay(total, current, step))
readingDialog?.show(SessionViewDelegateState.Delay(total, current, step, productType))
}

override fun onTagLost() {
override fun onTagLost(productType: ProductType) {
Log.view { "tag lost" }
readingDialog?.show(SessionViewDelegateState.TagLost)
readingDialog?.show(SessionViewDelegateState.TagLost(productType))
}

override fun onTagConnected() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import com.tangem.Message
import com.tangem.WrongValueType
import com.tangem.common.UserCodeType
import com.tangem.common.core.CompletionCallback
import com.tangem.common.core.ProductType
import com.tangem.common.core.TangemError
import com.tangem.operations.resetcode.ResetPinService

sealed class SessionViewDelegateState {
data class Error(val error: TangemError) : SessionViewDelegateState()
data class Success(val message: Message?) : SessionViewDelegateState()
data class SecurityDelay(val ms: Int, val totalDurationSeconds: Int) :
data class SecurityDelay(val ms: Int, val totalDurationSeconds: Int, val productType: ProductType) :
SessionViewDelegateState()

data class Delay(val total: Int, val current: Int, val step: Int) : SessionViewDelegateState()
data class Ready(val cardId: String?) : SessionViewDelegateState()
data class Delay(
val total: Int,
val current: Int,
val step: Int,
val productType: ProductType,
) : SessionViewDelegateState()

data class Ready(val cardId: String?, val productType: ProductType) : SessionViewDelegateState()
data class PinRequested(
val type: UserCodeType,
val isFirstAttempt: Boolean,
Expand All @@ -37,7 +44,7 @@ sealed class SessionViewDelegateState {
) : SessionViewDelegateState()

data class WrongCard(val wrongValueType: WrongValueType) : SessionViewDelegateState()
object TagLost : SessionViewDelegateState()
data class TagLost(val productType: ProductType) : SessionViewDelegateState()
object TagConnected : SessionViewDelegateState()
object HowToTap : SessionViewDelegateState()
object None : SessionViewDelegateState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.tangem.Log
import com.tangem.ViewDelegateMessage
import com.tangem.common.CompletionResult
import com.tangem.common.Timer
import com.tangem.common.core.ProductType
import com.tangem.common.core.ScanTagImage
import com.tangem.common.core.SessionEnvironment
import com.tangem.common.core.TangemSdkError
Expand Down Expand Up @@ -292,10 +293,18 @@ class NfcSessionDialog(
private fun activateTrickySecurityDelay(totalDuration: Long) {
val timer = Timer(period = totalDuration, step = 100L, delayMs = 1000L, dispatcher = Dispatchers.IO)
timer.onComplete = {
postUI { onSecurityDelay(SessionViewDelegateState.SecurityDelay(0, 0)) }
postUI { onSecurityDelay(SessionViewDelegateState.SecurityDelay(0, 0, ProductType.ANY)) }
}
timer.onTick = {
postUI { onSecurityDelay(SessionViewDelegateState.SecurityDelay((totalDuration - it).toInt(), 0)) }
postUI {
onSecurityDelay(
SessionViewDelegateState.SecurityDelay(
ms = (totalDuration - it).toInt(),
totalDurationSeconds = 0,
productType = ProductType.ANY,
),
)
}
}
timer.start()
emulateSecurityDelayTimer = timer
Expand All @@ -310,7 +319,7 @@ class NfcSessionDialog(
emulateSecurityDelayTimer?.period?.let { activateTrickySecurityDelay(it) }
}

SessionViewDelegateState.TagLost -> {
is SessionViewDelegateState.TagLost -> {
emulateSecurityDelayTimer?.cancel()
}

Expand All @@ -326,7 +335,7 @@ class NfcSessionDialog(
}

private fun getEmptyOnReadyEvent(): SessionViewDelegateState {
return SessionViewDelegateState.Ready(headerWidget.cardId)
return SessionViewDelegateState.Ready(headerWidget.cardId, ProductType.ANY)
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ class HeaderWidget(mainView: View) : BaseSessionDelegateStateWidget(mainView) {
if (scannedCardId == null) {
tvCard.text = ""
} else {
tvCard.show()
tvCard.text = getString(R.string.view_delegate_header_card)
tvCardId.show()
tvCardId.text = scannedCardId
}
Expand Down
Loading
Loading