OLD | NEW |
(Empty) | |
| 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 |
| 5 package org.chromium.chromecast.shell; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.graphics.Color; |
| 9 import android.util.AttributeSet; |
| 10 import android.view.LayoutInflater; |
| 11 import android.widget.FrameLayout; |
| 12 |
| 13 import org.chromium.base.CalledByNative; |
| 14 import org.chromium.base.JNINamespace; |
| 15 import org.chromium.content.browser.ContentViewCore; |
| 16 import org.chromium.content.browser.ContentViewRenderView; |
| 17 import org.chromium.ui.base.WindowAndroid; |
| 18 |
| 19 /** |
| 20 * Container and generator of CastWindow instances. |
| 21 */ |
| 22 @JNINamespace("chromecast::shell") |
| 23 public 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(St
ring)}. |
| 99 * @param nativeCastWindow Pointer of native cast shell instance returned |
| 100 * by {@link #launchCastWindow(String)}. |
| 101 * @see #launchCastWindow(String) |
| 102 */ |
| 103 public void stopCastWindow(long nativeCastWindow) { |
| 104 nativeStopCastWindow(nativeCastWindow); |
| 105 } |
| 106 |
| 107 @SuppressWarnings("unused") |
| 108 @CalledByNative |
| 109 private Object createCastWindow() { |
| 110 assert mContentViewRenderView != null; |
| 111 LayoutInflater inflater = |
| 112 (LayoutInflater) getContext().getSystemService(Context.LAYOUT_IN
FLATER_SERVICE); |
| 113 CastWindowAndroid shellView = |
| 114 (CastWindowAndroid) inflater.inflate(R.layout.cast_window_view,
null); |
| 115 shellView.setWindow(mWindow); |
| 116 |
| 117 if (mActiveCastWindow != null) closeCastWindow(mActiveCastWindow); |
| 118 |
| 119 shellView.setContentViewRenderView(mContentViewRenderView); |
| 120 addView(shellView, new FrameLayout.LayoutParams( |
| 121 FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.
MATCH_PARENT)); |
| 122 mActiveCastWindow = shellView; |
| 123 ContentViewCore contentViewCore = mActiveCastWindow.getContentViewCore()
; |
| 124 if (contentViewCore != null) { |
| 125 mContentViewRenderView.setCurrentContentViewCore(contentViewCore); |
| 126 contentViewCore.onShow(); |
| 127 } |
| 128 |
| 129 if (mDelegate != null) { |
| 130 mDelegate.onCreated(); |
| 131 } |
| 132 |
| 133 return shellView; |
| 134 } |
| 135 |
| 136 @SuppressWarnings("unused") |
| 137 @CalledByNative |
| 138 private void closeCastWindow(CastWindowAndroid shellView) { |
| 139 if (shellView == mActiveCastWindow) mActiveCastWindow = null; |
| 140 ContentViewCore contentViewCore = shellView.getContentViewCore(); |
| 141 if (contentViewCore != null) contentViewCore.onHide(); |
| 142 shellView.setContentViewRenderView(null); |
| 143 shellView.setWindow(null); |
| 144 removeView(shellView); |
| 145 |
| 146 if (mDelegate != null) { |
| 147 mDelegate.onClosed(); |
| 148 } |
| 149 } |
| 150 |
| 151 private static native void nativeInit(Object shellManagerInstance); |
| 152 private static native long nativeLaunchCastWindow(String url); |
| 153 private static native void nativeStopCastWindow(long pointerOfNativeCastWind
ow); |
| 154 public static native void nativeEnableDevTools(boolean enable); |
| 155 } |
OLD | NEW |