From d7f7508fa2a34a3c36a2d5446f3f95abe1a6a4f4 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 11 Sep 2020 02:31:41 -0700 Subject: [PATCH] Move setContentView out of onCreate --- .../topjohnwu/magisk/arch/BaseUIActivity.kt | 22 ++++++++------- .../com/topjohnwu/magisk/events/ViewEvents.kt | 6 ++++ .../com/topjohnwu/magisk/ui/MainActivity.kt | 2 +- .../magisk/ui/surequest/SuRequestViewModel.kt | 28 ++++++------------- app/src/main/res/layout/activity_request.xml | 2 +- 5 files changed, 28 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/arch/BaseUIActivity.kt b/app/src/main/java/com/topjohnwu/magisk/arch/BaseUIActivity.kt index f102e7e24..010954aca 100644 --- a/app/src/main/java/com/topjohnwu/magisk/arch/BaseUIActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/arch/BaseUIActivity.kt @@ -48,6 +48,9 @@ abstract class BaseUIActivity : override fun onCreate(savedInstanceState: Bundle?) { setTheme(themeRes) + super.onCreate(savedInstanceState) + + startObserveEvents() // We need to set the window background explicitly since for whatever reason it's not // propagated upstream @@ -55,16 +58,6 @@ abstract class BaseUIActivity : .use { it.getDrawable(0) } .also { window.setBackgroundDrawable(it) } - super.onCreate(savedInstanceState) - startObserveEvents() - - binding = DataBindingUtil.setContentView(this, layoutRes).also { - it.setVariable(BR.viewModel, viewModel) - it.lifecycleOwner = this - } - - ensureInsets() - directionsDispatcher.observe(this) { it?.navigate() // we don't want the directions to be re-dispatched, so we preemptively set them to null @@ -74,6 +67,15 @@ abstract class BaseUIActivity : } } + fun setContentView() { + binding = DataBindingUtil.setContentView(this, layoutRes).also { + it.setVariable(BR.viewModel, viewModel) + it.lifecycleOwner = this + } + + ensureInsets() + } + override fun onResume() { super.onResume() viewModel.requestRefresh() diff --git a/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt b/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt index bc3e8e27e..a54897ecc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt +++ b/app/src/main/java/com/topjohnwu/magisk/events/ViewEvents.kt @@ -51,6 +51,12 @@ class DieEvent : ViewEvent(), ActivityExecutor { } } +class ShowUIEvent : ViewEvent(), ActivityExecutor { + override fun invoke(activity: BaseUIActivity<*, *>) { + activity.setContentView() + } +} + class RecreateEvent : ViewEvent(), ActivityExecutor { override fun invoke(activity: BaseUIActivity<*, *>) { activity.recreate() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt b/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt index da77e49ff..161331ea5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/MainActivity.kt @@ -14,7 +14,6 @@ import androidx.core.view.setPadding import androidx.core.view.updateLayoutParams import androidx.navigation.NavDirections import com.google.android.material.card.MaterialCardView -import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.MainDirections import com.topjohnwu.magisk.R import com.topjohnwu.magisk.arch.BaseUIActivity @@ -60,6 +59,7 @@ open class MainActivity : BaseUIActivity( return } + setContentView() showUnsupportedMessage() askForHomeShortcut() diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt index dfa2aad62..1ef14ee99 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/surequest/SuRequestViewModel.kt @@ -18,9 +18,9 @@ import com.topjohnwu.magisk.core.model.su.SuPolicy.Companion.DENY import com.topjohnwu.magisk.core.su.SuRequestHandler import com.topjohnwu.magisk.core.utils.BiometricHelper import com.topjohnwu.magisk.events.DieEvent +import com.topjohnwu.magisk.events.ShowUIEvent import com.topjohnwu.magisk.ui.superuser.SpinnerRvItem import com.topjohnwu.magisk.utils.set -import com.topjohnwu.superuser.internal.UiThreadHandler import kotlinx.coroutines.launch import me.tatarka.bindingcollectionadapter2.BindingListViewAdapter import me.tatarka.bindingcollectionadapter2.ItemBinding @@ -33,26 +33,14 @@ class SuRequestViewModel( private val res: Resources ) : BaseViewModel() { - @get:Bindable - var icon: Drawable? = null - set(value) = set(value, field, { field = it }, BR.icon) - - @get:Bindable - var title = "" - set(value) = set(value, field, { field = it }, BR.title) - - @get:Bindable - var packageName = "" - set(value) = set(value, field, { field = it }, BR.packageName) + lateinit var icon: Drawable + lateinit var title: String + lateinit var packageName: String @get:Bindable var denyText = res.getString(R.string.deny) set(value) = set(value, field, { field = it }, BR.denyText) - @get:Bindable - var warningText = res.getString(R.string.su_warning) - set(value) = set(value, field, { field = it }, BR.warningText) - @get:Bindable var selectedItemPosition = 0 set(value) = set(value, field, { field = it }, BR.selectedItemPosition) @@ -124,14 +112,14 @@ class SuRequestViewModel( icon = policy.applicationInfo.loadIcon(pm) title = policy.appName packageName = policy.packageName - UiThreadHandler.handler.post { - // Delay is required to properly do selection - selectedItemPosition = timeoutPrefs.getInt(policy.packageName, 0) - } + selectedItemPosition = timeoutPrefs.getInt(policy.packageName, 0) // Set timer val millis = SECONDS.toMillis(Config.suDefaultTimeout.toLong()) timer = SuTimer(millis, 1000).apply { start() } + + // Actually show the UI + ShowUIEvent().publish() } private inner class SuTimer( diff --git a/app/src/main/res/layout/activity_request.xml b/app/src/main/res/layout/activity_request.xml index 5eb122292..c49ee363b 100644 --- a/app/src/main/res/layout/activity_request.xml +++ b/app/src/main/res/layout/activity_request.xml @@ -109,7 +109,7 @@ android:layout_gravity="center_horizontal" android:layout_margin="@dimen/l1" android:gravity="center" - android:text="@{viewModel.warningText}" + android:text="@string/su_warning" android:textAppearance="@style/AppearanceFoundation.Body" android:textColor="?colorError" android:textStyle="bold"