From 9fe4fa304708a6c5427f45880f8a3612a1fa56f7 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 4 Dec 2025 17:13:31 +0100 Subject: [PATCH] fix: Magikeyboard notification #2288 --- CHANGELOG | 1 + .../keepass/activities/EntryActivity.kt | 2 +- .../EntrySelectionLauncherActivity.kt | 2 +- .../autofill/KeeAutofillService.kt | 31 +++++ .../magikeyboard/MagikeyboardService.kt | 65 +++++++++-- .../viewmodel/AutofillLauncherViewModel.kt | 21 ++-- .../KeyboardEntryNotificationService.kt | 22 ++-- .../settings/NestedAppSettingsFragment.kt | 109 +++++------------- .../keepass/settings/PreferencesUtil.kt | 1 - .../main/res/xml/preferences_form_filling.xml | 10 +- app/src/main/res/xml/preferences_passkeys.xml | 5 + .../metadata/android/en-US/changelogs/150.txt | 3 +- .../metadata/android/fr-FR/changelogs/150.txt | 3 +- 13 files changed, 156 insertions(+), 119 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 538de1c9f..0484419bb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ KeePassDX(4.3.0) * Add Passkey User Verification #2283 * Fix autofill username detection #2276 * Fix Passkey in passwordless mode #2282 + * Fix Magikeyboard notification #2288 KeePassDX(4.2.4) * Fix remembering database location #2262 diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt index 4fdabea73..174716489 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntryActivity.kt @@ -262,7 +262,7 @@ class EntryActivity : DatabaseLockActivity() { ClipboardEntryNotificationService.checkAndLaunchNotification(this, entryInfo) // Manage entry to populate Magikeyboard and launch keyboard notification if allowed if (PreferencesUtil.isKeyboardEntrySelectionEnable(this)) { - MagikeyboardService.addEntryAndLaunchNotificationIfAllowed(this, entryInfo) + MagikeyboardService.addEntry(this, entryInfo) } } // Assign title icon diff --git a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/EntrySelectionLauncherActivity.kt b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/EntrySelectionLauncherActivity.kt index efa4d236f..13d7691be 100644 --- a/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/EntrySelectionLauncherActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/credentialprovider/activity/EntrySelectionLauncherActivity.kt @@ -67,7 +67,7 @@ class EntrySelectionLauncherActivity : DatabaseModeActivity() { when (uiState) { is EntrySelectionViewModel.UIState.Loading -> {} is EntrySelectionViewModel.UIState.PopulateKeyboard -> { - MagikeyboardService.addEntryAndLaunchNotificationIfAllowed( + MagikeyboardService.addEntry( context = this@EntrySelectionLauncherActivity, entry = uiState.entryInfo, toast = true 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 6ac09d620..852e72219 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 @@ -27,6 +27,7 @@ import android.graphics.BlendMode import android.graphics.drawable.Icon import android.os.Build import android.os.CancellationSignal +import android.provider.Settings import android.service.autofill.AutofillService import android.service.autofill.FillCallback import android.service.autofill.FillRequest @@ -38,10 +39,13 @@ import android.service.autofill.SaveInfo import android.service.autofill.SaveRequest import android.util.Log import android.view.autofill.AutofillId +import android.view.autofill.AutofillManager import android.widget.RemoteViews import androidx.annotation.RequiresApi import androidx.autofill.inline.UiVersions import androidx.autofill.inline.v1.InlineSuggestionUi +import androidx.core.content.ContextCompat +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 @@ -136,6 +140,12 @@ class KeeAutofillService : AutofillService() { database = mDatabase, searchInfo = searchInfo, onItemsFound = { openedDatabase, items -> + /* TODO Share context + MagikeyboardService.addEntries( + context = this, + entryList = items, + toast = true + )*/ callback.onSuccess( AutofillHelper.buildResponse( context = this, @@ -466,5 +476,26 @@ class KeeAutofillService : AutofillService() { } return true } + + fun Context.isKeeAutofillActivated(): Boolean { + val activated = ContextCompat.getSystemService( + this, + AutofillManager::class.java + )?.hasEnabledAutofillServices() == true + return activated + } + + fun Context.showAutofillDeviceSettings() { + try { + startActivity( + Intent(Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE).apply { + data = "package:${KeeAutofillService::class.java.canonicalName}".toUri() + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + ) + } catch (e: Exception) { + Log.e(TAG, "Unable to choose the autofill service", e) + } + } } } 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 8677698bd..d149a0005 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 @@ -20,11 +20,11 @@ package com.kunzisoft.keepass.credentialprovider.magikeyboard -import android.app.Activity import android.content.Context import android.content.Intent import android.inputmethodservice.InputMethodService import android.media.AudioManager +import android.provider.Settings import android.view.Gravity import android.view.HapticFeedbackConstants import android.view.KeyEvent @@ -32,10 +32,13 @@ import android.view.LayoutInflater import android.view.View import android.view.WindowManager import android.view.inputmethod.EditorInfo +import android.view.inputmethod.InputMethodManager import android.widget.FrameLayout import android.widget.ImageView import android.widget.PopupWindow import android.widget.TextView +import android.widget.Toast +import androidx.core.content.ContextCompat import androidx.core.graphics.BlendModeColorFilterCompat import androidx.core.graphics.BlendModeCompat import androidx.recyclerview.widget.LinearLayoutManager @@ -43,7 +46,6 @@ import androidx.recyclerview.widget.RecyclerView import com.kunzisoft.keepass.R import com.kunzisoft.keepass.adapters.FieldsAdapter import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper -import com.kunzisoft.keepass.credentialprovider.EntrySelectionHelper.removeModes import com.kunzisoft.keepass.credentialprovider.activity.EntrySelectionLauncherActivity import com.kunzisoft.keepass.database.ContextualDatabase import com.kunzisoft.keepass.database.DatabaseTaskProvider @@ -96,8 +98,8 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL } // Remove the entry and lock the keyboard when the lock signal is receive lockReceiver = LockReceiver { - removeEntryInfo() - assignKeyboardView() + removeEntryInfo() + assignKeyboardView() } lockReceiver?.backToPreviousKeyboardAction = { switchToPreviousKeyboard() @@ -351,7 +353,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL items, { // Automatically populate keyboard - addEntryAndLaunchNotificationIfAllowed( + addEntry( this, items[0], true @@ -452,14 +454,44 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL } fun removeEntry(context: Context) { + removeEntryInfo() context.sendBroadcast(Intent(REMOVE_ENTRY_MAGIKEYBOARD_ACTION)) } - fun addEntryAndLaunchNotificationIfAllowed(context: Context, entry: EntryInfo, toast: Boolean = false) { - // Add a new entry - entryUUID = entry.id + fun addEntry( + context: Context, + entry: EntryInfo, + toast: Boolean = false + ) { // Launch notification if allowed - KeyboardEntryNotificationService.launchNotificationIfAllowed(context, entry, toast) + addEntries(context, listOf(entry), toast) + } + + fun addEntries( + context: Context, + entryList: List, + toast: Boolean = false + ) { + // Add a new entry if keyboard activated + if (context.isMagikeyboardActivated()) { + val entry = entryList[0] + entryUUID = entry.id + // Show the message + if (toast) { + Toast.makeText( + context, + context.getString( + R.string.keyboard_notification_entry_content_title, + entry.getVisualTitle() + ), + Toast.LENGTH_SHORT + ).show() + } + // Launch notification if allowed + KeyboardEntryNotificationService.launchNotificationIfAllowed(context, entry) + } else { + removeEntryInfo() + } } fun performSelection( @@ -480,5 +512,20 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL actionEntrySelection = actionEntrySelection ) } + + fun Context.isMagikeyboardActivated(): Boolean { + return ContextCompat.getSystemService( + this, + InputMethodManager::class.java + )?.enabledInputMethodList?.any { inputMethod -> + inputMethod.packageName == this.packageName + } ?: false + } + + fun Context.showKeyboardDeviceSettings() { + startActivity(Intent(Settings.ACTION_INPUT_METHOD_SETTINGS).apply { + addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + }) + } } } \ No newline at end of file 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 13b109a5f..0964e562f 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 @@ -60,18 +60,18 @@ class AutofillLauncherViewModel(application: Application): CredentialLauncherVie // Retrieve selection mode when (intent.retrieveSpecialMode()) { SpecialMode.SELECTION -> { - val searchInfo = intent.retrieveSearchInfo() - if (searchInfo == null) - throw IOException("Search info is null") + val searchInfo = + intent.retrieveSearchInfo() + ?: throw IOException("Search info is null") mAutofillComponent = intent.retrieveAutofillComponent() // Build search param launchSelection(database, mAutofillComponent, searchInfo) } SpecialMode.REGISTRATION -> { // To register info - val registerInfo = intent.retrieveRegisterInfo() - if (registerInfo == null) - throw IOException("Register info is null") + val registerInfo = + intent.retrieveRegisterInfo() + ?: throw IOException("Register info is null") launchRegistration(database, registerInfo) } else -> { @@ -157,9 +157,14 @@ class AutofillLauncherViewModel(application: Application): CredentialLauncherVie throw IOException("Intent is null") val entries = intent.retrieveAndRemoveEntries(database) val autofillComponent = mAutofillComponent - if (autofillComponent == null) - throw IOException("Autofill component is null") + ?: throw IOException("Autofill component is null") withContext(Dispatchers.Main) { + /* TODO Share context + MagikeyboardService.addEntries( + context = getApplication(), + entryList = entries, + toast = true + )*/ AutofillHelper.buildResponse( context = getApplication(), autofillComponent = autofillComponent, diff --git a/app/src/main/java/com/kunzisoft/keepass/services/KeyboardEntryNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/KeyboardEntryNotificationService.kt index 9a1c555e7..0f9f316ea 100644 --- a/app/src/main/java/com/kunzisoft/keepass/services/KeyboardEntryNotificationService.kt +++ b/app/src/main/java/com/kunzisoft/keepass/services/KeyboardEntryNotificationService.kt @@ -24,10 +24,10 @@ import android.content.Context import android.content.Intent import android.os.Build import android.util.Log -import android.widget.Toast import androidx.preference.PreferenceManager import com.kunzisoft.keepass.R import com.kunzisoft.keepass.credentialprovider.magikeyboard.MagikeyboardService +import com.kunzisoft.keepass.credentialprovider.magikeyboard.MagikeyboardService.Companion.isMagikeyboardActivated import com.kunzisoft.keepass.model.EntryInfo import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.timeout.TimeoutHelper @@ -61,6 +61,9 @@ class KeyboardEntryNotificationService : LockNotificationService() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { super.onStartCommand(intent, flags, startId) + if (this.isMagikeyboardActivated().not()) + stopService() + //Get settings mNotificationTimeoutMilliSecs = PreferenceManager.getDefaultSharedPreferences(this) .getString(getString(R.string.keyboard_entry_timeout_key), @@ -150,24 +153,17 @@ class KeyboardEntryNotificationService : LockNotificationService() { private const val CHANNEL_MAGIKEYBOARD_ID = "com.kunzisoft.keepass.notification.channel.magikeyboard" - const val ENTRY_INFO_KEY = "ENTRY_INFO_KEY" - const val ACTION_CLEAN_KEYBOARD_ENTRY = "ACTION_CLEAN_KEYBOARD_ENTRY" + private const val ENTRY_INFO_KEY = "ENTRY_INFO_KEY" + private const val ACTION_CLEAN_KEYBOARD_ENTRY = "ACTION_CLEAN_KEYBOARD_ENTRY" - fun launchNotificationIfAllowed(context: Context, entry: EntryInfo, toast: Boolean) { + fun launchNotificationIfAllowed(context: Context, entry: EntryInfo) { var startService = false val intent = Intent(context, KeyboardEntryNotificationService::class.java) - if (toast) { - Toast.makeText(context, - context.getString(R.string.keyboard_notification_entry_content_title, - entry.getVisualTitle() - ), - Toast.LENGTH_SHORT).show() - } - // Show the notification if allowed in Preferences - if (PreferencesUtil.isKeyboardNotificationEntryEnable(context)) { + if (PreferencesUtil.isKeyboardNotificationEntryEnable(context) + && context.isMagikeyboardActivated()) { startService = true context.startService(intent.apply { putExtra(ENTRY_INFO_KEY, entry) diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt b/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt index 7e0df5669..66b9da1a9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt @@ -19,17 +19,11 @@ */ package com.kunzisoft.keepass.settings -import android.content.ActivityNotFoundException import android.content.Intent import android.os.Build import android.os.Bundle -import android.provider.Settings -import android.util.Log -import android.view.autofill.AutofillManager import android.widget.Toast -import androidx.annotation.RequiresApi import androidx.appcompat.app.AlertDialog -import androidx.core.net.toUri import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentActivity import androidx.preference.ListPreference @@ -42,6 +36,10 @@ import com.kunzisoft.keepass.activities.dialogs.UnavailableFeatureDialogFragment import com.kunzisoft.keepass.activities.stylish.Stylish import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction import com.kunzisoft.keepass.biometric.DeviceUnlockManager +import com.kunzisoft.keepass.credentialprovider.autofill.KeeAutofillService.Companion.isKeeAutofillActivated +import com.kunzisoft.keepass.credentialprovider.autofill.KeeAutofillService.Companion.showAutofillDeviceSettings +import com.kunzisoft.keepass.credentialprovider.magikeyboard.MagikeyboardService.Companion.isMagikeyboardActivated +import com.kunzisoft.keepass.credentialprovider.magikeyboard.MagikeyboardService.Companion.showKeyboardDeviceSettings import com.kunzisoft.keepass.education.Education import com.kunzisoft.keepass.icons.IconPackChooser import com.kunzisoft.keepass.services.ClipboardEntryNotificationService @@ -117,71 +115,21 @@ class NestedAppSettingsFragment : NestedSettingsFragment() { private fun onCreateFormFillingPreference(rootKey: String?) { setPreferencesFromResource(R.xml.preferences_form_filling, rootKey) - activity?.let { activity -> - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - - // Hide Passkeys settings if needed - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - findPreference(getString(R.string.passkeys_explanation_key)) - ?.isVisible = false - findPreference(getString(R.string.settings_passkeys_key)) - ?.isVisible = false - } - - val autoFillEnablePreference: TwoStatePreference? = findPreference(getString(R.string.settings_credential_provider_enable_key)) - activity.getSystemService(AutofillManager::class.java)?.let { autofillManager -> - if (autofillManager.hasEnabledAutofillServices()) - autoFillEnablePreference?.isChecked = autofillManager.hasEnabledAutofillServices() - - autoFillEnablePreference?.onPreferenceClickListener = - object : Preference.OnPreferenceClickListener { - @RequiresApi(api = Build.VERSION_CODES.O) - override fun onPreferenceClick(preference: Preference): Boolean { - if ((preference as TwoStatePreference).isChecked) { - try { - enableService() - } catch (e: ActivityNotFoundException) { - val error = - getString(R.string.error_autofill_enable_service) - preference.isChecked = false - Log.d(javaClass.name, error, e) - Toast.makeText(context, error, Toast.LENGTH_SHORT).show() - } - - } else { - disableService() - } - return false - } - - @RequiresApi(api = Build.VERSION_CODES.O) - private fun disableService() { - if (autofillManager.hasEnabledAutofillServices()) { - autofillManager.disableAutofillServices() - } else { - Log.d(javaClass.name, "Autofill service already disabled.") - } - } - - @RequiresApi(api = Build.VERSION_CODES.O) - @Throws(ActivityNotFoundException::class) - private fun enableService() { - if (!autofillManager.hasEnabledAutofillServices()) { - val intent = - Intent(Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE) - intent.data = - "package:com.kunzisoft.keepass.autofill.KeeAutofillService".toUri() - Log.d(javaClass.name, "Autofill enable service: intent=$intent") - startActivity(intent) - } else { - Log.d(javaClass.name, "Autofill service already enabled.") - } - } - } - } - } else { - findPreference(getString(R.string.credential_provider_key))?.isVisible = false + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // Hide Passkeys settings if needed + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + findPreference(getString(R.string.passkeys_explanation_key)) + ?.isVisible = false + findPreference(getString(R.string.settings_passkeys_key)) + ?.isVisible = false } + + findPreference(getString(R.string.settings_credential_provider_enable_key))?.setOnPreferenceClickListener { + context?.showAutofillDeviceSettings() + false + } + } else { + findPreference(getString(R.string.credential_provider_key))?.isVisible = false } findPreference(getString(R.string.magic_keyboard_explanation_key))?.setOnPreferenceClickListener { @@ -190,9 +138,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() { } findPreference(getString(R.string.magic_keyboard_key))?.setOnPreferenceClickListener { - startActivity(Intent(Settings.ACTION_INPUT_METHOD_SETTINGS).apply { - addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - }) + context?.showKeyboardDeviceSettings() false } @@ -546,14 +492,21 @@ class NestedAppSettingsFragment : NestedSettingsFragment() { override fun onResume() { super.onResume() - activity?.let { activity -> + context?.let { context -> + // Check Autofill service if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - findPreference(getString(R.string.settings_credential_provider_enable_key))?.let { autoFillEnablePreference -> - val autofillManager = activity.getSystemService(AutofillManager::class.java) - autoFillEnablePreference.isChecked = autofillManager != null - && autofillManager.hasEnabledAutofillServices() + findPreference( + getString(R.string.settings_credential_provider_enable_key) + )?.let { autoFillEnablePreference -> + autoFillEnablePreference.isChecked = context.isKeeAutofillActivated() } } + // Check Magikeyboard service + findPreference( + getString(R.string.magic_keyboard_key) + )?.let { magikeyboardEnablePreference -> + magikeyboardEnablePreference.isChecked = context.isMagikeyboardActivated() + } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt index 99df926b2..913804400 100644 --- a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt +++ b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt @@ -857,7 +857,6 @@ object PreferencesUtil { context.getString(R.string.clipboard_notifications_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.clear_clipboard_notification_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.clipboard_timeout_key) -> editor.putString(name, value.toLong().toString()) - context.getString(R.string.settings_credential_provider_enable_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.keyboard_notification_entry_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.keyboard_notification_entry_clear_close_key) -> editor.putBoolean(name, value.toBoolean()) context.getString(R.string.keyboard_entry_timeout_key) -> editor.putString(name, value.toLong().toString()) diff --git a/app/src/main/res/xml/preferences_form_filling.xml b/app/src/main/res/xml/preferences_form_filling.xml index 4738b117f..8ab97104e 100644 --- a/app/src/main/res/xml/preferences_form_filling.xml +++ b/app/src/main/res/xml/preferences_form_filling.xml @@ -26,17 +26,13 @@ android:key="@string/settings_credential_provider_enable_key" android:title="@string/set_credential_provider_service_title" android:defaultValue="@bool/settings_credential_provider_enable_default"/> - @@ -53,11 +50,12 @@ android:key="@string/magic_keyboard_explanation_key" android:icon="@drawable/prefs_info_24dp" android:summary="@string/magic_keyboard_explanation_summary"/> - diff --git a/app/src/main/res/xml/preferences_passkeys.xml b/app/src/main/res/xml/preferences_passkeys.xml index 2ae65c6f9..ea852d5b6 100644 --- a/app/src/main/res/xml/preferences_passkeys.xml +++ b/app/src/main/res/xml/preferences_passkeys.xml @@ -37,6 +37,11 @@ +