Ask for old apssword in SetupWizard

This commit is contained in:
Milos Kozak 2021-09-20 13:04:13 +02:00
parent 02b77ae554
commit 4b6d4d287c
4 changed files with 122 additions and 51 deletions

View file

@ -127,7 +127,7 @@ class SetupWizardActivity : NoSplashAppCompatActivity() {
override fun updateButtons() { override fun updateButtons() {
runOnUiThread { runOnUiThread {
val currentScreen = screens[currentWizardPage] val currentScreen = screens[currentWizardPage]
if (currentScreen.validator == null || currentScreen.validator!!.isValid || currentScreen.skippable) { if (currentScreen.validator == null || currentScreen.validator?.isValid == true || currentScreen.skippable) {
if (currentWizardPage == nextPage(null)) { if (currentWizardPage == nextPage(null)) {
findViewById<View>(R.id.finish_button).visibility = View.VISIBLE findViewById<View>(R.id.finish_button).visibility = View.VISIBLE
findViewById<View>(R.id.next_button).visibility = View.GONE findViewById<View>(R.id.next_button).visibility = View.GONE
@ -184,7 +184,7 @@ class SetupWizardActivity : NoSplashAppCompatActivity() {
private fun nextPage(view: View?): Int { private fun nextPage(view: View?): Int {
var page = currentWizardPage + 1 var page = currentWizardPage + 1
while (page < screens.size) { while (page < screens.size) {
if (screens[page].visibility == null || screens[page].visibility!!.isValid) return page if (screens[page].visibility == null || screens[page].visibility?.isValid == true) return page
page++ page++
} }
return min(currentWizardPage, screens.size - 1) return min(currentWizardPage, screens.size - 1)
@ -194,7 +194,7 @@ class SetupWizardActivity : NoSplashAppCompatActivity() {
private fun previousPage(view: View?): Int { private fun previousPage(view: View?): Int {
var page = currentWizardPage - 1 var page = currentWizardPage - 1
while (page >= 0) { while (page >= 0) {
if (screens[page].visibility == null || screens[page].visibility!!.isValid) return page if (screens[page].visibility == null || screens[page].visibility?.isValid == true) return page
page-- page--
} }
return max(currentWizardPage, 0) return max(currentWizardPage, 0)

View file

@ -5,11 +5,13 @@ import android.text.Editable
import android.text.InputType import android.text.InputType
import android.text.TextWatcher import android.text.TextWatcher
import android.view.View import android.view.View
import android.widget.Button
import android.widget.EditText import android.widget.EditText
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.setupwizard.SWTextValidator import info.nightscout.androidaps.setupwizard.SWTextValidator
import info.nightscout.androidaps.utils.CryptoUtil import info.nightscout.androidaps.utils.CryptoUtil
@ -17,36 +19,71 @@ class SWEditEncryptedPassword(injector: HasAndroidInjector, private val cryptoUt
private var validator: SWTextValidator = SWTextValidator(String::isNotEmpty) private var validator: SWTextValidator = SWTextValidator(String::isNotEmpty)
private var updateDelay = 0L private var updateDelay = 0L
private var button: Button? = null
private var editText: EditText? = null
private var editText2: EditText? = null
private var l: TextView? = null
private var c: TextView? = null
private var c2: TextView? = null
override fun generateDialog(layout: LinearLayout) { override fun generateDialog(layout: LinearLayout) {
val context = layout.context val context = layout.context
val l = TextView(context) val isPasswordSet = sp.contains(R.string.key_master_password) && sp.getString(R.string.key_master_password, "") != ""
l.id = View.generateViewId()
label?.let { l.setText(it) } button = Button(context)
l.setTypeface(l.typeface, Typeface.BOLD) button?.setText(R.string.unlock_settings)
layout.addView(l) button?.setOnClickListener {
val c = TextView(context) scanForActivity(context)?.let { activity ->
c.id = View.generateViewId() passwordCheck.queryPassword(activity, R.string.master_password, R.string.key_master_password, {
comment?.let { c.setText(it) } button?.visibility = View.GONE
c.setTypeface(c.typeface, Typeface.ITALIC) editText?.visibility = View.VISIBLE
layout.addView(c) editText2?.visibility = View.VISIBLE
val editText = EditText(context) l?.visibility = View.VISIBLE
editText.id = View.generateViewId() c?.visibility = View.VISIBLE
editText.inputType = InputType.TYPE_CLASS_TEXT c2?.visibility = View.VISIBLE
editText.maxLines = 1 })
editText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD }
}
button?.visibility = isPasswordSet.toVisibility()
layout.addView(button)
label?.let {
l = TextView(context)
l?.id = View.generateViewId()
l?.setText(it)
l?.setTypeface(l?.typeface, Typeface.BOLD)
layout.addView(l)
}
comment?.let {
c = TextView(context)
c?.id = View.generateViewId()
c?.setText(it)
c?.setTypeface(c?.typeface, Typeface.ITALIC)
c?.visibility = isPasswordSet.not().toVisibility()
layout.addView(c)
}
editText = EditText(context)
editText?.id = View.generateViewId()
editText?.inputType = InputType.TYPE_CLASS_TEXT
editText?.maxLines = 1
editText?.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
editText?.visibility = isPasswordSet.not().toVisibility()
layout.addView(editText) layout.addView(editText)
val c2 = TextView(context) c2 = TextView(context)
c2.id = View.generateViewId() c2?.id = View.generateViewId()
c2.setText(R.string.confirm) c2?.setText(R.string.confirm)
c2?.visibility = isPasswordSet.not().toVisibility()
layout.addView(c2) layout.addView(c2)
val editText2 = EditText(context) editText2 = EditText(context)
editText2.id = View.generateViewId() editText2?.id = View.generateViewId()
editText2.inputType = InputType.TYPE_CLASS_TEXT editText2?.inputType = InputType.TYPE_CLASS_TEXT
editText2.maxLines = 1 editText2?.maxLines = 1
editText2.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD editText2?.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
editText2?.visibility = isPasswordSet.not().toVisibility()
layout.addView(editText2) layout.addView(editText2)
super.generateDialog(layout) super.generateDialog(layout)
@ -55,14 +92,14 @@ class SWEditEncryptedPassword(injector: HasAndroidInjector, private val cryptoUt
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
sp.remove(preferenceId) sp.remove(preferenceId)
scheduleChange(updateDelay) scheduleChange(updateDelay)
if (validator.isValid(editText.text.toString()) && validator.isValid(editText2.text.toString()) && editText.text.toString() == editText2.text.toString()) if (validator.isValid(editText?.text.toString()) && validator.isValid(editText2?.text.toString()) && editText?.text.toString() == editText2?.text.toString())
save(s.toString(), updateDelay) save(s.toString(), updateDelay)
} }
override fun afterTextChanged(s: Editable) {} override fun afterTextChanged(s: Editable) {}
} }
editText.addTextChangedListener(watcher) editText?.addTextChangedListener(watcher)
editText2.addTextChangedListener(watcher) editText2?.addTextChangedListener(watcher)
} }
fun preferenceId(preferenceId: Int): SWEditEncryptedPassword { fun preferenceId(preferenceId: Int): SWEditEncryptedPassword {

View file

@ -1,14 +1,18 @@
package info.nightscout.androidaps.setupwizard.elements package info.nightscout.androidaps.setupwizard.elements
import android.content.Context
import android.content.ContextWrapper
import android.view.View import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.events.EventPreferenceChange import info.nightscout.androidaps.events.EventPreferenceChange
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.setupwizard.events.EventSWUpdate import info.nightscout.androidaps.setupwizard.events.EventSWUpdate
import info.nightscout.androidaps.utils.protection.PasswordCheck
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 java.util.concurrent.Executors import java.util.concurrent.Executors
@ -22,6 +26,7 @@ open class SWItem(val injector: HasAndroidInjector, var type: Type) {
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var resourceHelper: ResourceHelper @Inject lateinit var resourceHelper: ResourceHelper
@Inject lateinit var sp: SP @Inject lateinit var sp: SP
@Inject lateinit var passwordCheck: PasswordCheck
private val eventWorker = Executors.newSingleThreadScheduledExecutor() private val eventWorker = Executors.newSingleThreadScheduledExecutor()
private var scheduledEventPost: ScheduledFuture<*>? = null private var scheduledEventPost: ScheduledFuture<*>? = null
@ -33,6 +38,7 @@ open class SWItem(val injector: HasAndroidInjector, var type: Type) {
@Suppress("unused") @Suppress("unused")
enum class Type { enum class Type {
NONE, TEXT, HTML_LINK, BREAK, LISTENER, URL, STRING, NUMBER, DECIMAL_NUMBER, RADIOBUTTON, PLUGIN, BUTTON, FRAGMENT, UNIT_NUMBER, PREFERENCE NONE, TEXT, HTML_LINK, BREAK, LISTENER, URL, STRING, NUMBER, DECIMAL_NUMBER, RADIOBUTTON, PLUGIN, BUTTON, FRAGMENT, UNIT_NUMBER, PREFERENCE
} }
@ -79,4 +85,13 @@ open class SWItem(val injector: HasAndroidInjector, var type: Type) {
val task: Runnable = PostRunnable() val task: Runnable = PostRunnable()
scheduledEventPost = eventWorker.schedule(task, updateDelay, TimeUnit.SECONDS) scheduledEventPost = eventWorker.schedule(task, updateDelay, TimeUnit.SECONDS)
} }
fun scanForActivity(cont: Context?): AppCompatActivity? {
return when (cont) {
null -> null
is AppCompatActivity -> cont
is ContextWrapper -> scanForActivity(cont.baseContext)
else -> null
}
}
} }

View file

@ -43,6 +43,7 @@ abstract class Trigger(val injector: HasAndroidInjector) {
@Inject lateinit var dateUtil: DateUtil @Inject lateinit var dateUtil: DateUtil
init { init {
@Suppress("LeakingThis")
injector.androidInjector().inject(this) injector.androidInjector().inject(this)
} }
@ -55,16 +56,12 @@ abstract class Trigger(val injector: HasAndroidInjector) {
abstract fun icon(): Optional<Int?> abstract fun icon(): Optional<Int?>
abstract fun duplicate(): Trigger abstract fun duplicate(): Trigger
companion object { private fun scanForActivity(cont: Context?): AppCompatActivity? {
return when (cont) {
@JvmStatic null -> null
fun scanForActivity(cont: Context?): AppCompatActivity? { is AppCompatActivity -> cont
return when (cont) { is ContextWrapper -> scanForActivity(cont.baseContext)
null -> null else -> null
is AppCompatActivity -> cont
is ContextWrapper -> scanForActivity(cont.baseContext)
else -> null
}
} }
} }
@ -80,26 +77,34 @@ abstract class Trigger(val injector: HasAndroidInjector) {
.put("data", dataJSON()) .put("data", dataJSON())
.toString() .toString()
fun instantiate(obj: JSONObject): Trigger? { fun instantiate(obj: JSONObject): Trigger {
val type = obj.getString("type") val type = obj.getString("type")
val data = obj.getJSONObject("data") val data = obj.getJSONObject("data")
//val clazz = Class.forName(type).kotlin //val clazz = Class.forName(type).kotlin
//return (clazz.primaryConstructor?.call(injector) as Trigger).fromJSON(data?.toString() ?: "") //return (clazz.primaryConstructor?.call(injector) as Trigger).fromJSON(data?.toString() ?: "")
return when (type) { return when (type) {
TriggerAutosensValue::class.java.name, // backward compatibility TriggerAutosensValue::class.java.name, // backward compatibility
TriggerAutosensValue::class.java.simpleName -> TriggerAutosensValue(injector).fromJSON(data.toString()) TriggerAutosensValue::class.java.simpleName -> TriggerAutosensValue(injector).fromJSON(
data.toString()
)
TriggerBg::class.java.name, TriggerBg::class.java.name,
TriggerBg::class.java.simpleName -> TriggerBg(injector).fromJSON(data.toString()) TriggerBg::class.java.simpleName -> TriggerBg(injector).fromJSON(data.toString())
TriggerBolusAgo::class.java.name, TriggerBolusAgo::class.java.name,
TriggerBolusAgo::class.java.simpleName -> TriggerBolusAgo(injector).fromJSON(data.toString()) TriggerBolusAgo::class.java.simpleName -> TriggerBolusAgo(injector).fromJSON(
data.toString()
)
TriggerBTDevice::class.java.name, TriggerBTDevice::class.java.name,
TriggerBTDevice::class.java.simpleName -> TriggerBTDevice(injector).fromJSON(data.toString()) TriggerBTDevice::class.java.simpleName -> TriggerBTDevice(injector).fromJSON(
data.toString()
)
TriggerIob::class.java.name, TriggerIob::class.java.name,
TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString()) TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString())
TriggerCOB::class.java.name, TriggerCOB::class.java.name,
TriggerCOB::class.java.simpleName -> TriggerCOB(injector).fromJSON(data.toString()) TriggerCOB::class.java.simpleName -> TriggerCOB(injector).fromJSON(data.toString())
TriggerConnector::class.java.name, TriggerConnector::class.java.name,
TriggerConnector::class.java.simpleName -> TriggerConnector(injector).fromJSON(data.toString()) TriggerConnector::class.java.simpleName -> TriggerConnector(injector).fromJSON(
data.toString()
)
TriggerDelta::class.java.name, TriggerDelta::class.java.name,
TriggerDelta::class.java.simpleName -> TriggerDelta(injector).fromJSON(data.toString()) TriggerDelta::class.java.simpleName -> TriggerDelta(injector).fromJSON(data.toString())
TriggerDummy::class.java.name, TriggerDummy::class.java.name,
@ -107,21 +112,35 @@ abstract class Trigger(val injector: HasAndroidInjector) {
TriggerIob::class.java.name, TriggerIob::class.java.name,
TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString()) TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data.toString())
TriggerLocation::class.java.name, TriggerLocation::class.java.name,
TriggerLocation::class.java.simpleName -> TriggerLocation(injector).fromJSON(data.toString()) TriggerLocation::class.java.simpleName -> TriggerLocation(injector).fromJSON(
data.toString()
)
TriggerProfilePercent::class.java.name, TriggerProfilePercent::class.java.name,
TriggerProfilePercent::class.java.simpleName -> TriggerProfilePercent(injector).fromJSON(data.toString()) TriggerProfilePercent::class.java.simpleName -> TriggerProfilePercent(injector).fromJSON(
data.toString()
)
TriggerPumpLastConnection::class.java.name, TriggerPumpLastConnection::class.java.name,
TriggerPumpLastConnection::class.java.simpleName -> TriggerPumpLastConnection(injector).fromJSON(data.toString()) TriggerPumpLastConnection::class.java.simpleName -> TriggerPumpLastConnection(injector).fromJSON(
data.toString()
)
TriggerRecurringTime::class.java.name, TriggerRecurringTime::class.java.name,
TriggerRecurringTime::class.java.simpleName -> TriggerRecurringTime(injector).fromJSON(data.toString()) TriggerRecurringTime::class.java.simpleName -> TriggerRecurringTime(injector).fromJSON(
data.toString()
)
TriggerTempTarget::class.java.name, TriggerTempTarget::class.java.name,
TriggerTempTarget::class.java.simpleName -> TriggerTempTarget(injector).fromJSON(data.toString()) TriggerTempTarget::class.java.simpleName -> TriggerTempTarget(injector).fromJSON(
data.toString()
)
TriggerTime::class.java.name, TriggerTime::class.java.name,
TriggerTime::class.java.simpleName -> TriggerTime(injector).fromJSON(data.toString()) TriggerTime::class.java.simpleName -> TriggerTime(injector).fromJSON(data.toString())
TriggerTimeRange::class.java.name, TriggerTimeRange::class.java.name,
TriggerTimeRange::class.java.simpleName -> TriggerTimeRange(injector).fromJSON(data.toString()) TriggerTimeRange::class.java.simpleName -> TriggerTimeRange(injector).fromJSON(
data.toString()
)
TriggerWifiSsid::class.java.name, TriggerWifiSsid::class.java.name,
TriggerWifiSsid::class.java.simpleName -> TriggerWifiSsid(injector).fromJSON(data.toString()) TriggerWifiSsid::class.java.simpleName -> TriggerWifiSsid(injector).fromJSON(
data.toString()
)
else -> throw ClassNotFoundException(type) else -> throw ClassNotFoundException(type)
} }
} }