Skip to content

Add sort by most recent tracker detection#537

Merged
kasnder merged 3 commits intoTrackerControl:masterfrom
nmarteybotchway:feature/recent-tracker-sort
Apr 4, 2026
Merged

Add sort by most recent tracker detection#537
kasnder merged 3 commits intoTrackerControl:masterfrom
nmarteybotchway:feature/recent-tracker-sort

Conversation

@nmarteybotchway
Copy link
Copy Markdown
Contributor

Closes #504

Summary

Adds a new sort option to the main app list that orders apps by the most
recent tracker detection, with the most recently active at the top.

Changes

  • TrackerList.java: Added getLastTrackerTimes() which iterates through
    recorded host contacts, filters to known trackers only via findTracker(),
    and returns the most recent tracker contact timestamp per app UID
  • Rule.java: Added trackerRecent map, getLastTrackerTime() getter,
    and sort comparator case for trackers_recent
  • res/menu/main.xml: Added new menu item for the sort option
  • res/values/strings.xml: Added string resource menu_sort_trackers_recent
  • ActivityMain.java: Added menu selection handling in onOptionsItemSelected
    and checked state in onPrepareOptionsMenu

Approach

Rather than adding a new database query, this reuses the existing getHosts()
cursor which already returns the time column, following the same pattern as
getTrackerCountsAndTotal(). Only hostnames identified as trackers by
findTracker() are considered, so the sort reflects genuine tracker
detections rather than general network activity.

Testing

  • Verified sort order updates correctly when switching to new option
  • Verified selection persists across app restarts via SharedPreferences
  • Verified apps with no tracker detections sort to the bottom

@nmarteybotchway nmarteybotchway marked this pull request as draft April 2, 2026 12:54
@nmarteybotchway nmarteybotchway marked this pull request as ready for review April 2, 2026 12:54
@nmarteybotchway nmarteybotchway marked this pull request as draft April 2, 2026 12:54
@nmarteybotchway nmarteybotchway marked this pull request as ready for review April 2, 2026 12:55
@kasnder kasnder requested a review from Copilot April 2, 2026 22:30
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new sort option to the main app list to order apps by the most recent tracker detection (most recently active first), addressing issue #504.

Changes:

  • Added a new “Sort by latest detected tracker” menu option and wiring in ActivityMain.
  • Added logic to compute “last tracker detection time per UID” in TrackerList.
  • Added storage/accessor for recent tracker timestamps and a new sort comparator in Rule.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
app/src/main/res/values/strings.xml Adds the new menu title string for the recent-tracker sort option.
app/src/main/res/menu/main.xml Adds a new menu item under the sort menu group.
app/src/main/java/net/kollnig/missioncontrol/data/TrackerList.java Introduces getLastTrackerTimes() to compute most recent tracker contact per UID.
app/src/main/java/eu/faircode/netguard/Rule.java Stores recent tracker timestamps and adds comparator branch for the new sort.
app/src/main/java/eu/faircode/netguard/ActivityMain.java Persists the new sort selection and reflects checked state in the menu.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +512 to +514
else if ("menu_sort_by_latest_detected_tracker".equals(sort))
Collections.sort(listRules, (a, b) ->
Long.compare(b.getLastTrackerTime(), a.getLastTrackerTime()));
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sort key checked here doesn't match the value written to SharedPreferences. ActivityMain stores sort = "trackers_recent", but this branch checks for "menu_sort_by_latest_detected_tracker", so the new sort option will never be applied. Update this condition (and ideally centralize/constant-ize sort keys) so the comparator runs when sort is "trackers_recent". Also consider adding a deterministic tie-breaker (e.g., by name/package) when timestamps are equal to avoid UI jitter.

Copilot uses AI. Check for mistakes.
kasnder pushed a commit that referenced this pull request Apr 2, 2026
Implements feature from PR #537 (issue #504) with a bug fix:
the sort comparator in Rule.java incorrectly checked for
"menu_sort_by_latest_detected_tracker" instead of "trackers_recent",
which would have prevented the sort from ever activating.

https://claude.ai/code/session_01PUZFM4E3HjpFRAqcTDfEtU
@kasnder
Copy link
Copy Markdown
Member

kasnder commented Apr 2, 2026

@nmarteybotchway thanks for the PR. can you fix the above issue? "menu_sort_by_latest_detected_tracker" → "trackers_recent"

@kasnder
Copy link
Copy Markdown
Member

kasnder commented Apr 2, 2026

love this new feature. it's great. thanks!

@kasnder kasnder assigned kasnder and unassigned kasnder Apr 2, 2026
@kasnder
Copy link
Copy Markdown
Member

kasnder commented Apr 2, 2026

But to be honest, the best way to solve this would probably me to migrate to some timeline-like interface eventually.

…fix tiebreaking so that it is done in alphabetical order if both apps have the same last tracker detected time
@nmarteybotchway
Copy link
Copy Markdown
Contributor Author

nmarteybotchway commented Apr 2, 2026

Agreed - a timeline UI would be more intuitive. For now I’ve fixed the sort key and added a deterministic tie-breaker (alphabetical).

@kasnder
Copy link
Copy Markdown
Member

kasnder commented Apr 4, 2026

I think should still add this feature. It's very good @nmarteybotchway

@kasnder kasnder reopened this Apr 4, 2026
Copy link
Copy Markdown
Member

@kasnder kasnder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd improve this slightly for readability, otherwise I think we should merge.

int cmp = Long.compare(b.getLastTrackerTime(), a.getLastTrackerTime());
if (cmp != 0) return cmp;
int i = collator.compare(a.name, b.name);
return (i == 0 ? a.packageName.compareTo(b.packageName) : i);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code changes here are very dense. Not so easy to read in my view.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kasnder I've improved the readability of this section and added some comments so it is similar to the other comparison branches - hope it is better now. Thanks for the feedback

@kasnder kasnder merged commit 136e2e6 into TrackerControl:master Apr 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recent sort option

3 participants