blob: 9e3310b28fd60c3bbdc8f58e9a0e8549a0f1da68 [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
Michael Kolb66706532010-12-12 19:50:22 -080059 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080060 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080061 }
62
63 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080064 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070065 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070066 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070067 }
68 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080069 }
70
71 @Override
Michael Kolb66706532010-12-12 19:50:22 -080072 public boolean onBackKey() {
Michael Kolb9ef259a2011-07-12 15:33:08 -070073 if (mNavScreen != null) {
Michael Kolbf2055602011-04-09 17:20:03 -070074 mNavScreen.close();
75 return true;
Michael Kolb66706532010-12-12 19:50:22 -080076 }
77 return super.onBackKey();
78 }
79
80 @Override
Michael Kolb2814a362011-05-19 15:49:41 -070081 public boolean onMenuKey() {
Michael Kolb8441d4b2011-07-18 14:50:21 -070082 if (mNavigationBar.isMenuShowing()) {
83 mNavigationBar.dismissMenu();
Michael Kolb2814a362011-05-19 15:49:41 -070084 } else {
Michael Kolb8441d4b2011-07-18 14:50:21 -070085 mNavigationBar.showMenu();
Michael Kolb2814a362011-05-19 15:49:41 -070086 }
John Reckd3e4d5b2011-07-13 15:48:43 -070087 return true;
Michael Kolb2814a362011-05-19 15:49:41 -070088 }
89
90 @Override
Michael Kolbf2055602011-04-09 17:20:03 -070091 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -070092 return false;
93 }
94
95 @Override
John Reck30c714c2010-12-16 17:30:34 -080096 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -080097 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -080098 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -080099 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800100 if (progress == 100) {
101 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700102 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700103 if (mUseQuickControls) {
104 mTitleBar.setShowProgressOnly(false);
105 }
Michael Kolb66706532010-12-12 19:50:22 -0800106 }
107 } else {
108 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700109 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
110 mTitleBar.setShowProgressOnly(true);
111 setTitleGravity(Gravity.TOP);
112 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800113 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800114 }
115 }
116 }
117 }
118
119 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700120 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700121 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700122 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800123 // TabControl.setCurrentTab has been called before this,
124 // so the tab is guaranteed to have a webview
125 if (view == null) {
126 Log.e(LOGTAG, "active tab with no webview detected");
127 return;
128 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700129 // Request focus on the top window.
130 if (mUseQuickControls) {
131 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700132 } else {
133 // check if title bar is already attached by animation
134 if (mTitleBar.getParent() == null) {
135 view.setEmbeddedTitleBar(mTitleBar);
136 }
137 }
Michael Kolb376b5412010-12-15 11:52:57 -0800138 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700139 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800140 } else {
141 revertVoiceTitleBar(tab);
142 }
Michael Kolb629b22c2011-07-13 16:26:47 -0700143 // update nav bar state
144 mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
Michael Kolbfdb70242011-03-24 09:41:11 -0700145 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800146 tab.getTopWindow().requestFocus();
147 }
148
Michael Kolb017ffab2011-07-11 15:26:47 -0700149 /**
150 * Suggest to the UI that the title bar can be hidden. The UI will then
151 * decide whether or not to hide based off a number of factors, such
152 * as if the user is editing the URL bar or if the page is loading
153 */
154 @Override
155 public void suggestHideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700156 if (!mNavigationBar.isMenuShowing()) {
157 super.suggestHideTitleBar();
Michael Kolb017ffab2011-07-11 15:26:47 -0700158 }
159 }
160
Michael Kolb66706532010-12-12 19:50:22 -0800161 @Override
John Reck2bc80422011-06-30 15:11:49 -0700162 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700163 if (mNavScreen != null) {
164 hideNavScreen(false);
165 }
John Reck2bc80422011-06-30 15:11:49 -0700166 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700167 }
168
Michael Kolb66706532010-12-12 19:50:22 -0800169 // menu handling callbacks
170
171 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800172 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800173 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800174 }
175
176 @Override
177 public void onContextMenuClosed(Menu menu, boolean inLoad) {
178 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800179 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800180 }
181 }
182
183 // action mode callbacks
184
185 @Override
186 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800187 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800188 }
189
Michael Kolba4183062011-01-16 10:43:21 -0800190 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700191 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700192 if (inLoad) {
193 if (mUseQuickControls) {
194 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700195 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700196 showTitleBar();
197 }
198 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700199 }
200
201 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700202 protected void setTitleGravity(int gravity) {
203 if (mUseQuickControls) {
204 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700205 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700206 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700207 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700208 } else {
209 super.setTitleGravity(gravity);
210 }
211 }
212
Michael Kolb0241e752011-07-07 14:58:50 -0700213 @Override
214 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700215 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700216 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700217 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700218 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700219 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700220 WebView web = getWebView();
221 if (web != null) {
222 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -0700223 // don't show url bar on scrolling
224 web.setOnTouchListener(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700225 }
226 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700227 if (mPieControl != null) {
228 mPieControl.removeFromContainer(mContentView);
229 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700230 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700231 if (web != null) {
232 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700233 // show url bar on scrolling
234 web.setOnTouchListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700235 }
236 setTitleGravity(Gravity.NO_GRAVITY);
237 }
238 }
239
Michael Kolbf2055602011-04-09 17:20:03 -0700240 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700241 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700242 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700243 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700244 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700245 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700246 mCustomViewContainer.setVisibility(View.VISIBLE);
247 mCustomViewContainer.bringToFront();
248 }
249
Michael Kolb2814a362011-05-19 15:49:41 -0700250 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700251 if (mNavScreen == null) return;
Michael Kolb2814a362011-05-19 15:49:41 -0700252 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700253 mCustomViewContainer.removeView(mNavScreen);
254 mNavScreen = null;
255 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700256 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700257 // Show the content view.
258 mContentView.setVisibility(View.VISIBLE);
259 }
260
John Reck6b4bd5f2011-07-12 10:14:38 -0700261 @Override
262 public boolean needsRestoreAllTabs() {
263 return false;
264 }
265
Michael Kolb20be26d2011-07-18 16:38:02 -0700266 public void toggleNavScreen() {
267 if (mNavScreen == null) {
268 showNavScreen();
269 } else {
270 hideNavScreen(false);
271 }
272 }
273
Michael Kolb66706532010-12-12 19:50:22 -0800274}