1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.leanback;
18
19import android.net.Uri;
20import android.os.Bundle;
21import android.support.v17.leanback.app.VideoFragment;
22import android.support.v17.leanback.app.VideoFragmentGlueHost;
23import android.support.v17.leanback.media.MediaPlayerAdapter;
24import android.support.v17.leanback.media.PlaybackBannerControlGlue;
25import android.support.v17.leanback.media.PlaybackGlue;
26
27/**
28 * Fragment used as Control Glue's host
29 */
30public class VideoConsumptionWithDetailCardFragment extends VideoFragment {
31
32    public static final String TAG = "VideoConsumptionWithDetailCardFragment";
33    // A valid video URL to play video. So the progress bar can be seen to reproduce the bug.
34    private static final String VIDEO_URL =
35            "https://storage.googleapis.com/android-tv/Sample videos/"
36                    + "April Fool's 2013/Explore Treasure Mode with Google Maps.mp4";
37    public static final String TITLE = "Diving with Sharks";
38    public static final String SUBTITLE = "A Googler";
39
40    private PlaybackBannerControlGlue<MediaPlayerAdapter> mMediaPlayerGlue;
41    final VideoFragmentGlueHost mHost = new VideoFragmentGlueHost(this);
42
43    /**
44     * helper function for playBackGlue to add/ remove callbacks
45     *
46     * @param glue The playback glue attached to this fragment
47     */
48    private static void playWhenReady(PlaybackGlue glue) {
49        if (glue.isPrepared()) {
50            glue.play();
51        } else {
52            glue.addPlayerCallback(new PlaybackGlue.PlayerCallback() {
53                @Override
54                public void onPreparedStateChanged(PlaybackGlue glue) {
55                    if (glue.isPrepared()) {
56                        glue.removePlayerCallback(this);
57                        glue.play();
58                    }
59                }
60            });
61        }
62    }
63
64    @Override
65    public void onCreate(Bundle savedInstanceState) {
66        super.onCreate(savedInstanceState);
67
68        int[] defuatSpeed = new int[]{1};
69        mMediaPlayerGlue = new PlaybackBannerControlGlue<>(getActivity(), defuatSpeed,
70                new MediaPlayerAdapter(getActivity()));
71        // attach player glue to current host
72        mMediaPlayerGlue.setHost(mHost);
73
74        // add image resource to the PlaybackControlGlue
75        mMediaPlayerGlue.setArt(getActivity().getDrawable(R.drawable.google_map));
76
77        // meta information for video player
78        mMediaPlayerGlue.setTitle(TITLE);
79        mMediaPlayerGlue.setSubtitle(SUBTITLE);
80        mMediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse(VIDEO_URL));
81        playWhenReady(mMediaPlayerGlue);
82        setBackgroundType(BG_LIGHT);
83    }
84
85    @Override
86    public void onPause() {
87        if (mMediaPlayerGlue != null) {
88            mMediaPlayerGlue.pause();
89        }
90        super.onPause();
91    }
92}
93
94