Xiaomi Android TV Metro

repository·master·Indexed 20 days ago

https://github.com/xiaomi/android_tv_metro

An Android library and API specification for building TV applications with a 'Metro' style UI layout optimized for 540dp wide layouts. It provides a hierarchical data model based on Albums and Display Items, key UI components like RecommendCardView and GenericSubjectLoader, and a defined server API pattern for delivering content via namespaces (app, game, video) and JSON schemas.

Tokens
4.1K
Snippets
12
Records
19
Agent score
71%

What's inside android_tv_metro

  1. Understand GameCenter and AppStore API concepts

    master

    The API is organized around several core abstractions used to navigate and display content in the Metro UI:

    • Namespace (NS): Defines the content domain. Values are app (AppStore), game (GameCenter), or video (Videos).
    • Item: A single product, such as an App or a Game.
    • Album: A collection of Items or other Albums. Specific IDs have predefined purposes:
      • recommend: The recommendation/home page.
      • categories: The entry point for all categories.
      • {category}: All items within a specific category.
      • {category}/{subcategory}: All items within a specific subcategory.
    • Type: How items are organized. Use album for collections or item for individual products.
    • UI Type: Defines how content is rendered (e.g., metro for Metro-style layouts or detail for item details).
    • Sort: Determines the ordering of items. Options include weighting (default), created, updated, name, score, and download.
  2. Understand the Metro UI data model

    master

    The Metro style UI is built using a hierarchical structure of two primary elements: Albums and Display Items.

    • Album: A container that can hold multiple other Albums or Display Items. The main page of the application is also defined as an Album.
    • Display Item: The leaf nodes of the structure, representing specific content such as a video, game, app, or music. You can derive specific detail items from these display items.

    Hierarchy Model:

                                 Album
                                   |
                 __________________|___________________
                 |    ...  |           |       ...    |
               Album      Album    Display Item     Display Item
  3. Integrate the Android Metro library

    master

    To integrate the Android Metro library into your application, you must follow these steps:

    1. Inherit from MainActivity.
    2. Implement a Tabs Loader (using GenericSubjectLoader) to handle the loading of Albums.

    For a concrete implementation reference, examine the TVMetroSample application included in the repository.

    Key components provided by the library:

    • RecommendCardView: A specialized Card view for Metro layouts.
    • GenericSubjectLoader: A loader designed for Albums (note that Tabs are treated as instances of an Album).
  4. Implement a Metro-compatible Server API

    master

    To support the Metro UI, your server must provide data following the defined API style and JSON structures.

    API Endpoint Pattern

    The general pattern for requesting resources is: http://host/v1/ns/type/?id=res_id

    • ns: Namespace (resource type).
    • type: item or item list.
    • id: Resource ID from the backend system.

    Specific Endpoint Examples

    • Item List: http://host/game(video/app)/item?id=12346
    • Album: http://host/game(video/app)/album?id=6464
    • Category: http://host/game(video/app)/category?id=123456 (Note: Categories and Albums are conceptually similar).

    Implementation Steps

    1. Define your Home Data structure (the top-level Album containing tabs).
    2. Implement your Details/List APIs to return specific items or albums based on the requested IDs.
  5. Resource URL patterns for GameCenter and AppStore

    master

    Resources follow a universal URL pattern: /{NS}/{Type}?id={ID}.

    Examples:

    • /game/album?id=album1_id: A specific game album in GameCenter.
    • /game/album?id=recommand: The GameCenter home page content.
    • /game/album?id=action: The album containing all 'action' category games.
    • /game/item?id=game1_id: Details for a single game in GameCenter.
    /{NS}/{Type}?id={ID}
  6. Reference: API Common Parameters

    master

    All API requests must include the following parameters passed as HTTP query parameters:

    • locale: Locale code (e.g., zh_CN).
    • res: Resolution (hd720 | hd1080 | hd2160).
    • device: Device type.
    • device_id: Device ID (typically the MD5 hash of the device MAC address).
    • ns_key: The Key for the Namespace.
    • opaque: A URL signature calculated using the ns_key and the URI/parameters.
  7. Reference Home display data JSON schema

    master

    The top-level Home data is an object containing an array of albums (tabs). Each album entry includes its items, images, name, ID, type, and namespace.

    {
        "data": [
            {
                "items": [display items],
                "images": {},
                "name": "TAB 1",
                "id": "recommend",
                "type": "album",
                "ns": "video"
            },
            {
                "items": [display item],
                "images": {},
                "name": "TAB 2",
                "id": "recommend",
                "type": "album",
                "ns": "video"
            }
        ]
    }
    {
        "data": [
            {
                "items": [display items],
                "images": {},
                "name": "TAB 1",
                "id": "recommend",
                "type": "album",
                "ns": "video"
            },
            {
                "items": [display item],
                "images": {},
                "name": "TAB 2",
                "id": "recommend",
                "type": "album",
                "ns": "video"
            }
        ]
    }
  8. Reference Album JSON schema

    master

    An Album contains a collection of items (which can be Display Items or other Albums) and metadata for the container.

    {
        "data": [
            {
                "items": [display items],
                "images": { },
                "name":"game tab name",
                "times": {
                    "updated": 0,
                    "created": 0
                },
                "_ui": {
                    "type": "metro"
                },
                "id": "recommend",
                "type": "album",
                "ns": "game"
            }
        ],
        "preload": {
            "images": []
        },
        "update_time": 0
    }
    {
        "data": [
            {
                "items": [display items],
                "images": { },
                "name":"game tab name",
                "times": {
                    "updated": 0,
                    "created": 0
                },
                "_ui": {
                    "type": "metro"
                },
                "id": "recommend",
                "type": "album",
                "ns": "game"
            }
        ],
        "preload": {
            "images": []
        },
        "update_time": 0
    }
  9. Reference: API Endpoints

    master

    The following endpoints are available for retrieving content:

    • GET /1/{ns}/category?id={id}&sort={sort}&page={page}&filter=...: Returns an {AlbumList} for a specific category. Note that page is 1-based (default 1) and sort does not apply to non-category albums.
    • GET /1/{ns}/album?id={id}: Returns an {AlbumList} for a specific album.
    • GET /1/{ns}/search?q=word&category=c1/c2: Performs a search. Returns an {AlbumList}.
    • GET /1/{ns}/home: Returns the home page content as an {AlbumList}.
    • GET /1/{ns}/item?id={id}: Returns an {ItemList} containing detailed information for a single item.
    • GET /1/{ns}/download?item={id}: (Status: TBD) Initiates a download.
    GET /1/{ns}/category?id={id}&sort={sort}&page={page}&filter=...
    GET /1/{ns}/album?id={id}
    GET /1/{ns}/search?q=word&category=c1/c2
    GET /1/{ns}/home
    GET /1/{ns}/item?id={id}
    GET /1/{ns}/download?item={id}
  10. Reference: Data Entity Schemas

    master

    The API returns JSON entities. Key structures include:

    Image

    {
        "url": "http://...",
        "bgcolor": "#3Fde88",
        "pos": {"x":100, "y":300}, 
        "ani": { ... },
        "size": {"w": 890, "h": 789}
    }

    DisplayItem (Base fields for items in a list)

    {
        "ns": "app|game|video",
        "type": "category|album|item",
        "id": "...",
        "name": "...",
        "images": { "bg": {Image}, "text": {Image}, ... },
        "_ui": { "type": "...", "layout": { "w": 0, "h": 0 } }
    }

    Item (Extends DisplayItem with full details)

    {
        "package": "...",
        "phrase": "...",
        "vendor": { "id": "...", "name": "..." },
        "flags": { "upgrade": true },
        "description": "...",
        "times": { "updated": 1909088888 },
        "ver": { "code": "200", "name": "1.0.1" },
        "sizes": { "package": 1941662, "installed": 20098900 },
        "category": { "id": "c1/c2", "name": "..." },
        "score": 70,
        "screenshots": [ { "bg": {Image}, ... } ]
    }

    Album (Extends DisplayItem, contains a list of items)

    {
        "is_category": true,
        "sort": "...",
        "pagi": 1,
        "items": [ { "DisplayItem" } ]
    }
  11. Reference Display Item JSON schema

    master

    A Display Item represents a single piece of content. It includes metadata, UI layout instructions, and image assets.

    {
        "target": {
            "type": "item"
        },
        "images": {
            "text": {
                "url": "",
                "ani": {},
                "pos": {}
            },
            "icon": {
                "url": "",
                "ani": {},
                "pos": {}
            },
            "back": {
                "url": "http://xxx/fffff.png",
                "ani": {},
                "pos": {}
            },
            "spirit": {
                "url": "",
                "ani": {},
                "pos": {}
            }
        },
        "name": "name",
        "times": {
            "updated": 1404466152,
            "created": 1404454443
        },
        "_ui": {
            "type": "metro_cell_banner",
            "layout": {
                "y": 1,
                "x": 1,
                "w": 1,
                "h": 2
            }
        },
        "id": "180",
        "type": "item",
        "ns": "game"
    }
    {
        "target": {
            "type": "item"
        },
        "images": {
            "text": {
                "url": "",
                "ani": {},
                "pos": {}
            },
            "icon": {
                "url": "",
                "ani": {},
                "pos": {}
            },
            "back": {
                "url": "http://xxx/fffff.png",
                "ani": {},
                "pos": {}
            },
            "spirit": {
                "url": "",
                "ani": {},
                "pos": {}
            }
        },
        "name": "name",
        "times": {
            "updated": 1404466152,
            "created": 1404454443
        },
        "_ui": {
            "type": "metro_cell_banner",
            "layout": {
                "y": 1,
                "x": 1,
                "w": 1,
                "h": 2
            }
        },
        "id": "180",
        "type": "item",
        "ns": "game"
    }