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