Example usage for android.media.tv TvContract buildChannelUri

List of usage examples for android.media.tv TvContract buildChannelUri

Introduction

In this page you can find the example usage for android.media.tv TvContract buildChannelUri.

Prototype

public static Uri buildChannelUri(long channelId) 

Source Link

Document

Builds a URI that points to a specific channel.

Usage

From source file:com.felkertech.n.tv.fragments.VideoDetailsFragment.java

private void setupDetailsOverviewRowPresenter() {
    // Set detail background and style.
    DetailsOverviewRowPresenter detailsPresenter = new DetailsOverviewRowPresenter(
            new DetailsDescriptionPresenter());
    detailsPresenter.setBackgroundColor(getResources().getColor(R.color.selected_background));
    detailsPresenter.setStyleLarge(true);

    // Hook up transition element.
    detailsPresenter.setSharedElementEnterTransition(getActivity(), DetailsActivity.SHARED_ELEMENT_NAME);

    detailsPresenter.setOnActionClickedListener(new OnActionClickedListener() {
        @Override//from  w w w.jav  a 2  s  . co  m
        public void onActionClicked(Action action) {
            if (action.getId() == ACTION_EDIT) {
                ActivityUtils.editChannel(getActivity(), jsonChannel.getMediaUrl());
            } else if (action.getId() == ACTION_WATCH) {
                if (ChannelDatabase.getInstance(getActivity()).getHashMap()
                        .containsKey(jsonChannel.getMediaUrl())) {
                    // Open in Live Channels
                    Uri liveChannelsUri = TvContract.buildChannelUri(ChannelDatabase.getInstance(getActivity())
                            .getHashMap().get(jsonChannel.getMediaUrl()));
                    getActivity().startActivity(new Intent(Intent.ACTION_VIEW, liveChannelsUri));
                } else {
                    ActivityUtils.openStream(getActivity(), jsonChannel.getMediaUrl());
                }
            } else if (action.getId() == ACTION_ADD) {
                Log.d(TAG, "Adding " + jsonChannel.toString());
                ActivityUtils.addChannel(getActivity(), gapi, jsonChannel);
                getActivity().setResult(LeanbackActivity.RESULT_CODE_REFRESH_UI);
                getActivity().finish();
            }
        }
    });
    mPresenterSelector.addClassPresenter(DetailsOverviewRow.class, detailsPresenter);
}

From source file:com.android.tv.MainActivity.java

private void startTv(Uri channelUri) {
    if (DEBUG)//  ww w  .  ja v a2s  . c  om
        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  . ja va2 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;
}