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