1 | // Copyright (c) 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.content.Context; |
8 | import android.util.AttributeSet; |
9 | import android.view.LayoutInflater; |
10 | import android.widget.FrameLayout; |
11 | |
12 | import org.chromium.base.CalledByNative; |
13 | import org.chromium.base.JNINamespace; |
14 | import org.chromium.content.browser.ContentView; |
15 | import org.chromium.content.browser.ContentViewRenderView; |
16 | import org.chromium.ui.WindowAndroid; |
17 | |
18 | /** |
19 | * Container and generator of ShellViews. |
20 | */ |
21 | @JNINamespace("content") |
22 | public class ShellManager extends FrameLayout { |
23 | |
24 | public static final String DEFAULT_SHELL_URL = "http://www.google.com"; |
25 | private static boolean sStartup = true; |
26 | private WindowAndroid mWindow; |
27 | private Shell mActiveShell; |
28 | |
29 | private String mStartupUrl = DEFAULT_SHELL_URL; |
30 | |
31 | // The target for all content rendering. |
32 | private ContentViewRenderView mContentViewRenderView; |
33 | |
34 | /** |
35 | * Constructor for inflating via XML. |
36 | */ |
37 | public ShellManager(Context context, AttributeSet attrs) { |
38 | super(context, attrs); |
39 | nativeInit(this); |
40 | mContentViewRenderView = new ContentViewRenderView(context) { |
41 | @Override |
42 | protected void onReadyToRender() { |
43 | if (sStartup) { |
44 | mActiveShell.loadUrl(mStartupUrl); |
45 | sStartup = false; |
46 | } |
47 | } |
48 | }; |
49 | } |
50 | |
51 | /** |
52 | * @param window The window used to generate all shells. |
53 | */ |
54 | public void setWindow(WindowAndroid window) { |
55 | mWindow = window; |
56 | } |
57 | |
58 | /** |
59 | * @return The window used to generate all shells. |
60 | */ |
61 | public WindowAndroid getWindow() { |
62 | return mWindow; |
63 | } |
64 | |
65 | /** |
66 | * Sets the startup URL for new shell windows. |
67 | */ |
68 | public void setStartupUrl(String url) { |
69 | mStartupUrl = url; |
70 | } |
71 | |
72 | /** |
73 | * @return The currently visible shell view or null if one is not showing. |
74 | */ |
75 | public Shell getActiveShell() { |
76 | return mActiveShell; |
77 | } |
78 | |
79 | /** |
80 | * Creates a new shell pointing to the specified URL. |
81 | * @param url The URL the shell should load upon creation. |
82 | */ |
83 | public void launchShell(String url) { |
84 | nativeLaunchShell(url); |
85 | } |
86 | |
87 | @SuppressWarnings("unused") |
88 | @CalledByNative |
89 | private Object createShell() { |
90 | LayoutInflater inflater = |
91 | (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
92 | Shell shellView = (Shell) inflater.inflate(R.layout.shell_view, null); |
93 | shellView.setWindow(mWindow); |
94 | |
95 | if (mActiveShell != null) closeShell(mActiveShell); |
96 | |
97 | shellView.setContentViewRenderView(mContentViewRenderView); |
98 | addView(shellView, new FrameLayout.LayoutParams( |
99 | FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); |
100 | mActiveShell = shellView; |
101 | ContentView contentView = mActiveShell.getContentView(); |
102 | if (contentView != null) { |
103 | mContentViewRenderView.setCurrentContentView(contentView); |
104 | contentView.onShow(); |
105 | } |
106 | |
107 | return shellView; |
108 | } |
109 | |
110 | @SuppressWarnings("unused") |
111 | @CalledByNative |
112 | private void closeShell(Shell shellView) { |
113 | if (shellView == mActiveShell) mActiveShell = null; |
114 | ContentView contentView = shellView.getContentView(); |
115 | if (contentView != null) contentView.onHide(); |
116 | shellView.setContentViewRenderView(null); |
117 | shellView.setWindow(null); |
118 | removeView(shellView); |
119 | } |
120 | |
121 | private static native void nativeInit(Object shellManagerInstance); |
122 | private static native void nativeLaunchShell(String url); |
123 | } |