Bug 442918 - speed up transfer of messages when kdeconnect first connects, by an appropriate mechanism
Summary: speed up transfer of messages when kdeconnect first connects, by an appropria...
Status: RESOLVED FIXED
Alias: None
Product: kdeconnect
Classification: Applications
Component: messaging-application (show other bugs)
Version: unspecified
Platform: Ubuntu Linux
: NOR wishlist
Target Milestone: ---
Assignee: Simon Redman
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-09-25 01:10 UTC by robin
Modified: 2023-01-16 21:28 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description robin 2021-09-25 01:10:22 UTC
SUMMARY
Loading messages from the Android device is very slow when kdeconnect first connects to the device. Would it be possible to cache messages on the desktop and when the two devices connect, only update those which have changed or are new? I'm not a prolific user of sms, I have maybe 200 messages on my device from the past year. I'm sure there are people with 1000s of messages, this would be frustrating to wait for.

STEPS TO REPRODUCE
1. Connect Android device to a device with kdeconnect installed
2. Wait for messages to be transferred from the former to the latter
3. Wait a bit longer

OBSERVED RESULT
Messages take a surprising amount of time to synchronise.


EXPECTED RESULT
Messages synchronise more quickly, making use of caching on the desktop side.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: 20.12.3
(available in About System)
KDE Plasma Version: no idea
KDE Frameworks Version: no idea
Qt Version: 5.15

ADDITIONAL INFORMATION
Comment 1 Simon Redman 2021-09-25 01:50:46 UTC
Hi,

Thanks for taking the time to make this suggestion.

First, a small bite of background. When you first open the app, it does not load every message, it only loads the most-recent one for each conversation. When you open a conversation, it loads more messages for that conversation.

The heart of the issue is that the current implementation for collecting the list of conversations on the Android side is (very) slow. When I first wrote the interface, using standard Android SDK calls, it loaded very quickly (~few seconds for my hundreds of conversations). However, it turns out that a vast number of Android manufacturers (Samsung, Huawei... See here: https://bugs.kde.org/show_bug.cgi?id=401677) are using non-standard SMS/MMS implementations.

Given that those non-standard implementations are not documented (and even if they were, there's no way I can support multiple "families" of devices), I chose a "hack" solution where, instead of using the slick, fast interface that Android provides for exactly this purpose, it opens the SMS database and then the MMS database _for each conversation_ and then returns the most-recent one. That's a lot of connections and is pretty much the exact opposite of what any database expert would tell you is optimal.

Now, to your point. Yes, the desktop could cache conversations and present the cache to users. However (without any other changes to the Android implementation), it would take the same amount of time to update that cache as it does today to load messages, meaning the user might happily spend quite awhile responding to what they thought was the most-recent message, only to discover that they've just replied to an old/ancient message.

In my mind, a better fix would be one (or both) of:
- Re-implement my original conversation-getting scheme with some mechanism of failing over to the safe-but-slow implementation we have today. Users of non-standard-Android phones would still be stuck with this slow interface, but at least everyone else wouldn't suffer for their choices.
- Sort conversations by most-recently-replied, send them back in that order, streaming those conversations rather than waiting for all of the conversations to be collected. It would still take a long time to get all the conversations, but most likely a user cares most about the top handful of conversations.

All that is needed is a developer to volunteer their time to implement one of these solutions. I am close to being annoyed enough with the ~20s wait to do something, but it could be a awhile still before I am annoyed enough and have time enough to do the job :)

(As an aside, I don't like dealing with caches. Defining the phone as the only source of truth simplifies the entire design significantly!)
Comment 2 robin 2021-09-25 03:10:33 UTC
Wow, thanks so much for such a comprehensive reply Simon, that's very helpful. It seems the more I get involved with dev on Android, the more I realise it's an endless series of exceptions, everyone has their own special way of doing a thing, with little advantage gained to the wider ecosystem/other developers/users.

The database access method does indeed sound screwy, not at all efficient or good practice.

> it would take the same amount of time to update that cache as it does today to load messages,

I assume there is no metadata stored anywhere to say "this message/conversation has changed since it was last synced"?. I'm thinking of the model which "unison" uses to store sync information.

Your suggestion of "try the best method, fail to the lowest-common denominator if necessary" sounds like a good step forward, I'd love to volunteer my time, but it's a bit beyond my skills I'm afraid.
Comment 3 robin 2021-09-27 08:48:48 UTC
Also, it sounds like you might be about to volunteer to write a standards-compliant SMS app, to get around the problem. ;-)

Or at least recommend something which follows the appropriate spec.
Comment 4 Simon Redman 2023-01-16 21:25:11 UTC
I forgot about this bug. However, several months ago I implemented this: https://invent.kde.org/network/kdeconnect-android/-/merge_requests/296. It should be in the latest (early January 2023) KDE Connect Android release and does not require any change on the desktop side.

Conversations are now returned one-by-one, with more-recent conversations returned first. This means the GUI starts being populated nearly instantly, and presumably the more-useful conversations get loaded first since those are likely the most-recent.

There is still plenty of room to improve in this space, but I am going to consider this bug resolved.
Comment 5 Simon Redman 2023-01-16 21:28:33 UTC
(In reply to robin from comment #3)
> Also, it sounds like you might be about to volunteer to write a
> standards-compliant SMS app, to get around the problem. ;-)
> 
> Or at least recommend something which follows the appropriate spec.

Annoyingly, the issue is with non-standard _Android_ implementations. If everyone ran ASOP, there would be no problem. The SMS app which sits on top just uses the interfaces Android provides. There are SMS apps which do everything by themselves (Signal, for example), but that's a special case.