1 // CHECKSTYLE:OFF Generated code 2 /* This file is auto-generated from VideoSupportFragment.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 */ 17 package androidx.leanback.app; 18 19 import android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.SurfaceHolder; 22 import android.view.SurfaceView; 23 import android.view.View; 24 import android.view.ViewGroup; 25 26 import androidx.leanback.R; 27 28 /** 29 * Subclass of {@link PlaybackFragment} that is responsible for providing a {@link SurfaceView} 30 * and rendering video. 31 * @deprecated use {@link VideoSupportFragment} 32 */ 33 @Deprecated 34 public class VideoFragment extends PlaybackFragment { 35 static final int SURFACE_NOT_CREATED = 0; 36 static final int SURFACE_CREATED = 1; 37 38 SurfaceView mVideoSurface; 39 SurfaceHolder.Callback mMediaPlaybackCallback; 40 41 int mState = SURFACE_NOT_CREATED; 42 43 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)44 public View onCreateView( 45 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 46 ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); 47 mVideoSurface = (SurfaceView) LayoutInflater.from(FragmentUtil.getContext(VideoFragment.this)).inflate( 48 R.layout.lb_video_surface, root, false); 49 root.addView(mVideoSurface, 0); 50 mVideoSurface.getHolder().addCallback(new SurfaceHolder.Callback() { 51 52 @Override 53 public void surfaceCreated(SurfaceHolder holder) { 54 if (mMediaPlaybackCallback != null) { 55 mMediaPlaybackCallback.surfaceCreated(holder); 56 } 57 mState = SURFACE_CREATED; 58 } 59 60 @Override 61 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 62 if (mMediaPlaybackCallback != null) { 63 mMediaPlaybackCallback.surfaceChanged(holder, format, width, height); 64 } 65 } 66 67 @Override 68 public void surfaceDestroyed(SurfaceHolder holder) { 69 if (mMediaPlaybackCallback != null) { 70 mMediaPlaybackCallback.surfaceDestroyed(holder); 71 } 72 mState = SURFACE_NOT_CREATED; 73 } 74 }); 75 setBackgroundType(PlaybackFragment.BG_LIGHT); 76 return root; 77 } 78 79 /** 80 * Adds {@link SurfaceHolder.Callback} to {@link android.view.SurfaceView}. 81 */ setSurfaceHolderCallback(SurfaceHolder.Callback callback)82 public void setSurfaceHolderCallback(SurfaceHolder.Callback callback) { 83 mMediaPlaybackCallback = callback; 84 85 if (callback != null) { 86 if (mState == SURFACE_CREATED) { 87 mMediaPlaybackCallback.surfaceCreated(mVideoSurface.getHolder()); 88 } 89 } 90 } 91 92 @Override onVideoSizeChanged(int width, int height)93 protected void onVideoSizeChanged(int width, int height) { 94 int screenWidth = getView().getWidth(); 95 int screenHeight = getView().getHeight(); 96 97 ViewGroup.LayoutParams p = mVideoSurface.getLayoutParams(); 98 if (screenWidth * height > width * screenHeight) { 99 // fit in screen height 100 p.height = screenHeight; 101 p.width = screenHeight * width / height; 102 } else { 103 // fit in screen width 104 p.width = screenWidth; 105 p.height = screenWidth * height / width; 106 } 107 mVideoSurface.setLayoutParams(p); 108 } 109 110 /** 111 * Returns the surface view. 112 */ getSurfaceView()113 public SurfaceView getSurfaceView() { 114 return mVideoSurface; 115 } 116 117 @Override onDestroyView()118 public void onDestroyView() { 119 mVideoSurface = null; 120 mState = SURFACE_NOT_CREATED; 121 super.onDestroyView(); 122 } 123 } 124