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 android.support.v17.leanback.app; 15 16 import android.os.Bundle; 17 import android.support.v17.leanback.R; 18 import android.view.LayoutInflater; 19 import android.view.SurfaceHolder; 20 import android.view.SurfaceView; 21 import android.view.View; 22 import android.view.ViewGroup; 23 24 /** 25 * Subclass of {@link PlaybackSupportFragment} that is responsible for providing a {@link SurfaceView} 26 * and rendering video. 27 */ 28 public class VideoSupportFragment extends PlaybackSupportFragment { 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 onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)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(getContext()).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(PlaybackSupportFragment.BG_LIGHT); 70 return root; 71 } 72 73 /** 74 * Adds {@link SurfaceHolder.Callback} to {@link android.view.SurfaceView}. 75 */ setSurfaceHolderCallback(SurfaceHolder.Callback callback)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 onVideoSizeChanged(int width, int height)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 */ getSurfaceView()107 public SurfaceView getSurfaceView() { 108 return mVideoSurface; 109 } 110 111 @Override onDestroyView()112 public void onDestroyView() { 113 mVideoSurface = null; 114 mState = SURFACE_NOT_CREATED; 115 super.onDestroyView(); 116 } 117 } 118