Architecture
The Link Handler system consists of several key components:Core Classes
LinkHandler - The base data class that holds URL information:originalUrl- The original URL as provided by the userurl- The cleaned/normalized URLid- The extracted resource ID
contentFilters- List of content type filters (e.g., “videos”, “playlists”)sortFilter- Sort order filter (e.g., “recent”, “popular”)
How It Works
The Link Handler system follows a factory pattern:- URL Parsing: A URL is passed to a factory’s
fromUrl()method - Validation: The factory checks if it can handle the URL via
acceptUrl() - ID Extraction: The factory extracts the resource ID from the URL
- Handler Creation: A LinkHandler object is created with the original URL, normalized URL, and ID
Example Flow
Factory Types
LinkHandlerFactory
The base factory for simple resources like videos, channels, or playlists. Use this when you only need to handle basic URL-to-ID conversion. Use cases:- Video URLs
- Channel URLs
- Playlist URLs
- Any single resource without filtering
ListLinkHandlerFactory
Extends LinkHandlerFactory to support content and sort filters. Use this for resources that return lists of items with optional filtering. Use cases:- Channel tabs (videos, playlists, shorts)
- Kiosk pages (trending, top rated)
- Category pages
- Any list that supports filtering/sorting
SearchQueryHandlerFactory
Specialized for search functionality. The ID field represents the search query string. Use cases:- Search pages
- Search suggestions
- Any query-based content
Google Redirect Handling
The Link Handler system automatically handles Google search redirects. When a URL is processed throughfromUrl(), it’s automatically polished using Utils.followGoogleRedirectIfNeeded() to extract the actual destination URL.
Implementation Guidelines
When implementing a new service, you need to create concrete factory classes:-
Override required abstract methods:
getId(String url)- Extract ID from URLgetUrl(String id)- Construct URL from IDonAcceptUrl(String url)- Validate if URL is supported
-
For ListLinkHandlerFactory, also override:
getUrl(String id, List<String> contentFilter, String sortFilter)- Optionally:
getAvailableContentFilter()andgetAvailableSortFilter()
-
For SearchQueryHandlerFactory, also override:
getUrl(String query, List<String> contentFilter, String sortFilter)- Optionally:
getSearchString(String url)to extract query from URL
Thread Safety
LinkHandler instances are immutable and implementSerializable, making them safe to use across threads and suitable for caching or persistence.
The contentFilters list in ListLinkHandler is wrapped with Collections.unmodifiableList() to ensure immutability.
Next Steps
- LinkHandlerFactory API - Detailed API reference
- ListLinkHandlerFactory API - List handler API
- SearchQueryHandlerFactory API - Search handler API