2020-01-12 17:43:09 +01:00
|
|
|
package com.topjohnwu.magisk.ui.safetynet
|
2019-10-17 18:57:00 +02:00
|
|
|
|
2019-10-24 17:33:42 +02:00
|
|
|
import androidx.databinding.Bindable
|
2020-07-08 15:14:32 +02:00
|
|
|
import androidx.databinding.ObservableField
|
2019-10-24 17:33:42 +02:00
|
|
|
import com.topjohnwu.magisk.BR
|
2019-10-23 21:17:53 +02:00
|
|
|
import com.topjohnwu.magisk.R
|
2020-07-08 15:14:32 +02:00
|
|
|
import com.topjohnwu.magisk.extensions.value
|
2020-07-09 14:13:24 +02:00
|
|
|
import com.topjohnwu.magisk.model.events.CheckSafetyNetEvent
|
2020-01-13 15:01:46 +01:00
|
|
|
import com.topjohnwu.magisk.ui.base.BaseViewModel
|
2020-01-12 17:43:09 +01:00
|
|
|
import com.topjohnwu.magisk.ui.safetynet.SafetyNetState.*
|
2020-06-30 11:24:58 +02:00
|
|
|
import org.json.JSONObject
|
2019-10-17 18:57:00 +02:00
|
|
|
|
2020-01-12 09:07:30 +01:00
|
|
|
enum class SafetyNetState {
|
|
|
|
LOADING, PASS, FAILED, IDLE
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:13:24 +02:00
|
|
|
data class SafetyNetResult(
|
|
|
|
val response: JSONObject? = null,
|
|
|
|
val dismiss: Boolean = false
|
|
|
|
)
|
|
|
|
|
|
|
|
class SafetynetViewModel : BaseViewModel() {
|
2019-10-23 21:17:53 +02:00
|
|
|
|
2019-10-24 17:33:42 +02:00
|
|
|
private var currentState = IDLE
|
|
|
|
set(value) {
|
|
|
|
field = value
|
|
|
|
notifyStateChanged()
|
|
|
|
}
|
2020-07-08 15:14:32 +02:00
|
|
|
val safetyNetTitle = ObservableField(R.string.empty)
|
|
|
|
val ctsState = ObservableField(false)
|
|
|
|
val basicIntegrityState = ObservableField(false)
|
|
|
|
val evalType = ObservableField("")
|
2019-10-24 17:33:42 +02:00
|
|
|
|
|
|
|
val isChecking @Bindable get() = currentState == LOADING
|
|
|
|
val isFailed @Bindable get() = currentState == FAILED
|
|
|
|
val isSuccess @Bindable get() = currentState == PASS
|
2019-10-23 21:17:53 +02:00
|
|
|
|
|
|
|
init {
|
2020-06-30 11:24:58 +02:00
|
|
|
cachedResult?.also {
|
|
|
|
resolveResponse(SafetyNetResult(it))
|
|
|
|
} ?: attest()
|
2019-10-23 21:17:53 +02:00
|
|
|
}
|
|
|
|
|
2019-10-24 17:33:42 +02:00
|
|
|
override fun notifyStateChanged() {
|
|
|
|
super.notifyStateChanged()
|
|
|
|
notifyPropertyChanged(BR.loading)
|
|
|
|
notifyPropertyChanged(BR.failed)
|
|
|
|
notifyPropertyChanged(BR.success)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun attest() {
|
|
|
|
currentState = LOADING
|
2020-07-10 13:19:18 +02:00
|
|
|
CheckSafetyNetEvent() {
|
2020-07-09 14:13:24 +02:00
|
|
|
resolveResponse(it)
|
|
|
|
}.publish()
|
2019-10-24 17:33:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun reset() = attest()
|
2019-10-23 21:17:53 +02:00
|
|
|
|
2020-06-30 11:24:58 +02:00
|
|
|
private fun resolveResponse(response: SafetyNetResult) {
|
|
|
|
if (response.dismiss) {
|
2019-10-24 17:33:42 +02:00
|
|
|
back()
|
2020-06-30 11:24:58 +02:00
|
|
|
return
|
2019-10-23 21:17:53 +02:00
|
|
|
}
|
2020-06-30 11:24:58 +02:00
|
|
|
|
|
|
|
response.response?.apply {
|
|
|
|
runCatching {
|
|
|
|
val cts = getBoolean("ctsProfileMatch")
|
|
|
|
val basic = getBoolean("basicIntegrity")
|
2020-06-30 12:56:41 +02:00
|
|
|
val eval = optString("evaluationType")
|
2020-06-30 11:24:58 +02:00
|
|
|
val result = cts && basic
|
|
|
|
cachedResult = this
|
|
|
|
ctsState.value = cts
|
|
|
|
basicIntegrityState.value = basic
|
2020-06-30 12:56:41 +02:00
|
|
|
evalType.value = if (eval.contains("HARDWARE")) "HARDWARE" else "BASIC"
|
2020-06-30 11:24:58 +02:00
|
|
|
currentState = if (result) PASS else FAILED
|
|
|
|
safetyNetTitle.value =
|
|
|
|
if (result) R.string.safetynet_attest_success
|
|
|
|
else R.string.safetynet_attest_failure
|
|
|
|
}.onFailure {
|
|
|
|
currentState = FAILED
|
|
|
|
ctsState.value = false
|
|
|
|
basicIntegrityState.value = false
|
2020-06-30 12:56:41 +02:00
|
|
|
evalType.value = "N/A"
|
2020-06-30 11:24:58 +02:00
|
|
|
safetyNetTitle.value = R.string.safetynet_res_invalid
|
|
|
|
}
|
|
|
|
} ?: {
|
2019-10-24 17:33:42 +02:00
|
|
|
currentState = FAILED
|
|
|
|
ctsState.value = false
|
|
|
|
basicIntegrityState.value = false
|
2020-06-30 12:56:41 +02:00
|
|
|
evalType.value = "N/A"
|
2020-06-30 11:24:58 +02:00
|
|
|
safetyNetTitle.value = R.string.safetynet_api_error
|
|
|
|
}()
|
2019-10-23 21:17:53 +02:00
|
|
|
}
|
|
|
|
|
2019-10-26 16:03:07 +02:00
|
|
|
companion object {
|
2020-06-30 11:24:58 +02:00
|
|
|
private var cachedResult: JSONObject? = null
|
2019-10-26 16:03:07 +02:00
|
|
|
}
|
|
|
|
|
2020-01-12 09:07:30 +01:00
|
|
|
}
|