/* * We try to disconnect application but even if that fails, we need to remove notification since * that is the only way to get rid of it without going to the application */ private void stopApplication() { try { LOGD(TAG, "Calling stopApplication"); mCastManager.disconnect(); } catch (Exception e) { LOGE(TAG, "Failed to disconnect application", e); } LOGD(TAG, "Stopping the notification service"); stopSelf(); }
/* * (non-Javadoc) * @see android.app.Service#onDestroy() */ @Override public void onDestroy() { if (null != mBitmapDecoderTask) { mBitmapDecoderTask.cancel(false); } LOGD(TAG, "onDestroy was called"); removeNotification(); if (null != mCastManager && null != mConsumer) { mCastManager.removeVideoCastConsumer(mConsumer); mCastManager = null; } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { LOGD(TAG, "onStartCommand"); if (null != intent) { String action = intent.getAction(); if (ACTION_TOGGLE_PLAYBACK.equals(action) && mIsIcsOrAbove) { LOGD(TAG, "onStartCommand(): Action: ACTION_TOGGLE_PLAYBACK"); togglePlayback(); } else if (ACTION_STOP.equals(action) && mIsIcsOrAbove) { LOGD(TAG, "onStartCommand(): Action: ACTION_STOP"); stopApplication(); } else if (ACTION_VISIBILITY.equals(action)) { mVisible = intent.getBooleanExtra(NOTIFICATION_VISIBILITY, false); LOGD(TAG, "onStartCommand(): Action: ACTION_VISIBILITY " + mVisible); if (mVisible && null != mNotification) { startForeground(NOTIFICATION_ID, mNotification); mCastManager.setContext(this); } else { stopForeground(true); } } else { LOGD(TAG, "onStartCommand(): Action: none"); } } else { LOGD(TAG, "onStartCommand(): Intent was null"); } return Service.START_STICKY; }
@Override public void onCreate() { super.onCreate(); LOGD(TAG, "onCreate()"); readPersistedData(); mCastManager = VideoCastManager.initialize(this, mApplicationId, mTargetActivity, mDataNamespace); if (!mCastManager.isConnected() && !mCastManager.isConnecting()) { mCastManager.reconnectSessionIfPossible(this, false); } mConsumer = new VideoCastConsumerImpl() { @Override public void onApplicationDisconnected(int errorCode) { LOGD( TAG, "onApplicationDisconnected() was reached, stopping the notification" + " service"); stopSelf(); } @Override public void onRemoteMediaPlayerStatusUpdated() { int mediaStatus = mCastManager.getPlaybackStatus(); VideoCastNotificationService.this.onRemoteMediaPlayerStatusUpdated(mediaStatus); } @Override public void onUiVisibilityChanged(boolean visible) { mVisible = !visible; if (mVisible && null != mNotification) { startForeground(NOTIFICATION_ID, mNotification); mCastManager.setContext(VideoCastNotificationService.this); } else { stopForeground(true); } } }; mCastManager.addVideoCastConsumer(mConsumer); }
private void onRemoteMediaPlayerStatusUpdated(int mediaStatus) { mStatus = mediaStatus; LOGD(TAG, "onRemoteMediaPlayerMetadataUpdated() reached with status: " + mStatus); try { switch (mediaStatus) { case MediaStatus.PLAYER_STATE_BUFFERING: // (== 4) mIsPlaying = false; setupNotification(mCastManager.getRemoteMediaInformation()); break; case MediaStatus.PLAYER_STATE_PLAYING: // (== 2) mIsPlaying = true; setupNotification(mCastManager.getRemoteMediaInformation()); break; case MediaStatus.PLAYER_STATE_PAUSED: // (== 3) mIsPlaying = false; setupNotification(mCastManager.getRemoteMediaInformation()); break; case MediaStatus.PLAYER_STATE_IDLE: // (== 1) mIsPlaying = false; if (!mCastManager.shouldRemoteUiBeVisible(mediaStatus, mCastManager.getIdleReason())) { stopForeground(true); } else { setupNotification(mCastManager.getRemoteMediaInformation()); } break; case MediaStatus.PLAYER_STATE_UNKNOWN: // (== 0) mIsPlaying = false; stopForeground(true); break; default: break; } } catch (TransientNetworkDisconnectionException e) { LOGE(TAG, "Failed to update the playback status due to network issues", e); } catch (NoConnectionException e) { LOGE(TAG, "Failed to update the playback status due to network issues", e); } }