Recommendation card
– Android TV app Tutorial hands on 11

recommendation

Recommendation

Android TV’s home, LeanbackLauncher has a recommendation row at first row. Any application can suggest recommended contents for users. In this chapter, I will explain how to show recommendation card to LeanbackLauncher from your application.

This recommendation is achieved by using Notification framework whose structure was already existed in Android phone/tablet SDK. So showing recommendation in LeanbackLauncher is actually sending notification. Basic sequence is following 

  1. Declare NotificationManager
  2. Use your customized RecommendationBuilder class (it is custom class which usesNotificationCompat class)  to prepare recommendation.
  3. Make Notification by building RecommendationBuilder
  4. Notify this Notification using NotificationManager.

 

What is implemented in this chapter?

2 new class, RecommendationBuilder & RecommendationFactory, are implemented in this chapter. RecommendationBuilder is the custom class to create notification for your application. RecommendationFactory actually create notification using RecommendationBuilder.

In this chapter, we will send notification (make recommendation) by clicking the button in MainFragment. It will invoke recommend method in RecommendationFactory to achive recommendation. (As written later, recommendation usually should be done in service. However I implemented recommendation in onClick for easy understanding.) 

RecommendationBuilder

First, RecommendationBuilder is impelemented as follows.

You can use this ReccomendationBuilder class as a library. In that case you don’t need to know the detail of this implementation, so you can skip reading this section. Explanation about this class is written in the remaining of this section.

Most important part is build() function, which shows how to make Notification instance for Android TV recommendation. Recommendation in Android TV  is indeed Notification! It is making a instance of Notification class, by using NotificationCompat.BigPictureStyle method.

Many of the function and its related part in the recommendation card is straightforward.

Most tricky part in this function is setting background image. The background image will be displayed when user selects the recommendation card in LeanbackLauncher. This background image is specified by “extra” field, by using Bundle. Key is Notification. EXTRA_BACKGROUND_IMAGE_URI and the value is the URI of backgroundimage. Please note that you can specify bitmap file itself for

  • smallicon – used as application/company logo, shown in the right bottom side of recommendation card.
  • largeicon – used as main image for recommendation card.

But you cannot specify the background image by bitmap. In this implementation, we cache bitmap background image by using content provider and specifying URI of this cached background image.

Storing part is as follows

The part of getting background image URI is as follows. When Recommendation card is selected, it will refer extra fields with the key “Notification.EXTRA_BACKGROUND_IMAGE_URI“. The value is sent to Content provider’s openFile method. So the values are handled in the openFile method to extract the file path of already stored background image. 

 

RecommendationFactory

RecommendationFactory class uses the RecommendationBuilder to create notification for recommending Movie items. Implementation is following.

recommend method is the main method, which makes the recommend Notification. Procedure is following

  1. Declare NotificationManager
    It is done in the Constractor of RecommendationFactory class.
  2. Use your customized RecommendationBuilder class (it is custom class which usesNotificationCompat class)  to prepare recommendation.
    Brief explanation of each set function
    • Id – ID for this recommendation card.
    • Priority – set priority. high priority card is more likely to be shown in the recommendation row in LeanbackLauncher.
    • Background – set background bitmap, which will be shown when user select recommendation card.
    • Title – first row text of recommendation card.
    • Description – second row text of recommendation card.
    • Bitmap – main image of recommendation card.
    • SmallIcon – company/image icon.
    • FastLaneColor – set background color of text field in the recommendation card
    • Intent – set PendingIntent (action) to indicate that what will happen after user click this recommendation card. buildPendingIntent method is used to create intent in this example.
  3. Make Notification by building RecommendationBuilder
  4. Notify this Notification using NotificationManager.
    These are done in recommend method.

    That’s all. It is easy.

Build & run

recommendation

Recommendation card on LeanbackLauncher

You can send recommendation card by clicking “recommendation” button.

Source code is on github.

 

Running recommendation as service

Recommendation is a concept that application suggests user to navigate next action. So most of the time, recommendation may be done in the background. I will follow up about this in the remainder.

Official doc Recommending TV Content – Android developers explains how to do recommendation in the background service, and Google’s sample code is showing real source code implementation of usage of background service. In this sample source code, recommendation service will be launched after getting the intent of “BOOT_COMPLETED”, and it will execute recommendation in background every 30 min.

 

Sponsored Links

Leave a Reply

Your email address will not be published.