LiveCard

Added in API level XE12
public class

LiveCard

This class lets you create active cards and publish them on the timeline.

For more information on how to create live cards, see the Live cards developer guide.

Life of a live card

Live cards require a long-running context to own them for the entire time that they are published, so you should manage them in a background Service. You can then publish a live card as soon as the service is started or in response to other events that the service monitors. Likewise, you should unpublish cards once they are no longer relevant or when the service is destroyed.

When you publish a live card, you get to choose how this impacts users. When publishing silently, the card will be inserted into the timeline without any visible effect: users have to swipe to the card to see it. You can also have the live card automatically displayed right after it is published. This is often useful when the main user interface of your application is a live card and not an activity.

Displaying content on a live card

A live card can display content either from a layout resource or by allowing your code to render directly onto its drawing surface. Choose the method most appropriate for your application based on how frequently you need to update the content on the card and if you are primarily rendering widgets or free-form graphics.

Inflating a layout using remote views

If your application only requires standard widgets and infrequent updates (about a few seconds or longer between refreshes), then create your card with the help of the RemoteViews class. The RemoteViews object lets the Glass timeline, which runs in a separate process from your own application code, inflate a layout that you have created.

     LiveCard liveCard; // initialized elsewhere
     RemoteViews views = new RemoteViews(context.getPackageName(),
             R.layout.my_layout);
     liveCard.setViews(views);
 

Note that once a card using RemoteViews is published, changes made to the views by calling set* methods will not be visible in the timeline unless you explicitly call setViews on the live card again to force an update.

Drawing directly on the live card surface

If your application requires more frequent updates (several times per second) or rendering more elaborate graphics than the standard widgets support, enable direct rendering and add a SurfaceHolder.Callback to the card's surface.

     LiveCard liveCard; // initialized elsewhere
     liveCard.setDirectRenderingEnabled(true);
     liveCard.getSurfaceHolder().addCallback(callback);
 

You can then draw directly on the surface inside a background thread or in response to external events (for example, sensor or location updates). Use the surfaceCreated and surfaceDestroyed methods to start and stop your rendering logic when the card is displayed or hidden.

Note that the surface holder's callback methods are not invoked on the main UI thread.

Handling live card selection

A live card must provide an action (a PendingIntent to start an activity, service, or perform a broadcast) that will be executed when the user taps to select the card. Normally you will use this action to launch an activity that displays an options menu or takes the user into another part of your application. At the very least, you should provide an option that lets the user remove the live card from the timeline.

     LiveCard liveCard; // initialized elsewhere
     Intent intent = new Intent(context, MyActivity.class);
     liveCard.setAction(PendingIntent.getActivity(context, 0, intent, 0));
 

Live cards that do not have an action will not be displayed.

Nested Classes
enum LiveCard.PublishMode Determines the way the card is presented to the user when published. 
Constants
String EXTRA_FROM_LIVECARD_VOICE Boolean extra that denotes an Intent was activated by voice from a live card.
Public Constructors
LiveCard(Context context, String tag)
Public Methods
LiveCard
attach(Service service)
SurfaceHolder
boolean
void
void
LiveCard
LiveCard
setDirectRenderingEnabled(boolean enable)
LiveCard
LiveCard
LiveCard
setVoiceActionEnabled(boolean enable)
void
Inherited Methods

Constants

Added in API level XE21

public static final String EXTRA_FROM_LIVECARD_VOICE

Boolean extra that denotes an Intent was activated by voice from a live card.

Constant Value: "android.intent.extra.EXTRA_FROM_LIVECARD"

Public Constructors

Added in API level XE16

public LiveCard (Context context, String tag)

Creates a live card with the given tag.

Note that a card does not appear until it is explicitly published.

Parameters
context the application's context
tag non-null tag for the card; this is for debugging purposes

Public Methods

Added in API level XE16

public LiveCard attach (Service service)

Attach a background Service so that when this card gets published, the given service will be set to run in the foreground.

The service will automatically get removed from foreground when this live card is unpublished.

Parameters
service will be set to run in foregound
Returns
  • this object for call chaining
Added in API level XE12

public SurfaceHolder getSurfaceHolder ()

When direct rendering is enabled, gives access to the Surface on which to draw.

Note that the returned SurfaceHolder should be used simply as a means to access the managed surface. Methods for altering the shape and type of the surface are no-ops.

Note also that callbacks for this object are not made in the UI thread.

Added in API level XE12

public boolean isPublished ()

Returns true if the card is currently published.

Added in API level XE16

public void navigate ()

Sends the user to this card in the timeline.

Throws
IllegalStateException if the card is not published
Added in API level XE12

public void publish (LiveCard.PublishMode mode)

Publishes this card to the timeline.

The card will only be displayed if it has an action, and if direct rendering is enabled or remote views have been set.

Parameters
mode determines how the card is presented to the user
Added in API level XE12

public LiveCard setAction (PendingIntent intent)

Changes the action taken when the card is selected.

Parameters
intent will get fired when the card is selected
Returns
  • this object for call chaining
Added in API level XE12

public LiveCard setDirectRenderingEnabled (boolean enable)

Enables direct rendering.

In this mode, the card content will have to be rendered directly into a Surface.

Note that this method can only be called when the card is not published and will otherwise throw an exception.

Parameters
enable whether direct rendering should be enabled
Returns
  • this object for call chaining
Added in API level XE16

public LiveCard setRenderer (GlRenderer renderer)

Adds an OpenGL-based renderer.

The renderer will be used to draw onto a surface requested (automatically) for direct rendering.

Returns
  • this object for call chaining
Added in API level XE12

public LiveCard setViews (RemoteViews views)

Changes the RemoteViews used to display this card's UI.

This method should also be called after making any changes directly to the remote views of a published card, or those changes will not be reflected on the timeline.

This method has no effect if direct rendering is enabled.

Parameters
views the card's UI
Returns
  • this object for call chaining
Added in API level XE21

public LiveCard setVoiceActionEnabled (boolean enable)

Enables a voice action when the card is shown on the timeline.

When set, the card listens for "ok glass" when shown on the timeline and, when this guard phrase is spoken, fires the intent that has been set by the setAction(PendingIntent) method. If the intent starts an activity that implements contextual voice menus, the activity will automatically open on the first menu items (viz. as if "ok glass" was spoken inside the activity itself). This feature enables implementing contextual voice menus on live cards.

Parameters
enable whether voice action should be enabled
Returns
  • this object for call chaining
Added in API level XE12

public void unpublish ()

Unpublishes this card from the timeline.