feat: request protection from menu

This commit is contained in:
Andries Smit 2022-03-23 11:56:46 +01:00
parent 02e553ecc6
commit 2c60ec44e5
3 changed files with 94 additions and 64 deletions

View file

@ -10,23 +10,26 @@ import android.widget.*
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.interfaces.Config
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.PreferencesActivity
import info.nightscout.androidaps.activities.SingleFragmentActivity
import info.nightscout.androidaps.databinding.ConfigbuilderFragmentBinding
import info.nightscout.androidaps.events.EventRebuildTabs
import info.nightscout.androidaps.interfaces.*
import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.interfaces.Config
import info.nightscout.androidaps.interfaces.PluginBase
import info.nightscout.androidaps.interfaces.PluginType
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.configBuilder.events.EventConfigBuilderUpdateGui
import info.nightscout.androidaps.utils.FabricPrivacy
import io.reactivex.rxjava3.kotlin.plusAssign
import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.utils.buildHelper.BuildHelper
import info.nightscout.androidaps.utils.protection.ProtectionCheck
import info.nightscout.androidaps.utils.protection.ProtectionCheck.Protection.PREFERENCES
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.rxjava3.disposables.CompositeDisposable
import java.util.*
import io.reactivex.rxjava3.kotlin.plusAssign
import javax.inject.Inject
class ConfigBuilderFragment : DaggerFragment() {
@ -44,42 +47,30 @@ class ConfigBuilderFragment : DaggerFragment() {
private var disposable: CompositeDisposable = CompositeDisposable()
private val pluginViewHolders = ArrayList<PluginViewHolder>()
private var inMenu = false
private var queryingProtection = false
private var _binding: ConfigbuilderFragmentBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = ConfigbuilderFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES))
binding.mainLayout.visibility = View.GONE
else
binding.unlock.visibility = View.GONE
binding.unlock.setOnClickListener {
activity?.let { activity ->
protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, {
activity.runOnUiThread {
binding.mainLayout.visibility = View.VISIBLE
binding.unlock.visibility = View.GONE
}
})
}
}
val parentClass = this.activity?.let { it::class.java }
inMenu = parentClass == SingleFragmentActivity::class.java
updateProtectedUi()
binding.unlock.setOnClickListener { queryProtection() }
}
@Synchronized
override fun onResume() {
super.onResume()
if (inMenu) queryProtection() else updateProtectedUi()
disposable += rxBus
.toObservable(EventConfigBuilderUpdateGui::class.java)
.observeOn(aapsSchedulers.main)
@ -215,4 +206,21 @@ class ConfigBuilderFragment : DaggerFragment() {
return type == PluginType.GENERAL || type == PluginType.CONSTRAINTS || type == PluginType.LOOP
}
}
private fun updateProtectedUi() {
val isLocked = protectionCheck.isLocked(PREFERENCES)
binding.mainLayout.visibility = isLocked.not().toVisibility()
binding.unlock.visibility = isLocked.toVisibility()
}
private fun queryProtection() {
val isLocked = protectionCheck.isLocked(PREFERENCES)
if (isLocked && !queryingProtection) {
activity?.let { activity ->
queryingProtection = true
val doUpdate = { activity.runOnUiThread { queryingProtection = false; updateProtectedUi() } }
protectionCheck.queryProtection(activity, PREFERENCES, doUpdate, doUpdate, doUpdate)
}
}
}
}

View file

@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.SingleFragmentActivity
import info.nightscout.androidaps.dana.database.DanaHistoryDatabase
import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.database.entities.UserEntry.Action
@ -14,6 +15,7 @@ import info.nightscout.androidaps.database.entities.UserEntry.Sources
import info.nightscout.androidaps.databinding.MaintenanceFragmentBinding
import info.nightscout.androidaps.diaconn.database.DiaconnHistoryDatabase
import info.nightscout.androidaps.events.EventPreferenceChange
import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.insight.database.InsightDatabase
import info.nightscout.androidaps.interfaces.DataSyncSelector
import info.nightscout.androidaps.interfaces.ImportExportPrefs
@ -28,6 +30,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.Das
import info.nightscout.androidaps.plugins.pump.omnipod.eros.history.database.ErosHistoryDatabase
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.protection.ProtectionCheck
import info.nightscout.androidaps.utils.protection.ProtectionCheck.Protection.PREFERENCES
import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.rxjava3.core.Completable.fromAction
@ -57,11 +60,11 @@ class MaintenanceFragment : DaggerFragment() {
@Inject lateinit var overviewData: OverviewData
private val compositeDisposable = CompositeDisposable()
private var inMenu = false
private var queryingProtection = false
private var _binding: MaintenanceFragmentBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
@ -71,6 +74,9 @@ class MaintenanceFragment : DaggerFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val parentClass = this.activity?.let { it::class.java }
inMenu = parentClass == SingleFragmentActivity::class.java
updateProtectedUi()
binding.logSend.setOnClickListener { maintenancePlugin.sendLogs() }
binding.logDelete.setOnClickListener {
uel.log(Action.DELETE_LOGS, Sources.Maintenance)
@ -131,22 +137,12 @@ class MaintenanceFragment : DaggerFragment() {
}
}
if (protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES)) {
binding.mainLayout.visibility = View.GONE
} else {
binding.unlock.visibility = View.GONE
binding.unlock.setOnClickListener { queryProtection() }
}
binding.unlock.setOnClickListener {
activity?.let { activity ->
protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, {
activity.runOnUiThread {
binding.mainLayout.visibility = View.VISIBLE
binding.unlock.visibility = View.GONE
}
})
}
}
override fun onResume() {
super.onResume()
if (inMenu) queryProtection() else updateProtectedUi()
}
@Synchronized
@ -155,4 +151,21 @@ class MaintenanceFragment : DaggerFragment() {
compositeDisposable.clear()
_binding = null
}
private fun updateProtectedUi() {
val isLocked = protectionCheck.isLocked(PREFERENCES)
binding.mainLayout.visibility = isLocked.not().toVisibility()
binding.unlock.visibility = isLocked.toVisibility()
}
private fun queryProtection() {
val isLocked = protectionCheck.isLocked(PREFERENCES)
if (isLocked && !queryingProtection) {
activity?.let { activity ->
queryingProtection = true
val doUpdate = { activity.runOnUiThread { queryingProtection = false; updateProtectedUi() } }
protectionCheck.queryProtection(activity, PREFERENCES, doUpdate, doUpdate, doUpdate)
}
}
}
}

View file

@ -12,12 +12,14 @@ import android.widget.ArrayAdapter
import dagger.android.support.DaggerFragment
import info.nightscout.androidaps.Constants
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.SingleFragmentActivity
import info.nightscout.androidaps.data.ProfileSealed
import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Sources
import info.nightscout.androidaps.database.entities.ValueWithUnit
import info.nightscout.androidaps.databinding.LocalprofileFragmentBinding
import info.nightscout.androidaps.dialogs.ProfileSwitchDialog
import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.interfaces.ActivePlugin
import info.nightscout.androidaps.interfaces.GlucoseUnit
import info.nightscout.androidaps.interfaces.Profile
@ -56,7 +58,8 @@ class LocalProfileFragment : DaggerFragment() {
@Inject lateinit var uel: UserEntryLogger
private var disposable: CompositeDisposable = CompositeDisposable()
private var inMenu = false
private var queryingProtection = false
private var basalView: TimeListEdit? = null
private val save = Runnable {
@ -99,6 +102,9 @@ class LocalProfileFragment : DaggerFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val parentClass = this.activity?.let { it::class.java }
inMenu = parentClass == SingleFragmentActivity::class.java
updateProtectedUi()
// activate DIA tab
processVisibilityOnClick(binding.diaTab)
binding.diaPlaceholder.visibility = View.VISIBLE
@ -125,22 +131,7 @@ class LocalProfileFragment : DaggerFragment() {
}
binding.dia.editText?.id?.let { binding.diaLabel.labelFor = it }
if (protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES)) {
binding.mainLayout.visibility = View.GONE
} else {
binding.unlock.visibility = View.GONE
}
binding.unlock.setOnClickListener {
activity?.let { activity ->
protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, {
activity.runOnUiThread {
binding.mainLayout.visibility = View.VISIBLE
binding.unlock.visibility = View.GONE
}
})
}
}
binding.unlock.setOnClickListener { queryProtection() }
}
fun build() {
@ -345,6 +336,7 @@ class LocalProfileFragment : DaggerFragment() {
@Synchronized
override fun onResume() {
super.onResume()
if (inMenu) queryProtection() else updateProtectedUi()
disposable += rxBus
.toObservable(EventLocalProfileChanged::class.java)
.observeOn(aapsSchedulers.main)
@ -421,4 +413,21 @@ class LocalProfileFragment : DaggerFragment() {
binding.basal.visibility = View.GONE
binding.target.visibility = View.GONE
}
private fun updateProtectedUi() {
val isLocked = protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES)
binding.mainLayout.visibility = isLocked.not().toVisibility()
binding.unlock.visibility = isLocked.toVisibility()
}
private fun queryProtection() {
val isLocked = protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES)
if (isLocked && !queryingProtection) {
activity?.let { activity ->
queryingProtection = true
val doUpdate = { activity.runOnUiThread { queryingProtection = false; updateProtectedUi() } }
protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, doUpdate, doUpdate, doUpdate)
}
}
}
}