diff --git a/CHANGELOG b/CHANGELOG index 0484419bb..d1a17b60b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ KeePassDX(4.3.0) * Manual change of app language #1884 #1990 * Add Passkey User Verification #2283 + * Share URL/Entry from Autofill to Magikeyboard #1465 * Fix autofill username detection #2276 * Fix Passkey in passwordless mode #2282 * Fix Magikeyboard notification #2288 diff --git a/app/build.gradle b/app/build.gradle index 642cdbcc1..42117f753 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -131,7 +131,8 @@ dependencies { implementation 'androidx.media:media:1.6.0' // Lifecycle - LiveData - ViewModel - Coroutines implementation "androidx.core:core-ktx:$android_core_version" - implementation "androidx.lifecycle:lifecycle-process:2.6.2" + implementation "androidx.lifecycle:lifecycle-process:$android_lifecycle" + implementation "androidx.lifecycle:lifecycle-service:$android_lifecycle" implementation 'androidx.fragment:fragment-ktx:1.6.0' implementation "com.google.android.material:material:$android_material_version" // Token auto complete diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/AutofillHelper.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/AutofillHelper.kt index 6df679543..801e5c844 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/AutofillHelper.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/AutofillHelper.kt @@ -561,7 +561,7 @@ object AutofillHelper { if (entriesInfo.isEmpty()) { throw IOException("No entries found") } else { - StructureParser(autofillComponent.assistStructure).parse()?.let { result -> + StructureParser(autofillComponent.assistStructure).parseOrNull()?.let { result -> // New Response onIntentCreated(Intent().putExtra( AutofillManager.EXTRA_AUTHENTICATION_RESULT, diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/KeeAutofillService.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/KeeAutofillService.kt index 852e72219..2295eee59 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/KeeAutofillService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/KeeAutofillService.kt @@ -49,6 +49,7 @@ import androidx.core.net.toUri import com.kunzisoft.keepass.R import com.kunzisoft.keepass.credentialprovider.activity.AutofillLauncherActivity import com.kunzisoft.keepass.credentialprovider.autofill.StructureParser.Companion.APPLICATION_ID_POPUP_WINDOW +import com.kunzisoft.keepass.credentialprovider.magikeyboard.MagikeyboardService import com.kunzisoft.keepass.database.ContextualDatabase import com.kunzisoft.keepass.database.DatabaseTaskProvider import com.kunzisoft.keepass.database.helper.SearchHelper @@ -111,7 +112,16 @@ class KeeAutofillService : AutofillService() { // Check user's settings for authenticating Responses and Datasets. val latestStructure = request.fillContexts.last().structure - StructureParser(latestStructure).parse()?.let { parseResult -> + StructureParser(latestStructure).parse(saveValue = false)?.let { parseResult -> + + // Build the search info from the parser + val searchInfo = SearchInfo().apply { + //applicationId = parseResult.applicationId + webScheme = parseResult.webScheme + webDomain = parseResult.webDomain + } + // Add the search info to the magikeyboard service + MagikeyboardService.addSearchInfo(searchInfo) // Build search info only if applicationId or webDomain are not blocked if (autofillAllowedFor( @@ -120,53 +130,57 @@ class KeeAutofillService : AutofillService() { webDomain = parseResult.webDomain, webDomainBlocklist = webDomainBlocklist) ) { - val searchInfo = SearchInfo().apply { - applicationId = parseResult.applicationId - webDomain = parseResult.webDomain - webScheme = parseResult.webScheme - } - val inlineSuggestionsRequest = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R - && autofillInlineSuggestionsEnabled) { - CompatInlineSuggestionsRequest(request) - } else { - null - } - val autofillComponent = AutofillComponent( - latestStructure, - inlineSuggestionsRequest - ) - SearchHelper.checkAutoSearchInfo( - context = this, - database = mDatabase, - searchInfo = searchInfo, - onItemsFound = { openedDatabase, items -> - /* TODO Share context - MagikeyboardService.addEntries( - context = this, - entryList = items, - toast = true - )*/ - callback.onSuccess( - AutofillHelper.buildResponse( + + if (parseResult.isValid()) { + val inlineSuggestionsRequest = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R + && autofillInlineSuggestionsEnabled + ) { + CompatInlineSuggestionsRequest(request) + } else { + null + } + val autofillComponent = AutofillComponent( + latestStructure, + inlineSuggestionsRequest + ) + SearchHelper.checkAutoSearchInfo( + context = this, + database = mDatabase, + searchInfo = searchInfo, + onItemsFound = { openedDatabase, items -> + /* TODO Share context #1465 + MagikeyboardService.addEntries( context = this, - database = openedDatabase, - entriesInfo = items, - parseResult = parseResult, - autofillComponent = autofillComponent + entryList = items, + toast = true + )*/ + callback.onSuccess( + AutofillHelper.buildResponse( + context = this, + database = openedDatabase, + entriesInfo = items, + parseResult = parseResult, + autofillComponent = autofillComponent + ) ) - ) - }, - onItemNotFound = { openedDatabase -> - // Show UI if no search result - showUIForEntrySelection(parseResult, openedDatabase, - searchInfo, autofillComponent, callback) - }, - onDatabaseClosed = { - // Show UI if database not open - showUIForEntrySelection(parseResult, null, - searchInfo, autofillComponent, callback) - } - ) + }, + onItemNotFound = { openedDatabase -> + // Show UI if no search result + showUIForEntrySelection( + parseResult, openedDatabase, + searchInfo, autofillComponent, callback + ) + }, + onDatabaseClosed = { + // Show UI if database not open + showUIForEntrySelection( + parseResult, null, + searchInfo, autofillComponent, callback + ) + } + ) + } } } } @@ -372,9 +386,9 @@ class KeeAutofillService : AutofillService() { var success = false if (askToSaveData && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { val latestStructure = request.fillContexts.last().structure - StructureParser(latestStructure).parse(true)?.let { parseResult -> + StructureParser(latestStructure).parse(saveValue = true)?.let { parseResult -> - if (autofillAllowedFor( + if (parseResult.isValid() && autofillAllowedFor( applicationId = parseResult.applicationId, applicationIdBlocklist = applicationIdBlocklist, webDomain = parseResult.webDomain, diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/StructureParser.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/StructureParser.kt index 66a27435b..40ee36149 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/StructureParser.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/autofill/StructureParser.kt @@ -39,7 +39,14 @@ class StructureParser(private val structure: AssistStructure) { private var usernameIdCandidate: AutofillId? = null private var usernameValueCandidate: AutofillValue? = null - fun parse(saveValue: Boolean = false): Result? { + fun parseOrNull(saveValue: Boolean = false): Result? { + val result = parse(saveValue) + if (result != null && result.isValid()) + return result + return null + } + + fun parse(saveValue: Boolean): Result? { try { result = Result() result?.apply { @@ -59,17 +66,12 @@ class StructureParser(private val structure: AssistStructure) { // If not explicit username field found, add the field just before password field. if (usernameId == null && passwordId != null && usernameIdCandidate != null) { usernameId = usernameIdCandidate - if (allowSaveValues) { - usernameValue = usernameValueCandidate - } + usernameValue = usernameValueCandidate } } - - return if (result?.passwordId != null || result?.creditCardNumberId != null) - result - else - null + return result } catch (e: Exception) { + Log.e(TAG, "Autofill error", e) return null } } @@ -498,6 +500,10 @@ class StructureParser(private val structure: AssistStructure) { var creditCardExpirationDayId: AutofillId? = null var cardVerificationValueId: AutofillId? = null + fun isValid(): Boolean { + return passwordId != null || creditCardNumberId != null + } + fun allAutofillIds(): Array { val all = ArrayList() usernameId?.let { diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/magikeyboard/MagikeyboardService.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/magikeyboard/MagikeyboardService.kt index d149a0005..5bf459aa6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/magikeyboard/MagikeyboardService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/magikeyboard/MagikeyboardService.kt @@ -41,6 +41,10 @@ import android.widget.Toast import androidx.core.content.ContextCompat import androidx.core.graphics.BlendModeColorFilterCompat import androidx.core.graphics.BlendModeCompat +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleOwner +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ServiceLifecycleDispatcher import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.kunzisoft.keepass.R @@ -66,7 +70,9 @@ import com.kunzisoft.keepass.utils.registerLockReceiver import com.kunzisoft.keepass.utils.unregisterLockReceiver import java.util.UUID -class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionListener { +class MagikeyboardService : InputMethodService(), + KeyboardView.OnKeyboardActionListener, + LifecycleOwner { private var mDatabaseTaskProvider: DatabaseTaskProvider? = null private var mDatabase: ContextualDatabase? = null @@ -83,11 +89,18 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL private var fieldsAdapter: FieldsAdapter? = null private var playSoundDuringCLick: Boolean = false - private var mFormPackageName: String? = null - private var lockReceiver: LockReceiver? = null + private var mEntryId: UUID? = null + private var mSearchInfo: SearchInfo? = null + + private val lifecycleDispatcher = ServiceLifecycleDispatcher(this) + + override val lifecycle: Lifecycle + get() = lifecycleDispatcher.lifecycle + override fun onCreate() { + lifecycleDispatcher.onServicePreSuperOnCreate() super.onCreate() mDatabaseTaskProvider = DatabaseTaskProvider(this) @@ -113,9 +126,24 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL } } + entryUUID.observe(this) { entryId -> + mEntryId = entryId + assignKeyboardView() + } + + searchInfo.observe(this) { searchInfo -> + mSearchInfo = searchInfo + assignKeyboardView() + } + registerLockReceiver(lockReceiver, true) } + override fun onBindInput() { + lifecycleDispatcher.onServicePreSuperOnBind() + super.onBindInput() + } + override fun onCreateInputView(): View { val rootKeyboardView = layoutInflater.inflate(R.layout.keyboard_container, null) @@ -159,7 +187,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL private fun getEntryInfo(): EntryInfo? { var entryInfoRetrieved: EntryInfo? = null - entryUUID?.let { entryId -> + mEntryId?.let { entryId -> entryInfoRetrieved = mDatabase ?.getEntryById(NodeIdUUID(entryId)) ?.getEntryInfo(mDatabase) @@ -168,6 +196,13 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL } private fun assignKeyboardView() { + val searchString = mSearchInfo.toString() + if (searchString.isNotEmpty()) { + packageText?.text = searchString + packageText?.visibility = View.VISIBLE + } else { + packageText?.visibility = View.GONE + } dismissCustomKeys() if (keyboardView != null) { val entryInfo = getEntryInfo() @@ -218,12 +253,8 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL override fun onStartInputView(info: EditorInfo, restarting: Boolean) { super.onStartInputView(info, restarting) - mFormPackageName = info.packageName - if (!mFormPackageName.isNullOrEmpty()) { - packageText?.text = mFormPackageName - packageText?.visibility = View.VISIBLE - } else { - packageText?.visibility = View.GONE + mSearchInfo = searchInfo.value ?: SearchInfo().apply { + applicationId = info.packageName } assignKeyboardView() } @@ -266,13 +297,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL showKeyboardPicker() } KEY_ENTRY -> { - var searchInfo: SearchInfo? = null - if (mFormPackageName != null) { - searchInfo = SearchInfo().apply { - applicationId = mFormPackageName - } - } - actionKeyEntry(searchInfo) + actionKeyEntry(mSearchInfo) } KEY_ENTRY_ALT -> { actionKeyEntry() @@ -397,7 +422,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL } override fun onPress(primaryCode: Int) { - val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager? + val audioManager = getSystemService(AUDIO_SERVICE) as AudioManager? if (audioManager != null) when (primaryCode) { Keyboard.KEYCODE_DELETE -> keyboardView?.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS) @@ -426,6 +451,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL } override fun onDestroy() { + lifecycleDispatcher.onServicePreSuperOnDestroy() dismissCustomKeys() unregisterLockReceiver(lockReceiver) mDatabaseTaskProvider?.unregisterProgressTask() @@ -447,10 +473,11 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL const val KEY_URL = 520 const val KEY_FIELDS = 530 - private var entryUUID: UUID? = null + private val searchInfo = MutableLiveData() + private val entryUUID = MutableLiveData() private fun removeEntryInfo() { - entryUUID = null + this.entryUUID.value = null } fun removeEntry(context: Context) { @@ -458,6 +485,14 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL context.sendBroadcast(Intent(REMOVE_ENTRY_MAGIKEYBOARD_ACTION)) } + fun addSearchInfo(value: SearchInfo) { + this.searchInfo.value = value + } + + fun removeSearchInfo() { + this.searchInfo.value = null + } + fun addEntry( context: Context, entry: EntryInfo, @@ -475,7 +510,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL // Add a new entry if keyboard activated if (context.isMagikeyboardActivated()) { val entry = entryList[0] - entryUUID = entry.id + this.entryUUID.value = entry.id // Show the message if (toast) { Toast.makeText( @@ -502,7 +537,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL EntrySelectionHelper.performSelection( items = items, actionPopulateCredentialProvider = { itemFound -> - if (entryUUID != itemFound.id) { + if (this.entryUUID.value != itemFound.id) { actionPopulateKeyboard.invoke(itemFound) } else { // Force selection if magikeyboard already populated diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/viewmodel/AutofillLauncherViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/viewmodel/AutofillLauncherViewModel.kt index 0964e562f..e9aa54af2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/viewmodel/AutofillLauncherViewModel.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/viewmodel/AutofillLauncherViewModel.kt @@ -159,7 +159,7 @@ class AutofillLauncherViewModel(application: Application): CredentialLauncherVie val autofillComponent = mAutofillComponent ?: throw IOException("Autofill component is null") withContext(Dispatchers.Main) { - /* TODO Share context + /* TODO Share context #1465 MagikeyboardService.addEntries( context = getApplication(), entryList = entries, diff --git a/app/src/main/res/layout/keyboard_container.xml b/app/src/main/res/layout/keyboard_container.xml index abbfdca46..5e3195c36 100644 --- a/app/src/main/res/layout/keyboard_container.xml +++ b/app/src/main/res/layout/keyboard_container.xml @@ -28,6 +28,18 @@ android:paddingBottom="8dp" android:background="@color/grey_blue_deep" android:orientation="vertical"> + -