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 android.app.Activity; |
| 20 | import android.content.Context; |
| 21 | import android.graphics.PixelFormat; |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 22 | import android.util.Log; |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 23 | import android.view.ActionMode; |
| 24 | import android.view.Gravity; |
| 25 | import android.view.Menu; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | import android.view.WindowManager; |
| 29 | import android.webkit.WebView; |
| 30 | |
| 31 | /** |
| 32 | * Ui for regular phone screen sizes |
| 33 | */ |
| 34 | public class PhoneUi extends BaseUi { |
| 35 | |
| 36 | private static final String LOGTAG = "PhoneUi"; |
| 37 | |
| 38 | private TitleBar mTitleBar; |
| 39 | private TitleBar mFakeTitleBar; |
| 40 | private ActiveTabsPage mActiveTabsPage; |
| 41 | |
| 42 | boolean mExtendedMenuOpen; |
| 43 | boolean mOptionsMenuOpen; |
| 44 | |
| 45 | /** |
| 46 | * @param browser |
| 47 | * @param controller |
| 48 | */ |
| 49 | public PhoneUi(Activity browser, UiController controller) { |
| 50 | super(browser, controller); |
| 51 | mTitleBar = new TitleBar(mActivity, mUiController); |
| 52 | // mTitleBar will be always be shown in the fully loaded mode on |
| 53 | // phone |
| 54 | mTitleBar.setProgress(100); |
| 55 | mFakeTitleBar = new TitleBar(mActivity, mUiController); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | // webview factory |
| 60 | |
| 61 | @Override |
| 62 | public WebView createWebView(boolean privateBrowsing) { |
| 63 | // Create a new WebView |
| 64 | WebView w = new WebView(mActivity, null, |
| 65 | android.R.attr.webViewStyle, privateBrowsing); |
| 66 | initWebViewSettings(w); |
| 67 | return w; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public WebView createSubWebView(boolean privateBrowsing) { |
| 72 | WebView web = createWebView(privateBrowsing); |
| 73 | return web; |
| 74 | } |
| 75 | |
| 76 | // lifecycle |
| 77 | |
| 78 | @Override |
| 79 | public void onPause() { |
| 80 | // FIXME: This removes the active tabs page and resets the menu to |
| 81 | // MAIN_MENU. A better solution might be to do this work in onNewIntent |
| 82 | // but then we would need to save it in onSaveInstanceState and restore |
| 83 | // it in onCreate/onRestoreInstanceState |
| 84 | if (mActiveTabsPage != null) { |
| 85 | mUiController.removeActiveTabsPage(true); |
| 86 | } |
| 87 | super.onPause(); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onDestroy() { |
| 92 | hideFakeTitleBar(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean onBackKey() { |
| 97 | if (mActiveTabsPage != null) { |
| 98 | // if tab page is showing, hide it |
| 99 | mUiController.removeActiveTabsPage(true); |
| 100 | return true; |
| 101 | } |
| 102 | return super.onBackKey(); |
| 103 | } |
| 104 | |
| 105 | @Override |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 106 | public void onProgressChanged(Tab tab) { |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 107 | if (tab.inForeground()) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 108 | int progress = tab.getLoadProgress(); |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 109 | mFakeTitleBar.setProgress(progress); |
| 110 | if (progress == 100) { |
| 111 | if (!mOptionsMenuOpen || !mExtendedMenuOpen) { |
| 112 | hideFakeTitleBar(); |
| 113 | } |
| 114 | } else { |
| 115 | if (!mOptionsMenuOpen || mExtendedMenuOpen) { |
| 116 | showFakeTitleBar(); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 123 | public void setActiveTab(Tab tab) { |
| 124 | super.setActiveTab(tab); |
| 125 | WebView view = tab.getWebView(); |
| 126 | // TabControl.setCurrentTab has been called before this, |
| 127 | // so the tab is guaranteed to have a webview |
| 128 | if (view == null) { |
| 129 | Log.e(LOGTAG, "active tab with no webview detected"); |
| 130 | return; |
| 131 | } |
| 132 | view.setEmbeddedTitleBar(getEmbeddedTitleBar()); |
| 133 | if (tab.isInVoiceSearchMode()) { |
| 134 | showVoiceTitleBar(tab.getVoiceDisplayTitle()); |
| 135 | } else { |
| 136 | revertVoiceTitleBar(tab); |
| 137 | } |
Michael Kolb | 376b541 | 2010-12-15 11:52:57 -0800 | [diff] [blame] | 138 | tab.getTopWindow().requestFocus(); |
| 139 | } |
| 140 | |
| 141 | @Override |
Michael Kolb | 6670653 | 2010-12-12 19:50:22 -0800 | [diff] [blame] | 142 | protected void attachFakeTitleBar(WebView mainView) { |
| 143 | WindowManager manager = (WindowManager) |
| 144 | mActivity.getSystemService(Context.WINDOW_SERVICE); |
| 145 | |
| 146 | // Add the title bar to the window manager so it can receive |
| 147 | // touches while the menu is up |
| 148 | WindowManager.LayoutParams params = |
| 149 | new WindowManager.LayoutParams( |
| 150 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 151 | ViewGroup.LayoutParams.WRAP_CONTENT, |
| 152 | WindowManager.LayoutParams.TYPE_APPLICATION, |
| 153 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, |
| 154 | PixelFormat.TRANSLUCENT); |
| 155 | params.gravity = Gravity.TOP; |
| 156 | boolean atTop = mainView.getScrollY() == 0; |
| 157 | params.windowAnimations = atTop ? 0 : R.style.TitleBar; |
| 158 | manager.addView(mFakeTitleBar, params); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | protected void hideFakeTitleBar() { |
| 163 | if (!isFakeTitleBarShowing()) return; |
| 164 | WindowManager.LayoutParams params = |
| 165 | (WindowManager.LayoutParams) mFakeTitleBar.getLayoutParams(); |
| 166 | WebView mainView = mUiController.getCurrentWebView(); |
| 167 | // Although we decided whether or not to animate based on the |
| 168 | // current |
| 169 | // scroll position, the scroll position may have changed since the |
| 170 | // fake title bar was displayed. Make sure it has the appropriate |
| 171 | // animation/lack thereof before removing. |
| 172 | params.windowAnimations = |
| 173 | mainView != null && mainView.getScrollY() == 0 ? |
| 174 | 0 : R.style.TitleBar; |
| 175 | WindowManager manager = (WindowManager) mActivity |
| 176 | .getSystemService(Context.WINDOW_SERVICE); |
| 177 | manager.updateViewLayout(mFakeTitleBar, params); |
| 178 | manager.removeView(mFakeTitleBar); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | protected boolean isFakeTitleBarShowing() { |
| 183 | return (mFakeTitleBar.getParent() != null); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | protected TitleBarBase getFakeTitleBar() { |
| 188 | return mFakeTitleBar; |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | protected TitleBarBase getEmbeddedTitleBar() { |
| 193 | return mTitleBar; |
| 194 | } |
| 195 | |
| 196 | // active tabs page |
| 197 | |
| 198 | @Override |
| 199 | public void showActiveTabsPage() { |
| 200 | mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController); |
| 201 | mTitleBar.setVisibility(View.GONE); |
| 202 | hideFakeTitleBar(); |
| 203 | mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS); |
| 204 | mActiveTabsPage.requestFocus(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Remove the active tabs page. |
| 209 | */ |
| 210 | @Override |
| 211 | public void removeActiveTabsPage() { |
| 212 | mContentView.removeView(mActiveTabsPage); |
| 213 | mTitleBar.setVisibility(View.VISIBLE); |
| 214 | mActiveTabsPage = null; |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public boolean showsWeb() { |
| 219 | return super.showsWeb() && mActiveTabsPage == null; |
| 220 | } |
| 221 | |
| 222 | // menu handling callbacks |
| 223 | |
| 224 | @Override |
| 225 | public void onOptionsMenuOpened() { |
| 226 | mOptionsMenuOpen = true; |
| 227 | // options menu opened, show fake title bar |
| 228 | showFakeTitleBar(); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void onExtendedMenuOpened() { |
| 233 | // Switching the menu to expanded view, so hide the |
| 234 | // title bar. |
| 235 | mExtendedMenuOpen = true; |
| 236 | hideFakeTitleBar(); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void onOptionsMenuClosed(boolean inLoad) { |
| 241 | mOptionsMenuOpen = false; |
| 242 | if (!inLoad) { |
| 243 | hideFakeTitleBar(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void onExtendedMenuClosed(boolean inLoad) { |
| 249 | mExtendedMenuOpen = false; |
| 250 | showFakeTitleBar(); |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void onContextMenuCreated(Menu menu) { |
| 255 | hideFakeTitleBar(); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void onContextMenuClosed(Menu menu, boolean inLoad) { |
| 260 | if (inLoad) { |
| 261 | showFakeTitleBar(); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // action mode callbacks |
| 266 | |
| 267 | @Override |
| 268 | public void onActionModeStarted(ActionMode mode) { |
| 269 | hideFakeTitleBar(); |
| 270 | } |
| 271 | |
| 272 | } |