fix: Magikeyboard notification #2288

This commit is contained in:
J-Jamet committed 2025-12-04 17:13:31 +01:00
1 parent c88413f7f7
commit 9fe4fa3047
13 files changed
+156 -119

No files matched your search

@@ -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
@@ -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
@@ -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)
}
}
}
}
@@ -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<EntryInfo>,
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)
})
}
}
}
@@ -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,
@@ -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)
@@ -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<Preference>(getString(R.string.passkeys_explanation_key))
?.isVisible = false
findPreference<Preference>(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<Preference>(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<Preference>(getString(R.string.passkeys_explanation_key))
?.isVisible = false
findPreference<Preference>(getString(R.string.settings_passkeys_key))
?.isVisible = false
}
findPreference<TwoStatePreference>(getString(R.string.settings_credential_provider_enable_key))?.setOnPreferenceClickListener {
context?.showAutofillDeviceSettings()
false
}
} else {
findPreference<Preference>(getString(R.string.credential_provider_key))?.isVisible = false
}
findPreference<Preference>(getString(R.string.magic_keyboard_explanation_key))?.setOnPreferenceClickListener {
@@ -190,9 +138,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
}
findPreference<Preference>(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<TwoStatePreference?>(getString(R.string.settings_credential_provider_enable_key))?.let { autoFillEnablePreference ->
val autofillManager = activity.getSystemService(AutofillManager::class.java)
autoFillEnablePreference.isChecked = autofillManager != null
&& autofillManager.hasEnabledAutofillServices()
findPreference<TwoStatePreference?>(
getString(R.string.settings_credential_provider_enable_key)
)?.let { autoFillEnablePreference ->
autoFillEnablePreference.isChecked = context.isKeeAutofillActivated()
}
}
// Check Magikeyboard service
findPreference<TwoStatePreference?>(
getString(R.string.magic_keyboard_key)
)?.let { magikeyboardEnablePreference ->
magikeyboardEnablePreference.isChecked = context.isMagikeyboardActivated()
}
}
}
@@ -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())