CastWindowManager.java revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2014 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 5package org.chromium.chromecast.shell; 6 7import android.content.Context; 8import android.graphics.Color; 9import android.util.AttributeSet; 10import android.view.LayoutInflater; 11import android.widget.FrameLayout; 12 13import org.chromium.base.CalledByNative; 14import org.chromium.base.JNINamespace; 15import org.chromium.content.browser.ContentViewCore; 16import org.chromium.content.browser.ContentViewRenderView; 17import org.chromium.ui.base.WindowAndroid; 18 19/** 20 * Container and generator of CastWindow instances. 21 */ 22@JNINamespace("chromecast::shell") 23public class CastWindowManager extends FrameLayout { 24 private static final String TAG = "CastWindowManager"; 25 26 private WindowAndroid mWindow; 27 private CastWindowAndroid mActiveCastWindow; 28 29 // The target for all content rendering. 30 private ContentViewRenderView mContentViewRenderView; 31 32 /** 33 * Delegate to deliver events from the native window. 34 */ 35 public interface Delegate { 36 public void onCreated(); 37 public void onClosed(); 38 } 39 private Delegate mDelegate; 40 41 /** 42 * Constructor for inflating via XML. 43 */ 44 public CastWindowManager(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 nativeInit(this); 47 } 48 49 /** 50 * @param delegate Delegate to handle events. 51 */ 52 public void setDelegate(Delegate delegate) { 53 mDelegate = delegate; 54 } 55 56 /** 57 * @param window Represents the activity window. 58 */ 59 public void setWindow(WindowAndroid window) { 60 assert window != null; 61 mWindow = window; 62 mContentViewRenderView = new ContentViewRenderView(getContext()) { 63 @Override 64 protected void onReadyToRender() { 65 setOverlayVideoMode(true); 66 } 67 }; 68 mContentViewRenderView.onNativeLibraryLoaded(window); 69 // Setting the background color to black avoids rendering a white splash screen 70 // before the players are loaded. See crbug/307113 for details. 71 mContentViewRenderView.setSurfaceViewBackgroundColor(Color.BLACK); 72 } 73 74 /** 75 * @return The window used to generate all shells. 76 */ 77 public WindowAndroid getWindow() { 78 return mWindow; 79 } 80 81 /** 82 * @return The currently visible shell view or null if one is not showing. 83 */ 84 public CastWindowAndroid getActiveCastWindow() { 85 return mActiveCastWindow; 86 } 87 88 /** 89 * Creates a new shell pointing to the specified URL. 90 * @param url The URL the shell should load upon creation. 91 * @return Pointer of native cast shell instance. 92 */ 93 public long launchCastWindow(String url) { 94 return nativeLaunchCastWindow(url); 95 } 96 97 /** 98 * Stops a native cast shell instance created by {@link #launchCastWindow(String)}. 99 * @param nativeCastWindow Pointer of native cast shell instance returned 100 * by {@link #launchCastWindow(String)}. 101 * @param gracefully Whether or not to call RVH::ClosePage to deliver unload event. 102 * @see #launchCastWindow(String) 103 */ 104 public void stopCastWindow(long nativeCastWindow, boolean gracefully) { 105 nativeStopCastWindow(nativeCastWindow, gracefully); 106 } 107 108 @SuppressWarnings("unused") 109 @CalledByNative 110 private Object createCastWindow() { 111 assert mContentViewRenderView != null; 112 LayoutInflater inflater = 113 (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 114 CastWindowAndroid shellView = 115 (CastWindowAndroid) inflater.inflate(R.layout.cast_window_view, null); 116 shellView.setWindow(mWindow); 117 118 if (mActiveCastWindow != null) closeCastWindow(mActiveCastWindow); 119 120 shellView.setContentViewRenderView(mContentViewRenderView); 121 addView(shellView, new FrameLayout.LayoutParams( 122 FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); 123 mActiveCastWindow = shellView; 124 ContentViewCore contentViewCore = mActiveCastWindow.getContentViewCore(); 125 if (contentViewCore != null) { 126 mContentViewRenderView.setCurrentContentViewCore(contentViewCore); 127 contentViewCore.onShow(); 128 } 129 130 if (mDelegate != null) { 131 mDelegate.onCreated(); 132 } 133 134 return shellView; 135 } 136 137 @SuppressWarnings("unused") 138 @CalledByNative 139 private void closeCastWindow(CastWindowAndroid shellView) { 140 if (shellView == mActiveCastWindow) mActiveCastWindow = null; 141 ContentViewCore contentViewCore = shellView.getContentViewCore(); 142 if (contentViewCore != null) contentViewCore.onHide(); 143 shellView.setContentViewRenderView(null); 144 shellView.setWindow(null); 145 removeView(shellView); 146 147 if (mDelegate != null) { 148 mDelegate.onClosed(); 149 } 150 } 151 152 private static native void nativeInit(Object shellManagerInstance); 153 private static native long nativeLaunchCastWindow(String url); 154 private static native void nativeStopCastWindow(long pointerOfNativeCastWindow, 155 boolean gracefully); 156 public static native void nativeEnableDevTools(boolean enable); 157} 158