blob: 32bc0923a45a416b12e550b8a5254b06b29581c9 [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 Kolb30adae62011-07-29 13:37:05 -070020import android.content.Context;
Michael Kolb4bd767d2011-05-27 11:33:55 -070021import android.os.Bundle;
Michael Kolb376b5412010-12-15 11:52:57 -080022import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080023import android.view.ActionMode;
24import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080025import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080026import android.view.Menu;
Michael Kolb3ca12752011-07-20 13:52:25 -070027import android.view.MenuItem;
Michael Kolb66706532010-12-12 19:50:22 -080028import android.view.View;
Michael Kolb30adae62011-07-29 13:37:05 -070029import android.view.accessibility.AccessibilityEvent;
30import android.view.accessibility.AccessibilityManager;
Michael Kolb66706532010-12-12 19:50:22 -080031import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070032import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080033
Michael Kolb629b22c2011-07-13 16:26:47 -070034import com.android.browser.UrlInputView.StateListener;
35
Michael Kolb66706532010-12-12 19:50:22 -080036/**
37 * Ui for regular phone screen sizes
38 */
39public class PhoneUi extends BaseUi {
40
41 private static final String LOGTAG = "PhoneUi";
42
Michael Kolb0241e752011-07-07 14:58:50 -070043 private PieControlPhone mPieControl;
Michael Kolbf2055602011-04-09 17:20:03 -070044 private NavScreen mNavScreen;
John Reck0f602f32011-07-07 15:38:43 -070045 private NavigationBarPhone mNavigationBar;
Michael Kolb66706532010-12-12 19:50:22 -080046
47 boolean mExtendedMenuOpen;
48 boolean mOptionsMenuOpen;
Michael Kolb08a687a2011-04-22 16:13:02 -070049 boolean mAnimating;
Michael Kolb66706532010-12-12 19:50:22 -080050
51 /**
52 * @param browser
53 * @param controller
54 */
55 public PhoneUi(Activity browser, UiController controller) {
56 super(browser, controller);
John Reck285ef042011-02-11 15:44:17 -080057 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070058 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck0f602f32011-07-07 15:38:43 -070059 mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
John Reck285ef042011-02-11 15:44:17 -080060 }
Michael Kolb66706532010-12-12 19:50:22 -080061
John Reck285ef042011-02-11 15:44:17 -080062 @Override
Michael Kolb66706532010-12-12 19:50:22 -080063 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 Kolbf2055602011-04-09 17:20:03 -070085 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbf2055602011-04-09 17:20:03 -070086 return false;
87 }
88
89 @Override
John Reck30c714c2010-12-16 17:30:34 -080090 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -080091 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -080092 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -080093 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -080094 if (progress == 100) {
95 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
John Reck5d43ce82011-06-21 17:16:51 -070096 suggestHideTitleBar();
Michael Kolbc16c5952011-03-29 15:37:03 -070097 if (mUseQuickControls) {
98 mTitleBar.setShowProgressOnly(false);
99 }
Michael Kolb66706532010-12-12 19:50:22 -0800100 }
101 } else {
102 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700103 if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
104 mTitleBar.setShowProgressOnly(true);
105 setTitleGravity(Gravity.TOP);
106 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800107 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800108 }
109 }
110 }
111 }
112
113 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700114 public void setActiveTab(final Tab tab) {
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();
141 }
142
Michael Kolb017ffab2011-07-11 15:26:47 -0700143 /**
144 * Suggest to the UI that the title bar can be hidden. The UI will then
145 * decide whether or not to hide based off a number of factors, such
146 * as if the user is editing the URL bar or if the page is loading
147 */
148 @Override
149 public void suggestHideTitleBar() {
John Reck0f602f32011-07-07 15:38:43 -0700150 if (!mNavigationBar.isMenuShowing()) {
151 super.suggestHideTitleBar();
Michael Kolb017ffab2011-07-11 15:26:47 -0700152 }
153 }
154
Michael Kolb66706532010-12-12 19:50:22 -0800155 @Override
John Reck2bc80422011-06-30 15:11:49 -0700156 public void showComboView(ComboViews startWith, Bundle extras) {
Michael Kolb4bd767d2011-05-27 11:33:55 -0700157 if (mNavScreen != null) {
158 hideNavScreen(false);
159 }
John Reck2bc80422011-06-30 15:11:49 -0700160 super.showComboView(startWith, extras);
Michael Kolb4bd767d2011-05-27 11:33:55 -0700161 }
162
Michael Kolb66706532010-12-12 19:50:22 -0800163 // menu handling callbacks
164
165 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700166 public boolean onPrepareOptionsMenu(Menu menu) {
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700167 updateMenuState(mActiveTab, menu);
Michael Kolb3ca12752011-07-20 13:52:25 -0700168 return true;
169 }
170
171 @Override
Michael Kolb7bdee0b2011-08-01 11:55:38 -0700172 public void updateMenuState(Tab tab, Menu menu) {
173 menu.setGroupVisible(R.id.NAV_MENU, (mNavScreen == null));
174 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
175 if (bm != null) {
176 bm.setVisible(mNavScreen == null);
177 }
178 MenuItem nt = menu.findItem(R.id.new_tab_menu_id);
179 if (nt != null) {
180 nt.setVisible(mNavScreen == null);
181 }
182 MenuItem find = menu.findItem(R.id.find_menu_id);
183 if (find != null) {
184 find.setVisible(((tab != null) && !tab.isSnapshot()));
185 }
186 MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
187 if (abm != null) {
188 abm.setVisible((tab != null) && !tab.isSnapshot());
189 }
190 }
191
192 @Override
Michael Kolb3ca12752011-07-20 13:52:25 -0700193 public boolean onOptionsItemSelected(MenuItem item) {
194 if (mNavScreen != null) {
195 hideNavScreen(false);
196 }
197 return false;
198 }
199
200 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800201 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800202 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800203 }
204
205 @Override
206 public void onContextMenuClosed(Menu menu, boolean inLoad) {
207 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800208 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800209 }
210 }
211
212 // action mode callbacks
213
214 @Override
215 public void onActionModeStarted(ActionMode mode) {
Michael Kolb42c0c062011-08-02 12:56:06 -0700216 if (!isEditingUrl()) {
217 hideTitleBar();
218 }
Michael Kolb66706532010-12-12 19:50:22 -0800219 }
220
Michael Kolba4183062011-01-16 10:43:21 -0800221 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700222 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700223 if (inLoad) {
224 if (mUseQuickControls) {
225 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700226 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700227 showTitleBar();
228 }
229 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700230 }
231
232 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700233 protected void setTitleGravity(int gravity) {
234 if (mUseQuickControls) {
235 FrameLayout.LayoutParams lp =
John Reck0f602f32011-07-07 15:38:43 -0700236 (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
Michael Kolbfdb70242011-03-24 09:41:11 -0700237 lp.gravity = gravity;
John Reck0f602f32011-07-07 15:38:43 -0700238 mTitleBar.setLayoutParams(lp);
Michael Kolbfdb70242011-03-24 09:41:11 -0700239 } else {
240 super.setTitleGravity(gravity);
241 }
242 }
243
Michael Kolb0241e752011-07-07 14:58:50 -0700244 @Override
245 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700246 mUseQuickControls = useQuickControls;
John Reck0f602f32011-07-07 15:38:43 -0700247 mTitleBar.setUseQuickControls(mUseQuickControls);
Michael Kolbfdb70242011-03-24 09:41:11 -0700248 if (useQuickControls) {
Michael Kolb0241e752011-07-07 14:58:50 -0700249 mPieControl = new PieControlPhone(mActivity, mUiController, this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700250 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700251 WebView web = getWebView();
252 if (web != null) {
253 web.setEmbeddedTitleBar(null);
Michael Kolb0241e752011-07-07 14:58:50 -0700254 // don't show url bar on scrolling
255 web.setOnTouchListener(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700256 }
257 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700258 if (mPieControl != null) {
259 mPieControl.removeFromContainer(mContentView);
260 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700261 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700262 if (web != null) {
263 web.setEmbeddedTitleBar(mTitleBar);
Michael Kolb0241e752011-07-07 14:58:50 -0700264 // show url bar on scrolling
265 web.setOnTouchListener(this);
Michael Kolbfdb70242011-03-24 09:41:11 -0700266 }
267 setTitleGravity(Gravity.NO_GRAVITY);
268 }
269 }
270
Michael Kolbf2055602011-04-09 17:20:03 -0700271 void showNavScreen() {
Michael Kolb2814a362011-05-19 15:49:41 -0700272 detachTab(mActiveTab);
Michael Kolbf2055602011-04-09 17:20:03 -0700273 mNavScreen = new NavScreen(mActivity, mUiController, this);
Michael Kolbf2055602011-04-09 17:20:03 -0700274 // Add the custom view to its container.
Michael Kolb2814a362011-05-19 15:49:41 -0700275 mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
Michael Kolbf2055602011-04-09 17:20:03 -0700276 mContentView.setVisibility(View.GONE);
Michael Kolbf2055602011-04-09 17:20:03 -0700277 mCustomViewContainer.setVisibility(View.VISIBLE);
278 mCustomViewContainer.bringToFront();
Michael Kolb30adae62011-07-29 13:37:05 -0700279 // notify accessibility manager about the screen change
280 mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Kolbf2055602011-04-09 17:20:03 -0700281 }
282
Michael Kolb2814a362011-05-19 15:49:41 -0700283 void hideNavScreen(boolean animate) {
Michael Kolb09bf24f2011-06-09 14:34:30 -0700284 if (mNavScreen == null) return;
Michael Kolb2814a362011-05-19 15:49:41 -0700285 Tab tab = mNavScreen.getSelectedTab();
Michael Kolbf2055602011-04-09 17:20:03 -0700286 mCustomViewContainer.removeView(mNavScreen);
287 mNavScreen = null;
288 mCustomViewContainer.setVisibility(View.GONE);
Michael Kolb2814a362011-05-19 15:49:41 -0700289 mUiController.setActiveTab(tab);
Michael Kolbf2055602011-04-09 17:20:03 -0700290 // Show the content view.
291 mContentView.setVisibility(View.VISIBLE);
292 }
293
John Reck6b4bd5f2011-07-12 10:14:38 -0700294 @Override
295 public boolean needsRestoreAllTabs() {
296 return false;
297 }
298
Michael Kolb20be26d2011-07-18 16:38:02 -0700299 public void toggleNavScreen() {
300 if (mNavScreen == null) {
301 showNavScreen();
302 } else {
303 hideNavScreen(false);
304 }
305 }
306
John Reck1cf4b792011-07-26 10:22:22 -0700307 @Override
308 public boolean shouldCaptureThumbnails() {
309 return true;
310 }
311
Michael Kolb66706532010-12-12 19:50:22 -0800312}