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