blob: 1c9d5a0425e833be88128cff26d67d39117b0331 [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 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 Reck5d43ce82011-06-21 17:16:51 -0700112 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700113 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800114 // TabControl.setCurrentTab has been called before this,
115 // so the tab is guaranteed to have a webview
116 if (view == null) {
117 Log.e(LOGTAG, "active tab with no webview detected");
118 return;
119 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700120 // Request focus on the top window.
121 if (mUseQuickControls) {
122 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700123 } else {
124 // check if title bar is already attached by animation
125 if (mTitleBar.getParent() == null) {
126 view.setEmbeddedTitleBar(mTitleBar);
127 }
128 }
Michael Kolb376b5412010-12-15 11:52:57 -0800129 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700130 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800131 } else {
132 revertVoiceTitleBar(tab);
133 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700134 // update nav bar state
135 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700136 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800137 tab.getTopWindow().requestFocus();
138 }
139
Michael Kolb017ffab2011-07-11 15:26:47 -0700140 /**
141 * Suggest to the UI that the title bar can be hidden. The UI will then
142 * decide whether or not to hide based off a number of factors, such
143 * as if the user is editing the URL bar or if the page is loading
144 */
145 @Override
146 public void suggestHideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700147 if (!mNavigationBar.isMenuShowing()) {
148 super.suggestHideTitleBar();
Michael Kolb017ffab2011-07-11 15:26:47 -0700149 }
150 }
151
Michael Kolb66706532010-12-12 19:50:22 -0800152 @Override
John Reck2bc80422011-06-30 15:11:49 -0700153 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700154 if (mNavScreen != null) {
155 hideNavScreen(false);
156 }
John Reck2bc80422011-06-30 15:11:49 -0700157 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700158 }
159
Michael Kolb66706532010-12-12 19:50:22 -0800160 // menu handling callbacks
161
162 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700163 public boolean onPrepareOptionsMenu(Menu menu) {
164 menu.setGroupVisible(R.id.NAV_MENU, (mNavScreen == null));
165 return true;
166 }
167
168 @Override
169 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 Kolb7cdc4902011-02-03 17:54:40 -0800192 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800193 }
194
Michael Kolba4183062011-01-16 10:43:21 -0800195 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700196 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700197 if (inLoad) {
198 if (mUseQuickControls) {
199 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700200 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700201 showTitleBar();
202 }
203 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700204 }
205
206 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700207 protected void setTitleGravity(int gravity) {
208 if (mUseQuickControls) {
209 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700210 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700211 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700212 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700213 } else {
214 super.setTitleGravity(gravity);
215 }
216 }
217
Michael Kolb0241e752011-07-07 14:58:50 -0700218 @Override
219 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700220 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700221 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700222 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700223 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700224 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700225 WebView web = getWebView();
226 if (web != null) {
227 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -0700228 // don't show url bar on scrolling
229 web.setOnTouchListener(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);
Michael Kolb0241e752011-07-07 14:58:50 -0700238 // show url bar on scrolling
239 web.setOnTouchListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700240 }
241 setTitleGravity(Gravity.NO_GRAVITY);
242 }
243 }
244
Michael Kolbf2055602011-04-09 17:20:03 -0700245 void showNavScreen() {
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();
253 }
254
Michael Kolb2814a362011-05-19 15:49:41 -0700255 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700256 if (mNavScreen == null) return;
Michael Kolb2814a362011-05-19 15:49:41 -0700257 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700258 mCustomViewContainer.removeView(mNavScreen);
259 mNavScreen = null;
260 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700261 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700262 // Show the content view.
263 mContentView.setVisibility(View.VISIBLE);
264 }
265
John Reck6b4bd5f2011-07-12 10:14:38 -0700266 @Override
267 public boolean needsRestoreAllTabs() {
268 return false;
269 }
270
Michael Kolb20be26d2011-07-18 16:38:02 -0700271 public void toggleNavScreen() {
272 if (mNavScreen == null) {
273 showNavScreen();
274 } else {
275 hideNavScreen(false);
276 }
277 }
278
John Reck1cf4b792011-07-26 10:22:22 -0700279 @Override
280 public boolean shouldCaptureThumbnails() {
281 return true;
282 }
283
Michael Kolb66706532010-12-12 19:50:22 -0800284}