blob: f33dbef6d13fe220aff08bb0e63ae8b380c8e36d [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 Kolb4bd767d2011-05-27 11:33:55 -070020import android.os.Bundle;
Michael Kolb376b5412010-12-15 11:52:57 -080021import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080022import android.view.ActionMode;
23import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080024import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080025import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070026import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080027import android.view.View;
Michael Kolb30adae62011-07-29 13:37:05 -070028import android.view.accessibility.AccessibilityEvent;
Michael Kolb66706532010-12-12 19:50:22 -080029import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070030import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080031
Michael Kolb629b22c2011-07-13 16:26:47 -070032import com.android.browser.UrlInputView.StateListener;
33
Michael Kolb66706532010-12-12 19:50:22 -080034/**
35 * Ui for regular phone screen sizes
36 */
37public class PhoneUi extends BaseUi {
38
39 private static final String LOGTAG = "PhoneUi";
40
Michael Kolb0241e752011-07-07 14:58:50 -070041 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070042 private NavScreen mNavScreen;
John Reck0f602f32011-07-07 15:38:43 -070043 private NavigationBarPhone mNavigationBar;
Michael Kolb66706532010-12-12 19:50:22 -080044
45 boolean mExtendedMenuOpen;
46 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070047 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080048
49 /**
50 * @param browser
51 * @param controller
52 */
53 public PhoneUi(Activity browser, UiController controller) {
54 super(browser, controller);
John Reck285ef042011-02-11 15:44:17 -080055 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070056 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070057 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reck285ef042011-02-11 15:44:17 -080058 }
Michael Kolb66706532010-12-12 19:50:22 -080059
John Reck285ef042011-02-11 15:44:17 -080060 @Override
Michael Kolb66706532010-12-12 19:50:22 -080061 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080062 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080063 }
64
65 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080066 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070067 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070068 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070069 }
70 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080071 }
72
73 @Override
Michael Kolb66706532010-12-12 19:50:22 -080074 public boolean onBackKey() {
Michael Kolb9ef259a2011-07-12 15:33:08 -070075 if (mNavScreen != null) {
Michael Kolbf2055602011-04-09 17:20:03 -070076 mNavScreen.close();
77 return true;
Michael Kolb66706532010-12-12 19:50:22 -080078 }
79 return super.onBackKey();
80 }
81
82 @Override
Michael Kolbf2055602011-04-09 17:20:03 -070083 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -070084 return false;
85 }
86
87 @Override
John Reck30c714c2010-12-16 17:30:34 -080088 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -080089 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -080090 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -080091 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -080092 if (progress == 100) {
93 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -070094 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -070095 if (mUseQuickControls) {
96 mTitleBar.setShowProgressOnly(false);
97 }
Michael Kolb66706532010-12-12 19:50:22 -080098 }
99 } else {
100 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700101 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
102 mTitleBar.setShowProgressOnly(true);
103 setTitleGravity(Gravity.TOP);
104 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800105 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800106 }
107 }
108 }
109 }
110
111 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700112 public void setActiveTab(final Tab tab) {
John Reck8ee633f2011-08-09 16:00:35 -0700113 mTitleBar.cancelTitleBarAnimation(true);
114 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700115 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700116 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800117 // TabControl.setCurrentTab has been called before this,
118 // so the tab is guaranteed to have a webview
119 if (view == null) {
120 Log.e(LOGTAG, "active tab with no webview detected");
121 return;
122 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700123 // Request focus on the top window.
124 if (mUseQuickControls) {
125 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700126 } else {
127 // check if title bar is already attached by animation
128 if (mTitleBar.getParent() == null) {
129 view.setEmbeddedTitleBar(mTitleBar);
130 }
131 }
Michael Kolb376b5412010-12-15 11:52:57 -0800132 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700133 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800134 } else {
135 revertVoiceTitleBar(tab);
136 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700137 // update nav bar state
138 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700139 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800140 tab.getTopWindow().requestFocus();
John Reck8ee633f2011-08-09 16:00:35 -0700141 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb376b5412010-12-15 11:52:57 -0800142 }
143
Michael Kolb017ffab2011-07-11 15:26:47 -0700144 /**
145 * Suggest to the UI that the title bar can be hidden. The UI will then
146 * decide whether or not to hide based off a number of factors, such
147 * as if the user is editing the URL bar or if the page is loading
148 */
149 @Override
150 public void suggestHideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700151 if (!mNavigationBar.isMenuShowing()) {
152 super.suggestHideTitleBar();
Michael Kolb017ffab2011-07-11 15:26:47 -0700153 }
154 }
155
Michael Kolb66706532010-12-12 19:50:22 -0800156 @Override
John Reck2bc80422011-06-30 15:11:49 -0700157 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700158 if (mNavScreen != null) {
159 hideNavScreen(false);
160 }
John Reck2bc80422011-06-30 15:11:49 -0700161 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700162 }
163
Michael Kolb66706532010-12-12 19:50:22 -0800164 // menu handling callbacks
165
166 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700167 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700168 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700169 return true;
170 }
171
172 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700173 public void updateMenuState(Tab tab, Menu menu) {
174 menu.setGroupVisible(R.id.NAV_MENU, (mNavScreen == null));
175 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
176 if (bm != null) {
177 bm.setVisible(mNavScreen == null);
178 }
179 MenuItem nt = menu.findItem(R.id.new_tab_menu_id);
180 if (nt != null) {
181 nt.setVisible(mNavScreen == null);
182 }
183 MenuItem find = menu.findItem(R.id.find_menu_id);
184 if (find != null) {
185 find.setVisible(((tab != null) && !tab.isSnapshot()));
186 }
187 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
188 if (abm != null) {
189 abm.setVisible((tab != null) && !tab.isSnapshot());
190 }
191 }
192
193 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700194 public boolean onOptionsItemSelected(MenuItem item) {
195 if (mNavScreen != null) {
196 hideNavScreen(false);
197 }
198 return false;
199 }
200
201 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800202 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800203 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800204 }
205
206 @Override
207 public void onContextMenuClosed(Menu menu, boolean inLoad) {
208 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800209 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800210 }
211 }
212
213 // action mode callbacks
214
215 @Override
216 public void onActionModeStarted(ActionMode mode) {
Michael Kolb42c0c062011-08-02 12:56:06 -0700217 if (!isEditingUrl()) {
218 hideTitleBar();
219 }
Michael Kolb66706532010-12-12 19:50:22 -0800220 }
221
Michael Kolba4183062011-01-16 10:43:21 -0800222 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700223 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700224 if (inLoad) {
225 if (mUseQuickControls) {
226 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700227 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700228 showTitleBar();
229 }
230 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700231 }
232
233 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700234 protected void setTitleGravity(int gravity) {
235 if (mUseQuickControls) {
236 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700237 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700238 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700239 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700240 } else {
241 super.setTitleGravity(gravity);
242 }
243 }
244
Michael Kolb0241e752011-07-07 14:58:50 -0700245 @Override
246 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700247 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700248 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700249 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700250 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700251 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700252 WebView web = getWebView();
253 if (web != null) {
254 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -0700255 // don't show url bar on scrolling
256 web.setOnTouchListener(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700257 }
258 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700259 if (mPieControl != null) {
260 mPieControl.removeFromContainer(mContentView);
261 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700262 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700263 if (web != null) {
264 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700265 // show url bar on scrolling
266 web.setOnTouchListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700267 }
268 setTitleGravity(Gravity.NO_GRAVITY);
269 }
270 }
271
Michael Kolbf2055602011-04-09 17:20:03 -0700272 void showNavScreen() {
John Reck8ee633f2011-08-09 16:00:35 -0700273 mActiveTab.capture();
Michael Kolb2814a362011-05-19 15:49:41 -0700274 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700275 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700276 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700277 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700278 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700279 mCustomViewContainer.setVisibility(View.VISIBLE);
280 mCustomViewContainer.bringToFront();
Michael Kolb30adae62011-07-29 13:37:05 -0700281 // notify accessibility manager about the screen change
282 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
John Reck8ee633f2011-08-09 16:00:35 -0700283 mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
Michael Kolbf2055602011-04-09 17:20:03 -0700284 }
285
Michael Kolb2814a362011-05-19 15:49:41 -0700286 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700287 if (mNavScreen == null) return;
John Reck8ee633f2011-08-09 16:00:35 -0700288 mTabControl.setOnThumbnailUpdatedListener(null);
Michael Kolb2814a362011-05-19 15:49:41 -0700289 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700290 mCustomViewContainer.removeView(mNavScreen);
291 mNavScreen = null;
292 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700293 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700294 // Show the content view.
295 mContentView.setVisibility(View.VISIBLE);
296 }
297
John Reck6b4bd5f2011-07-12 10:14:38 -0700298 @Override
299 public boolean needsRestoreAllTabs() {
300 return false;
301 }
302
Michael Kolb20be26d2011-07-18 16:38:02 -0700303 public void toggleNavScreen() {
304 if (mNavScreen == null) {
305 showNavScreen();
306 } else {
307 hideNavScreen(false);
308 }
309 }
310
John Reck1cf4b792011-07-26 10:22:22 -0700311 @Override
312 public boolean shouldCaptureThumbnails() {
313 return true;
314 }
315
Michael Kolb66706532010-12-12 19:50:22 -0800316}