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