Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [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 | |
| 19 | import com.android.browser.ScrollWebView.ScrollListener; |
| 20 | |
| 21 | import android.app.ActionBar; |
| 22 | import android.app.Activity; |
| 23 | import android.graphics.Bitmap; |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 24 | import android.util.Log; |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 25 | import android.view.ActionMode; |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 26 | import android.view.Gravity; |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 27 | import android.webkit.WebView; |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 28 | import android.widget.FrameLayout; |
| 29 | import android.widget.FrameLayout.LayoutParams; |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 30 | |
| 31 | import java.util.List; |
| 32 | |
| 33 | /** |
| 34 | * Ui for xlarge screen sizes |
| 35 | */ |
| 36 | public class XLargeUi extends BaseUi implements ScrollListener { |
| 37 | |
| 38 | private static final String LOGTAG = "XLargeUi"; |
| 39 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 40 | private ActionBar mActionBar; |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 41 | private TabBar mTabBar; |
| 42 | |
| 43 | private TitleBarXLarge mTitleBar; |
| 44 | private TitleBarXLarge mFakeTitleBar; |
| 45 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 46 | private boolean mUseQuickControls; |
| 47 | private PieControl mPieControl; |
| 48 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 49 | /** |
| 50 | * @param browser |
| 51 | * @param controller |
| 52 | */ |
| 53 | public XLargeUi(Activity browser, UiController controller) { |
| 54 | super(browser, controller); |
| 55 | mTitleBar = new TitleBarXLarge(mActivity, mUiController, this); |
| 56 | mTitleBar.setProgress(100); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 57 | mTitleBar.setEditable(false); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 58 | mFakeTitleBar = new TitleBarXLarge(mActivity, mUiController, this); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 59 | mFakeTitleBar.setEditable(true); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 60 | mTabBar = new TabBar(mActivity, mUiController, this); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 61 | mActionBar = mActivity.getActionBar(); |
| 62 | mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); |
| 63 | mActionBar.setCustomView(mTabBar); |
| 64 | setUseQuickControls(BrowserSettings.getInstance().useQuickControls()); |
| 65 | } |
| 66 | |
| 67 | private void setUseQuickControls(boolean useQuickControls) { |
| 68 | mUseQuickControls = useQuickControls; |
| 69 | if (useQuickControls) { |
| 70 | checkTabCount(); |
| 71 | mPieControl = new PieControl(mActivity, mUiController, this); |
| 72 | mPieControl.attachToContainer(mContentView); |
| 73 | setFakeTitleBarGravity(Gravity.BOTTOM); |
| 74 | |
| 75 | // remove embedded title bar if present |
| 76 | WebView web = mTabControl.getCurrentWebView(); |
| 77 | if ((web != null) && (web.getVisibleTitleHeight() > 0)) { |
| 78 | web.setEmbeddedTitleBar(null); |
| 79 | } |
| 80 | } else { |
| 81 | mActivity.getActionBar().show(); |
| 82 | if (mPieControl != null) { |
| 83 | mPieControl.removeFromContainer(mContentView); |
| 84 | } |
| 85 | setFakeTitleBarGravity(Gravity.TOP); |
| 86 | // remove embedded title bar if present |
| 87 | WebView web = mTabControl.getCurrentWebView(); |
| 88 | if ((web != null) && (web.getVisibleTitleHeight() == 0)) { |
| 89 | web.setEmbeddedTitleBar(mTitleBar); |
| 90 | } |
| 91 | } |
| 92 | mTabBar.setUseQuickControls(mUseQuickControls); |
| 93 | mFakeTitleBar.setUseQuickControls(mUseQuickControls); |
| 94 | } |
| 95 | |
| 96 | private void checkTabCount() { |
| 97 | if (mUseQuickControls) { |
| 98 | int n = mTabBar.getTabCount(); |
| 99 | if (n >= 2) { |
| 100 | mActivity.getActionBar().show(); |
| 101 | } else if (n == 1) { |
| 102 | mActivity.getActionBar().hide(); |
| 103 | } |
| 104 | } |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onDestroy() { |
| 109 | hideFakeTitleBar(); |
| 110 | } |
| 111 | |
| 112 | // webview factory |
| 113 | |
| 114 | @Override |
| 115 | public WebView createWebView(boolean privateBrowsing) { |
| 116 | // Create a new WebView |
| 117 | ScrollWebView w = new ScrollWebView(mActivity, null, |
| 118 | android.R.attr.webViewStyle, privateBrowsing); |
| 119 | initWebViewSettings(w); |
| 120 | w.setScrollListener(this); |
| 121 | w.getSettings().setDisplayZoomControls(false); |
| 122 | return w; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public WebView createSubWebView(boolean privateBrowsing) { |
| 127 | ScrollWebView web = (ScrollWebView) createWebView(privateBrowsing); |
| 128 | // no scroll listener for subview |
| 129 | web.setScrollListener(null); |
| 130 | return web; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onScroll(int visibleTitleHeight) { |
| 135 | mTabBar.onScroll(visibleTitleHeight); |
| 136 | } |
| 137 | |
| 138 | void stopWebViewScrolling() { |
| 139 | ScrollWebView web = (ScrollWebView) mUiController.getCurrentWebView(); |
| 140 | if (web != null) { |
| 141 | web.stopScroll(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // WebView callbacks |
| 146 | |
| 147 | @Override |
| 148 | public void onPageStarted(Tab tab, String url, Bitmap favicon) { |
| 149 | super.onPageStarted(tab, url, favicon); |
| 150 | mTabBar.onPageStarted(tab, url, favicon); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void bookmarkedStatusHasChanged(Tab tab) { |
| 155 | if (tab.inForeground()) { |
| 156 | boolean isBookmark = tab.isBookmarkedSite(); |
| 157 | mTitleBar.setCurrentUrlIsBookmark(isBookmark); |
| 158 | mFakeTitleBar.setCurrentUrlIsBookmark(isBookmark); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void onPageFinished(Tab tab, String url) { |
| 164 | mTabBar.onPageFinished(tab); |
| 165 | super.onPageFinished(tab, url); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 166 | if (mUseQuickControls) { |
| 167 | mFakeTitleBar.setShowProgressOnly(false); |
| 168 | } |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void onProgressChanged(Tab tab, int progress) { |
| 173 | mTabBar.onProgress(tab, progress); |
| 174 | if (tab.inForeground()) { |
| 175 | mFakeTitleBar.setProgress(progress); |
| 176 | if (progress == 100) { |
| 177 | hideFakeTitleBar(); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 178 | if (mUseQuickControls) { |
| 179 | mFakeTitleBar.setShowProgressOnly(false); |
| 180 | setFakeTitleBarGravity(Gravity.BOTTOM); |
| 181 | } |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 182 | } else { |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 183 | if (mUseQuickControls) { |
| 184 | mFakeTitleBar.setShowProgressOnly(true); |
| 185 | if (!isFakeTitleBarShowing()) { |
| 186 | setFakeTitleBarGravity(Gravity.TOP); |
| 187 | } |
| 188 | } |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 189 | showFakeTitleBar(); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public boolean needsRestoreAllTabs() { |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void addTab(Tab tab) { |
| 201 | mTabBar.onNewTab(tab); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 202 | checkTabCount(); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void setActiveTab(Tab tab) { |
| 207 | super.setActiveTab(tab); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 208 | ScrollWebView view = (ScrollWebView) tab.getWebView(); |
| 209 | // TabControl.setCurrentTab has been called before this, |
| 210 | // so the tab is guaranteed to have a webview |
| 211 | if (view == null) { |
| 212 | Log.e(LOGTAG, "active tab with no webview detected"); |
| 213 | return; |
| 214 | } |
| 215 | // Request focus on the top window. |
| 216 | if (mUseQuickControls) { |
| 217 | mPieControl.forceToTop(mContentView); |
| 218 | view.setScrollListener(null); |
| 219 | mTabBar.showTitleBarIndicator(false); |
| 220 | } else { |
| 221 | view.setEmbeddedTitleBar(mTitleBar); |
| 222 | view.setScrollListener(this); |
| 223 | } |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 224 | mTabBar.onSetActiveTab(tab); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 225 | if (tab.isInVoiceSearchMode()) { |
| 226 | showVoiceTitleBar(tab.getVoiceDisplayTitle()); |
| 227 | } else { |
| 228 | revertVoiceTitleBar(tab); |
| 229 | } |
| 230 | resetTitleIconAndProgress(tab); |
| 231 | updateLockIconToLatest(tab); |
| 232 | tab.getTopWindow().requestFocus(); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void updateTabs(List<Tab> tabs) { |
| 237 | mTabBar.updateTabs(tabs); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 238 | checkTabCount(); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void removeTab(Tab tab) { |
| 243 | super.removeTab(tab); |
| 244 | mTabBar.onRemoveTab(tab); |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 245 | checkTabCount(); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 248 | int getContentWidth() { |
| 249 | if (mContentView != null) { |
| 250 | return mContentView.getWidth(); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 251 | } |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | void editUrl(boolean clearInput) { |
| 256 | showFakeTitleBar(); |
| 257 | mFakeTitleBar.onEditUrl(clearInput); |
| 258 | } |
| 259 | |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 260 | void setFakeTitleBarGravity(int gravity) { |
| 261 | FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) |
| 262 | mFakeTitleBar.getLayoutParams(); |
| 263 | if (lp == null) { |
| 264 | lp = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
| 265 | LayoutParams.WRAP_CONTENT); |
| 266 | } |
| 267 | lp.gravity = gravity; |
| 268 | mFakeTitleBar.setLayoutParams(lp); |
| 269 | } |
| 270 | |
| 271 | void showFakeTitleBarAndEdit() { |
| 272 | showFakeTitleBar(); |
| 273 | mFakeTitleBar.onEditUrl(false); |
| 274 | } |
| 275 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 276 | @Override |
| 277 | protected void attachFakeTitleBar(WebView mainView) { |
| 278 | mContentView.addView(mFakeTitleBar); |
| 279 | mTabBar.onShowTitleBar(); |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | protected void hideFakeTitleBar() { |
| 284 | if (isFakeTitleBarShowing()) { |
| 285 | mContentView.removeView(mFakeTitleBar); |
| 286 | mTabBar.onHideTitleBar(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | protected boolean isFakeTitleBarShowing() { |
| 292 | return (mFakeTitleBar.getParent() != null); |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | protected TitleBarBase getFakeTitleBar() { |
| 297 | return mFakeTitleBar; |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | protected TitleBarBase getEmbeddedTitleBar() { |
| 302 | return mTitleBar; |
| 303 | } |
| 304 | |
| 305 | // action mode callbacks |
| 306 | |
| 307 | @Override |
| 308 | public void onActionModeStarted(ActionMode mode) { |
| 309 | if (mFakeTitleBar.isEditingUrl()) { |
| 310 | // hide the fake title bar when CAB is shown |
| 311 | hideFakeTitleBar(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | @Override |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 316 | public void onActionModeFinished(boolean inLoad) { |
| 317 | checkTabCount(); |
| 318 | if (inLoad) { |
| 319 | // the titlebar was removed when the CAB was shown |
| 320 | // if the page is loading, show it again |
| 321 | mFakeTitleBar.setShowProgressOnly(true); |
| 322 | if (!isFakeTitleBarShowing()) { |
| 323 | setFakeTitleBarGravity(Gravity.TOP); |
| 324 | } |
| 325 | showFakeTitleBar(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | @Override |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 330 | public void setUrlTitle(Tab tab, String url, String title) { |
| 331 | super.setUrlTitle(tab, url, title); |
| 332 | mTabBar.onUrlAndTitle(tab, url, title); |
| 333 | } |
| 334 | |
| 335 | // Set the favicon in the title bar. |
| 336 | @Override |
| 337 | public void setFavicon(Tab tab, Bitmap icon) { |
| 338 | super.setFavicon(tab, icon); |
| 339 | mTabBar.onFavicon(tab, icon); |
| 340 | } |
| 341 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 342 | @Override |
| 343 | public void showVoiceTitleBar(String title) { |
| 344 | List<String> vsresults = null; |
| 345 | if (getActiveTab() != null) { |
| 346 | vsresults = getActiveTab().getVoiceSearchResults(); |
| 347 | } |
| 348 | mTitleBar.setInVoiceMode(true, null); |
| 349 | mTitleBar.setDisplayTitle(title); |
| 350 | mFakeTitleBar.setInVoiceMode(true, vsresults); |
| 351 | mFakeTitleBar.setDisplayTitle(title); |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public void revertVoiceTitleBar(Tab tab) { |
| 356 | mTitleBar.setInVoiceMode(false, null); |
| 357 | String url = tab.getCurrentUrl(); |
| 358 | mTitleBar.setDisplayTitle(url); |
| 359 | mFakeTitleBar.setInVoiceMode(false, null); |
| 360 | mFakeTitleBar.setDisplayTitle(url); |
| 361 | } |
| 362 | |
| 363 | |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 364 | } |