2f83d28ae91fbc8473749d70a9c7f3bf47af5b41
[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.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;
25
26 /**
27  * Container and generator of ShellViews.
28  */
29 @JNINamespace("content")
30 public class ShellManager extends FrameLayout {
31
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;
36
37     private String mStartupUrl = DEFAULT_SHELL_URL;
38
39     // The target for all content rendering.
40     private ContentViewRenderView mContentViewRenderView;
41     private ContentViewClient mContentViewClient;
42
43     /**
44      * Constructor for inflating via XML.
45      */
46     public ShellManager(final Context context, AttributeSet attrs) {
47         super(context, attrs);
48         nativeInit(this);
49         mContentViewClient = new ContentViewClient() {
50             @Override
51             public ContentVideoViewClient getContentVideoViewClient() {
52                 return new ActivityContentVideoViewClient((Activity) context) {
53                     @Override
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);
59                         }
60                         return success;
61                     }
62
63                     @Override
64                     public void onDestroyContentVideoView() {
65                         super.onDestroyContentVideoView();
66                         if (!CommandLine.getInstance().hasSwitch(
67                                 ContentSwitches.DISABLE_OVERLAY_FULLSCREEN_VIDEO_SUBTITLE)) {
68                             setOverlayVideoMode(false);
69                         }
70                     }
71                 };
72             }
73         };
74     }
75
76     /**
77      * @param window The window used to generate all shells.
78      */
79     public void setWindow(WindowAndroid window) {
80         assert window != null;
81         mWindow = window;
82         mContentViewRenderView = new ContentViewRenderView(getContext(), window) {
83             @Override
84             protected void onReadyToRender() {
85                 if (sStartup) {
86                     mActiveShell.loadUrl(mStartupUrl);
87                     sStartup = false;
88                 }
89             }
90         };
91     }
92
93     /**
94      * @return The window used to generate all shells.
95      */
96     public WindowAndroid getWindow() {
97         return mWindow;
98     }
99
100     /**
101      * Sets the startup URL for new shell windows.
102      */
103     public void setStartupUrl(String url) {
104         mStartupUrl = url;
105     }
106
107     /**
108      * @return The currently visible shell view or null if one is not showing.
109      */
110     public Shell getActiveShell() {
111         return mActiveShell;
112     }
113
114     /**
115      * Creates a new shell pointing to the specified URL.
116      * @param url The URL the shell should load upon creation.
117      */
118     public void launchShell(String url) {
119         ThreadUtils.assertOnUiThread();
120         Shell previousShell = mActiveShell;
121         nativeLaunchShell(url);
122         if (previousShell != null) previousShell.close();
123     }
124
125     /**
126      * Enter or leave overlay video mode.
127      * @param enabled Whether overlay mode is enabled.
128      */
129     public void setOverlayVideoMode(boolean enabled) {
130         if (mContentViewRenderView == null) return;
131         mContentViewRenderView.setOverlayVideoMode(enabled);
132     }
133
134     @SuppressWarnings("unused")
135     @CalledByNative
136     private Object createShell(long nativeShellPtr) {
137         assert mContentViewRenderView != null;
138         LayoutInflater inflater =
139                 (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
140         Shell shellView = (Shell) inflater.inflate(R.layout.shell_view, null);
141         shellView.initialize(nativeShellPtr, mWindow, mContentViewClient);
142
143         // TODO(tedchoc): Allow switching back to these inactive shells.
144         if (mActiveShell != null) removeShell(mActiveShell);
145
146         showShell(shellView);
147         return shellView;
148     }
149
150     private void showShell(Shell shellView) {
151         shellView.setContentViewRenderView(mContentViewRenderView);
152         addView(shellView, new FrameLayout.LayoutParams(
153                 FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
154         mActiveShell = shellView;
155         ContentViewCore contentViewCore = mActiveShell.getContentViewCore();
156         if (contentViewCore != null) {
157             mContentViewRenderView.setCurrentContentViewCore(contentViewCore);
158             contentViewCore.onShow();
159         }
160     }
161
162     @CalledByNative
163     private void removeShell(Shell shellView) {
164         if (shellView == mActiveShell) mActiveShell = null;
165         if (shellView.getParent() == null) return;
166         ContentViewCore contentViewCore = shellView.getContentViewCore();
167         if (contentViewCore != null) contentViewCore.onHide();
168         shellView.setContentViewRenderView(null);
169         removeView(shellView);
170     }
171
172     private static native void nativeInit(Object shellManagerInstance);
173     private static native void nativeLaunchShell(String url);
174 }