diff --git a/src/org/kde/kdeconnect/DeviceInfo.kt b/src/org/kde/kdeconnect/DeviceInfo.kt index 2415cde5..ceb3d291 100644 --- a/src/org/kde/kdeconnect/DeviceInfo.kt +++ b/src/org/kde/kdeconnect/DeviceInfo.kt @@ -94,7 +94,7 @@ class DeviceInfo( with(identityPacket) { DeviceInfo( id = getString("deviceId"), // Redundant: We could read this from the certificate instead - name = DeviceHelper.filterName(getString("deviceName", "unknown")), + name = DeviceHelper.filterInvalidCharactersFromDeviceNameAndLimitLength(getString("deviceName", "unknown")), type = DeviceType.fromString(getString("deviceType", "desktop")), certificate = certificate, protocolVersion = getInt("protocolVersion"), @@ -106,7 +106,7 @@ class DeviceInfo( @JvmStatic fun isValidIdentityPacket(identityPacket: NetworkPacket): Boolean = with(identityPacket) { type == NetworkPacket.PACKET_TYPE_IDENTITY && - DeviceHelper.filterName(getString("deviceName", "")).isNotBlank() && + DeviceHelper.filterInvalidCharactersFromDeviceNameAndLimitLength(getString("deviceName", "")).isNotBlank() && isValidDeviceId(getString("deviceId", "")) } diff --git a/src/org/kde/kdeconnect/Helpers/DeviceHelper.kt b/src/org/kde/kdeconnect/Helpers/DeviceHelper.kt index 8c5079c3..96de01dc 100644 --- a/src/org/kde/kdeconnect/Helpers/DeviceHelper.kt +++ b/src/org/kde/kdeconnect/Helpers/DeviceHelper.kt @@ -122,7 +122,7 @@ object DeviceHelper { } fun setDeviceName(context: Context, name: String) { - val filteredName = filterName(name) + val filteredName = filterInvalidCharactersFromDeviceNameAndLimitLength(name) val preferences = PreferenceManager.getDefaultSharedPreferences(context) preferences.edit { putString(KEY_DEVICE_NAME_PREFERENCE, filteredName) } } @@ -157,5 +157,9 @@ object DeviceHelper { } @JvmStatic - fun filterName(input: String): String = input.replace(NAME_INVALID_CHARACTERS_REGEX, "").trim().take(MAX_DEVICE_NAME_LENGTH) + fun filterInvalidCharactersFromDeviceNameAndLimitLength(input: String): String = filterInvalidCharactersFromDeviceName(input).trim().take(MAX_DEVICE_NAME_LENGTH) + + @JvmStatic + fun filterInvalidCharactersFromDeviceName(input: String): String = input.replace(NAME_INVALID_CHARACTERS_REGEX, "") + } diff --git a/src/org/kde/kdeconnect/UserInterface/SettingsFragment.kt b/src/org/kde/kdeconnect/UserInterface/SettingsFragment.kt index f7de7345..fad49a61 100644 --- a/src/org/kde/kdeconnect/UserInterface/SettingsFragment.kt +++ b/src/org/kde/kdeconnect/UserInterface/SettingsFragment.kt @@ -37,7 +37,7 @@ import org.kde.kdeconnect.BackgroundService import org.kde.kdeconnect.Helpers.CreateFileParams import org.kde.kdeconnect.Helpers.CreateFileResultContract import org.kde.kdeconnect.Helpers.DeviceHelper -import org.kde.kdeconnect.Helpers.DeviceHelper.filterName +import org.kde.kdeconnect.Helpers.DeviceHelper.filterInvalidCharactersFromDeviceName import org.kde.kdeconnect.Helpers.DeviceHelper.getDeviceName import org.kde.kdeconnect.Helpers.NotificationHelper import org.kde.kdeconnect.UserInterface.ThemeUtil.applyTheme @@ -83,7 +83,9 @@ class SettingsFragment : PreferenceFragmentCompat() { setOnBindEditTextListener(EditText::setSingleLine) setOnBindEditTextListener { editText: EditText -> editText.filters = arrayOf( - InputFilter { source: CharSequence, start: Int, end: Int, _: Spanned?, _: Int, _: Int -> filterName(source.subSequence(start, end).toString()) }, + InputFilter { source: CharSequence, start: Int, end: Int, _: Spanned?, _: Int, _: Int -> + filterInvalidCharactersFromDeviceName(source.subSequence(start, end).toString()) + }, LengthFilter(DeviceHelper.MAX_DEVICE_NAME_LENGTH), ) }