| 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.chrome.testshell; |
| 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.content.browser.ContentViewRenderView; |
| 15 | import org.chromium.ui.WindowAndroid; |
| 16 | |
| 17 | /** |
| 18 | * The TabManager hooks together all of the related {@link View}s that are used to represent |
| 19 | * a {@link TestShellTab}. It properly builds a {@link TestShellTab} and makes sure that the |
| 20 | * {@link Toolbar} and {@link ContentViewRenderView} show the proper content. |
| 21 | */ |
| 22 | public class TabManager extends LinearLayout { |
| 23 | private static final String DEFAULT_URL = "http://www.google.com"; |
| 24 | |
| 25 | private WindowAndroid mWindow; |
| 26 | private ViewGroup mContentViewHolder; |
| 27 | private ContentViewRenderView mContentViewRenderView; |
| 28 | private TestShellToolbar mToolbar; |
| 29 | |
| 30 | private TestShellTab mCurrentTab; |
| 31 | |
| 32 | private String mStartupUrl = DEFAULT_URL; |
| 33 | |
| 34 | /** |
| 35 | * @param context The Context the view is running in. |
| 36 | * @param attrs The attributes of the XML tag that is inflating the view. |
| 37 | */ |
| 38 | public TabManager(Context context, AttributeSet attrs) { |
| 39 | super(context, attrs); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | protected void onFinishInflate() { |
| 44 | super.onFinishInflate(); |
| 45 | |
| 46 | mContentViewHolder = (ViewGroup) findViewById(R.id.content_container); |
| 47 | mToolbar = (TestShellToolbar) findViewById(R.id.toolbar); |
| 48 | mContentViewRenderView = new ContentViewRenderView(getContext()) { |
| 49 | @Override |
| 50 | protected void onReadyToRender() { |
| 51 | if (mCurrentTab == null) createTab(mStartupUrl); |
| 52 | } |
| 53 | }; |
| 54 | mContentViewHolder.addView(mContentViewRenderView, |
| 55 | new FrameLayout.LayoutParams( |
| 56 | FrameLayout.LayoutParams.MATCH_PARENT, |
| 57 | FrameLayout.LayoutParams.MATCH_PARENT)); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param window The window used to generate all ContentViews. |
| 62 | */ |
| 63 | public void setWindow(WindowAndroid window) { |
| 64 | mWindow = window; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param startupUrl The URL that the first tab should navigate to. |
| 69 | */ |
| 70 | public void setStartupUrl(String startupUrl) { |
| 71 | mStartupUrl = startupUrl; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return The currently visible {@link TestShellTab}. |
| 76 | */ |
| 77 | public TestShellTab getCurrentTab() { |
| 78 | return mCurrentTab; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Creates a {@link TestShellTab} with a URL specified by {@code url}. |
| 83 | * @param url The URL the new {@link TestShellTab} should start with. |
| 84 | */ |
| 85 | public void createTab(String url) { |
| 86 | if (!isContentViewRenderViewInitialized()) return; |
| 87 | |
| 88 | TestShellTab tab = new TestShellTab(getContext(), url, mWindow); |
| 89 | setCurrentTab(tab); |
| 90 | } |
| 91 | |
| 92 | private boolean isContentViewRenderViewInitialized() { |
| 93 | return mContentViewRenderView != null && mContentViewRenderView.isInitialized(); |
| 94 | } |
| 95 | |
| 96 | private void setCurrentTab(TestShellTab tab) { |
| 97 | if (mCurrentTab != null) { |
| 98 | mContentViewHolder.removeView(mCurrentTab.getContentView()); |
| 99 | mCurrentTab.destroy(); |
| 100 | } |
| 101 | |
| 102 | mCurrentTab = tab; |
| 103 | |
| 104 | mToolbar.showTab(mCurrentTab); |
| 105 | mContentViewHolder.addView(mCurrentTab.getContentView()); |
| 106 | mContentViewRenderView.setCurrentContentView(mCurrentTab.getContentView()); |
| 107 | mCurrentTab.getContentView().requestFocus(); |
| 108 | } |
| 109 | } |