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.JNINamespace;
16 import org.chromium.base.ThreadUtils;
17 import org.chromium.content.browser.ActivityContentVideoViewClient;
18 import org.chromium.content.browser.ContentVideoViewClient;
19 import org.chromium.content.browser.ContentViewClient;
20 import org.chromium.content.browser.ContentViewCore;
21 import org.chromium.content.browser.ContentViewRenderView;
22 import org.chromium.ui.base.WindowAndroid;
25 * Container and generator of ShellViews.
27 @JNINamespace("content")
28 public class ShellManager extends FrameLayout {
30 public static final String DEFAULT_SHELL_URL = "http://www.google.com";
31 private static boolean sStartup = true;
32 private WindowAndroid mWindow;
33 private Shell mActiveShell;
35 private String mStartupUrl = DEFAULT_SHELL_URL;
37 // The target for all content rendering.
38 private ContentViewRenderView mContentViewRenderView;
39 private ContentViewClient mContentViewClient;
42 * Constructor for inflating via XML.
44 public ShellManager(final Context context, AttributeSet attrs) {
45 super(context, attrs);
47 mContentViewClient = new ContentViewClient() {
49 public ContentVideoViewClient getContentVideoViewClient() {
50 return new ActivityContentVideoViewClient((Activity) context) {
52 public void enterFullscreenVideo(View view) {
53 super.enterFullscreenVideo(view);
54 setOverlayVideoMode(true);
58 public void exitFullscreenVideo() {
59 super.exitFullscreenVideo();
60 setOverlayVideoMode(false);
68 * @param window The window used to generate all shells.
70 public void setWindow(WindowAndroid window) {
71 assert window != null;
73 mContentViewRenderView = new ContentViewRenderView(getContext()) {
75 protected void onReadyToRender() {
77 mActiveShell.loadUrl(mStartupUrl);
82 mContentViewRenderView.onNativeLibraryLoaded(window);
86 * @return The window used to generate all shells.
88 public WindowAndroid getWindow() {
93 * Sets the startup URL for new shell windows.
95 public void setStartupUrl(String url) {
100 * @return The currently visible shell view or null if one is not showing.
102 public Shell getActiveShell() {
107 * Creates a new shell pointing to the specified URL.
108 * @param url The URL the shell should load upon creation.
110 public void launchShell(String url) {
111 ThreadUtils.assertOnUiThread();
112 Shell previousShell = mActiveShell;
113 nativeLaunchShell(url);
114 if (previousShell != null) previousShell.close();
118 * Enter or leave overlay video mode.
119 * @param enabled Whether overlay mode is enabled.
121 public void setOverlayVideoMode(boolean enabled) {
122 if (mContentViewRenderView == null) return;
123 mContentViewRenderView.setOverlayVideoMode(enabled);
126 @SuppressWarnings("unused")
128 private Object createShell(long nativeShellPtr) {
129 assert mContentViewRenderView != null;
130 LayoutInflater inflater =
131 (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
132 Shell shellView = (Shell) inflater.inflate(R.layout.shell_view, null);
133 shellView.initialize(nativeShellPtr, mWindow, mContentViewClient);
135 // TODO(tedchoc): Allow switching back to these inactive shells.
136 if (mActiveShell != null) removeShell(mActiveShell);
138 showShell(shellView);
142 private void showShell(Shell shellView) {
143 shellView.setContentViewRenderView(mContentViewRenderView);
144 addView(shellView, new FrameLayout.LayoutParams(
145 FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
146 mActiveShell = shellView;
147 ContentViewCore contentViewCore = mActiveShell.getContentViewCore();
148 if (contentViewCore != null) {
149 mContentViewRenderView.setCurrentContentViewCore(contentViewCore);
150 contentViewCore.onShow();
155 private void removeShell(Shell shellView) {
156 if (shellView == mActiveShell) mActiveShell = null;
157 if (shellView.getParent() == null) return;
158 ContentViewCore contentViewCore = shellView.getContentViewCore();
159 if (contentViewCore != null) contentViewCore.onHide();
160 shellView.setContentViewRenderView(null);
161 removeView(shellView);
164 private static native void nativeInit(Object shellManagerInstance);
165 private static native void nativeLaunchShell(String url);