Skip to content

Refactor data layer: extract data models, add repository pattern, fix null safety and lifecycle leaks#212

Draft
Copilot wants to merge 3 commits intokt-rewritefrom
copilot/refactor-accessibility-service
Draft

Refactor data layer: extract data models, add repository pattern, fix null safety and lifecycle leaks#212
Copilot wants to merge 3 commits intokt-rewritefrom
copilot/refactor-accessibility-service

Conversation

Copy link

Copilot AI commented Feb 16, 2026

Refactors the core system services and data/usage tracking layer to improve readability, modularity, and correctness. Fixes memory leaks from unregistered receivers and null-unsafe UsageStatsManager access.

Service layer refactoring

  • Naming: Renamed generic variables to describe Android system semantics — lastPackagelastForegroundPackage, lastBackPressTimeStamplastGlobalActionTimestamp, handler/updateRunnablerecheckHandler/recheckRunnable, KbIgnoredAppskeywordBlockerIgnoredApps, checkIfUserGettingFreaky()detectBlockedKeywords(), cheatMinutesEndTImecheatMinutesEndTime
  • Event handling modularity: Extracted handleContentChanged(), handleViewScrolled(), handleYouTubeScroll() from monolithic onAccessibilityEvent in UsageTrackingService; extracted applyGrayscaleForPackage() in GeneralFeaturesService
  • Lifecycle leak fixes:
Service Fix
GeneralFeaturesService Added missing onDestroy()refreshReceiver was never unregistered
UsageTrackingService Added unregisterReceiver(refreshReceiver) + removeOverlay() in onDestroy()
AppBlockerService Added recheckRunnable cancellation in onDestroy()

Data layer refactoring

  • Model extraction: Moved AllAppsUsageFragment.Statdata/models/AppUsageStat.kt to decouple data from UI
  • Repository pattern: Added UsageStatsRepository interface + UsageStatsRepositoryImpl with Dispatchers.IO and Result wrapping via runCatching
interface UsageStatsRepository {
    suspend fun getStatsByDay(date: LocalDate): Result<List<AppUsageStat>>
    // ...
}
  • Null safety: UsageStatsManager cast changed from as to as?; null-checks added for runningAppProcesses and queryEvents() returns
  • Extension functions: Added Long.toFormattedTime() and Long.toFormattedTimeCompact() in TimeTools.kt
  • Updated all consumers (AllAppsUsageFragment, AppUsageBreakdown, ScreentimeWidgetProvider, AppBlocker) to use AppUsageStat

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dl.google.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -Xmx2048m -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.10.2-bin/a04bxjujx95o3nb99gddekhwo/gradle-8.10.2/lib/gradle-daemon-main-8.10.2.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.10.2-bin/a04bxjujx95o3nb99gddekhwo/gradle-8.10.2/lib/agents/gradle-instrumentation-agent-8.10.2.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.10.2 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…issues in services

- Rename lastBackPressTimeStamp → lastGlobalActionTimestamp (BaseBlockingService)
- Rename lastPackage → lastForegroundPackage (AppBlockerService)
- Rename lastPackageName → lastForegroundPackage (GeneralFeaturesService)
- Rename handler/updateRunnable → recheckHandler/recheckRunnable (AppBlockerService)
- Rename handler/updateRunnable → timeTrackingHandler/timeTrackingRunnable (UsageTrackingService)
- Rename userYSwipeEventCounter → scrollEventCounter (UsageTrackingService)
- Rename accumulatedTime → accumulatedScreenTime (UsageTrackingService)
- Rename lastEventTimeStamp → lastContentChangeTimestamp (ViewBlockerService, KeywordBlockerService, UsageTrackingService)
- Rename refreshCooldown → eventThrottleDelayMs (KeywordBlockerService)
- Rename KbIgnoredApps → keywordBlockerIgnoredApps (KeywordBlockerService)
- Rename rootnode → rootNode (KeywordBlockerService)
- Rename checkIfUserGettingFreaky → detectBlockedKeywords (KeywordBlocker)
- Rename cheatMinutesEndTIme → cheatMinutesEndTime (ViewBlocker)
- Fix var → val for displayMetrics (ViewBlockerService)
- Extract applyGrayscaleForPackage method (GeneralFeaturesService)
- Extract handleContentChanged, handleViewScrolled, handleYouTubeScroll methods (UsageTrackingService)
- Remove unused supportsViewScrolled field (UsageTrackingService)
- Fix memory leak: add missing unregisterReceiver(refreshReceiver) in UsageTrackingService.onDestroy
- Fix memory leak: add missing onDestroy with unregisterReceiver in GeneralFeaturesService
- Fix memory leak: clean up recheckHandler in AppBlockerService.onDestroy
- Fix resource leak: add overlay cleanup in UsageTrackingService.onDestroy

Co-authored-by: nethical6 <79095297+nethical6@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor core system services for improved readability Refactor AccessibilityService implementations: naming, modularity, lifecycle leak fixes Feb 16, 2026
Copilot AI requested a review from nethical6 February 16, 2026 17:46
…tory, improve null safety

- Create AppUsageStat data class in data/models/ to decouple from UI layer
- Create UsageStatsRepository interface with suspend functions and Result wrapping
- Create UsageStatsRepositoryImpl with Dispatchers.IO threading via coroutines
- Add Long.toFormattedTime() and Long.toFormattedTimeCompact() extension functions
- Update UsageStatsHelper to use AppUsageStat, add null safety for UsageStatsManager/appProcesses
- Update AllAppsUsageFragment, AppUsageBreakdown, ScreentimeWidgetProvider to use AppUsageStat
- Deprecate AllAppsUsageFragment.Stat with migration guidance

Co-authored-by: nethical6 <79095297+nethical6@users.noreply.github.com>
Copilot AI changed the title Refactor AccessibilityService implementations: naming, modularity, lifecycle leak fixes Refactor data layer: extract data models, add repository pattern, fix null safety and lifecycle leaks Feb 17, 2026
}

fun checkIfUserGettingFreaky(
fun detectBlockedKeywords(

Choose a reason for hiding this comment

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

Heartbreaking.

Copy link
Owner

Choose a reason for hiding this comment

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

indeed 🥀

Choose a reason for hiding this comment

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

fuck no copilot

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.

4 participants