diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt index bdfa14014..0ad978870 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryEditActivity.kt @@ -40,6 +40,7 @@ import com.kunzisoft.keepass.notifications.ClipboardEntryNotificationService import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_CREATE_ENTRY_TASK import com.kunzisoft.keepass.notifications.DatabaseTaskNotificationService.Companion.ACTION_DATABASE_UPDATE_ENTRY_TASK import com.kunzisoft.keepass.notifications.KeyboardEntryNotificationService +import com.kunzisoft.keepass.otp.OtpElement import com.kunzisoft.keepass.otp.OtpEntryFields import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.timeout.TimeoutHelper @@ -49,7 +50,8 @@ import java.util.* class EntryEditActivity : LockingHideActivity(), IconPickerDialogFragment.IconPickerListener, - GeneratePasswordDialogFragment.GeneratePasswordListener { + GeneratePasswordDialogFragment.GeneratePasswordListener, + SetOTPDialogFragment.CreateOtpListener { private var mDatabase: Database? = null @@ -358,15 +360,8 @@ class EntryEditActivity : LockingHideActivity(), R.id.menu_add_otp -> { // Retrieve the current otpElement if exists // and open the dialog to set up the OTP - SetOTPDialogFragment.build(mEntry?.getOtpElement()?.otpModel).apply { - createOTPElementListener = { otpElement -> - // Update the otp field with otpauth:// url - val otpField = OtpEntryFields.buildOtpField(otpElement, - mEntry?.title, mEntry?.username) - entryEditContentsView?.putCustomField(otpField.name, otpField.protectedValue) - mEntry?.putExtraField(otpField.name, otpField.protectedValue) - } - }.show(supportFragmentManager, "addOTPDialog") + SetOTPDialogFragment.build(mEntry?.getOtpElement()?.otpModel) + .show(supportFragmentManager, "addOTPDialog") return true } @@ -376,6 +371,14 @@ class EntryEditActivity : LockingHideActivity(), return super.onOptionsItemSelected(item) } + override fun onOtpCreated(otpElement: OtpElement) { + // Update the otp field with otpauth:// url + val otpField = OtpEntryFields.buildOtpField(otpElement, + mEntry?.title, mEntry?.username) + entryEditContentsView?.putCustomField(otpField.name, otpField.protectedValue) + mEntry?.putExtraField(otpField.name, otpField.protectedValue) + } + override fun iconPicked(bundle: Bundle) { IconPickerDialogFragment.getIconStandardFromBundle(bundle)?.let { icon -> temporarilySaveAndShowSelectedIcon(icon) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/SetOTPDialogFragment.kt b/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/SetOTPDialogFragment.kt index 513a983ef..7d8f4f9d2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/SetOTPDialogFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/dialogs/SetOTPDialogFragment.kt @@ -2,6 +2,7 @@ package com.kunzisoft.keepass.activities.dialogs import android.annotation.SuppressLint import android.app.Dialog +import android.content.Context import android.os.Bundle import android.text.Editable import android.text.TextWatcher @@ -31,7 +32,7 @@ import com.kunzisoft.keepass.otp.TokenCalculator class SetOTPDialogFragment : DialogFragment() { - var createOTPElementListener: ((OtpElement) -> Unit)? = null + private var mCreateOTPElementListener: CreateOtpListener? = null private var mOtpElement: OtpElement = OtpElement() @@ -68,6 +69,19 @@ class SetOTPDialogFragment : DialogFragment() { private var mPeriodWellFormed = true private var mDigitsWellFormed = true + override fun onAttach(context: Context) { + super.onAttach(context) + // Verify that the host activity implements the callback interface + try { + // Instantiate the NoticeDialogListener so we can send events to the host + mCreateOTPElementListener = context as CreateOtpListener + } catch (e: ClassCastException) { + // The activity doesn't implement the interface, throw exception + throw ClassCastException(context.toString() + + " must implement " + CreateOtpListener::class.java.name) + } + } + @SuppressLint("ClickableViewAccessibility") override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { @@ -169,7 +183,7 @@ class SetOTPDialogFragment : DialogFragment() { && mCounterWellFormed && mPeriodWellFormed && mDigitsWellFormed) { - createOTPElementListener?.invoke(mOtpElement) + mCreateOTPElementListener?.onOtpCreated(mOtpElement) dismiss() } } @@ -334,6 +348,10 @@ class SetOTPDialogFragment : DialogFragment() { outState.putParcelable(KEY_OTP, mOtpElement.otpModel) } + interface CreateOtpListener { + fun onOtpCreated(otpElement: OtpElement) + } + companion object { private const val KEY_OTP = "KEY_OTP"