blob: 863a628f2a5ad1ec9fb18cdc9995158f4c4874d0 [file] [log] [blame]
Michael Kolb66706532010-12-12 19:50:22 -08001/*
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;
Michael Kolb376b5412010-12-15 11:52:57 -080020import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080021import android.view.ActionMode;
22import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080023import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080024import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070025import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080026import android.view.View;
Michael Kolb30adae62011-07-29 13:37:05 -070027import android.view.accessibility.AccessibilityEvent;
Michael Kolb66706532010-12-12 19:50:22 -080028import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070029import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080030
Michael Kolb629b22c2011-07-13 16:26:47 -070031import com.android.browser.UrlInputView.StateListener;
32
Michael Kolb66706532010-12-12 19:50:22 -080033/**
34 * Ui for regular phone screen sizes
35 */
36public class PhoneUi extends BaseUi {
37
38 private static final String LOGTAG = "PhoneUi";
39
Michael Kolb0241e752011-07-07 14:58:50 -070040 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070041 private NavScreen mNavScreen;
John Reck0f602f32011-07-07 15:38:43 -070042 private NavigationBarPhone mNavigationBar;
Michael Kolb66706532010-12-12 19:50:22 -080043
44 boolean mExtendedMenuOpen;
45 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070046 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080047
48 /**
49 * @param browser
50 * @param controller
51 */
52 public PhoneUi(Activity browser, UiController controller) {
53 super(browser, controller);
John Reck285ef042011-02-11 15:44:17 -080054 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070055 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070056 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reck285ef042011-02-11 15:44:17 -080057 }
Michael Kolb66706532010-12-12 19:50:22 -080058
John Reck285ef042011-02-11 15:44:17 -080059 @Override
Michael Kolb66706532010-12-12 19:50:22 -080060 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080061 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080062 }
63
64 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080065 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070066 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070067 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070068 }
69 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080070 }
71
72 @Override
Michael Kolb66706532010-12-12 19:50:22 -080073 public boolean onBackKey() {
Michael Kolb9ef259a2011-07-12 15:33:08 -070074 if (mNavScreen != null) {
Michael Kolbf2055602011-04-09 17:20:03 -070075 mNavScreen.close();
76 return true;
Michael Kolb66706532010-12-12 19:50:22 -080077 }
78 return super.onBackKey();
79 }
80
81 @Override
Michael Kolbf2055602011-04-09 17:20:03 -070082 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -070083 return false;
84 }
85
86 @Override
John Reck30c714c2010-12-16 17:30:34 -080087 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -080088 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -080089 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -080090 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -080091 if (progress == 100) {
92 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -070093 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -070094 if (mUseQuickControls) {
95 mTitleBar.setShowProgressOnly(false);
96 }
Michael Kolb66706532010-12-12 19:50:22 -080097 }
98 } else {
99 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700100 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
101 mTitleBar.setShowProgressOnly(true);
102 setTitleGravity(Gravity.TOP);
103 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800104 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800105 }
106 }
107 }
108 }
109
110 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700111 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700112 mTitleBar.cancelTitleBarAnimation(true);
113 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700114 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700115 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800116 // TabControl.setCurrentTab has been called before this,
117 // so the tab is guaranteed to have a webview
118 if (view == null) {
119 Log.e(LOGTAG, "active tab with no webview detected");
120 return;
121 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700122 // Request focus on the top window.
123 if (mUseQuickControls) {
124 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700125 } else {
126 // check if title bar is already attached by animation
127 if (mTitleBar.getParent() == null) {
128 view.setEmbeddedTitleBar(mTitleBar);
129 }
130 }
Michael Kolb376b5412010-12-15 11:52:57 -0800131 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700132 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800133 } else {
134 revertVoiceTitleBar(tab);
135 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700136 // update nav bar state
137 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700138 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800139 tab.getTopWindow().requestFocus();
John Reck8ee633f2011-08-09 16:00:35 -0700140 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800141 }
142
Michael Kolb66706532010-12-12 19:50:22 -0800143 // menu handling callbacks
144
145 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700146 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700147 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700148 return true;
149 }
150
151 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700152 public void updateMenuState(Tab tab, Menu menu) {
153 menu.setGroupVisible(R.id.NAV_MENU, (mNavScreen == null));
154 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
155 if (bm != null) {
156 bm.setVisible(mNavScreen == null);
157 }
158 MenuItem nt = menu.findItem(R.id.new_tab_menu_id);
159 if (nt != null) {
160 nt.setVisible(mNavScreen == null);
161 }
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700162 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
163 if (abm != null) {
164 abm.setVisible((tab != null) && !tab.isSnapshot());
165 }
166 }
167
168 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700169 public boolean onOptionsItemSelected(MenuItem item) {
170 if (mNavScreen != null) {
171 hideNavScreen(false);
172 }
173 return false;
174 }
175
176 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800177 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800178 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800179 }
180
181 @Override
182 public void onContextMenuClosed(Menu menu, boolean inLoad) {
183 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800184 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800185 }
186 }
187
188 // action mode callbacks
189
190 @Override
191 public void onActionModeStarted(ActionMode mode) {
Michael Kolb42c0c062011-08-02 12:56:06 -0700192 if (!isEditingUrl()) {
193 hideTitleBar();
194 }
Michael Kolb66706532010-12-12 19:50:22 -0800195 }
196
Michael Kolba4183062011-01-16 10:43:21 -0800197 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700198 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700199 if (inLoad) {
200 if (mUseQuickControls) {
201 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700202 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700203 showTitleBar();
204 }
205 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700206 }
207
208 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700209 protected void setTitleGravity(int gravity) {
210 if (mUseQuickControls) {
211 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700212 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700213 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700214 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700215 } else {
216 super.setTitleGravity(gravity);
217 }
218 }
219
Michael Kolb0241e752011-07-07 14:58:50 -0700220 @Override
221 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700222 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700223 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700224 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700225 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700226 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700227 WebView web = getWebView();
228 if (web != null) {
229 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700230 }
231 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700232 if (mPieControl != null) {
233 mPieControl.removeFromContainer(mContentView);
234 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700235 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700236 if (web != null) {
237 web.setEmbeddedTitleBar(mTitleBar);
238 }
239 setTitleGravity(Gravity.NO_GRAVITY);
240 }
John Reck718a24d2011-08-12 11:08:30 -0700241 updateUrlBarAutoShowManagerTarget();
Michael Kolbfdb70242011-03-24 09:41:11 -0700242 }
243
Michael Kolbf2055602011-04-09 17:20:03 -0700244 void showNavScreen() {
John Reck8ee633f2011-08-09 16:00:35 -0700245 mActiveTab.capture();
Michael Kolb2814a362011-05-19 15:49:41 -0700246 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700247 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700248 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700249 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700250 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700251 mCustomViewContainer.setVisibility(View.VISIBLE);
252 mCustomViewContainer.bringToFront();
Michael Kolb30adae62011-07-29 13:37:05 -0700253 // notify accessibility manager about the screen change
254 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
John Reck8ee633f2011-08-09 16:00:35 -0700255 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
Michael Kolbf2055602011-04-09 17:20:03 -0700256 }
257
Michael Kolb2814a362011-05-19 15:49:41 -0700258 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700259 if (mNavScreen == null) return;
John Reck8ee633f2011-08-09 16:00:35 -0700260 mTabControl.setOnThumbnailUpdatedListener(null);
Michael Kolb2814a362011-05-19 15:49:41 -0700261 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700262 mCustomViewContainer.removeView(mNavScreen);
263 mNavScreen = null;
264 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700265 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700266 // Show the content view.
267 mContentView.setVisibility(View.VISIBLE);
268 }
269
John Reck6b4bd5f2011-07-12 10:14:38 -0700270 @Override
271 public boolean needsRestoreAllTabs() {
272 return false;
273 }
274
Michael Kolb20be26d2011-07-18 16:38:02 -0700275 public void toggleNavScreen() {
276 if (mNavScreen == null) {
277 showNavScreen();
278 } else {
279 hideNavScreen(false);
280 }
281 }
282
John Reck1cf4b792011-07-26 10:22:22 -0700283 @Override
284 public boolean shouldCaptureThumbnails() {
285 return true;
286 }
287
John Reck3ba45532011-08-11 16:26:53 -0700288 @Override
289 public boolean isWebShowing() {
290 return super.isWebShowing() && mNavScreen == null;
291 }
292
293 @Override
294 public void showWeb(boolean animate) {
295 super.showWeb(animate);
296 hideNavScreen(animate);
297 }
298
Michael Kolb66706532010-12-12 19:50:22 -0800299}