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
|
|
|
|
import com.topjohnwu.magisk.BR
|
2019-10-23 21:17:53 +02:00
|
|
|
import com.topjohnwu.magisk.R
|
2020-08-18 15:31:15 +02:00
|
|
|
import com.topjohnwu.magisk.arch.BaseViewModel
|
2020-07-15 10:21:57 +02:00
|
|
|
import com.topjohnwu.magisk.utils.set
|
2020-06-30 11:24:58 +02:00
|
|
|
import org.json.JSONObject
|
2019-10-17 18:57:00 +02:00
|
|
|
|
2020-07-09 14:13:24 +02:00
|
|
|
data class SafetyNetResult(
|
|
|
|
val response: JSONObject? = null,
|
|
|
|
val dismiss: Boolean = false
|
|
|
|
)
|
|
|
|
|
2020-09-17 07:16:28 +02:00
|
|
|
class SafetynetViewModel : BaseViewModel() {
|
2019-10-23 21:17:53 +02:00
|
|
|
|
2020-07-12 12:17:50 +02:00
|
|
|
@get:Bindable
|
2020-07-15 10:21:57 +02:00
|
|
|
var safetyNetTitle = R.string.empty
|
|
|
|
set(value) = set(value, field, { field = it }, BR.safetyNetTitle)
|
|
|
|
|
2020-07-12 12:17:50 +02:00
|
|
|
@get:Bindable
|
2020-07-15 10:21:57 +02:00
|
|
|
var ctsState = false
|
|
|
|
set(value) = set(value, field, { field = it }, BR.ctsState)
|
|
|
|
|
2020-07-12 12:17:50 +02:00
|
|
|
@get:Bindable
|
2020-07-15 10:21:57 +02:00
|
|
|
var basicIntegrityState = false
|
|
|
|
set(value) = set(value, field, { field = it }, BR.basicIntegrityState)
|
|
|
|
|
2020-07-12 12:17:50 +02:00
|
|
|
@get:Bindable
|
2020-07-15 10:21:57 +02:00
|
|
|
var evalType = ""
|
|
|
|
set(value) = set(value, field, { field = it }, BR.evalType)
|
2020-07-12 12:17:50 +02:00
|
|
|
|
|
|
|
@get:Bindable
|
2020-09-13 09:23:23 +02:00
|
|
|
var isChecking = false
|
|
|
|
set(value) = set(value, field, { field = it }, BR.checking)
|
|
|
|
|
2020-07-12 12:17:50 +02:00
|
|
|
@get:Bindable
|
2020-09-13 09:23:23 +02:00
|
|
|
var isSuccess = false
|
2020-09-17 07:16:28 +02:00
|
|
|
set(value) = set(value, field, { field = it }, BR.success, BR.textColorAttr)
|
2020-09-13 09:23:23 +02:00
|
|
|
|
2020-07-12 12:17:50 +02:00
|
|
|
@get:Bindable
|
2020-09-17 07:16:28 +02:00
|
|
|
val textColorAttr get() = if (isSuccess) R.attr.colorOnPrimary else R.attr.colorOnError
|
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
|
|
|
private fun attest() {
|
2020-09-13 09:23:23 +02:00
|
|
|
isChecking = true
|
2020-08-19 11:05:23 +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) {
|
2020-09-13 09:23:23 +02:00
|
|
|
isChecking = false
|
|
|
|
|
2020-06-30 11:24:58 +02:00
|
|
|
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
|
2020-07-12 12:17:50 +02:00
|
|
|
ctsState = cts
|
|
|
|
basicIntegrityState = basic
|
|
|
|
evalType = if (eval.contains("HARDWARE")) "HARDWARE" else "BASIC"
|
2020-09-13 09:23:23 +02:00
|
|
|
isSuccess = result
|
2020-07-12 12:17:50 +02:00
|
|
|
safetyNetTitle =
|
2020-06-30 11:24:58 +02:00
|
|
|
if (result) R.string.safetynet_attest_success
|
|
|
|
else R.string.safetynet_attest_failure
|
|
|
|
}.onFailure {
|
2020-09-13 09:23:23 +02:00
|
|
|
isSuccess = false
|
2020-07-12 12:17:50 +02:00
|
|
|
ctsState = false
|
|
|
|
basicIntegrityState = false
|
|
|
|
evalType = "N/A"
|
|
|
|
safetyNetTitle = R.string.safetynet_res_invalid
|
2020-06-30 11:24:58 +02:00
|
|
|
}
|
|
|
|
} ?: {
|
2020-09-13 09:23:23 +02:00
|
|
|
isSuccess = false
|
2020-07-12 12:17:50 +02:00
|
|
|
ctsState = false
|
|
|
|
basicIntegrityState = false
|
|
|
|
evalType = "N/A"
|
|
|
|
safetyNetTitle = R.string.safetynet_api_error
|
2020-06-30 11:24:58 +02:00
|
|
|
}()
|
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
|
|
|
}
|