blob: f2d5da0a5ba313f6c1e56aa931d2678bef694e14 [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
30/**
31 * Ui for regular phone screen sizes
32 */
33public class PhoneUi extends BaseUi {
34
35 private static final String LOGTAG = "PhoneUi";
36
Michael Kolb0241e752011-07-07 14:58:50 -070037 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070038 private NavScreen mNavScreen;
John Reck0f602f32011-07-07 15:38:43 -070039 private NavigationBarPhone mNavigationBar;
Michael Kolb66706532010-12-12 19:50:22 -080040
41 boolean mExtendedMenuOpen;
42 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070043 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080044
45 /**
46 * @param browser
47 * @param controller
48 */
49 public PhoneUi(Activity browser, UiController controller) {
50 super(browser, controller);
John Reck285ef042011-02-11 15:44:17 -080051 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070052 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070053 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reck285ef042011-02-11 15:44:17 -080054 }
Michael Kolb66706532010-12-12 19:50:22 -080055
John Reck285ef042011-02-11 15:44:17 -080056 @Override
57 public void hideComboView() {
58 super.hideComboView();
59 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080060 }
61
Michael Kolb66706532010-12-12 19:50:22 -080062 @Override
63 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080064 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080065 }
66
67 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080068 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070069 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -070070 mTitleBar.setShowProgressOnly(false);
Michael Kolb46f987e2011-04-05 10:39:10 -070071 }
72 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080073 }
74
75 @Override
Michael Kolb66706532010-12-12 19:50:22 -080076 public boolean onBackKey() {
Michael Kolb9ef259a2011-07-12 15:33:08 -070077 if (mNavScreen != null) {
Michael Kolbf2055602011-04-09 17:20:03 -070078 mNavScreen.close();
79 return true;
Michael Kolb66706532010-12-12 19:50:22 -080080 }
81 return super.onBackKey();
82 }
83
84 @Override
Michael Kolb2814a362011-05-19 15:49:41 -070085 public boolean onMenuKey() {
Michael Kolb4bd767d2011-05-27 11:33:55 -070086 if (!isComboViewShowing()) {
87 if (mNavScreen == null) {
88 showNavScreen();
89 } else {
90 mNavScreen.close();
91 }
92 return true;
Michael Kolb2814a362011-05-19 15:49:41 -070093 } else {
Michael Kolb4bd767d2011-05-27 11:33:55 -070094 return false;
Michael Kolb2814a362011-05-19 15:49:41 -070095 }
Michael Kolb2814a362011-05-19 15:49:41 -070096 }
97
98 @Override
Michael Kolbf2055602011-04-09 17:20:03 -070099 public boolean dispatchKey(int code, KeyEvent event) {
100 if (!isComboViewShowing()) {
Michael Kolbf2055602011-04-09 17:20:03 -0700101 }
102 return false;
103 }
104
105 @Override
John Reck30c714c2010-12-16 17:30:34 -0800106 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800107 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800108 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800109 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800110 if (progress == 100) {
111 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -0700112 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -0700113 if (mUseQuickControls) {
114 mTitleBar.setShowProgressOnly(false);
115 }
Michael Kolb66706532010-12-12 19:50:22 -0800116 }
117 } else {
118 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700119 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
120 mTitleBar.setShowProgressOnly(true);
121 setTitleGravity(Gravity.TOP);
122 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800123 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800124 }
125 }
126 }
127 }
128
129 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700130 public void setActiveTab(final Tab tab) {
John Reck5d43ce82011-06-21 17:16:51 -0700131 super.setActiveTab(tab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700132 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800133 // TabControl.setCurrentTab has been called before this,
134 // so the tab is guaranteed to have a webview
135 if (view == null) {
136 Log.e(LOGTAG, "active tab with no webview detected");
137 return;
138 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700139 // Request focus on the top window.
140 if (mUseQuickControls) {
141 mPieControl.forceToTop(mContentView);
Michael Kolbfdb70242011-03-24 09:41:11 -0700142 } else {
143 // check if title bar is already attached by animation
144 if (mTitleBar.getParent() == null) {
145 view.setEmbeddedTitleBar(mTitleBar);
146 }
147 }
Michael Kolb376b5412010-12-15 11:52:57 -0800148 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700149 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800150 } else {
151 revertVoiceTitleBar(tab);
152 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700153 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800154 tab.getTopWindow().requestFocus();
155 }
156
Michael Kolb017ffab2011-07-11 15:26:47 -0700157 /**
158 * Suggest to the UI that the title bar can be hidden. The UI will then
159 * decide whether or not to hide based off a number of factors, such
160 * as if the user is editing the URL bar or if the page is loading
161 */
162 @Override
163 public void suggestHideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700164 if (!mNavigationBar.isMenuShowing()) {
165 super.suggestHideTitleBar();
Michael Kolb017ffab2011-07-11 15:26:47 -0700166 }
167 }
168
Michael Kolb66706532010-12-12 19:50:22 -0800169 @Override
John Reck2bc80422011-06-30 15:11:49 -0700170 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700171 if (mNavScreen != null) {
172 hideNavScreen(false);
173 }
John Reck2bc80422011-06-30 15:11:49 -0700174 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700175 }
176
Michael Kolb66706532010-12-12 19:50:22 -0800177 // menu handling callbacks
178
179 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800180 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800181 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800182 }
183
184 @Override
185 public void onContextMenuClosed(Menu menu, boolean inLoad) {
186 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800187 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800188 }
189 }
190
191 // action mode callbacks
192
193 @Override
194 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800195 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800196 }
197
Michael Kolba4183062011-01-16 10:43:21 -0800198 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700199 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700200 if (inLoad) {
201 if (mUseQuickControls) {
202 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700203 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700204 showTitleBar();
205 }
206 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700207 }
208
209 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700210 protected void setTitleGravity(int gravity) {
211 if (mUseQuickControls) {
212 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700213 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700214 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700215 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700216 } else {
217 super.setTitleGravity(gravity);
218 }
219 }
220
Michael Kolb0241e752011-07-07 14:58:50 -0700221 @Override
222 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700223 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700224 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700225 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700226 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700227 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700228 WebView web = getWebView();
229 if (web != null) {
230 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -0700231 // don't show url bar on scrolling
232 web.setOnTouchListener(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700233 }
234 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700235 if (mPieControl != null) {
236 mPieControl.removeFromContainer(mContentView);
237 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700238 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700239 if (web != null) {
240 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700241 // show url bar on scrolling
242 web.setOnTouchListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700243 }
244 setTitleGravity(Gravity.NO_GRAVITY);
245 }
246 }
247
Michael Kolbf2055602011-04-09 17:20:03 -0700248 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700249 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700250 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700251 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700252 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700253 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700254 mCustomViewContainer.setVisibility(View.VISIBLE);
255 mCustomViewContainer.bringToFront();
256 }
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;
Michael Kolb2814a362011-05-19 15:49:41 -0700260 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700261 mCustomViewContainer.removeView(mNavScreen);
262 mNavScreen = null;
263 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700264 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700265 // Show the content view.
266 mContentView.setVisibility(View.VISIBLE);
267 }
268
John Reck6b4bd5f2011-07-12 10:14:38 -0700269 @Override
270 public boolean needsRestoreAllTabs() {
271 return false;
272 }
273
Michael Kolb66706532010-12-12 19:50:22 -0800274}