Browse DevX
Sign up for e-mail newsletters from DevX


Teach Your TiVO New Tricks with the HME SDK : Page 3

With the release of the TiVO Home Media Engine SDK, the TiVO isn't simply a standalone digital video recorder any more, it's a gateway to a massive convergence of information and multimedia.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Enhancing the Application Using the HME
The sample application makes a good start, but it is a little basic. But you can take the code you've already seen as a foundation and use it to generate a portfolio grid of many stocks on the screen, updating them every few seconds. You can see the finished version (available in the downloadable code) in Figure 5.

Figure 5. Multiple Stock Quotes: The figure shows the multiple stock quote application running in the TiVO simulator listing five stocks that update every five seconds.
To do this, the first thing you need to do is to make your class runnable, by having its declaration specify implements Runnable like this:

public class StockQuote extends Application implements Runnable

Next, you will need to use HME "views" to specify the areas of the screen where you want to put the information. Earlier you used root.SetResource to write to the screen, but that method just writes to the root screen object. If you have a number of objects that you want to write to, you define a content view that overlays the root, and then have panels that divide the screen up—in this case into the five colored areas that you see in Figure 5. This application is hard coded for five stocks, but changing that number or making it dynamic should not be a big deal. Finally, the code adds views to these panels that will hold the text. The code from the init() function shown below handles all this.

View content = new View(root, SAFE_ACTION_H, SAFE_ACTION_V, root.width - SAFE_ACTION_H * 2, root.height - SAFE_ACTION_V * 2); int panelHeight = content.height / views.length; for(int lp=0;lp<5;++lp) { View panel = new View(content, 0, lp * panelHeight, content.width, panelHeight); views[lp] = new View(panel, 0, 0, 200, 70); } new Thread(this).start();

The constants that you see (SAFE_ACTION_H, etc.) are part of the SDK. Check the documentation for more details. The last line of the preceding code starts a new thread, which runs the application, causing the run() function to be called. Run() is the workhorse of the app; it uses the SOAP code you saw earlier to get a quote for each of the five stock tickers hard-coded into this sample app. It then updates each of the views with the appropriate text, as shown in the code fragment below.

while (context != null) { //update(views[1],"waiting..."); flush(); try { for(int lp=0;lp<5;lp++) { fQuotes[lp] = getQuote(strTickers[lp]); update(views[lp],strTickers[lp] + ":" + fQuotes[lp]); } } catch(Exception e) { e.printStackTrace(); update(views[1],"Error : " + e); flush(); } flush(); // Wait 5 seconds and do it again synchronized (this) { try { wait(5 * 1000); } catch (InterruptedException e) { return; } } }

The flush() function triggers a redraw of the TiVO screen. The run() function code is pretty straightforward, as you can see. Every time it runs, it loops through the tickers and gets the quote using getQuote(strTickers[lp]) where strTickers is the array of pre-defined ticker strings. It passes the returned value to the update function, which you'll see in just a moment. After it's done with this, it calls flush() again to make sure that the screen is updated with the new values before going into a five-second (5,000 millisecond) wait cycle. At the end of this cycle, it runs again.

The update() function is a helper function that takes the string value and the reference to the view (the five views are stored in an array), and sets its text to the string value. It also randomly changes the background of the panel on which the view resides, causing a color shifting effect in the banded colors. See Figure 6 and Figure 7 to see the different colors. The function sets the view's text using view.setResource in much the same way as you called root.setResource in the earlier example. Here's the code:

void update(View view, String strValue) { // Tweak the parent's background color, just for // fun. This lets the user know that the RSS feed // was updated. int red = random.nextInt(0x80); int green = random.nextInt(0x80); int blue = random.nextInt(0x80); view.parent.setResource(new Color( red, green, blue)); // Now update the view with the new text. view.setResource(createText("default-36.font", Color.white, strValue)); }

Figure 6 One Set of Colors: The update() function updates the views, using white text and a randomly chosen background color.
Figure 7. Colors Changed by the Update() Function: As the sample application calls update(), the background colors change.

The TiVO HME SDK is a very cool piece of software. Just imagine, you can write custom Java applications that run on your computer, communicate with an embedded device, and render on your TV! Not only that, but you can connect your TiVO to the Internet and use it to consume Web services as discussed in this article. This is really the beginning of a massive convergence of information and multimedia.

This article showed you how to get your TiVO to connect to a Web service using manually generated and parsed SOAP packets, and then to display the information on your TV showing the delayed values of some stocks with a color changing background, which you could run as a useful screensaver! With the emergence of Web services, and with outlets such as Amazon and eBay opening their data centers via Web services, the stage is set for a whole new way to access information—through your TV set. The promise has been around for some time (remember WebTV?), but now, TiVO appears to be much closer to delivering on that promise—bringing the information highway into the living room. What you've seen here isn't all the SDK can do; it also lets you build applications for home media such as MP3s and your digital pictures. Finally, when TiVO opens the SDK up so it can access the program guide and interact with digital recordings on your TiVO—or live TV—a whole new wealth of application development opportunities will arise. And the best part is that you don't even need a TiVO to begin playing with them. Be forewarned though—after you start building to the emulator, you'll probably want to try it out on the real thing!

Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date