Example usage for android.media.tv TvInputManager ACTION_SETUP_INPUTS

List of usage examples for android.media.tv TvInputManager ACTION_SETUP_INPUTS

Introduction

In this page you can find the example usage for android.media.tv TvInputManager ACTION_SETUP_INPUTS.

Prototype

String ACTION_SETUP_INPUTS

To view the source code for android.media.tv TvInputManager ACTION_SETUP_INPUTS.

Click Source Link

Document

Activity action to set up channel sources i.e. TV inputs of type TvInputInfo#TYPE_TUNER .

Usage

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 www  .  j ava2  s .  c  om*/

    // 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;
}