Skip to content

Fix live location sharing when several accounts are used - #8312

Draft
bmarty wants to merge 8 commits into
developfrom
feature/bma/liveLocationMultiAccount
Draft

bmarty wants to merge 8 commits into
developfrom
feature/bma/liveLocationMultiAccount

Conversation

@bmarty

@bmarty bmarty commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Content

Live location sharing is now owned by a single app-scoped LiveLocationShareEngine, which keeps every share started on this device, keyed by session and room. ActiveLiveLocationShareManager, used by the presenters, is now implemented by a new, stateless DefaultActiveLiveLocationShareManager: a session-scoped view of the engine for one session.

The engine:

  • starts the live location foreground service when the first share starts, and stops it when the last one ends, whichever accounts own the shares;
  • throttles the location updates and sends them to the shares of every session;
  • binds the resources of a session (beacon updates listener, expiry timeouts) to the scope of its MatrixClient;
  • restores the persisted shares of every session when the app starts (AppStartupHook) and when the system restarts the sticky service;
  • forgets the shares and persisted data of a session when it is deleted.

The previous DefaultActiveLiveLocationShareManager, which held the state, is renamed to LiveLocationShareEngine and changed in place. The new DefaultActiveLiveLocationShareManager reuses its name, as required by the Konsist naming rule for @ContributesBinding classes, but only forwards to the engine. LiveLocationSharingCoordinator and LiveLocationReceiver are removed, and the engine takes over their role. setup() is removed from ActiveLiveLocationShareManager and from LoggedInFlowNode. The persisted expiries move to an app-scoped LiveLocationExpiryStore, which uses the same DataStore files, so no migration is needed.

Reviewing: please review commit by commit. The second commit is a pure rename, so the third one shows how the manager logic evolved into the engine. The last commit only fixes detekt and Konsist issues. In the whole-branch diff, the engine appears as a new file. This is one PR with several commits rather than several PRs, which is why it goes over the 500-line guideline for production code (about +615/−447).

Motivation and context

The manager was @SingleIn(SessionScope). The session graph is created by the UI and rebuilt each time the user switches account, but a live location share has to outlive the UI and the account on screen. Consequences (the first one is confirmed on a real device):

  1. Stale manager. Account A shares in R1. The user switches to B, back to A, and A shares in R3. The previous manager of A is still running. When R1 stops, by hand or on expiry, it unregisters A from the coordinator, which removes the new manager: R3 stops receiving locations, and if B is not sharing, the service stops. Each switch also leaked one beacon listener and one SessionListener.
  2. Recovery depended on the UI. Persisted shares were only restored for the account on screen.
  3. Service left running after process death. The sticky service was restarted with no receiver, so GPS and the notification kept running and nothing was sent.
  4. Late echo. The late "not live" echo of a stopped beacon could cancel the new share in the same room. A share is now only dropped by an update for its own beacon.

Screenshots / GIFs

No UI change.

Tests

Unit tests: LiveLocationShareEngineTest (20 tests, covering two accounts sharing at once, rooms not affecting each other, remote stop and replacement, expiry, restore, session deletion) and LiveLocationExpiryStoreTest.

Manual, with two accounts A and B:

  • A shares in R1. Switch to B, and B shares in R2. Switch back to A, and A shares in R3. Stop R1: R2 and R3 keep updating, and the notification stays.
  • With A sharing, display B and kill the process (adb shell am kill). Reopen the app: A's share resumes without opening A.
  • Let all the shares expire: the notification disappears.

Tested devices

  • Physical
  • Emulator
  • OS version(s):

Checklist

  • I am aware of the etiquette.
  • This PR was made with the help of AI:
    • Yes. In this case, please get the PR reviewed by an AI (e.g. a GitHub AI reviewer, or /code-review run in a fresh session) before requesting a human review.
    • No.
  • Changes have been tested on an Android device or Android emulator with API 24
  • UI change has been tested on both light and dark themes
  • Accessibility has been taken into account. See https://github.com/element-hq/element-x-android/blob/develop/CONTRIBUTING.md#accessibility
  • Pull request is based on the develop branch
  • Pull request title will be used in the release note, it clearly defines what will change for the user
  • Pull request includes screenshots or videos if containing UI changes
  • You've made a self review of your PR

🤖 Generated with Claude Code

bmarty and others added 5 commits November 30, 2026 15:59
The expiries of the live location shares are needed independently of the
session displayed in the UI, to resume or stop the shares of every session.
Extract them from the session-scoped LiveLocationStore into the app-scoped
LiveLocationExpiryStore. Both stores use the same DataStore file, so no
migration is needed. LiveLocationStore only keeps the disclaimer flag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pure rename, no behavior change. The next commit turns this class into the
app-scoped engine owning the live location shares of every session; renaming
it first lets the diff of that commit show how the logic evolves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ession

The live location manager was session-scoped, but the session graph is
recreated when switching account: the previous manager of the session kept
running, and could unregister the new one from the coordinator, so the other
shares of the session stopped receiving locations.

LiveLocationShareEngine now lives as long as the application and keeps all the
shares started on this device, keyed by session and room. It also takes over
the role of LiveLocationSharingCoordinator: it starts the live location service
when the first share starts and stops it when the last one ends, throttles the
location updates and sends them to every share. LiveLocationSharingCoordinator
and LiveLocationReceiver are removed.

The resources of a session (beacon updates listener, timeouts) are bound to the
scope of its MatrixClient, so they are released with the client. A share is
only dropped by a beacon update matching its beacon, so the late echo of a
stopped beacon cannot stop another share.

ActiveLiveLocationShareManager is now a stateless view of the engine for a
session (SessionActiveLiveLocationShareManager). Remove setup() from the API,
the engine needs no initialisation from the UI.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The persisted shares were only restored when the UI of their session was
displayed, so the shares of the other accounts were not resumed after the
process had been killed.

Restore the shares of every session when the app starts, using an
AppStartupHook, and when the system restarts the sticky live location service.
The service now stops itself if there is nothing to share, instead of keeping
the location updates and the notification running without any share.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The engine is app-scoped, so it registers a single SessionListener. When a
session is deleted, release its resources and forget its shares and persisted
data. The shares cannot be stopped on the server anymore, the session is gone.

Also guard the restoration against a failure to read the sessions, and
complete the documentation of ActiveShare.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bmarty bmarty added the PR-Bugfix For bug fix label Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

📱 Scan the QR code below to install the build (arm64 only) for this PR.
QR code
If you can't scan the QR code you can install the build via this link: https://i.diawi.com/fdn2Ef

@codecov

codecov Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 62 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.05%. Comparing base (f5d9da2) to head (84bcc29).
⚠️ Report is 45 commits behind head on develop.

Files with missing lines Patch % Lines
...ures/location/impl/live/LiveLocationShareEngine.kt 78.12% 20 Missing and 22 partials ⚠️
...on/impl/live/service/LiveLocationSharingService.kt 0.00% 10 Missing ⚠️
...impl/live/DefaultActiveLiveLocationShareManager.kt 0.00% 6 Missing ⚠️
...cation/impl/live/LiveLocationRestoreStartupHook.kt 0.00% 3 Missing ⚠️
...ures/location/impl/live/LiveLocationExpiryStore.kt 97.14% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #8312      +/-   ##
===========================================
+ Coverage    81.03%   81.05%   +0.01%     
===========================================
  Files         2903     2905       +2     
  Lines        87386    87427      +41     
  Branches     11891    11899       +8     
===========================================
+ Hits         70814    70862      +48     
+ Misses       11930    11925       -5     
+ Partials      4642     4640       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

bmarty and others added 3 commits December 1, 2026 11:51
Rename SessionActiveLiveLocationShareManager to
DefaultActiveLiveLocationShareManager, to follow the naming convention of the
classes contributing a binding, and make ActiveShare a data class.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… restored

The live location engine can restore a session at startup before RootFlowNode
restores the saved state. restoreWithSavedState then returned early because the
cache was not empty, and the other saved sessions were missing when the node
graph was restored. Restore every saved session which is not in the cache yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fix races between restoring, starting and stopping shares, keep shares which
may still be live on the server, detect remote stops, and avoid crashing the
service when it cannot enter the foreground. Start the service directly when
sharing starts instead of relying on isSharing observers, and only stop the
shares once when errors are reported concurrently.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant