ProtectionCheck injection, setDeviceCredentialAllowed

This commit is contained in:
Milos Kozak 2020-03-22 20:49:36 +01:00
parent 0e9f643241
commit 4716b04972
9 changed files with 87 additions and 67 deletions

View file

@ -87,6 +87,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
@Inject BuildHelper buildHelper; @Inject BuildHelper buildHelper;
@Inject ActivePluginProvider activePlugin; @Inject ActivePluginProvider activePlugin;
@Inject FabricPrivacy fabricPrivacy; @Inject FabricPrivacy fabricPrivacy;
@Inject ProtectionCheck protectionCheck;
@Override @Override
@ -192,7 +193,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
ProtectionCheck.INSTANCE.queryProtection(this, ProtectionCheck.Protection.APPLICATION, null, this::finish, this::finish); protectionCheck.queryProtection(this, ProtectionCheck.Protection.APPLICATION, null, this::finish, this::finish);
} }
private void setWakeLock() { private void setWakeLock() {
@ -308,7 +309,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
int id = item.getItemId(); int id = item.getItemId();
switch (id) { switch (id) {
case R.id.nav_preferences: case R.id.nav_preferences:
ProtectionCheck.INSTANCE.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, () -> { protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, () -> {
Intent i = new Intent(this, PreferencesActivity.class); Intent i = new Intent(this, PreferencesActivity.class);
i.putExtra("id", -1); i.putExtra("id", -1);
startActivity(i); startActivity(i);
@ -348,7 +349,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
case R.id.nav_plugin_preferences: case R.id.nav_plugin_preferences:
ViewPager viewPager = findViewById(R.id.pager); ViewPager viewPager = findViewById(R.id.pager);
final PluginBase plugin = ((TabPageAdapter) viewPager.getAdapter()).getPluginAt(viewPager.getCurrentItem()); final PluginBase plugin = ((TabPageAdapter) viewPager.getAdapter()).getPluginAt(viewPager.getCurrentItem());
ProtectionCheck.INSTANCE.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, () -> { protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, () -> {
Intent i = new Intent(this, PreferencesActivity.class); Intent i = new Intent(this, PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId()); i.putExtra("id", plugin.getPreferencesId());
startActivity(i); startActivity(i);

View file

@ -15,6 +15,7 @@ import javax.inject.Inject
class SingleFragmentActivity : DaggerAppCompatActivity() { class SingleFragmentActivity : DaggerAppCompatActivity() {
@Inject lateinit var pluginStore: PluginStore @Inject lateinit var pluginStore: PluginStore
@Inject lateinit var protectionCheck: ProtectionCheck
private var plugin: PluginBase? = null private var plugin: PluginBase? = null
@ -36,7 +37,7 @@ class SingleFragmentActivity : DaggerAppCompatActivity() {
finish() finish()
return true return true
} else if (item.itemId == R.id.nav_plugin_preferences) { } else if (item.itemId == R.id.nav_plugin_preferences) {
ProtectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, Runnable { protectionCheck.queryProtection(this, ProtectionCheck.Protection.PREFERENCES, Runnable {
val i = Intent(this, PreferencesActivity::class.java) val i = Intent(this, PreferencesActivity::class.java)
i.putExtra("id", plugin?.preferencesId) i.putExtra("id", plugin?.preferencesId)
startActivity(i) startActivity(i)

View file

@ -34,6 +34,7 @@ class ConfigBuilderFragment : DaggerFragment() {
@Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin @Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin
@Inject lateinit var fabricPrivacy: FabricPrivacy @Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var activePlugin: ActivePluginProvider @Inject lateinit var activePlugin: ActivePluginProvider
@Inject lateinit var protectionCheck: ProtectionCheck
private var disposable: CompositeDisposable = CompositeDisposable() private var disposable: CompositeDisposable = CompositeDisposable()
private val pluginViewHolders = ArrayList<PluginViewHolder>() private val pluginViewHolders = ArrayList<PluginViewHolder>()
@ -46,14 +47,14 @@ class ConfigBuilderFragment : DaggerFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
if (ProtectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES)) if (protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES))
configbuilder_main_layout.visibility = View.GONE configbuilder_main_layout.visibility = View.GONE
else else
unlock.visibility = View.GONE unlock.visibility = View.GONE
unlock.setOnClickListener { unlock.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable {
activity.runOnUiThread { activity.runOnUiThread {
configbuilder_main_layout.visibility = View.VISIBLE configbuilder_main_layout.visibility = View.VISIBLE
unlock.visibility = View.GONE unlock.visibility = View.GONE
@ -148,7 +149,7 @@ class ConfigBuilderFragment : DaggerFragment() {
pluginPreferences.setOnClickListener { pluginPreferences.setOnClickListener {
fragment.activity?.let { activity -> fragment.activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable {
val i = Intent(fragment.context, PreferencesActivity::class.java) val i = Intent(fragment.context, PreferencesActivity::class.java)
i.putExtra("id", plugin.preferencesId) i.putExtra("id", plugin.preferencesId)
fragment.startActivity(i) fragment.startActivity(i)

View file

@ -52,6 +52,7 @@ class ActionsFragment : DaggerFragment() {
@Inject lateinit var activePlugin: ActivePluginProvider @Inject lateinit var activePlugin: ActivePluginProvider
@Inject lateinit var commandQueue: CommandQueueProvider @Inject lateinit var commandQueue: CommandQueueProvider
@Inject lateinit var buildHelper: BuildHelper @Inject lateinit var buildHelper: BuildHelper
@Inject lateinit var protectionCheck: ProtectionCheck
private var disposable: CompositeDisposable = CompositeDisposable() private var disposable: CompositeDisposable = CompositeDisposable()
@ -74,7 +75,7 @@ class ActionsFragment : DaggerFragment() {
} }
actions_extendedbolus.setOnClickListener { actions_extendedbolus.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.BOLUS, Runnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.BOLUS, Runnable {
OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.extended_bolus), resourceHelper.gs(R.string.ebstopsloop), OKDialog.showConfirmation(activity, resourceHelper.gs(R.string.extended_bolus), resourceHelper.gs(R.string.ebstopsloop),
Runnable { Runnable {
fragmentManager?.let { ExtendedBolusDialog().show(it, "Actions") } fragmentManager?.let { ExtendedBolusDialog().show(it, "Actions") }
@ -121,7 +122,7 @@ class ActionsFragment : DaggerFragment() {
} }
actions_fill.setOnClickListener { actions_fill.setOnClickListener {
activity?.let { activity -> activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.BOLUS, Runnable { fragmentManager?.let { FillDialog().show(it, "FillDialog") } }) protectionCheck.queryProtection(activity, ProtectionCheck.Protection.BOLUS, Runnable { fragmentManager?.let { FillDialog().show(it, "FillDialog") } })
} }
} }
actions_historybrowser.setOnClickListener { startActivity(Intent(context, HistoryBrowseActivity::class.java)) } actions_historybrowser.setOnClickListener { startActivity(Intent(context, HistoryBrowseActivity::class.java)) }

View file

@ -117,12 +117,12 @@ import info.nightscout.androidaps.utils.SingleClickButton;
import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.T;
import info.nightscout.androidaps.utils.ToastUtils; import info.nightscout.androidaps.utils.ToastUtils;
import info.nightscout.androidaps.utils.buildHelper.BuildHelper; import info.nightscout.androidaps.utils.buildHelper.BuildHelper;
import info.nightscout.androidaps.utils.protection.ProtectionCheck;
import info.nightscout.androidaps.utils.resources.ResourceHelper; import info.nightscout.androidaps.utils.resources.ResourceHelper;
import info.nightscout.androidaps.utils.sharedPreferences.SP; import info.nightscout.androidaps.utils.sharedPreferences.SP;
import info.nightscout.androidaps.utils.wizard.BolusWizard; import info.nightscout.androidaps.utils.wizard.BolusWizard;
import info.nightscout.androidaps.utils.wizard.QuickWizard; import info.nightscout.androidaps.utils.wizard.QuickWizard;
import info.nightscout.androidaps.utils.wizard.QuickWizardEntry; import info.nightscout.androidaps.utils.wizard.QuickWizardEntry;
import info.nightscout.androidaps.utils.protection.ProtectionCheck;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
@ -153,6 +153,7 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
@Inject QuickWizard quickWizard; @Inject QuickWizard quickWizard;
@Inject BuildHelper buildHelper; @Inject BuildHelper buildHelper;
@Inject CommandQueue commandQueue; @Inject CommandQueue commandQueue;
@Inject ProtectionCheck protectionCheck;
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
@ -857,10 +858,10 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
onClickAcceptTemp(); onClickAcceptTemp();
break; break;
case R.id.overview_quickwizardbutton: case R.id.overview_quickwizardbutton:
ProtectionCheck.INSTANCE.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, this::onClickQuickwizard); protectionCheck.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, this::onClickQuickwizard);
break; break;
case R.id.overview_wizardbutton: case R.id.overview_wizardbutton:
ProtectionCheck.INSTANCE.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, () -> new WizardDialog().show(manager, "WizardDialog")); protectionCheck.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, () -> new WizardDialog().show(manager, "WizardDialog"));
break; break;
case R.id.overview_calibrationbutton: case R.id.overview_calibrationbutton:
if (xdrip) { if (xdrip) {
@ -894,13 +895,13 @@ public class OverviewFragment extends DaggerFragment implements View.OnClickList
} }
break; break;
case R.id.overview_treatmentbutton: case R.id.overview_treatmentbutton:
ProtectionCheck.INSTANCE.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, () -> new TreatmentDialog().show(manager, "Overview")); protectionCheck.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, () -> new TreatmentDialog().show(manager, "Overview"));
break; break;
case R.id.overview_insulinbutton: case R.id.overview_insulinbutton:
ProtectionCheck.INSTANCE.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, () -> new InsulinDialog().show(manager, "Overview")); protectionCheck.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, () -> new InsulinDialog().show(manager, "Overview"));
break; break;
case R.id.overview_carbsbutton: case R.id.overview_carbsbutton:
ProtectionCheck.INSTANCE.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, () -> new CarbsDialog().show(manager, "Overview")); protectionCheck.queryProtection(getActivity(), ProtectionCheck.Protection.BOLUS, () -> new CarbsDialog().show(manager, "Overview"));
break; break;
case R.id.overview_pumpstatus: case R.id.overview_pumpstatus:
if (activePlugin.getActivePump().isSuspended() || !activePlugin.getActivePump().isInitialized()) if (activePlugin.getActivePump().isSuspended() || !activePlugin.getActivePump().isInitialized())

View file

@ -57,7 +57,8 @@ class SWDefinition @Inject constructor(
private val configBuilderPlugin: ConfigBuilderPlugin, private val configBuilderPlugin: ConfigBuilderPlugin,
private val loopPlugin: LoopPlugin, private val loopPlugin: LoopPlugin,
private val nsClientPlugin: NSClientPlugin, private val nsClientPlugin: NSClientPlugin,
private val nsProfilePlugin: NSProfilePlugin private val nsProfilePlugin: NSProfilePlugin,
private val protectionCheck: ProtectionCheck
) { ) {
var activity: AppCompatActivity? = null var activity: AppCompatActivity? = null
@ -219,7 +220,7 @@ class SWDefinition @Inject constructor(
.action(Runnable { .action(Runnable {
val plugin = activePlugin.activeInsulin as PluginBase val plugin = activePlugin.activeInsulin as PluginBase
activity?.let { activity -> activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable {
val i = Intent(activity, PreferencesActivity::class.java) val i = Intent(activity, PreferencesActivity::class.java)
i.putExtra("id", plugin.preferencesId) i.putExtra("id", plugin.preferencesId)
activity.startActivity(i) activity.startActivity(i)
@ -238,7 +239,7 @@ class SWDefinition @Inject constructor(
.action(Runnable { .action(Runnable {
val plugin = activePlugin.activeBgSource as PluginBase val plugin = activePlugin.activeBgSource as PluginBase
activity?.let { activity -> activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable {
val i = Intent(activity, PreferencesActivity::class.java) val i = Intent(activity, PreferencesActivity::class.java)
i.putExtra("id", plugin.preferencesId) i.putExtra("id", plugin.preferencesId)
activity.startActivity(i) activity.startActivity(i)
@ -288,7 +289,7 @@ class SWDefinition @Inject constructor(
.action(Runnable { .action(Runnable {
val plugin = activePlugin.activePump as PluginBase val plugin = activePlugin.activePump as PluginBase
activity?.let { activity -> activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable {
val i = Intent(activity, PreferencesActivity::class.java) val i = Intent(activity, PreferencesActivity::class.java)
i.putExtra("id", plugin.preferencesId) i.putExtra("id", plugin.preferencesId)
activity.startActivity(i) activity.startActivity(i)
@ -317,7 +318,7 @@ class SWDefinition @Inject constructor(
.action(Runnable { .action(Runnable {
val plugin = activePlugin.activeAPS as PluginBase val plugin = activePlugin.activeAPS as PluginBase
activity?.let { activity -> activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable {
val i = Intent(activity, PreferencesActivity::class.java) val i = Intent(activity, PreferencesActivity::class.java)
i.putExtra("id", plugin.preferencesId) i.putExtra("id", plugin.preferencesId)
activity.startActivity(i) activity.startActivity(i)
@ -367,7 +368,7 @@ class SWDefinition @Inject constructor(
.action(Runnable { .action(Runnable {
val plugin = activePlugin.activeSensitivity as PluginBase val plugin = activePlugin.activeSensitivity as PluginBase
activity?.let { activity -> activity?.let { activity ->
ProtectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable { protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, Runnable {
val i = Intent(activity, PreferencesActivity::class.java) val i = Intent(activity, PreferencesActivity::class.java)
i.putExtra("id", plugin.preferencesId) i.putExtra("id", plugin.preferencesId)
activity.startActivity(i) activity.startActivity(i)

View file

@ -3,7 +3,6 @@ package info.nightscout.androidaps.utils.protection
import androidx.biometric.BiometricConstants import androidx.biometric.BiometricConstants
import androidx.biometric.BiometricPrompt import androidx.biometric.BiometricPrompt
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.ToastUtils
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -22,19 +21,22 @@ object BiometricCheck {
BiometricConstants.ERROR_LOCKOUT, BiometricConstants.ERROR_LOCKOUT,
BiometricConstants.ERROR_VENDOR, BiometricConstants.ERROR_VENDOR,
BiometricConstants.ERROR_LOCKOUT_PERMANENT, BiometricConstants.ERROR_LOCKOUT_PERMANENT,
BiometricConstants.ERROR_USER_CANCELED -> { BiometricConstants.ERROR_USER_CANCELED -> {
ToastUtils.showToastInUiThread(activity.baseContext, errString.toString()) ToastUtils.showToastInUiThread(activity.baseContext, errString.toString())
fail?.run() fail?.run()
} }
BiometricConstants.ERROR_NEGATIVE_BUTTON -> BiometricConstants.ERROR_NEGATIVE_BUTTON ->
cancel?.run() cancel?.run()
BiometricConstants.ERROR_NO_SPACE, BiometricConstants.ERROR_NO_SPACE,
BiometricConstants.ERROR_HW_UNAVAILABLE, BiometricConstants.ERROR_HW_UNAVAILABLE,
BiometricConstants.ERROR_HW_NOT_PRESENT, BiometricConstants.ERROR_HW_NOT_PRESENT,
BiometricConstants.ERROR_NO_DEVICE_CREDENTIAL, BiometricConstants.ERROR_NO_DEVICE_CREDENTIAL,
BiometricConstants.ERROR_NO_BIOMETRICS -> BiometricConstants.ERROR_NO_BIOMETRICS ->
// call ok, because it's not possible to bypass it when biometrics fail // call ok, because it's not possible to bypass it when biometrics fail
ok?.run() // ok?.run()
// changed to fail as you can use PIN instead with setDeviceCredentialAllowed enabled
fail?.run()
} }
} }
@ -52,10 +54,11 @@ object BiometricCheck {
}) })
val promptInfo = BiometricPrompt.PromptInfo.Builder() val promptInfo = BiometricPrompt.PromptInfo.Builder()
.setTitle(MainApp.gs(title)) .setTitle(activity.getString(title))
.setDescription(MainApp.gs(R.string.biometric_title)) .setDescription(activity.getString(R.string.biometric_title))
.setNegativeButtonText(MainApp.gs(R.string.cancel)) // .setNegativeButtonText(activity.getString(R.string.cancel)) // not possible with setDeviceCredentialAllowed
.build() .setDeviceCredentialAllowed(true)
.build()
biometricPrompt.authenticate(promptInfo) biometricPrompt.authenticate(promptInfo)
} }

View file

@ -1,5 +1,6 @@
package info.nightscout.androidaps.utils.protection package info.nightscout.androidaps.utils.protection
import android.annotation.SuppressLint
import android.app.AlertDialog import android.app.AlertDialog
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
@ -7,14 +8,18 @@ import android.widget.EditText
import android.widget.TextView import android.widget.TextView
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.utils.SP
import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.androidaps.utils.ToastUtils
import info.nightscout.androidaps.utils.sharedPreferences.SP
import javax.inject.Inject
import javax.inject.Singleton
object PasswordCheck { @Singleton
class PasswordCheck @Inject constructor(val sp: SP) {
@SuppressLint("InflateParams")
fun queryPassword(activity: FragmentActivity, @StringRes labelId: Int, @StringRes preference: Int, ok: Runnable?, cancel: Runnable? = null, fail: Runnable? = null) { fun queryPassword(activity: FragmentActivity, @StringRes labelId: Int, @StringRes preference: Int, ok: Runnable?, cancel: Runnable? = null, fail: Runnable? = null) {
val password = SP.getString(preference, "") val password = sp.getString(preference, "")
if (password == "") { if (password == "") {
ok?.run() ok?.run()
return return
@ -25,24 +30,24 @@ object PasswordCheck {
alertDialogBuilder.setView(promptsView) alertDialogBuilder.setView(promptsView)
val label = promptsView.findViewById<View>(R.id.passwordprompt_text) as TextView val label = promptsView.findViewById<View>(R.id.passwordprompt_text) as TextView
label.text = MainApp.gs(labelId) label.text = activity.getString(labelId)
val userInput = promptsView.findViewById<View>(R.id.passwordprompt_pass) as EditText val userInput = promptsView.findViewById<View>(R.id.passwordprompt_pass) as EditText
alertDialogBuilder alertDialogBuilder
.setCancelable(false) .setCancelable(false)
.setPositiveButton(MainApp.gs(R.string.ok)) { _, _ -> .setPositiveButton(activity.getString(R.string.ok)) { _, _ ->
val enteredPassword = userInput.text.toString() val enteredPassword = userInput.text.toString()
if (password == enteredPassword) ok?.run() if (password == enteredPassword) ok?.run()
else { else {
ToastUtils.showToastInUiThread(activity, MainApp.gs(R.string.wrongpassword)) ToastUtils.showToastInUiThread(activity, activity.getString(R.string.wrongpassword))
fail?.run() fail?.run()
}
}
.setNegativeButton(MainApp.gs(R.string.cancel)
) { dialog, _ ->
cancel?.run()
dialog.cancel()
} }
}
.setNegativeButton(activity.getString(R.string.cancel)
) { dialog, _ ->
cancel?.run()
dialog.cancel()
}
alertDialogBuilder.create().show() alertDialogBuilder.create().show()
} }

View file

@ -2,9 +2,16 @@ package info.nightscout.androidaps.utils.protection
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.utils.SP import info.nightscout.androidaps.utils.sharedPreferences.SP
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class ProtectionCheck @Inject constructor(
val sp: SP,
val passwordCheck: PasswordCheck
) {
object ProtectionCheck {
enum class Protection { enum class Protection {
PREFERENCES, PREFERENCES,
APPLICATION, APPLICATION,
@ -18,39 +25,38 @@ object ProtectionCheck {
} }
private val passwordsResourceIDs = listOf( private val passwordsResourceIDs = listOf(
R.string.key_settings_password, R.string.key_settings_password,
R.string.key_application_password, R.string.key_application_password,
R.string.key_bolus_password) R.string.key_bolus_password)
private val protectionTypeResourceIDs = listOf( private val protectionTypeResourceIDs = listOf(
R.string.key_settings_protection, R.string.key_settings_protection,
R.string.key_application_protection, R.string.key_application_protection,
R.string.key_bolus_protection) R.string.key_bolus_protection)
private val titleResourceIDs = listOf( private val titleResourceIDs = listOf(
R.string.settings_password, R.string.settings_password,
R.string.application_password, R.string.application_password,
R.string.bolus_password) R.string.bolus_password)
fun isLocked(protection: Protection): Boolean { fun isLocked(protection: Protection): Boolean {
when (ProtectionType.values()[SP.getInt(protectionTypeResourceIDs[protection.ordinal], ProtectionType.NONE.ordinal)]) { return when (ProtectionType.values()[sp.getInt(protectionTypeResourceIDs[protection.ordinal], ProtectionType.NONE.ordinal)]) {
ProtectionType.NONE -> return false ProtectionType.NONE -> false
ProtectionType.BIOMETRIC -> return true ProtectionType.BIOMETRIC -> true
ProtectionType.PASSWORD -> return SP.getString(passwordsResourceIDs[protection.ordinal], "") != "" ProtectionType.PASSWORD -> sp.getString(passwordsResourceIDs[protection.ordinal], "") != ""
} }
} }
@JvmOverloads @JvmOverloads
fun queryProtection(activity: FragmentActivity, protection: Protection, fun queryProtection(activity: FragmentActivity, protection: Protection,
ok: Runnable?, cancel: Runnable? = null, fail: Runnable? = null) { ok: Runnable?, cancel: Runnable? = null, fail: Runnable? = null) {
when (ProtectionType.values()[SP.getInt(protectionTypeResourceIDs[protection.ordinal], ProtectionType.NONE.ordinal)]) { when (ProtectionType.values()[sp.getInt(protectionTypeResourceIDs[protection.ordinal], ProtectionType.NONE.ordinal)]) {
ProtectionType.NONE -> ProtectionType.NONE ->
ok?.run() ok?.run()
ProtectionType.BIOMETRIC -> ProtectionType.BIOMETRIC ->
BiometricCheck.biometricPrompt(activity, titleResourceIDs[protection.ordinal], ok, cancel, fail) BiometricCheck.biometricPrompt(activity, titleResourceIDs[protection.ordinal], ok, cancel, fail)
ProtectionType.PASSWORD -> ProtectionType.PASSWORD ->
PasswordCheck.queryPassword(activity, titleResourceIDs[protection.ordinal], passwordsResourceIDs[protection.ordinal], ok, cancel, fail) passwordCheck.queryPassword(activity, titleResourceIDs[protection.ordinal], passwordsResourceIDs[protection.ordinal], ok, cancel, fail)
} }
} }
} }