What APIs are used to draw over other apps like Facebook's Chat Heads?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Overview
APIs that allow applications to draw over other apps are pivotal in enhancing user experience by providing multitasking capabilities and pop-up interfaces. One of the most recognized implementations of such functionality is Facebook's Chat Heads on Android, which appeared over other applications for ease of chat interaction without switching contexts. This article delves into how such overlays are technically implemented, with a focus on Android, which provides specific capabilities for creating overlay interfaces.
Key APIs and Permissions
Creating an overlay interface involves using particular APIs and permissions. In Android, this is achieved primarily with the `WindowManager` and `TYPE_APPLICATION_OVERLAY`. Here's a detailed technical overview:
WindowManager API
`WindowManager` is a system service that provides all window-related functionalities.
- Usage: Allows an application to add and manage views directly on the window manager.
- Layout Parameters: Using `LayoutParams`, you can specify how your view behaves and appears. `WindowManager.LayoutParams` provides various customizable options like width, height, positioning, and more.
Key Attributes
- `WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY`: This parameter helps create a non-interruptive overlay that does not interfere with system-level interactions.
- `SYSTEM_ALERT_WINDOW` Permission: Initially, this permission allowed apps to draw over the entire screen. However, due to its powerful capabilities, Google restricted its use in place of scoped permissions like `TYPE_APPLICATION_OVERLAY`.
- Display over other apps: Users must manually allow applications to display over other applications via Android's "App Permissions" settings to enable overlays.
- Permission Management: Users must explicitly grant permission for overlays, and applications should guide users through this process.
- Performance: Excessive overlays can affect device performance. Developers need to ensure overlays are lightweight.
- Interaction: Overlays should not impede core functionality of active applications. Using the `FLAG_NOT_FOCUSABLE` helps in allowing interactions with apps beneath the overlay.
- Security Implications: Misuse of overlays can lead to phishing attacks or input hijacking, so Android has restricted certain overlays and revised permissions to mitigate risks.

