13bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu/*
23bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Copyright (C) 2016 The Android Open Source Project
33bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu *
43bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Licensed under the Apache License, Version 2.0 (the "License");
53bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * you may not use this file except in compliance with the License.
63bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * You may obtain a copy of the License at
73bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu *
83bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu *      http://www.apache.org/licenses/LICENSE-2.0
93bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu *
103bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Unless required by applicable law or agreed to in writing, software
113bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * distributed under the License is distributed on an "AS IS" BASIS,
123bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * See the License for the specific language governing permissions and
143bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * limitations under the License.
153bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */
163bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
173bcad88cbf4488e747d84893c35f2351b8f84afeDake Gupackage com.example.android.leanback;
183bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
193bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.content.Context;
203bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.os.Build;
213bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.os.Bundle;
223bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.v17.leanback.app.PlaybackFragmentGlueHost;
233bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.v17.leanback.widget.Action;
243bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.v17.leanback.widget.ArrayObjectAdapter;
253bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.v17.leanback.widget.ClassPresenterSelector;
263bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.v17.leanback.widget.HeaderItem;
273bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.v17.leanback.widget.ListRow;
283bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.v17.leanback.widget.ListRowPresenter;
293bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.support.v17.leanback.widget.SparseArrayObjectAdapter;
303bcad88cbf4488e747d84893c35f2351b8f84afeDake Guimport android.util.Log;
313bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
323bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu/**
333bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu * Example of PlaybackFragment working with a PlaybackControlGlue.
343bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu */
353bcad88cbf4488e747d84893c35f2351b8f84afeDake Gupublic class PlaybackTransportControlFragment
363bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        extends android.support.v17.leanback.app.PlaybackFragment
373bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        implements PlaybackTransportControlActivity.PictureInPictureListener {
383bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    private static final String TAG = "TransportFragment";
393bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
403bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    /**
413bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu     * Change this to choose a different overlay background.
423bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu     */
433bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    private static final int BACKGROUND_TYPE = BG_DARK;
443bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
453bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    /**
463bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu     * Change the number of related content rows.
473bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu     */
483bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    private static final int RELATED_CONTENT_ROWS = 3;
493bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
503bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    private static final int ROW_CONTROLS = 0;
513bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
523bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    private PlaybackTransportControlGlueSample mGlue;
533bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
543bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public SparseArrayObjectAdapter getAdapter() {
553bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        return (SparseArrayObjectAdapter) super.getAdapter();
563bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
573bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
583bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Override
593bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void onCreate(Bundle savedInstanceState) {
603bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        Log.i(TAG, "onCreate");
613bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        super.onCreate(savedInstanceState);
623bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
633bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        setBackgroundType(BACKGROUND_TYPE);
643bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
653bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        createComponents(getActivity());
663bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
673bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
683bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    private void createComponents(Context context) {
693bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue = new PlaybackTransportControlGlueSample(context, new PlayerAdapter()) {
703bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            @Override
713bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            public void onActionClicked(Action action) {
723bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                if (action.getId() == R.id.lb_control_picture_in_picture) {
733bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                    if (Build.VERSION.SDK_INT >= 24) {
743bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                        getActivity().enterPictureInPictureMode();
753bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                    }
763bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                    return;
773bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                }
783bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                super.onActionClicked(action);
793bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            }
803bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
813bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        };
823bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
833bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setTitle("Title");
843bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setSubtitle("Android developer");
853bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setHost(new PlaybackFragmentGlueHost(this));
863bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        mGlue.setSeekProvider(new PlaybackSeekDataProviderSample(
873bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu                PlayerAdapter.FAUX_DURATION, 100));
883bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
893bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        ClassPresenterSelector selector = new ClassPresenterSelector();
903bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        selector.addClassPresenter(ListRow.class, new ListRowPresenter());
913bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        setAdapter(new SparseArrayObjectAdapter(selector));
923bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
933bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        // Add related content rows
943bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        for (int i = 0; i < RELATED_CONTENT_ROWS; ++i) {
953bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter());
963bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            listRowAdapter.add("Some related content");
973bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            listRowAdapter.add("Other related content");
983bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            HeaderItem header = new HeaderItem(i, "Row " + i);
993bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            getAdapter().set(ROW_CONTROLS + 1 + i, new ListRow(header, listRowAdapter));
1003bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        }
1013bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
1023bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1033bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Override
1043bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void onStart() {
1053bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        super.onStart();
1063bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        ((PlaybackTransportControlActivity) getActivity()).registerPictureInPictureListener(this);
1073bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
1083bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1093bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Override
1103bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void onStop() {
1113bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        ((PlaybackTransportControlActivity) getActivity()).unregisterPictureInPictureListener(this);
1123bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        super.onStop();
1133bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
1143bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu
1153bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    @Override
1163bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
1173bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        if (isInPictureInPictureMode) {
1183bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            // Hide the controls in picture-in-picture mode.
1193bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            setControlsOverlayAutoHideEnabled(true);
1203bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            hideControlsOverlay(true);
1213bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        } else {
1223bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu            setControlsOverlayAutoHideEnabled(mGlue.isPlaying());
1233bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu        }
1243bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu    }
1253bcad88cbf4488e747d84893c35f2351b8f84afeDake Gu}
126