1dfc60a632494ad504ec9d34e4f5f0987841f1ea
[platform/framework/web/crosswalk.git] / src / content / shell / android / java / src / org / chromium / content_shell / ShellManager.java
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.
4
5 package org.chromium.content_shell;
6
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;
13
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;
23
24 /**
25  * Container and generator of ShellViews.
26  */
27 @JNINamespace("content")
28 public class ShellManager extends FrameLayout {
29
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;
34
35     private String mStartupUrl = DEFAULT_SHELL_URL;
36
37     // The target for all content rendering.
38     private ContentViewRenderView mContentViewRenderView;
39     private ContentViewClient mContentViewClient;
40
41     /**
42      * Constructor for inflating via XML.
43      */
44     public ShellManager(final Context context, AttributeSet attrs) {
45         super(context, attrs);
46         nativeInit(this);
47         mContentViewClient = new ContentViewClient() {
48             @Override
49             public ContentVideoViewClient getContentVideoViewClient() {
50                 return new ActivityContentVideoViewClient((Activity) context) {
51                     @Override
52                     public void enterFullscreenVideo(View view) {
53                         super.enterFullscreenVideo(view);
54                         setOverlayVideoMode(true);
55                     }
56
57                     @Override
58                     public void exitFullscreenVideo() {
59                         super.exitFullscreenVideo();
60                         setOverlayVideoMode(false);
61                     }
62                 };
63             }
64         };
65     }
66
67     /**
68      * @param window The window used to generate all shells.
69      */
70     public void setWindow(WindowAndroid window) {
71         assert window != null;
72         mWindow = window;
73         mContentViewRenderView = new ContentViewRenderView(getContext()) {
74             @Override
75             protected void onReadyToRender() {
76                 if (sStartup) {
77                     mActiveShell.loadUrl(mStartupUrl);
78                     sStartup = false;
79                 }
80             }
81         };
82         mContentViewRenderView.onNativeLibraryLoaded(window);
83     }
84
85     /**
86      * @return The window used to generate all shells.
87      */
88     public WindowAndroid getWindow() {
89         return mWindow;
90     }
91
92     /**
93      * Sets the startup URL for new shell windows.
94      */
95     public void setStartupUrl(String url) {
96         mStartupUrl = url;
97     }
98
99     /**
100      * @return The currently visible shell view or null if one is not showing.
101      */
102     public Shell getActiveShell() {
103         return mActiveShell;
104     }
105
106     /**
107      * Creates a new shell pointing to the specified URL.
108      * @param url The URL the shell should load upon creation.
109      */
110     public void launchShell(String url) {
111         ThreadUtils.assertOnUiThread();
112         Shell previousShell = mActiveShell;
113         nativeLaunchShell(url);
114         if (previousShell != null) previousShell.close();
115     }
116
117     /**
118      * Enter or leave overlay video mode.
119      * @param enabled Whether overlay mode is enabled.
120      */
121     public void setOverlayVideoMode(boolean enabled) {
122         if (mContentViewRenderView == null) return;
123         mContentViewRenderView.setOverlayVideoMode(enabled);
124     }
125
126     @SuppressWarnings("unused")
127     @CalledByNative
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);
134
135         // TODO(tedchoc): Allow switching back to these inactive shells.
136         if (mActiveShell != null) removeShell(mActiveShell);
137
138         showShell(shellView);
139         return shellView;
140     }
141
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();
151         }
152     }
153
154     @CalledByNative
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);
162     }
163
164     private static native void nativeInit(Object shellManagerInstance);
165     private static native void nativeLaunchShell(String url);
166 }