List of usage examples for android.media.tv TvContract isChannelUriForPassthroughInput
public static boolean isChannelUriForPassthroughInput(@NonNull Uri uri)
From source file:com.android.tv.MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { if (DEBUG)//from w w w .j a v a 2 s . com Log.d(TAG, "onCreate()"); super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M && !PermissionUtils.hasAccessAllEpg(this)) { Toast.makeText(this, R.string.msg_not_supported_device, Toast.LENGTH_LONG).show(); finish(); return; } boolean skipToShowOnboarding = getIntent().getAction() == Intent.ACTION_VIEW && TvContract.isChannelUriForPassthroughInput(getIntent().getData()); if (Features.ONBOARDING_EXPERIENCE.isEnabled(this) && OnboardingUtils.needToShowOnboarding(this) && !skipToShowOnboarding && !TvCommonUtils.isRunningInTest()) { // TODO: The onboarding is turned off in test, because tests are broken by the // onboarding. We need to enable the feature for tests later. startActivity(OnboardingActivity.buildIntent(this, getIntent())); finish(); return; } TvApplication tvApplication = (TvApplication) getApplication(); tvApplication.getMainActivityWrapper().onMainActivityCreated(this); if (BuildConfig.ENG && SystemProperties.ALLOW_STRICT_MODE.getValue()) { Toast.makeText(this, "Using Strict Mode for eng builds", Toast.LENGTH_SHORT).show(); } mTracker = tvApplication.getTracker(); mTvInputManagerHelper = tvApplication.getTvInputManagerHelper(); mTvInputManagerHelper.addCallback(mTvInputCallback); mUsbTunerInputId = UsbTunerTvInputService.getInputId(this); mChannelDataManager = tvApplication.getChannelDataManager(); mProgramDataManager = tvApplication.getProgramDataManager(); mProgramDataManager.addOnCurrentProgramUpdatedListener(Channel.INVALID_ID, mOnCurrentProgramUpdatedListener); mProgramDataManager.setPrefetchEnabled(true); mChannelTuner = new ChannelTuner(mChannelDataManager, mTvInputManagerHelper); mChannelTuner.addListener(mChannelTunerListener); mChannelTuner.start(); mPipInputManager = new PipInputManager(this, mTvInputManagerHelper, mChannelTuner); mPipInputManager.start(); mMemoryManageables.add(mProgramDataManager); mMemoryManageables.add(ImageCache.getInstance()); mMemoryManageables.add(TvContentRatingCache.getInstance()); if (CommonFeatures.DVR.isEnabled(this) && BuildCompat.isAtLeastN()) { mDvrManager = tvApplication.getDvrManager(); mDvrDataManager = tvApplication.getDvrDataManager(); } DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); Display display = displayManager.getDisplay(Display.DEFAULT_DISPLAY); Point size = new Point(); display.getSize(size); int screenWidth = size.x; int screenHeight = size.y; mDefaultRefreshRate = display.getRefreshRate(); mOverlayRootView = (OverlayRootView) getLayoutInflater().inflate(R.layout.overlay_root_view, null, false); setContentView(R.layout.activity_tv); mTvView = (TunableTvView) findViewById(R.id.main_tunable_tv_view); int shrunkenTvViewHeight = getResources().getDimensionPixelSize(R.dimen.shrunken_tvview_height); mTvView.initialize((AppLayerTvView) findViewById(R.id.main_tv_view), false, screenHeight, shrunkenTvViewHeight); mTvView.setOnUnhandledInputEventListener(new OnUnhandledInputEventListener() { @Override public boolean onUnhandledInputEvent(InputEvent event) { if (isKeyEventBlocked()) { return true; } if (event instanceof KeyEvent) { KeyEvent keyEvent = (KeyEvent) event; if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.isLongPress()) { if (onKeyLongPress(keyEvent.getKeyCode(), keyEvent)) { return true; } } if (keyEvent.getAction() == KeyEvent.ACTION_UP) { return onKeyUp(keyEvent.getKeyCode(), keyEvent); } else if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) { return onKeyDown(keyEvent.getKeyCode(), keyEvent); } } return false; } }); mTimeShiftManager = new TimeShiftManager(this, mTvView, mProgramDataManager, mTracker, new OnCurrentProgramUpdatedListener() { @Override public void onCurrentProgramUpdated(long channelId, Program program) { updateMediaSession(); switch (mTimeShiftManager.getLastActionId()) { case TimeShiftManager.TIME_SHIFT_ACTION_ID_REWIND: case TimeShiftManager.TIME_SHIFT_ACTION_ID_FAST_FORWARD: case TimeShiftManager.TIME_SHIFT_ACTION_ID_JUMP_TO_PREVIOUS: case TimeShiftManager.TIME_SHIFT_ACTION_ID_JUMP_TO_NEXT: updateChannelBannerAndShowIfNeeded(UPDATE_CHANNEL_BANNER_REASON_FORCE_SHOW); break; default: updateChannelBannerAndShowIfNeeded(UPDATE_CHANNEL_BANNER_REASON_UPDATE_INFO); break; } } }); mPipView = (TunableTvView) findViewById(R.id.pip_tunable_tv_view); mPipView.initialize((AppLayerTvView) findViewById(R.id.pip_tv_view), true, screenHeight, shrunkenTvViewHeight); if (!PermissionUtils.hasAccessWatchedHistory(this)) { WatchedHistoryManager watchedHistoryManager = new WatchedHistoryManager(getApplicationContext()); watchedHistoryManager.start(); mTvView.setWatchedHistoryManager(watchedHistoryManager); } mTvViewUiManager = new TvViewUiManager(this, mTvView, mPipView, (FrameLayout) findViewById(android.R.id.content), mTvOptionsManager); mPipView.setFixedSurfaceSize(screenWidth / 2, screenHeight / 2); mPipView.setBlockScreenType(TunableTvView.BLOCK_SCREEN_TYPE_SHRUNKEN_TV_VIEW); ViewGroup sceneContainer = (ViewGroup) findViewById(R.id.scene_container); mChannelBannerView = (ChannelBannerView) getLayoutInflater().inflate(R.layout.channel_banner, sceneContainer, false); mKeypadChannelSwitchView = (KeypadChannelSwitchView) getLayoutInflater() .inflate(R.layout.keypad_channel_switch, sceneContainer, false); InputBannerView inputBannerView = (InputBannerView) getLayoutInflater().inflate(R.layout.input_banner, sceneContainer, false); SelectInputView selectInputView = (SelectInputView) getLayoutInflater().inflate(R.layout.select_input, sceneContainer, false); selectInputView.setOnInputSelectedCallback(new OnInputSelectedCallback() { @Override public void onTunerInputSelected() { Channel currentChannel = mChannelTuner.getCurrentChannel(); if (currentChannel != null && !currentChannel.isPassthrough()) { hideOverlays(); } else { tuneToLastWatchedChannelForTunerInput(); } } @Override public void onPassthroughInputSelected(TvInputInfo input) { Channel currentChannel = mChannelTuner.getCurrentChannel(); String currentInputId = currentChannel == null ? null : currentChannel.getInputId(); if (TextUtils.equals(input.getId(), currentInputId)) { hideOverlays(); } else { tuneToChannel(Channel.createPassthroughChannel(input.getId())); } } private void hideOverlays() { getOverlayManager().hideOverlays(TvOverlayManager.FLAG_HIDE_OVERLAYS_KEEP_DIALOG | TvOverlayManager.FLAG_HIDE_OVERLAYS_KEEP_SIDE_PANELS | TvOverlayManager.FLAG_HIDE_OVERLAYS_KEEP_PROGRAM_GUIDE | TvOverlayManager.FLAG_HIDE_OVERLAYS_KEEP_MENU | TvOverlayManager.FLAG_HIDE_OVERLAYS_KEEP_FRAGMENT); } }); mSearchFragment = new ProgramGuideSearchFragment(); mOverlayManager = new TvOverlayManager(this, mChannelTuner, mKeypadChannelSwitchView, mChannelBannerView, inputBannerView, selectInputView, sceneContainer, mSearchFragment); mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); mAudioFocusStatus = AudioManager.AUDIOFOCUS_LOSS; mMediaSession = new MediaSession(this, MEDIA_SESSION_TAG); mMediaSession.setCallback(new MediaSession.Callback() { @Override public boolean onMediaButtonEvent(Intent mediaButtonIntent) { // Consume the media button event here. Should not send it to other apps. return true; } }); mMediaSession .setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); mNowPlayingCardWidth = getResources().getDimensionPixelSize(R.dimen.notif_card_img_max_width); mNowPlayingCardHeight = getResources().getDimensionPixelSize(R.dimen.notif_card_img_height); mTvViewUiManager.restoreDisplayMode(false); if (!handleIntent(getIntent())) { finish(); return; } mAudioCapabilitiesReceiver = new AudioCapabilitiesReceiver(this, new AudioCapabilitiesReceiver.OnAc3PassthroughCapabilityChangeListener() { @Override public void onAc3PassthroughCapabilityChange(boolean capability) { mAc3PassthroughSupported = capability; } }); mAudioCapabilitiesReceiver.register(); mAccessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE); mSendConfigInfoRecurringRunner = new RecurringRunner(this, TimeUnit.DAYS.toMillis(1), new SendConfigInfoRunnable(mTracker, mTvInputManagerHelper), null); mSendConfigInfoRecurringRunner.start(); mChannelStatusRecurringRunner = SendChannelStatusRunnable.startChannelStatusRecurringRunner(this, mTracker, mChannelDataManager); // To avoid not updating Rating systems when changing language. mTvInputManagerHelper.getContentRatingsManager().update(); initForTest(); }
From source file:com.android.tv.MainActivity.java
private void resumeTvIfNeeded() { if (DEBUG)//from www .j a v a2 s. c om Log.d(TAG, "resumeTvIfNeeded()"); if (!mTvView.isPlaying() || mInitChannelUri != null || (mShouldTuneToTunerChannel && mChannelTuner.isCurrentChannelPassthrough())) { if (TvContract.isChannelUriForPassthroughInput(mInitChannelUri)) { // The target input may not be ready yet, especially, just after screen on. String inputId = mInitChannelUri.getPathSegments().get(1); TvInputInfo input = mTvInputManagerHelper.getTvInputInfo(inputId); if (input == null) { input = mTvInputManagerHelper.getTvInputInfo(mParentInputIdWhenScreenOff); if (input == null) { SoftPreconditions.checkState(false, TAG, "Input disappear." + input); finish(); } else { mInitChannelUri = TvContract.buildChannelUriForPassthroughInput(input.getId()); } } } mParentInputIdWhenScreenOff = null; startTv(mInitChannelUri); mInitChannelUri = null; } // Make sure TV app has the main TV view to handle the case that TvView is used in other // application. restoreMainTvView(); mTvView.setBlockScreenType(getDesiredBlockScreenType()); }
From source file:com.android.tv.MainActivity.java
private void startTv(Uri channelUri) { if (DEBUG)/*from w ww.java 2s . c o m*/ Log.d(TAG, "startTv Uri=" + channelUri); if ((channelUri == null || !TvContract.isChannelUriForPassthroughInput(channelUri)) && mChannelTuner.isCurrentChannelPassthrough()) { // For passthrough TV input, channelUri is always given. If TV app is launched // by TV app icon in a launcher, channelUri is null. So if passthrough TV input // is playing, we stop the passthrough TV input. stopTv(); } SoftPreconditions.checkState( TvContract.isChannelUriForPassthroughInput(channelUri) || mChannelTuner.areAllChannelsLoaded(), TAG, "startTV assumes that ChannelDataManager is already loaded."); if (mTvView.isPlaying()) { // TV has already started. if (channelUri == null) { // Simply adjust the volume without tune. setVolumeByAudioFocusStatus(); return; } if (channelUri.equals(mChannelTuner.getCurrentChannelUri())) { // The requested channel is already tuned. setVolumeByAudioFocusStatus(); return; } stopTv(); } if (mChannelTuner.getCurrentChannel() != null) { Log.w(TAG, "The current channel should be reset before"); mChannelTuner.resetCurrentChannel(); } if (channelUri == null) { // If any initial channel id is not given, remember the last channel the user watched. long channelId = Utils.getLastWatchedChannelId(this); if (channelId != Channel.INVALID_ID) { channelUri = TvContract.buildChannelUri(channelId); } } if (channelUri == null) { mChannelTuner.moveToChannel(mChannelTuner.findNearestBrowsableChannel(0)); } else { if (TvContract.isChannelUriForPassthroughInput(channelUri)) { Channel channel = Channel.createPassthroughChannel(channelUri); mChannelTuner.moveToChannel(channel); } else { long channelId = ContentUris.parseId(channelUri); Channel channel = mChannelDataManager.getChannel(channelId); if (channel == null || !mChannelTuner.moveToChannel(channel)) { mChannelTuner.moveToChannel(mChannelTuner.findNearestBrowsableChannel(0)); Log.w(TAG, "The requested channel (id=" + channelId + ") doesn't exist. " + "The first channel will be tuned to."); } } } mTvView.start(mTvInputManagerHelper); setVolumeByAudioFocusStatus(); if (mRecordingUri != null) { playRecording(mRecordingUri); mRecordingUri = null; } else { tune(); } }
From source file:com.android.tv.MainActivity.java
private boolean handleIntent(Intent intent) { // Reset the closed caption settings when the activity is 1)created or 2) restarted. // And do not reset while TvView is playing. if (!mTvView.isPlaying()) { mCaptionSettings = new CaptionSettings(this); }/*from w w w .j ava 2 s .co m*/ // Handle the passed key press, if any. Note that only the key codes that are currently // handled in the TV app will be handled via Intent. // TODO: Consider defining a separate intent filter as passing data of mime type // vnd.android.cursor.item/channel isn't really necessary here. int keyCode = intent.getIntExtra(Utils.EXTRA_KEY_KEYCODE, KeyEvent.KEYCODE_UNKNOWN); if (keyCode != KeyEvent.KEYCODE_UNKNOWN) { if (DEBUG) Log.d(TAG, "Got an intent with keycode: " + keyCode); KeyEvent event = new KeyEvent(KeyEvent.ACTION_UP, keyCode); onKeyUp(keyCode, event); return true; } mShouldTuneToTunerChannel = intent.getBooleanExtra(Utils.EXTRA_KEY_FROM_LAUNCHER, false); mInitChannelUri = null; String extraAction = intent.getStringExtra(Utils.EXTRA_KEY_ACTION); if (!TextUtils.isEmpty(extraAction)) { if (DEBUG) Log.d(TAG, "Got an extra action: " + extraAction); if (Utils.EXTRA_ACTION_SHOW_TV_INPUT.equals(extraAction)) { String lastWatchedChannelUri = Utils.getLastWatchedChannelUri(this); if (lastWatchedChannelUri != null) { mInitChannelUri = Uri.parse(lastWatchedChannelUri); } mShowSelectInputView = true; } } if (CommonFeatures.DVR.isEnabled(this) && BuildCompat.isAtLeastN()) { mRecordingUri = intent.getParcelableExtra(Utils.EXTRA_KEY_RECORDING_URI); if (mRecordingUri != null) { return true; } } // TODO: remove the checkState once N API is finalized. SoftPreconditions .checkState(TvInputManager.ACTION_SETUP_INPUTS.equals("android.media.tv.action.SETUP_INPUTS")); if (TvInputManager.ACTION_SETUP_INPUTS.equals(intent.getAction())) { runAfterAttachedToWindow(new Runnable() { @Override public void run() { mOverlayManager.showSetupFragment(); } }); } else if (Intent.ACTION_VIEW.equals(intent.getAction())) { Uri uri = intent.getData(); try { mSource = uri.getQueryParameter(Utils.PARAM_SOURCE); } catch (UnsupportedOperationException e) { // ignore this exception. } // When the URI points to the programs (directory, not an individual item), go to the // program guide. The intention here is to respond to // "content://android.media.tv/program", not "content://android.media.tv/program/XXX". // Later, we might want to add handling of individual programs too. if (Utils.isProgramsUri(uri)) { // The given data is a programs URI. Open the Program Guide. mShowProgramGuide = true; return true; } // In case the channel is given explicitly, use it. mInitChannelUri = uri; if (DEBUG) Log.d(TAG, "ACTION_VIEW with " + mInitChannelUri); if (Channels.CONTENT_URI.equals(mInitChannelUri)) { // Tune to default channel. mInitChannelUri = null; mShouldTuneToTunerChannel = true; return true; } if ((!Utils.isChannelUriForOneChannel(mInitChannelUri) && !Utils.isChannelUriForInput(mInitChannelUri))) { Log.w(TAG, "Malformed channel uri " + mInitChannelUri + " tuning to default instead"); mInitChannelUri = null; return true; } mTuneParams = intent.getExtras(); if (mTuneParams == null) { mTuneParams = new Bundle(); } if (Utils.isChannelUriForTunerInput(mInitChannelUri)) { long channelId = ContentUris.parseId(mInitChannelUri); mTuneParams.putLong(KEY_INIT_CHANNEL_ID, channelId); } else if (TvContract.isChannelUriForPassthroughInput(mInitChannelUri)) { // If mInitChannelUri is for a passthrough TV input. String inputId = mInitChannelUri.getPathSegments().get(1); TvInputInfo input = mTvInputManagerHelper.getTvInputInfo(inputId); if (input == null) { mInitChannelUri = null; Toast.makeText(this, R.string.msg_no_specific_input, Toast.LENGTH_SHORT).show(); return false; } else if (!input.isPassthroughInput()) { mInitChannelUri = null; Toast.makeText(this, R.string.msg_not_passthrough_input, Toast.LENGTH_SHORT).show(); return false; } } else if (mInitChannelUri != null) { // Handle the URI built by TvContract.buildChannelsUriForInput(). // TODO: Change hard-coded "input" to TvContract.PARAM_INPUT. String inputId = mInitChannelUri.getQueryParameter("input"); long channelId = Utils.getLastWatchedChannelIdForInput(this, inputId); if (channelId == Channel.INVALID_ID) { String[] projection = { Channels._ID }; try (Cursor cursor = getContentResolver().query(uri, projection, null, null, null)) { if (cursor != null && cursor.moveToNext()) { channelId = cursor.getLong(0); } } } if (channelId == Channel.INVALID_ID) { // Couldn't find any channel probably because the input hasn't been set up. // Try to set it up. mInitChannelUri = null; mInputToSetUp = mTvInputManagerHelper.getTvInputInfo(inputId); } else { mInitChannelUri = TvContract.buildChannelUri(channelId); mTuneParams.putLong(KEY_INIT_CHANNEL_ID, channelId); } } } return true; }
From source file:com.android.tv.MainActivity.java
private void tune() { if (DEBUG)//from w w w .jav a2 s . co m Log.d(TAG, "tune()"); mTuneDurationTimer.start(); lazyInitializeIfNeeded(LAZY_INITIALIZATION_DELAY); // Prerequisites to be able to tune. if (mInputIdUnderSetup != null) { mTunePending = true; return; } mTunePending = false; final Channel channel = mChannelTuner.getCurrentChannel(); if (!mChannelTuner.isCurrentChannelPassthrough()) { if (mTvInputManagerHelper.getTunerTvInputSize() == 0) { Toast.makeText(this, R.string.msg_no_input, Toast.LENGTH_SHORT).show(); // TODO: Direct the user to a Play Store landing page for TvInputService apps. finish(); return; } SetupUtils setupUtils = SetupUtils.getInstance(this); if (setupUtils.isFirstTune()) { if (!mChannelTuner.areAllChannelsLoaded()) { // tune() will be called, once all channels are loaded. stopTv("tune()", false); return; } if (mChannelDataManager.getChannelCount() > 0) { mOverlayManager.showIntroDialog(); } else if (!Features.ONBOARDING_EXPERIENCE.isEnabled(this)) { mOverlayManager.showSetupFragment(); return; } } if (!TvCommonUtils.isRunningInTest() && mShowNewSourcesFragment && setupUtils.hasUnrecognizedInput(mTvInputManagerHelper)) { // Show new channel sources fragment. runAfterAttachedToWindow(new Runnable() { @Override public void run() { mOverlayManager.runAfterOverlaysAreClosed(new Runnable() { @Override public void run() { mOverlayManager.showNewSourcesFragment(); } }); } }); } mShowNewSourcesFragment = false; if (mChannelTuner.getBrowsableChannelCount() == 0 && mChannelDataManager.getChannelCount() > 0 && !mOverlayManager.getSideFragmentManager().isActive()) { if (!mChannelTuner.areAllChannelsLoaded()) { return; } if (mTvInputManagerHelper.getTunerTvInputSize() == 1) { mOverlayManager.getSideFragmentManager().show(new CustomizeChannelListFragment()); } else { showSettingsFragment(); } return; } // TODO: need to refactor the following code to put in startTv. if (channel == null) { // There is no channel to tune to. stopTv("tune()", false); if (!mChannelDataManager.isDbLoadFinished()) { // Wait until channel data is loaded in order to know the number of channels. // tune() will be retried, once the channel data is loaded. return; } if (mOverlayManager.getSideFragmentManager().isActive()) { return; } mOverlayManager.showSetupFragment(); return; } setupUtils.onTuned(); if (mTuneParams != null) { Long initChannelId = mTuneParams.getLong(KEY_INIT_CHANNEL_ID); if (initChannelId == channel.getId()) { mTuneParams.remove(KEY_INIT_CHANNEL_ID); } else { mTuneParams = null; } } } mIsCurrentChannelUnblockedByUser = false; if (!isUnderShrunkenTvView()) { mLastAllowedRatingForCurrentChannel = null; } mHandler.removeMessages(MSG_UPDATE_CHANNEL_BANNER_BY_INFO_UPDATE); if (mAccessibilityManager.isEnabled()) { // For every tune, we need to inform the tuned channel or input to a user, // if Talkback is turned on. AccessibilityEvent event = AccessibilityEvent.obtain(); event.setClassName(getClass().getName()); event.setPackageName(getPackageName()); event.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT); if (TvContract.isChannelUriForPassthroughInput(channel.getUri())) { TvInputInfo input = mTvInputManagerHelper.getTvInputInfo(channel.getInputId()); event.getText().add(Utils.loadLabel(this, input)); } else if (TextUtils.isEmpty(channel.getDisplayName())) { event.getText().add(channel.getDisplayNumber()); } else { event.getText().add(channel.getDisplayNumber() + " " + channel.getDisplayName()); } mAccessibilityManager.sendAccessibilityEvent(event); } boolean success = mTvView.tuneTo(channel, mTuneParams, mOnTuneListener); mOnTuneListener.onTune(channel, isUnderShrunkenTvView()); mTuneParams = null; if (!success) { Toast.makeText(this, R.string.msg_tune_failed, Toast.LENGTH_SHORT).show(); return; } // Explicitly make the TV view main to make the selected input an HDMI-CEC active source. mTvView.setMain(); scheduleRestoreMainTvView(); if (!isUnderShrunkenTvView()) { if (!channel.isPassthrough()) { addToRecentChannels(channel.getId()); } Utils.setLastWatchedChannel(this, channel); TvApplication.getSingletons(this).getMainActivityWrapper().notifyCurrentChannelChange(this, channel); } checkChannelLockNeeded(mTvView); updateChannelBannerAndShowIfNeeded(UPDATE_CHANNEL_BANNER_REASON_TUNE); if (mActivityResumed) { // requestVisibleBehind should be called after onResume() is called. But, when // launcher is over the TV app and the screen is turned off and on, tune() can // be called during the pause state by mBroadcastReceiver (Intent.ACTION_SCREEN_ON). requestVisibleBehind(true); } updateMediaSession(); }