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