b48a5bd657f56e3aba6ad9d76ed7c1f3d7bddb5d
[platform/framework/web/crosswalk.git] / src / chrome / android / shell / java / src / org / chromium / chrome / shell / TabManager.java
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.
4
5 package org.chromium.chrome.shell;
6
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;
13
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;
21
22 /**
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.
26  */
27 public class TabManager extends LinearLayout {
28     private static final String DEFAULT_URL = "http://www.google.com";
29
30     private WindowAndroid mWindow;
31     private ContentVideoViewClient mContentVideoViewClient;
32     private ViewGroup mContentViewHolder;
33     private ContentViewRenderView mContentViewRenderView;
34     private ChromeShellToolbar mToolbar;
35
36     private ChromeShellTab mCurrentTab;
37
38     private String mStartupUrl = DEFAULT_URL;
39     private ContentView mCurrentContentView;
40
41     /**
42      * @param context The Context the view is running in.
43      * @param attrs   The attributes of the XML tag that is inflating the view.
44      */
45     public TabManager(Context context, AttributeSet attrs) {
46         super(context, attrs);
47     }
48
49     @Override
50     protected void onFinishInflate() {
51         super.onFinishInflate();
52     }
53
54     /**
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.
58      */
59     public void initialize(WindowAndroid window, ContentVideoViewClient videoViewClient) {
60         assert window != null;
61         mWindow = window;
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) {
67             @Override
68             protected void onReadyToRender() {
69                 if (mCurrentTab == null) createTab(mStartupUrl);
70             }
71         };
72         mContentViewHolder.addView(mContentViewRenderView,
73                 new FrameLayout.LayoutParams(
74                         FrameLayout.LayoutParams.MATCH_PARENT,
75                         FrameLayout.LayoutParams.MATCH_PARENT));
76     }
77
78     /**
79      * @param startupUrl The URL that the first tab should navigate to.
80      */
81     public void setStartupUrl(String startupUrl) {
82         mStartupUrl = startupUrl;
83     }
84
85     /**
86      * Enter or leave overlay video mode.
87      * @param enabled Whether overlay mode is enabled.
88      */
89     public void setOverlayVideoMode(boolean enabled) {
90         if (mContentViewRenderView == null) return;
91         mContentViewRenderView.setOverlayVideoMode(enabled);
92     }
93
94     /**
95      * @return The currently visible {@link ChromeShellTab}.
96      */
97     public ChromeShellTab getCurrentTab() {
98         return mCurrentTab;
99     }
100
101     /**
102      * Creates a {@link ChromeShellTab} with a URL specified by {@code url}.
103      * @param url The URL the new {@link ChromeShellTab} should start with.
104      */
105     public void createTab(String url) {
106         if (!isContentViewRenderViewInitialized()) return;
107
108         ContentViewClient client = new ContentViewClient() {
109             @Override
110             public ContentVideoViewClient getContentVideoViewClient() {
111                 return mContentVideoViewClient;
112             }
113         };
114         ChromeShellTab tab = new ChromeShellTab(getContext(), url, mWindow, client);
115         setCurrentTab(tab);
116     }
117
118     private boolean isContentViewRenderViewInitialized() {
119         return mContentViewRenderView != null && mContentViewRenderView.isInitialized();
120     }
121
122     private void setCurrentTab(ChromeShellTab tab) {
123         if (mCurrentTab != null) {
124             mContentViewHolder.removeView(mCurrentTab.getContentView());
125             mCurrentTab.destroy();
126         }
127
128         mCurrentTab = tab;
129         mCurrentContentView = tab.getContentView();
130
131         mCurrentTab.addObserver(new EmptyTabObserver() {
132             @Override
133             public void onContentChanged(Tab tab) {
134                 mContentViewHolder.removeView(mCurrentContentView);
135                 setupContent(tab);
136             }
137         });
138
139         mToolbar.showTab(mCurrentTab);
140         setupContent(mCurrentTab);
141     }
142
143     private void setupContent(Tab tab) {
144         mContentViewHolder.addView(tab.getContentView());
145         mContentViewRenderView.setCurrentContentViewCore(tab.getContentViewCore());
146         tab.getContentView().requestFocus();
147         tab.getContentViewCore().onShow();
148     }
149 }