From 9e3afcfe7aa181629dbb7a60ee2f5f79ef7cacd3 Mon Sep 17 00:00:00 2001 From: Viktor De Pasquale Date: Thu, 24 Oct 2019 17:33:42 +0200 Subject: [PATCH] Added safetynet implementation --- .../redesign/safetynet/SafetynetFragment.kt | 40 +++ .../redesign/safetynet/SafetynetViewModel.kt | 63 +++-- .../bg_divider_rounded_on_primary.xml | 7 + .../res/drawable/ic_refresh_safetynet_md2.xml | 10 + .../res/layout/fragment_safetynet_md2.xml | 262 ++++++++++++++++-- app/src/main/res/values/strings_md2.xml | 5 + 6 files changed, 342 insertions(+), 45 deletions(-) create mode 100644 app/src/main/res/drawable/bg_divider_rounded_on_primary.xml create mode 100644 app/src/main/res/drawable/ic_refresh_safetynet_md2.xml diff --git a/app/src/main/java/com/topjohnwu/magisk/redesign/safetynet/SafetynetFragment.kt b/app/src/main/java/com/topjohnwu/magisk/redesign/safetynet/SafetynetFragment.kt index 44e83d184..bcd5d44ad 100644 --- a/app/src/main/java/com/topjohnwu/magisk/redesign/safetynet/SafetynetFragment.kt +++ b/app/src/main/java/com/topjohnwu/magisk/redesign/safetynet/SafetynetFragment.kt @@ -1,9 +1,19 @@ package com.topjohnwu.magisk.redesign.safetynet +import android.os.Build +import android.view.View +import android.view.ViewAnimationUtils +import androidx.core.animation.doOnEnd +import androidx.core.view.isInvisible +import androidx.core.view.isVisible +import androidx.databinding.BindingAdapter +import androidx.interpolator.view.animation.FastOutSlowInInterpolator import com.topjohnwu.magisk.R import com.topjohnwu.magisk.databinding.FragmentSafetynetMd2Binding import com.topjohnwu.magisk.redesign.compat.CompatFragment import org.koin.androidx.viewmodel.ext.android.viewModel +import kotlin.math.hypot +import kotlin.math.roundToInt class SafetynetFragment : CompatFragment() { @@ -15,4 +25,34 @@ class SafetynetFragment : CompatFragment() .subscribeK { resolveResponse(it.responseCode) } .add() + + attest() } - fun attest() = UpdateSafetyNetEvent().publish() + override fun notifyStateChanged() { + super.notifyStateChanged() + notifyPropertyChanged(BR.loading) + notifyPropertyChanged(BR.failed) + notifyPropertyChanged(BR.success) + } + + private fun attest() { + currentState = LOADING + UpdateSafetyNetEvent().publish() + } + + fun reset() = attest() private fun resolveResponse(response: Int) = when { - //todo animate (reveal) to result (green/error) response and 0x0F == 0 -> { val hasCtsPassed = response and SafetyNetHelper.CTS_PASS != 0 val hasBasicIntegrityPassed = response and SafetyNetHelper.BASIC_PASS != 0 - safetyNetTitle.value = R.string.safetyNet_check_success - ctsState.value = if (hasCtsPassed) { - PASS - } else { - FAILED - } - basicIntegrityState.value = if (hasBasicIntegrityPassed) { - PASS - } else { - FAILED - } + val result = hasCtsPassed && hasBasicIntegrityPassed + ctsState.value = hasCtsPassed + basicIntegrityState.value = hasBasicIntegrityPassed + currentState = if (result) PASS else FAILED + safetyNetTitle.value = + if (result) R.string.safetynet_attest_success + else R.string.safetynet_attest_failure } - //todo animate (collapse) back to initial (fade error) response == -2 -> { - ctsState.value = IDLE - basicIntegrityState.value = IDLE + currentState = FAILED + ctsState.value = false + basicIntegrityState.value = false + back() } - //todo animate (collapse) back to initial (surface) else -> { - ctsState.value = IDLE - basicIntegrityState.value = IDLE + currentState = FAILED + ctsState.value = false + basicIntegrityState.value = false safetyNetTitle.value = when (response) { SafetyNetHelper.RESPONSE_ERR -> R.string.safetyNet_res_invalid else -> R.string.safetyNet_api_error diff --git a/app/src/main/res/drawable/bg_divider_rounded_on_primary.xml b/app/src/main/res/drawable/bg_divider_rounded_on_primary.xml new file mode 100644 index 000000000..783719e7d --- /dev/null +++ b/app/src/main/res/drawable/bg_divider_rounded_on_primary.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_refresh_safetynet_md2.xml b/app/src/main/res/drawable/ic_refresh_safetynet_md2.xml new file mode 100644 index 000000000..b5285029e --- /dev/null +++ b/app/src/main/res/drawable/ic_refresh_safetynet_md2.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_safetynet_md2.xml b/app/src/main/res/layout/fragment_safetynet_md2.xml index f2ff97488..f262834d5 100644 --- a/app/src/main/res/layout/fragment_safetynet_md2.xml +++ b/app/src/main/res/layout/fragment_safetynet_md2.xml @@ -1,50 +1,264 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools"> + + + + - + android:clipToPadding="false" + android:fillViewport="true" + android:paddingTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size}" + android:paddingBottom="@{viewModel.insets.bottom}" + tools:paddingBottom="48dp" + tools:paddingTop="24dp"> - - + android:layout_gravity="center" + android:text="@string/safetynet_attest_loading" + android:textAppearance="?appearanceTextTitleNormal" /> - + - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings_md2.xml b/app/src/main/res/values/strings_md2.xml index 17dd1ab75..41d1cdf87 100644 --- a/app/src/main/res/values/strings_md2.xml +++ b/app/src/main/res/values/strings_md2.xml @@ -29,6 +29,11 @@ Hide + Success! + Attestation failed! + Just a sec… + Try again + Theme Mode Select mode which best suits your style! Always Light