DEV Community

Ujjaval Saini
Ujjaval Saini

Posted on

How I Built Reel Counter: Engineering an Accessibility-Driven Short-Form Video Tracker & Blocker in Android (Kotlin + Jetpack Compose)

How I Built Reel Counter: Engineering an Accessibility-Driven Short-Form Video Tracker & Blocker in Android

Author: Ujjaval Saini (Dev Ujvl)

Android Developer & Software Engineer


We have all been there: you open Instagram or YouTube just to reply to a single message, and before you know it, an hour has vanished down the infinite-scroll rabbit hole of algorithmic Reels and Shorts.

Most screen-time apps only track total application time (e.g., "You spent 45 minutes on Instagram"), but they fail to distinguish between productive usage (chatting, searching, creator work) and mindless vertical video scrolling.

To solve this, I designed and developed Reel Counter — a modern digital wellbeing Android application built with Kotlin and Jetpack Compose that specifically tracks, alerts, and helps users limit short-form video consumption across Instagram Reels, YouTube Shorts, Facebook Reels, and Snapchat Spotlight.

In this deep dive, I’ll walk through the architectural decisions, technical challenges, and engineering patterns behind Reel Counter.


1. The Core Engineering Challenge: Detecting Reels in Real Time

Android's standard UsageStatsManager provides coarse-grained foreground package data, but it cannot see what an application is displaying on screen. To detect when a user scrolls into a Reel or Short, we needed a low-latency, privacy-first mechanism.

The Solution: Android Accessibility Service

By creating a custom AccessibilityService, Reel Counter listens to window content state changes:

class ReelDetectionService : AccessibilityService() {

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event == null) return

        when (event.eventType) {
            AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED,
            AccessibilityEvent.TYPE_VIEW_SCROLLED -> {
                inspectNodeHierarchy(rootInActiveWindow, event.packageName?.toString())
            }
        }
    }

    private fun inspectNodeHierarchy(root: AccessibilityNodeInfo?, packageName: String?) {
        if (root == null || packageName == null) return

        val isReelActive = when (packageName) {
            "com.instagram.android" -> detectInstagramReels(root)
            "com.google.android.youtube" -> detectYouTubeShorts(root)
            else -> false
        }

        if (isReelActive) {
            ReelTrackerEngine.onReelEngaged(packageName)
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Privacy & Efficiency Constraints

  • Zero Content Scraping: The inspector only looks for specific view hierarchy layout IDs and accessibility role indicators. No personal chats, captions, or account credentials are ever parsed or stored.
  • Battery Preservation: Traversal is bounded to a maximum tree depth to prevent CPU spikes and thermal throttling during fast gestures.

2. Floating Interactive HUD Overlay

One of Reel Counter's standout features is the non-intrusive floating pill that appears directly over the active social app, giving users real-time feedback on their daily scroll count.

Rendering Jetpack Compose Inside a WindowManager Service

Traditionally, Android floating overlays relied on legacy XML inflated views. Reel Counter renders declarative Jetpack Compose directly inside a background Service using ComposeView and custom lifecycle owners:

class FloatingOverlayService : Service() {

    private lateinit var windowManager: WindowManager
    private var overlayComposeView: ComposeView? = null

    override fun onCreate() {
        super.onCreate()
        windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
        createComposeOverlay()
    }

    private fun createComposeOverlay() {
        val params = WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            else
                WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            PixelFormat.TRANSLUCENT
        ).apply {
            gravity = Gravity.TOP or Gravity.END
            x = 32
            y = 120
        }

        overlayComposeView = ComposeView(this).apply {
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnDetachedFromWindow)
            setContent {
                ReelCounterFloatingPill(
                    count = liveReelCount.value,
                    dailyLimit = dailyLimit.value,
                    onDismiss = { stopSelf() }
                )
            }
        }

        windowManager.addView(overlayComposeView, params)
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Architecture & Data Flow (Clean MVVM + StateFlow)

To keep the application responsive and maintainable, Reel Counter uses Clean Architecture with unidirectional data flow:

[Accessibility Engine]
│
▼
[Repository Layer] ────► [Local Room Database (Offline First)]
│
▼
[ViewModel StateFlow] ──► [Jetpack Compose UI (Dashboard + Overlay)]
│
▼
[Cloud Firestore] ────► [Friends Battle & Global Leaderboard]

Key Technical Stack:
Language: Kotlin 2.0 (Coroutines & StateFlow)
UI Framework: Jetpack Compose (Material 3 with custom dark theme)
Persistence: Room Database (100% offline-first local cache)
Cloud Backend: Firebase Auth & Cloud Firestore (for sync and friend challenges)
Dependency Injection: Modern architectural scoping


4. Key Takeaways from Shipping Reel Counter

  1. Accessibility Services Require Extreme Care: Memory leaks or heavy recursive node operations will degrade the entire Android OS responsiveness. Benchmarking with Android Profiler was essential.
  2. Positive Friction Works: Instead of hard-locking users out abruptly, giving visual cues via the floating counter helps users build mindful awareness.
  3. Offline-First is Non-Negotiable: A utility app must function seamlessly on airplane mode. Cloud sync should always be secondary to instant on-device speed.

About the Author

I am Ujjaval Saini (known online as Dev Ujvl), an Android Developer and software engineer based in Noida, India. I specialize in building native Android apps with Kotlin, Jetpack Compose, and robust background systems.

🌐 Portfolio & Case Studies: devujvl.in
📱 Reel Counter App: reelcounter.vercel.app
💻 GitHub: @ujjaval01
💼 LinkedIn: /in/ujjavalsaini

Top comments (0)