6d6db1c98e96ad59be3eb2d7e158042d5eb53916
[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.ContentView;
21 import org.chromium.content.browser.ContentViewClient;
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 void onShowCustomView(View view) {
55                         super.onShowCustomView(view);
56                         if (!CommandLine.getInstance().hasSwitch(
57                                 ContentSwitches.DISABLE_OVERLAY_FULLSCREEN_VIDEO_SUBTITLE)) {
58                             setOverlayVideoMode(true);
59                         }
60                     }
61
62                     @Override
63                     public void onDestroyContentVideoView() {
64                         super.onDestroyContentVideoView();
65                         if (!CommandLine.getInstance().hasSwitch(
66                                 ContentSwitches.DISABLE_OVERLAY_FULLSCREEN_VIDEO_SUBTITLE)) {
67                             setOverlayVideoMode(false);
68                         }
69                     }
70                 };
71             }
72         };
73     }
74
75     /**
76      * @param window The window used to generate all shells.
77      */
78     public void setWindow(WindowAndroid window) {
79         assert window != null;
80         mWindow = window;
81         mContentViewRenderView = new ContentViewRenderView(getContext(), window) {
82             @Override
83             protected void onReadyToRender() {
84                 if (sStartup) {
85                     mActiveShell.loadUrl(mStartupUrl);
86                     sStartup = false;
87                 }
88             }
89         };
90     }
91
92     /**
93      * @return The window used to generate all shells.
94      */
95     public WindowAndroid getWindow() {
96         return mWindow;
97     }
98
99     /**
100      * Sets the startup URL for new shell windows.
101      */
102     public void setStartupUrl(String url) {
103         mStartupUrl = url;
104     }
105
106     /**
107      * @return The currently visible shell view or null if one is not showing.
108      */
109     public Shell getActiveShell() {
110         return mActiveShell;
111     }
112
113     /**
114      * Creates a new shell pointing to the specified URL.
115      * @param url The URL the shell should load upon creation.
116      */
117     public void launchShell(String url) {
118         ThreadUtils.assertOnUiThread();
119         Shell previousShell = mActiveShell;
120         nativeLaunchShell(url);
121         if (previousShell != null) previousShell.close();
122     }
123
124     /**
125      * Enter or leave overlay video mode.
126      * @param enabled Whether overlay mode is enabled.
127      */
128     public void setOverlayVideoMode(boolean enabled) {
129         if (mContentViewRenderView == null) return;
130         mContentViewRenderView.setOverlayVideoMode(enabled);
131     }
132
133     @SuppressWarnings("unused")
134     @CalledByNative
135     private Object createShell(long nativeShellPtr) {
136         assert mContentViewRenderView != null;
137         LayoutInflater inflater =
138                 (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
139         Shell shellView = (Shell) inflater.inflate(R.layout.shell_view, null);
140         shellView.initialize(nativeShellPtr, mWindow, mContentViewClient);
141
142         // TODO(tedchoc): Allow switching back to these inactive shells.
143         if (mActiveShell != null) removeShell(mActiveShell);
144
145         showShell(shellView);
146         return shellView;
147     }
148
149     private void showShell(Shell shellView) {
150         shellView.setContentViewRenderView(mContentViewRenderView);
151         addView(shellView, new FrameLayout.LayoutParams(
152                 FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
153         mActiveShell = shellView;
154         ContentView contentView = mActiveShell.getContentView();
155         if (contentView != null) {
156             mContentViewRenderView.setCurrentContentView(contentView);
157             contentView.onShow();
158         }
159     }
160
161     @CalledByNative
162     private void removeShell(Shell shellView) {
163         if (shellView == mActiveShell) mActiveShell = null;
164         if (shellView.getParent() == null) return;
165         ContentView contentView = shellView.getContentView();
166         if (contentView != null) contentView.onHide();
167         shellView.setContentViewRenderView(null);
168         removeView(shellView);
169     }
170
171     private static native void nativeInit(Object shellManagerInstance);
172     private static native void nativeLaunchShell(String url);
173 }