Exemple #1
0
  /**
   * Initializes a new {@link CastSessionImpl} instance.
   *
   * @param apiClient The Google Play Services client used to create the session.
   * @param sessionId The session identifier to use with the Cast SDK.
   * @param origin The origin of the frame requesting the route.
   * @param tabId The id of the tab containing the frame requesting the route.
   * @param isIncognito Whether the route is beging requested from an Incognito profile.
   * @param source The {@link MediaSource} corresponding to this session.
   * @param routeProvider The {@link CastMediaRouteProvider} instance managing this session.
   */
  public CastSessionImpl(
      GoogleApiClient apiClient,
      String sessionId,
      ApplicationMetadata metadata,
      String applicationStatus,
      CastDevice castDevice,
      String origin,
      int tabId,
      boolean isIncognito,
      MediaSource source,
      CastMediaRouteProvider routeProvider) {
    mSessionId = sessionId;
    mRouteProvider = routeProvider;
    mApiClient = apiClient;
    mSource = source;
    mApplicationMetadata = metadata;
    mApplicationStatus = applicationStatus;
    mCastDevice = castDevice;
    mMessageHandler = mRouteProvider.getMessageHandler();
    mMessageChannel = new CastMessagingChannel(this);
    updateNamespaces();

    final Context context = ContextUtils.getApplicationContext();

    if (mNamespaces.contains(CastMessageHandler.MEDIA_NAMESPACE)) {
      mMediaPlayer = new RemoteMediaPlayer();
      mMediaPlayer.setOnStatusUpdatedListener(
          new RemoteMediaPlayer.OnStatusUpdatedListener() {
            @Override
            public void onStatusUpdated() {
              MediaStatus mediaStatus = mMediaPlayer.getMediaStatus();
              if (mediaStatus == null) return;

              int playerState = mediaStatus.getPlayerState();
              if (playerState == MediaStatus.PLAYER_STATE_PAUSED
                  || playerState == MediaStatus.PLAYER_STATE_PLAYING) {
                mNotificationBuilder.setPaused(playerState != MediaStatus.PLAYER_STATE_PLAYING);
                mNotificationBuilder.setActions(
                    MediaNotificationInfo.ACTION_STOP | MediaNotificationInfo.ACTION_PLAY_PAUSE);
              } else {
                mNotificationBuilder.setActions(MediaNotificationInfo.ACTION_STOP);
              }
              MediaNotificationManager.show(context, mNotificationBuilder.build());
            }
          });
      mMediaPlayer.setOnMetadataUpdatedListener(
          new RemoteMediaPlayer.OnMetadataUpdatedListener() {
            @Override
            public void onMetadataUpdated() {
              setNotificationMetadata(mNotificationBuilder);
              MediaNotificationManager.show(context, mNotificationBuilder.build());
            }
          });
    }

    Intent contentIntent = Tab.createBringTabToFrontIntent(tabId);
    if (contentIntent != null) {
      contentIntent.putExtra(
          MediaNotificationUma.INTENT_EXTRA_NAME, MediaNotificationUma.SOURCE_PRESENTATION);
    }
    mNotificationBuilder =
        new MediaNotificationInfo.Builder()
            .setPaused(false)
            .setOrigin(origin)
            // TODO(avayvod): the same session might have more than one tab id. Should we track
            // the last foreground alive tab and update the notification with it?
            .setTabId(tabId)
            .setPrivate(isIncognito)
            .setActions(MediaNotificationInfo.ACTION_STOP)
            .setContentIntent(contentIntent)
            .setIcon(R.drawable.ic_notification_media_route)
            .setDefaultLargeIcon(R.drawable.cast_playing_square)
            .setId(R.id.presentation_notification)
            .setListener(this);
    setNotificationMetadata(mNotificationBuilder);
    MediaNotificationManager.show(context, mNotificationBuilder.build());
  }
Exemple #2
0
 @Override
 public void onStop(int actionSource) {
   stopApplication();
   mRouteProvider.onSessionStopAction();
 }