Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 19 | import android.graphics.Matrix; |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 20 | import com.android.browser.ScrollWebView.ScrollListener; |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 21 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.res.Resources; |
| 25 | import android.graphics.Bitmap; |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 26 | import android.graphics.BitmapShader; |
| 27 | import android.graphics.Canvas; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 28 | import android.graphics.Color; |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 29 | import android.graphics.Paint; |
| 30 | import android.graphics.Path; |
| 31 | import android.graphics.Shader; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 32 | import android.graphics.drawable.BitmapDrawable; |
| 33 | import android.graphics.drawable.Drawable; |
| 34 | import android.graphics.drawable.LayerDrawable; |
| 35 | import android.graphics.drawable.PaintDrawable; |
| 36 | import android.view.ContextMenu; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 37 | import android.view.Gravity; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 38 | import android.view.LayoutInflater; |
| 39 | import android.view.MenuInflater; |
| 40 | import android.view.View; |
| 41 | import android.view.View.OnClickListener; |
| 42 | import android.webkit.WebView; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 43 | import android.widget.ImageButton; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 44 | import android.widget.ImageView; |
| 45 | import android.widget.LinearLayout; |
| 46 | import android.widget.TextView; |
| 47 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 48 | import java.util.HashMap; |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 49 | import java.util.List; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 50 | import java.util.Map; |
| 51 | |
| 52 | /** |
| 53 | * tabbed title bar for xlarge screen browser |
| 54 | */ |
| 55 | public class TabBar extends LinearLayout |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 56 | implements ScrollListener, OnClickListener { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 57 | |
| 58 | private static final int PROGRESS_MAX = 100; |
| 59 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 60 | private Activity mActivity; |
| 61 | private UiController mUiController; |
| 62 | private TabControl mTabControl; |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 63 | private XLargeUi mUi; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 64 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 65 | private final int mTabWidthSelected; |
| 66 | private final int mTabWidthUnselected; |
| 67 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 68 | private TabScrollView mTabs; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 69 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 70 | private ImageButton mNewTab; |
| 71 | private int mButtonWidth; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 72 | |
| 73 | private Map<Tab, TabViewData> mTabMap; |
| 74 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 75 | private boolean mUserRequestedUrlbar; |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 76 | private int mVisibleTitleHeight; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 77 | |
John Reck | aff60fb | 2010-10-25 13:47:58 -0700 | [diff] [blame] | 78 | private Drawable mGenericFavicon; |
| 79 | |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 80 | private int mCurrentTextureWidth = 0; |
| 81 | private int mCurrentTextureHeight = 0; |
| 82 | |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 83 | private Drawable mActiveDrawable; |
| 84 | private Drawable mInactiveDrawable; |
| 85 | |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 86 | private final Paint mActiveShaderPaint = new Paint(); |
| 87 | private final Paint mInactiveShaderPaint = new Paint(); |
| 88 | private final Matrix mActiveMatrix = new Matrix(); |
| 89 | private final Matrix mInactiveMatrix = new Matrix(); |
| 90 | |
| 91 | private BitmapShader mActiveShader; |
| 92 | private BitmapShader mInactiveShader; |
| 93 | |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 94 | private int mTabOverlap; |
| 95 | private int mTabSliceWidth; |
| 96 | private int mTabPadding; |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 97 | private boolean mUseQuickControls; |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 98 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 99 | public TabBar(Activity activity, UiController controller, XLargeUi ui) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 100 | super(activity); |
| 101 | mActivity = activity; |
| 102 | mUiController = controller; |
| 103 | mTabControl = mUiController.getTabControl(); |
| 104 | mUi = ui; |
| 105 | Resources res = activity.getResources(); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 106 | mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected); |
| 107 | mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 108 | mActiveDrawable = res.getDrawable(R.drawable.bg_urlbar); |
| 109 | mInactiveDrawable = res.getDrawable(R.drawable.browsertab_inactive); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 110 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 111 | mTabMap = new HashMap<Tab, TabViewData>(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 112 | LayoutInflater factory = LayoutInflater.from(activity); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 113 | factory.inflate(R.layout.tab_bar, this); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 114 | setPadding(12, 12, 0, 0); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 115 | mTabs = (TabScrollView) findViewById(R.id.tabs); |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 116 | mNewTab = (ImageButton) findViewById(R.id.newtab); |
| 117 | mNewTab.setOnClickListener(this); |
John Reck | aff60fb | 2010-10-25 13:47:58 -0700 | [diff] [blame] | 118 | mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 119 | setChildrenDrawingOrderEnabled(true); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 120 | |
| 121 | // TODO: Change enabled states based on whether you can go |
| 122 | // back/forward. Probably should be done inside onPageStarted. |
| 123 | |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 124 | updateTabs(mUiController.getTabs()); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 125 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 126 | mUserRequestedUrlbar = false; |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 127 | mVisibleTitleHeight = 1; |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 128 | mButtonWidth = -1; |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 129 | // tab dimensions |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 130 | mTabOverlap = (int) res.getDimension(R.dimen.tab_overlap); |
| 131 | mTabSliceWidth = (int) res.getDimension(R.dimen.tab_slice); |
| 132 | mTabPadding = (int) res.getDimension(R.dimen.tab_padding); |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 133 | |
| 134 | mActiveShaderPaint.setStyle(Paint.Style.FILL); |
| 135 | mActiveShaderPaint.setAntiAlias(true); |
| 136 | |
| 137 | mInactiveShaderPaint.setStyle(Paint.Style.FILL); |
| 138 | mInactiveShaderPaint.setAntiAlias(true); |
| 139 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 142 | void setUseQuickControls(boolean useQuickControls) { |
| 143 | mUseQuickControls = useQuickControls; |
| 144 | } |
| 145 | |
| 146 | int getTabCount() { |
| 147 | return mTabMap.size(); |
| 148 | } |
| 149 | |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 150 | void updateTabs(List<Tab> tabs) { |
| 151 | mTabs.clearTabs(); |
| 152 | mTabMap.clear(); |
| 153 | for (Tab tab : tabs) { |
| 154 | TabViewData data = buildTab(tab); |
| 155 | TabView tv = buildView(data); |
| 156 | } |
| 157 | mTabs.setSelectedTab(mTabControl.getCurrentIndex()); |
| 158 | } |
| 159 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 160 | @Override |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 161 | protected void onMeasure(int hspec, int vspec) { |
| 162 | super.onMeasure(hspec, vspec); |
| 163 | int w = getMeasuredWidth(); |
| 164 | // adjust for new tab overlap |
| 165 | w -= mTabOverlap; |
| 166 | setMeasuredDimension(w, getMeasuredHeight()); |
| 167 | } |
| 168 | |
| 169 | @Override |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 170 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 171 | // use paddingLeft and paddingTop |
| 172 | int pl = getPaddingLeft(); |
| 173 | int pt = getPaddingTop(); |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 174 | if (mButtonWidth == -1) { |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 175 | mButtonWidth = mNewTab.getMeasuredWidth() - mTabOverlap; |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 176 | } |
| 177 | int sw = mTabs.getMeasuredWidth(); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 178 | int w = right - left - pl; |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 179 | if (w-sw < mButtonWidth) { |
| 180 | sw = w - mButtonWidth; |
| 181 | } |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 182 | mTabs.layout(pl, pt, pl + sw, bottom - top); |
| 183 | // adjust for overlap |
| 184 | mNewTab.layout(pl + sw - mTabOverlap, pt, |
| 185 | pl + sw + mButtonWidth - mTabOverlap, bottom - top); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | public void onClick(View view) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 189 | mUi.hideComboView(); |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 190 | if (mNewTab == view) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 191 | mUiController.openTabToHomePage(); |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 192 | } else if (mTabs.getSelectedTab() == view) { |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 193 | if (mUseQuickControls) return; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 194 | if (mUi.isFakeTitleBarShowing() && !isLoading()) { |
| 195 | mUi.hideFakeTitleBar(); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 196 | } else { |
| 197 | showUrlBar(); |
| 198 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 199 | } else { |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 200 | int ix = mTabs.getChildIndex(view); |
| 201 | if (ix >= 0) { |
| 202 | mTabs.setSelectedTab(ix); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 203 | mUiController.switchToTab(ix); |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 204 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 208 | private void showUrlBar() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 209 | mUi.stopWebViewScrolling(); |
| 210 | mUi.showFakeTitleBar(); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 211 | mUserRequestedUrlbar = true; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 214 | void showTitleBarIndicator(boolean show) { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 215 | Tab tab = mTabControl.getCurrentTab(); |
| 216 | if (tab != null) { |
| 217 | TabViewData tvd = mTabMap.get(tab); |
| 218 | if (tvd != null) { |
| 219 | tvd.mTabView.showIndicator(show); |
| 220 | } |
| 221 | } |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // callback after fake titlebar is shown |
| 225 | void onShowTitleBar() { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 226 | showTitleBarIndicator(false); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // callback after fake titlebar is hidden |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 230 | void onHideTitleBar() { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 231 | showTitleBarIndicator(mVisibleTitleHeight == 0); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 232 | Tab tab = mTabControl.getCurrentTab(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 233 | tab.getWebView().requestFocus(); |
| 234 | mUserRequestedUrlbar = false; |
| 235 | } |
| 236 | |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 237 | // webview scroll listener |
| 238 | |
| 239 | @Override |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 240 | public void onScroll(int visibleTitleHeight) { |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 241 | if (mUseQuickControls) return; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 242 | // isLoading is using the current tab, which initially might not be set yet |
John Reck | dcf911c | 2010-12-22 16:32:31 -0800 | [diff] [blame] | 243 | if (mTabControl.getCurrentTab() != null |
| 244 | && !isLoading()) { |
| 245 | if (visibleTitleHeight == 0) { |
| 246 | mUi.hideFakeTitleBar(); |
| 247 | showTitleBarIndicator(true); |
| 248 | } else { |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 249 | showTitleBarIndicator(false); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 252 | mVisibleTitleHeight = visibleTitleHeight; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 253 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 254 | |
| 255 | @Override |
| 256 | public void createContextMenu(ContextMenu menu) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 257 | MenuInflater inflater = mActivity.getMenuInflater(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 258 | inflater.inflate(R.menu.title_context, menu); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 259 | mActivity.onCreateContextMenu(menu, this, null); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | private TabViewData buildTab(Tab tab) { |
| 263 | TabViewData data = new TabViewData(tab); |
| 264 | mTabMap.put(tab, data); |
| 265 | return data; |
| 266 | } |
| 267 | |
| 268 | private TabView buildView(final TabViewData data) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 269 | TabView tv = new TabView(mActivity, data); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 270 | tv.setTag(data); |
| 271 | tv.setOnClickListener(this); |
| 272 | mTabs.addTab(tv); |
| 273 | return tv; |
| 274 | } |
| 275 | |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 276 | @Override |
| 277 | protected int getChildDrawingOrder(int count, int i) { |
| 278 | // reverse |
| 279 | return count - 1 - i; |
| 280 | } |
| 281 | |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 282 | private static Bitmap getDrawableAsBitmap(Drawable drawable, int width, int height) { |
| 283 | Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 284 | Canvas c = new Canvas(b); |
| 285 | drawable.setBounds(0, 0, width, height); |
| 286 | drawable.draw(c); |
| 287 | return b; |
| 288 | } |
| 289 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 290 | /** |
| 291 | * View used in the tab bar |
| 292 | */ |
| 293 | class TabView extends LinearLayout implements OnClickListener { |
| 294 | |
| 295 | TabViewData mTabData; |
| 296 | View mTabContent; |
| 297 | TextView mTitle; |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 298 | View mIndicator; |
Michael Kolb | ae62fd4 | 2010-08-18 16:33:28 -0700 | [diff] [blame] | 299 | View mIncognito; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 300 | ImageView mIconView; |
| 301 | ImageView mLock; |
| 302 | ImageView mClose; |
| 303 | boolean mSelected; |
| 304 | boolean mInLoad; |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 305 | Path mPath; |
| 306 | int[] mWindowPos; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 307 | |
| 308 | /** |
| 309 | * @param context |
| 310 | */ |
| 311 | public TabView(Context context, TabViewData tab) { |
| 312 | super(context); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 313 | setWillNotDraw(false); |
| 314 | mPath = new Path(); |
| 315 | mWindowPos = new int[2]; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 316 | mTabData = tab; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 317 | setGravity(Gravity.CENTER_VERTICAL); |
| 318 | setOrientation(LinearLayout.HORIZONTAL); |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 319 | setPadding(mTabPadding, 0, 0, 0); |
Bjorn Bringert | b1402a5 | 2010-10-12 10:53:12 +0100 | [diff] [blame] | 320 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 321 | mTabContent = inflater.inflate(R.layout.tab_title, this, true); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 322 | mTitle = (TextView) mTabContent.findViewById(R.id.title); |
| 323 | mIconView = (ImageView) mTabContent.findViewById(R.id.favicon); |
| 324 | mLock = (ImageView) mTabContent.findViewById(R.id.lock); |
| 325 | mClose = (ImageView) mTabContent.findViewById(R.id.close); |
| 326 | mClose.setOnClickListener(this); |
Michael Kolb | ae62fd4 | 2010-08-18 16:33:28 -0700 | [diff] [blame] | 327 | mIncognito = mTabContent.findViewById(R.id.incognito); |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 328 | mIndicator = mTabContent.findViewById(R.id.chevron); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 329 | mSelected = false; |
| 330 | mInLoad = false; |
| 331 | // update the status |
| 332 | updateFromData(); |
| 333 | } |
| 334 | |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 335 | void showIndicator(boolean show) { |
Michael Kolb | 6a85e2a | 2010-12-13 18:12:46 -0800 | [diff] [blame] | 336 | if (mSelected) { |
| 337 | mIndicator.setVisibility(show ? View.VISIBLE : View.GONE); |
| 338 | } else { |
| 339 | mIndicator.setVisibility(View.GONE); |
| 340 | } |
Michael Kolb | a0e2a75 | 2010-12-12 15:27:56 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 343 | @Override |
| 344 | public void onClick(View v) { |
| 345 | if (v == mClose) { |
| 346 | closeTab(); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | private void updateFromData() { |
| 351 | mTabData.mTabView = this; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 352 | Tab tab = mTabData.mTab; |
| 353 | String displayTitle = tab.getTitle(); |
| 354 | if (displayTitle == null) { |
| 355 | displayTitle = tab.getUrl(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 356 | } |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 357 | setDisplayTitle(displayTitle); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 358 | setProgress(mTabData.mProgress); |
| 359 | if (mTabData.mIcon != null) { |
| 360 | setFavicon(mTabData.mIcon); |
| 361 | } |
Michael Kolb | ae62fd4 | 2010-08-18 16:33:28 -0700 | [diff] [blame] | 362 | if (mTabData.mTab != null) { |
| 363 | mIncognito.setVisibility( |
Michael Kolb | 1bf2313 | 2010-11-19 12:55:12 -0800 | [diff] [blame] | 364 | mTabData.mTab.isPrivateBrowsingEnabled() ? |
Michael Kolb | ae62fd4 | 2010-08-18 16:33:28 -0700 | [diff] [blame] | 365 | View.VISIBLE : View.GONE); |
| 366 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Override |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 370 | public void setActivated(boolean selected) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 371 | mSelected = selected; |
| 372 | mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE); |
Michael Kolb | 6a85e2a | 2010-12-13 18:12:46 -0800 | [diff] [blame] | 373 | mIndicator.setVisibility(View.GONE); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 374 | mTitle.setTextAppearance(mActivity, mSelected ? |
Michael Kolb | c7485ae | 2010-09-03 10:10:58 -0700 | [diff] [blame] | 375 | R.style.TabTitleSelected : R.style.TabTitleUnselected); |
| 376 | setHorizontalFadingEdgeEnabled(!mSelected); |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 377 | super.setActivated(selected); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 378 | LayoutParams lp = (LinearLayout.LayoutParams) getLayoutParams(); |
| 379 | lp.width = selected ? mTabWidthSelected : mTabWidthUnselected; |
| 380 | lp.height = LayoutParams.MATCH_PARENT; |
| 381 | setLayoutParams(lp); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void setDisplayTitle(String title) { |
| 385 | mTitle.setText(title); |
| 386 | } |
| 387 | |
| 388 | void setFavicon(Drawable d) { |
| 389 | mIconView.setImageDrawable(d); |
| 390 | } |
| 391 | |
| 392 | void setLock(Drawable d) { |
| 393 | if (null == d) { |
| 394 | mLock.setVisibility(View.GONE); |
| 395 | } else { |
| 396 | mLock.setImageDrawable(d); |
| 397 | mLock.setVisibility(View.VISIBLE); |
| 398 | } |
| 399 | } |
| 400 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 401 | void setProgress(int newProgress) { |
| 402 | if (newProgress >= PROGRESS_MAX) { |
| 403 | mInLoad = false; |
| 404 | } else { |
| 405 | if (!mInLoad && getWindowToken() != null) { |
| 406 | mInLoad = true; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | private void closeTab() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 412 | if (mTabData.mTab == mTabControl.getCurrentTab()) { |
| 413 | mUiController.closeCurrentTab(); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 414 | } else { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 415 | mUiController.closeTab(mTabData.mTab); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 419 | @Override |
| 420 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 421 | super.onLayout(changed, l, t, r, b); |
| 422 | setTabPath(mPath, 0, 0, r - l, b - t); |
| 423 | } |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 424 | |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 425 | @Override |
| 426 | protected void dispatchDraw(Canvas canvas) { |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 427 | if (mCurrentTextureWidth != mUi.getContentWidth() || |
| 428 | mCurrentTextureHeight != getHeight()) { |
| 429 | mCurrentTextureWidth = mUi.getContentWidth(); |
| 430 | mCurrentTextureHeight = getHeight(); |
| 431 | |
| 432 | if (mCurrentTextureWidth > 0 && mCurrentTextureHeight > 0) { |
| 433 | Bitmap activeTexture = getDrawableAsBitmap(mActiveDrawable, |
| 434 | mCurrentTextureWidth, mCurrentTextureHeight); |
| 435 | Bitmap inactiveTexture = getDrawableAsBitmap(mInactiveDrawable, |
| 436 | mCurrentTextureWidth, mCurrentTextureHeight); |
| 437 | |
| 438 | mActiveShader = new BitmapShader(activeTexture, |
| 439 | Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); |
| 440 | mActiveShaderPaint.setShader(mActiveShader); |
| 441 | |
| 442 | mInactiveShader = new BitmapShader(inactiveTexture, |
| 443 | Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); |
| 444 | mInactiveShaderPaint.setShader(mInactiveShader); |
| 445 | } |
| 446 | } |
| 447 | |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 448 | int state = canvas.save(); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 449 | getLocationInWindow(mWindowPos); |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 450 | Paint paint = mSelected ? mActiveShaderPaint : mInactiveShaderPaint; |
| 451 | drawClipped(canvas, paint, mPath, mWindowPos[0]); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 452 | canvas.restoreToCount(state); |
| 453 | super.dispatchDraw(canvas); |
| 454 | } |
| 455 | |
Romain Guy | c837321 | 2011-01-07 19:15:16 -0800 | [diff] [blame^] | 456 | private void drawClipped(Canvas canvas, Paint paint, Path clipPath, int left) { |
| 457 | // TODO: We should change the matrix/shader only when needed |
| 458 | final Matrix matrix = mSelected ? mActiveMatrix : mInactiveMatrix; |
| 459 | matrix.setTranslate(-left, 0.0f); |
| 460 | (mSelected ? mActiveShader : mInactiveShader).setLocalMatrix(matrix); |
| 461 | canvas.drawPath(clipPath, paint); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | private void setTabPath(Path path, int l, int t, int r, int b) { |
| 465 | path.reset(); |
| 466 | path.moveTo(l, b); |
| 467 | path.lineTo(l, t); |
| 468 | path.lineTo(r - mTabSliceWidth, t); |
| 469 | path.lineTo(r, b); |
| 470 | path.close(); |
| 471 | } |
| 472 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /** |
| 476 | * Store tab state within the title bar |
| 477 | */ |
| 478 | class TabViewData { |
| 479 | |
| 480 | Tab mTab; |
| 481 | TabView mTabView; |
| 482 | int mProgress; |
| 483 | Drawable mIcon; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 484 | |
| 485 | TabViewData(Tab tab) { |
| 486 | mTab = tab; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 487 | setUrlAndTitle(mTab.getUrl(), mTab.getTitle()); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void setUrlAndTitle(String url, String title) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 491 | if (mTabView != null) { |
| 492 | if (title != null) { |
| 493 | mTabView.setDisplayTitle(title); |
| 494 | } else if (url != null) { |
John Reck | fb3017f | 2010-10-26 19:01:24 -0700 | [diff] [blame] | 495 | mTabView.setDisplayTitle(UrlUtils.stripUrl(url)); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void setProgress(int newProgress) { |
| 501 | mProgress = newProgress; |
| 502 | if (mTabView != null) { |
| 503 | mTabView.setProgress(mProgress); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | void setFavicon(Bitmap icon) { |
| 508 | Drawable[] array = new Drawable[3]; |
| 509 | array[0] = new PaintDrawable(Color.BLACK); |
| 510 | array[1] = new PaintDrawable(Color.WHITE); |
| 511 | if (icon == null) { |
John Reck | aff60fb | 2010-10-25 13:47:58 -0700 | [diff] [blame] | 512 | array[2] = mGenericFavicon; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 513 | } else { |
| 514 | array[2] = new BitmapDrawable(icon); |
| 515 | } |
| 516 | LayerDrawable d = new LayerDrawable(array); |
| 517 | d.setLayerInset(1, 1, 1, 1, 1); |
| 518 | d.setLayerInset(2, 2, 2, 2, 2); |
| 519 | mIcon = d; |
| 520 | if (mTabView != null) { |
| 521 | mTabView.setFavicon(mIcon); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | } |
| 526 | |
| 527 | // TabChangeListener implementation |
| 528 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 529 | public void onSetActiveTab(Tab tab) { |
| 530 | mTabs.setSelectedTab(mTabControl.getTabIndex(tab)); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 531 | TabViewData tvd = mTabMap.get(tab); |
| 532 | if (tvd != null) { |
| 533 | tvd.setProgress(tvd.mProgress); |
| 534 | // update the scroll state |
| 535 | WebView webview = tab.getWebView(); |
Michael Kolb | 6a85e2a | 2010-12-13 18:12:46 -0800 | [diff] [blame] | 536 | if (webview != null) { |
| 537 | int h = webview.getVisibleTitleHeight(); |
| 538 | mVisibleTitleHeight = h -1; |
| 539 | onScroll(h); |
| 540 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 544 | public void onFavicon(Tab tab, Bitmap favicon) { |
| 545 | TabViewData tvd = mTabMap.get(tab); |
| 546 | if (tvd != null) { |
| 547 | tvd.setFavicon(favicon); |
| 548 | } |
| 549 | } |
| 550 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 551 | public void onNewTab(Tab tab) { |
| 552 | TabViewData tvd = buildTab(tab); |
| 553 | buildView(tvd); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 556 | public void onProgress(Tab tab, int progress) { |
| 557 | TabViewData tvd = mTabMap.get(tab); |
| 558 | if (tvd != null) { |
| 559 | tvd.setProgress(progress); |
| 560 | } |
| 561 | } |
| 562 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 563 | public void onRemoveTab(Tab tab) { |
| 564 | TabViewData tvd = mTabMap.get(tab); |
Michael Kolb | e2752f7 | 2010-10-25 16:50:16 -0700 | [diff] [blame] | 565 | if (tvd != null) { |
| 566 | TabView tv = tvd.mTabView; |
| 567 | if (tv != null) { |
| 568 | mTabs.removeTab(tv); |
| 569 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 570 | } |
| 571 | mTabMap.remove(tab); |
| 572 | } |
| 573 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 574 | public void onUrlAndTitle(Tab tab, String url, String title) { |
| 575 | TabViewData tvd = mTabMap.get(tab); |
| 576 | if (tvd != null) { |
| 577 | tvd.setUrlAndTitle(url, title); |
| 578 | } |
| 579 | } |
| 580 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 581 | private boolean isLoading() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 582 | TabViewData tvd = mTabMap.get(mTabControl.getCurrentTab()); |
| 583 | if ((tvd != null) && (tvd.mTabView != null)) { |
| 584 | return tvd.mTabView.mInLoad; |
| 585 | } else { |
| 586 | return false; |
| 587 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 590 | } |