1 // Copyright 2014 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.chrome.shell;
7 import android.content.Context;
8 import android.util.AttributeSet;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.FrameLayout;
12 import android.widget.LinearLayout;
14 import org.chromium.chrome.browser.EmptyTabObserver;
15 import org.chromium.chrome.browser.Tab;
16 import org.chromium.content.browser.ContentVideoViewClient;
17 import org.chromium.content.browser.ContentView;
18 import org.chromium.content.browser.ContentViewClient;
19 import org.chromium.content.browser.ContentViewRenderView;
20 import org.chromium.ui.base.WindowAndroid;
23 * The TabManager hooks together all of the related {@link View}s that are used to represent
24 * a {@link ChromeShellTab}. It properly builds a {@link ChromeShellTab} and makes sure that the
25 * {@link ChromeShellToolbar} and {@link ContentViewRenderView} show the proper content.
27 public class TabManager extends LinearLayout {
28 private static final String DEFAULT_URL = "http://www.google.com";
30 private WindowAndroid mWindow;
31 private ContentVideoViewClient mContentVideoViewClient;
32 private ViewGroup mContentViewHolder;
33 private ContentViewRenderView mContentViewRenderView;
34 private ChromeShellToolbar mToolbar;
36 private ChromeShellTab mCurrentTab;
38 private String mStartupUrl = DEFAULT_URL;
39 private ContentView mCurrentContentView;
42 * @param context The Context the view is running in.
43 * @param attrs The attributes of the XML tag that is inflating the view.
45 public TabManager(Context context, AttributeSet attrs) {
46 super(context, attrs);
50 protected void onFinishInflate() {
51 super.onFinishInflate();
55 * Initialize the components required for Tab creation.
56 * @param window The window used to generate all ContentViews.
57 * @param videoViewClient The client to handle interactions from ContentVideoViews.
59 public void initialize(WindowAndroid window, ContentVideoViewClient videoViewClient) {
60 assert window != null;
62 assert videoViewClient != null;
63 mContentVideoViewClient = videoViewClient;
64 mContentViewHolder = (ViewGroup) findViewById(R.id.content_container);
65 mToolbar = (ChromeShellToolbar) findViewById(R.id.toolbar);
66 mContentViewRenderView = new ContentViewRenderView(getContext(), mWindow) {
68 protected void onReadyToRender() {
69 if (mCurrentTab == null) createTab(mStartupUrl);
72 mContentViewHolder.addView(mContentViewRenderView,
73 new FrameLayout.LayoutParams(
74 FrameLayout.LayoutParams.MATCH_PARENT,
75 FrameLayout.LayoutParams.MATCH_PARENT));
79 * @param startupUrl The URL that the first tab should navigate to.
81 public void setStartupUrl(String startupUrl) {
82 mStartupUrl = startupUrl;
86 * Enter or leave overlay video mode.
87 * @param enabled Whether overlay mode is enabled.
89 public void setOverlayVideoMode(boolean enabled) {
90 if (mContentViewRenderView == null) return;
91 mContentViewRenderView.setOverlayVideoMode(enabled);
95 * @return The currently visible {@link ChromeShellTab}.
97 public ChromeShellTab getCurrentTab() {
102 * Creates a {@link ChromeShellTab} with a URL specified by {@code url}.
103 * @param url The URL the new {@link ChromeShellTab} should start with.
105 public void createTab(String url) {
106 if (!isContentViewRenderViewInitialized()) return;
108 ContentViewClient client = new ContentViewClient() {
110 public ContentVideoViewClient getContentVideoViewClient() {
111 return mContentVideoViewClient;
114 ChromeShellTab tab = new ChromeShellTab(getContext(), url, mWindow, client);
118 private boolean isContentViewRenderViewInitialized() {
119 return mContentViewRenderView != null && mContentViewRenderView.isInitialized();
122 private void setCurrentTab(ChromeShellTab tab) {
123 if (mCurrentTab != null) {
124 mContentViewHolder.removeView(mCurrentTab.getContentView());
125 mCurrentTab.destroy();
129 mCurrentContentView = tab.getContentView();
131 mCurrentTab.addObserver(new EmptyTabObserver() {
133 public void onContentChanged(Tab tab) {
134 mContentViewHolder.removeView(mCurrentContentView);
139 mToolbar.showTab(mCurrentTab);
140 setupContent(mCurrentTab);
143 private void setupContent(Tab tab) {
144 mContentViewHolder.addView(tab.getContentView());
145 mContentViewRenderView.setCurrentContentViewCore(tab.getContentViewCore());
146 tab.getContentView().requestFocus();
147 tab.getContentViewCore().onShow();