1 // Copyright 2012 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.
5 package org.chromium.content_shell;
7 import android.app.Activity;
8 import android.content.Context;
9 import android.util.AttributeSet;
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.widget.FrameLayout;
14 import org.chromium.base.CalledByNative;
15 import org.chromium.base.CommandLine;
16 import org.chromium.base.JNINamespace;
17 import org.chromium.base.ThreadUtils;
18 import org.chromium.content.browser.ActivityContentVideoViewClient;
19 import org.chromium.content.browser.ContentVideoViewClient;
20 import org.chromium.content.browser.ContentViewClient;
21 import org.chromium.content.browser.ContentViewCore;
22 import org.chromium.content.browser.ContentViewRenderView;
23 import org.chromium.content.common.ContentSwitches;
24 import org.chromium.ui.base.WindowAndroid;
27 * Container and generator of ShellViews.
29 @JNINamespace("content")
30 public class ShellManager extends FrameLayout {
32 public static final String DEFAULT_SHELL_URL = "http://www.google.com";
33 private static boolean sStartup = true;
34 private WindowAndroid mWindow;
35 private Shell mActiveShell;
37 private String mStartupUrl = DEFAULT_SHELL_URL;
39 // The target for all content rendering.
40 private ContentViewRenderView mContentViewRenderView;
41 private ContentViewClient mContentViewClient;
44 * Constructor for inflating via XML.
46 public ShellManager(final Context context, AttributeSet attrs) {
47 super(context, attrs);
49 mContentViewClient = new ContentViewClient() {
51 public ContentVideoViewClient getContentVideoViewClient() {
52 return new ActivityContentVideoViewClient((Activity) context) {
54 public boolean onShowCustomView(View view) {
55 boolean success = super.onShowCustomView(view);
56 if (!CommandLine.getInstance().hasSwitch(
57 ContentSwitches.DISABLE_OVERLAY_FULLSCREEN_VIDEO_SUBTITLE)) {
58 setOverlayVideoMode(true);
64 public void onDestroyContentVideoView() {
65 super.onDestroyContentVideoView();
66 if (!CommandLine.getInstance().hasSwitch(
67 ContentSwitches.DISABLE_OVERLAY_FULLSCREEN_VIDEO_SUBTITLE)) {
68 setOverlayVideoMode(false);
77 * @param window The window used to generate all shells.
79 public void setWindow(WindowAndroid window) {
80 assert window != null;
82 mContentViewRenderView = new ContentViewRenderView(getContext()) {
84 protected void onReadyToRender() {
86 mActiveShell.loadUrl(mStartupUrl);
91 mContentViewRenderView.onNativeLibraryLoaded(window);
95 * @return The window used to generate all shells.
97 public WindowAndroid getWindow() {
102 * Sets the startup URL for new shell windows.
104 public void setStartupUrl(String url) {
109 * @return The currently visible shell view or null if one is not showing.
111 public Shell getActiveShell() {
116 * Creates a new shell pointing to the specified URL.
117 * @param url The URL the shell should load upon creation.
119 public void launchShell(String url) {
120 ThreadUtils.assertOnUiThread();
121 Shell previousShell = mActiveShell;
122 nativeLaunchShell(url);
123 if (previousShell != null) previousShell.close();
127 * Enter or leave overlay video mode.
128 * @param enabled Whether overlay mode is enabled.
130 public void setOverlayVideoMode(boolean enabled) {
131 if (mContentViewRenderView == null) return;
132 mContentViewRenderView.setOverlayVideoMode(enabled);
135 @SuppressWarnings("unused")
137 private Object createShell(long nativeShellPtr) {
138 assert mContentViewRenderView != null;
139 LayoutInflater inflater =
140 (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
141 Shell shellView = (Shell) inflater.inflate(R.layout.shell_view, null);
142 shellView.initialize(nativeShellPtr, mWindow, mContentViewClient);
144 // TODO(tedchoc): Allow switching back to these inactive shells.
145 if (mActiveShell != null) removeShell(mActiveShell);
147 showShell(shellView);
151 private void showShell(Shell shellView) {
152 shellView.setContentViewRenderView(mContentViewRenderView);
153 addView(shellView, new FrameLayout.LayoutParams(
154 FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
155 mActiveShell = shellView;
156 ContentViewCore contentViewCore = mActiveShell.getContentViewCore();
157 if (contentViewCore != null) {
158 mContentViewRenderView.setCurrentContentViewCore(contentViewCore);
159 contentViewCore.onShow();
164 private void removeShell(Shell shellView) {
165 if (shellView == mActiveShell) mActiveShell = null;
166 if (shellView.getParent() == null) return;
167 ContentViewCore contentViewCore = shellView.getContentViewCore();
168 if (contentViewCore != null) contentViewCore.onHide();
169 shellView.setContentViewRenderView(null);
170 removeView(shellView);
173 private static native void nativeInit(Object shellManagerInstance);
174 private static native void nativeLaunchShell(String url);