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