1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.content.browser; 6 7 import android.annotation.SuppressLint; 8 import android.app.Activity; 9 import android.os.Build; 10 import android.view.Gravity; 11 import android.view.View; 12 import android.view.ViewGroup; 13 import android.view.WindowManager; 14 import android.widget.FrameLayout; 15 16 /** 17 * Uses an existing Activity to handle displaying video in full screen. 18 */ 19 public class ActivityContentVideoViewClient implements ContentVideoViewClient { 20 private final Activity mActivity; 21 private View mView; 22 23 public ActivityContentVideoViewClient(Activity activity) { 24 this.mActivity = activity; 25 } 26 27 @Override 28 public boolean onShowCustomView(View view) { 29 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView(); 30 decor.addView(view, 0, 31 new FrameLayout.LayoutParams( 32 ViewGroup.LayoutParams.MATCH_PARENT, 33 ViewGroup.LayoutParams.MATCH_PARENT, 34 Gravity.CENTER)); 35 setSystemUiVisibility(decor, true); 36 mView = view; 37 return true; 38 } 39 40 @Override 41 public void onDestroyContentVideoView() { 42 FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView(); 43 decor.removeView(mView); 44 setSystemUiVisibility(decor, false); 45 mView = null; 46 } 47 48 @Override 49 public View getVideoLoadingProgressView() { 50 return null; 51 } 52 53 /** 54 * Returns the system ui visibility after entering or exiting fullscreen. 55 * @param view The decor view belongs to the activity window 56 * @param enterFullscreen True if video is going fullscreen, or false otherwise. 57 */ 58 @SuppressLint("InlinedApi") 59 private void setSystemUiVisibility(View view, boolean enterFullscreen) { 60 if (enterFullscreen) { 61 mActivity.getWindow().setFlags( 62 WindowManager.LayoutParams.FLAG_FULLSCREEN, 63 WindowManager.LayoutParams.FLAG_FULLSCREEN); 64 } else { 65 mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 66 } 67 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 68 return; 69 } 70 71 int systemUiVisibility = view.getSystemUiVisibility(); 72 int flags = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION 73 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN 74 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 75 | View.SYSTEM_UI_FLAG_FULLSCREEN 76 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; 77 if (enterFullscreen) { 78 systemUiVisibility |= flags; 79 } else { 80 systemUiVisibility &= ~flags; 81 } 82 view.setSystemUiVisibility(systemUiVisibility); 83 } 84 } 85