1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.app;
15
16import android.os.Bundle;
17import android.support.v17.leanback.R;
18import android.view.LayoutInflater;
19import android.view.SurfaceHolder;
20import android.view.SurfaceView;
21import android.view.View;
22import android.view.ViewGroup;
23
24/**
25 * Subclass of {@link PlaybackFragment} that is responsible for providing a {@link SurfaceView}
26 * and rendering video.
27 */
28public class VideoFragment extends PlaybackFragment {
29    static final int SURFACE_NOT_CREATED = 0;
30    static final int SURFACE_CREATED = 1;
31
32    SurfaceView mVideoSurface;
33    SurfaceHolder.Callback mMediaPlaybackCallback;
34
35    int mState = SURFACE_NOT_CREATED;
36
37    @Override
38    public View onCreateView(
39            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
40        ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);
41        mVideoSurface = (SurfaceView) LayoutInflater.from(FragmentUtil.getContext(this)).inflate(
42                R.layout.lb_video_surface, root, false);
43        root.addView(mVideoSurface, 0);
44        mVideoSurface.getHolder().addCallback(new SurfaceHolder.Callback() {
45
46            @Override
47            public void surfaceCreated(SurfaceHolder holder) {
48                if (mMediaPlaybackCallback != null) {
49                    mMediaPlaybackCallback.surfaceCreated(holder);
50                }
51                mState = SURFACE_CREATED;
52            }
53
54            @Override
55            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
56                if (mMediaPlaybackCallback != null) {
57                    mMediaPlaybackCallback.surfaceChanged(holder, format, width, height);
58                }
59            }
60
61            @Override
62            public void surfaceDestroyed(SurfaceHolder holder) {
63                if (mMediaPlaybackCallback != null) {
64                    mMediaPlaybackCallback.surfaceDestroyed(holder);
65                }
66                mState = SURFACE_NOT_CREATED;
67            }
68        });
69        setBackgroundType(PlaybackFragment.BG_LIGHT);
70        return root;
71    }
72
73    /**
74     * Adds {@link SurfaceHolder.Callback} to {@link android.view.SurfaceView}.
75     */
76    public void setSurfaceHolderCallback(SurfaceHolder.Callback callback) {
77        mMediaPlaybackCallback = callback;
78
79        if (callback != null) {
80            if (mState == SURFACE_CREATED) {
81                mMediaPlaybackCallback.surfaceCreated(mVideoSurface.getHolder());
82            }
83        }
84    }
85
86    @Override
87    protected void onVideoSizeChanged(int width, int height) {
88        int screenWidth = getView().getWidth();
89        int screenHeight = getView().getHeight();
90
91        ViewGroup.LayoutParams p = mVideoSurface.getLayoutParams();
92        if (screenWidth * height > width * screenHeight) {
93            // fit in screen height
94            p.height = screenHeight;
95            p.width = screenHeight * width / height;
96        } else {
97            // fit in screen width
98            p.width = screenWidth;
99            p.height = screenWidth * height / width;
100        }
101        mVideoSurface.setLayoutParams(p);
102    }
103
104    /**
105     * Returns the surface view.
106     */
107    public SurfaceView getSurfaceView() {
108        return mVideoSurface;
109    }
110
111    @Override
112    public void onDestroyView() {
113        mVideoSurface = null;
114        mState = SURFACE_NOT_CREATED;
115        super.onDestroyView();
116    }
117}
118