blob: 7c3641c800cf2aff2df83c657de93bfe094d4442 [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;
26import android.view.View;
Michael Kolb66706532010-12-12 19:50:22 -080027import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070028import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080029
Michael Kolb629b22c2011-07-13 16:26:47 -070030import com.android.browser.UrlInputView.StateListener;
31
Michael Kolb66706532010-12-12 19:50:22 -080032/**
33 * Ui for regular phone screen sizes
34 */
35public class PhoneUi extends BaseUi {
36
37 private static final String LOGTAG = "PhoneUi";
38
Michael Kolb0241e752011-07-07 14:58:50 -070039 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070040 private NavScreen mNavScreen;
John Reck0f602f32011-07-07 15:38:43 -070041 private NavigationBarPhone mNavigationBar;
Michael Kolb66706532010-12-12 19:50:22 -080042
43 boolean mExtendedMenuOpen;
44 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070045 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080046
47 /**
48 * @param browser
49 * @param controller
50 */
51 public PhoneUi(Activity browser, UiController controller) {
52 super(browser, controller);
John Reck285ef042011-02-11 15:44:17 -080053 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070054 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070055 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reck285ef042011-02-11 15:44:17 -080056 }
Michael Kolb66706532010-12-12 19:50:22 -080057
John Reck285ef042011-02-11 15:44:17 -080058 @Override
59 public void hideComboView() {
60 super.hideComboView();
61 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080062 }
63
Michael Kolb66706532010-12-12 19:50:22 -080064 @Override
65 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080066 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080067 }
68
69 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080070 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070071 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070072 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070073 }
74 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080075 }
76
77 @Override
Michael Kolb66706532010-12-12 19:50:22 -080078 public boolean onBackKey() {
Michael Kolb9ef259a2011-07-12 15:33:08 -070079 if (mNavScreen != null) {
Michael Kolbf2055602011-04-09 17:20:03 -070080 mNavScreen.close();
81 return true;
Michael Kolb66706532010-12-12 19:50:22 -080082 }
83 return super.onBackKey();
84 }
85
86 @Override
Michael Kolb2814a362011-05-19 15:49:41 -070087 public boolean onMenuKey() {
Michael Kolb4bd767d2011-05-27 11:33:55 -070088 if (!isComboViewShowing()) {
89 if (mNavScreen == null) {
90 showNavScreen();
91 } else {
92 mNavScreen.close();
93 }
94 return true;
Michael Kolb2814a362011-05-19 15:49:41 -070095 } else {
Michael Kolb4bd767d2011-05-27 11:33:55 -070096 return false;
Michael Kolb2814a362011-05-19 15:49:41 -070097 }
Michael Kolb2814a362011-05-19 15:49:41 -070098 }
99
100 @Override
Michael Kolbf2055602011-04-09 17:20:03 -0700101 public boolean dispatchKey(int code, KeyEvent event) {
102 if (!isComboViewShowing()) {
Michael Kolbf2055602011-04-09 17:20:03 -0700103 }
104 return false;
105 }
106
107 @Override
John Reck30c714c2010-12-16 17:30:34 -0800108 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800109 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800110 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800111 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800112 if (progress == 100) {
113 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700114 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700115 if (mUseQuickControls) {
116 mTitleBar.setShowProgressOnly(false);
117 }
Michael Kolb66706532010-12-12 19:50:22 -0800118 }
119 } else {
120 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700121 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
122 mTitleBar.setShowProgressOnly(true);
123 setTitleGravity(Gravity.TOP);
124 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800125 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800126 }
127 }
128 }
129 }
130
131 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700132 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700133 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700134 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800135 // TabControl.setCurrentTab has been called before this,
136 // so the tab is guaranteed to have a webview
137 if (view == null) {
138 Log.e(LOGTAG, "active tab with no webview detected");
139 return;
140 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700141 // Request focus on the top window.
142 if (mUseQuickControls) {
143 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700144 } else {
145 // check if title bar is already attached by animation
146 if (mTitleBar.getParent() == null) {
147 view.setEmbeddedTitleBar(mTitleBar);
148 }
149 }
Michael Kolb376b5412010-12-15 11:52:57 -0800150 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700151 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800152 } else {
153 revertVoiceTitleBar(tab);
154 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700155 // update nav bar state
156 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700157 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800158 tab.getTopWindow().requestFocus();
159 }
160
Michael Kolb017ffab2011-07-11 15:26:47 -0700161 /**
162 * Suggest to the UI that the title bar can be hidden. The UI will then
163 * decide whether or not to hide based off a number of factors, such
164 * as if the user is editing the URL bar or if the page is loading
165 */
166 @Override
167 public void suggestHideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700168 if (!mNavigationBar.isMenuShowing()) {
169 super.suggestHideTitleBar();
Michael Kolb017ffab2011-07-11 15:26:47 -0700170 }
171 }
172
Michael Kolb66706532010-12-12 19:50:22 -0800173 @Override
John Reck2bc80422011-06-30 15:11:49 -0700174 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700175 if (mNavScreen != null) {
176 hideNavScreen(false);
177 }
John Reck2bc80422011-06-30 15:11:49 -0700178 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700179 }
180
Michael Kolb66706532010-12-12 19:50:22 -0800181 // menu handling callbacks
182
183 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800184 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800185 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800186 }
187
188 @Override
189 public void onContextMenuClosed(Menu menu, boolean inLoad) {
190 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800191 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800192 }
193 }
194
195 // action mode callbacks
196
197 @Override
198 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800199 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800200 }
201
Michael Kolba4183062011-01-16 10:43:21 -0800202 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700203 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700204 if (inLoad) {
205 if (mUseQuickControls) {
206 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700207 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700208 showTitleBar();
209 }
210 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700211 }
212
213 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700214 protected void setTitleGravity(int gravity) {
215 if (mUseQuickControls) {
216 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700217 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700218 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700219 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700220 } else {
221 super.setTitleGravity(gravity);
222 }
223 }
224
Michael Kolb0241e752011-07-07 14:58:50 -0700225 @Override
226 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700227 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700228 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700229 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700230 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700231 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700232 WebView web = getWebView();
233 if (web != null) {
234 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -0700235 // don't show url bar on scrolling
236 web.setOnTouchListener(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700237 }
238 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700239 if (mPieControl != null) {
240 mPieControl.removeFromContainer(mContentView);
241 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700242 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700243 if (web != null) {
244 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700245 // show url bar on scrolling
246 web.setOnTouchListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700247 }
248 setTitleGravity(Gravity.NO_GRAVITY);
249 }
250 }
251
Michael Kolbf2055602011-04-09 17:20:03 -0700252 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700253 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700254 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700255 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700256 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700257 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700258 mCustomViewContainer.setVisibility(View.VISIBLE);
259 mCustomViewContainer.bringToFront();
260 }
261
Michael Kolb2814a362011-05-19 15:49:41 -0700262 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700263 if (mNavScreen == null) return;
Michael Kolb2814a362011-05-19 15:49:41 -0700264 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700265 mCustomViewContainer.removeView(mNavScreen);
266 mNavScreen = null;
267 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700268 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700269 // Show the content view.
270 mContentView.setVisibility(View.VISIBLE);
271 }
272
John Reck6b4bd5f2011-07-12 10:14:38 -0700273 @Override
274 public boolean needsRestoreAllTabs() {
275 return false;
276 }
277
Michael Kolb66706532010-12-12 19:50:22 -0800278}