public final class

CastContext

extends Object
java.lang.Object
   ↳ com.google.android.gms.cast.framework.CastContext

Class Overview

A singleton class containing global context for the Cast SDK. The shared instance can be accessed by calling getSharedInstance(Context). To properly initialize CastContext, the application must have a class that implements OptionsProvider interface:

     package com.example.app;

     public class CastOptionsProvider implements OptionsProvider {
         @Override
         public CastOptions getCastOptions(Context appContext) {
             ...
         }
     }
 
and specify its fully qualified class name in the AndroidManifest.xml with the key OPTIONS_PROVIDER_CLASS_NAME_KEY:
     ...
     <meta-data
         android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
         android:value="com.example.app.CastOptionsProvider" />
     ...
 
All public methods must be called from the main thread.

Summary

Constants
String OPTIONS_PROVIDER_CLASS_NAME_KEY The metadata key to specify the fully qualified name of the OptionsProvider implementation in the AndroidManifest.xml.
Public Methods
void addAppVisibilityListener(AppVisibilityListener listener)
Adds an AppVisibilityListener to listen for UI visibility events.
void addCastStateListener(CastStateListener listener)
Adds a CastStateListener to monitor changes of the current CastState.
CastOptions getCastOptions()
Returns the CastOptions.
int getCastState()
Returns the current CastState.
MediaNotificationManager getMediaNotificationManager()
MediaRouteSelector getMergedSelector()
Returns the merged MediaRouteSelector used for discovering devices and deciding whether to show the MediaRouteButton.
PrecacheManager getPrecacheManager()
Returns the PrecacheManager.
SessionManager getSessionManager()
Returns the SessionManager, never returns null.
static CastContext getSharedInstance(Context context)
Returns a shared instance of CastContext.
boolean isAppVisible()
Returns true if the application is visible.
boolean onDispatchVolumeKeyEventBeforeJellyBean(KeyEvent event)
If your application supports SDK versions before JELLY BEAN, you should call this method to delegate the handling of physical volume key events to the SDK so that users can use the physical volume keys to control device volume of the Cast receiver.
void removeAppVisibilityListener(AppVisibilityListener listener)
void removeCastStateListener(CastStateListener listener)
void setReceiverApplicationId(String applicationId)
Sets the receiver application ID.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String OPTIONS_PROVIDER_CLASS_NAME_KEY

The metadata key to specify the fully qualified name of the OptionsProvider implementation in the AndroidManifest.xml.

Constant Value: "com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"

Public Methods

public void addAppVisibilityListener (AppVisibilityListener listener)

Adds an AppVisibilityListener to listen for UI visibility events.

Parameters
listener AppVisibilityListener
Throws
IllegalStateException If this method is not called on the main thread.
NullPointerException If listener is null.

public void addCastStateListener (CastStateListener listener)

Adds a CastStateListener to monitor changes of the current CastState.

Parameters
listener CastStateListener
Throws
IllegalStateException If this method is not called on the main thread.
NullPointerException If listener is null.

public CastOptions getCastOptions ()

Returns the CastOptions.

Returns
CastOptions
Throws
IllegalStateException If this method is not called on the main thread.

public int getCastState ()

Returns the current CastState.

Returns
int
Throws
IllegalStateException If this method is not called on the main thread.

public MediaNotificationManager getMediaNotificationManager ()

Returns the MediaNotificationManager.

Returns
MediaNotificationManager
Throws
IllegalStateException If this method is not called on the main thread.

public MediaRouteSelector getMergedSelector ()

Returns the merged MediaRouteSelector used for discovering devices and deciding whether to show the MediaRouteButton. This selector is merged with any additional SessionProviders that might be registered with the CastContext.

Returns
MediaRouteSelector
Throws
IllegalStateException If this method is not called on the main thread.

public PrecacheManager getPrecacheManager ()

Returns the PrecacheManager.

Returns
PrecacheManager

public SessionManager getSessionManager ()

Returns the SessionManager, never returns null.

Returns
SessionManager
Throws
IllegalStateException If this method is not called on the main thread.

public static CastContext getSharedInstance (Context context)

Returns a shared instance of CastContext. The shared instance will be initialized on the first time this method is called.

Parameters
context Context: An application Context. If this is not an application Context, getApplicationContext() will be called on the given context, to retrieve it.
Returns
CastContext
Throws
IllegalStateException If

  • This method is not called on the main thread.
  • The fully qualified name of the OptionsProvider implementation is not specified as a metadata in the AndroidManifest.xml with key OPTIONS_PROVIDER_CLASS_NAME_KEY.
  • optionsProviderClass or its nullary constructor is not accessible.
  • Instantiation of OptionsProvider fails for some other reason.

public boolean isAppVisible ()

Returns true if the application is visible.

Returns
boolean
Throws
IllegalStateException If this method is not called on the main thread.

public boolean onDispatchVolumeKeyEventBeforeJellyBean (KeyEvent event)

If your application supports SDK versions before JELLY BEAN, you should call this method to delegate the handling of physical volume key events to the SDK so that users can use the physical volume keys to control device volume of the Cast receiver. Clients should override dispatchKeyEvent(KeyEvent) and call this method:

     @Override
     public boolean dispatchKeyEvent(KeyEvent event) {
         return CastContext.getSharedInstance().onDispatchVolumeKeyEventBeforeJellyBean(event)
                 || super.dispatchKeyEvent(event);
     }
 
The amount of volume delta to increase or decrease is set through setVolumeDeltaBeforeIceCreamSandwich(double). This method is a no-op on platform JELLY BEAN and above.

Parameters
event KeyEvent: The dispatched event.
Returns
boolean

public void removeAppVisibilityListener (AppVisibilityListener listener)

Removes an AppVisibilityListener.

Parameters
listener AppVisibilityListener
Throws
IllegalStateException If this method is not called on the main thread.

public void removeCastStateListener (CastStateListener listener)

Removes a CastStateListener.

Parameters
listener CastStateListener
Throws
IllegalStateException If this method is not called on the main thread.

public void setReceiverApplicationId (String applicationId)

Sets the receiver application ID. This overrides the receiver application ID specified in CastOptions.

Calling this method will result in the receiver ID used for discovery and application launch being changed. The Cast button status will also changed accordingly. If the new application ID is different than the current one, the current session will be ended if there is any.

Specifying null or an empty string indicates that the app does not want to discover receiver devices or launch a receiver application.

For discovering for multiple application IDs at the same time, or for discovering for other application ID without affect the current session, use MediaRouter APIs instead.

Parameters
applicationId String: The receiver application ID that will be used to discover the receiver device and launch the receiver application.
Throws
IllegalStateException If this method is not called on the main thread.