blob: 471a9e6c6f4641fde4eb0fbb056db39da169252b [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 Kolbb0868f12011-02-14 14:36:13 -080020import android.content.Context;
21import android.graphics.PixelFormat;
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;
John Reckd29abda2011-02-14 17:51:40 -080027import android.view.MotionEvent;
Michael Kolb66706532010-12-12 19:50:22 -080028import android.view.View;
Michael Kolbb0868f12011-02-14 14:36:13 -080029import android.view.WindowManager;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.webkit.WebView;
Michael Kolbfdb70242011-03-24 09:41:11 -070031import android.widget.FrameLayout;
Michael Kolb66706532010-12-12 19:50:22 -080032
33/**
34 * Ui for regular phone screen sizes
35 */
36public class PhoneUi extends BaseUi {
37
38 private static final String LOGTAG = "PhoneUi";
39
Michael Kolb11d19782011-03-20 10:17:40 -070040 private TitleBarPhone mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080041 private ActiveTabsPage mActiveTabsPage;
John Reckd29abda2011-02-14 17:51:40 -080042 private TouchProxy mTitleOverlay;
Michael Kolbfdb70242011-03-24 09:41:11 -070043 private boolean mUseQuickControls;
44 private PieControl mPieControl;
Michael Kolb66706532010-12-12 19:50:22 -080045
46 boolean mExtendedMenuOpen;
47 boolean mOptionsMenuOpen;
48
49 /**
50 * @param browser
51 * @param controller
52 */
53 public PhoneUi(Activity browser, UiController controller) {
54 super(browser, controller);
Michael Kolb46f987e2011-04-05 10:39:10 -070055 mTitleBar = new TitleBarPhone(mActivity, mUiController, this,
56 mContentView);
Michael Kolb66706532010-12-12 19:50:22 -080057 // mTitleBar will be always be shown in the fully loaded mode on
58 // phone
59 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080060 mActivity.getActionBar().hide();
Michael Kolbfdb70242011-03-24 09:41:11 -070061 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
John Reck285ef042011-02-11 15:44:17 -080062 }
Michael Kolb66706532010-12-12 19:50:22 -080063
John Reck285ef042011-02-11 15:44:17 -080064 @Override
65 public void hideComboView() {
66 super.hideComboView();
67 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080068 }
69
Michael Kolb66706532010-12-12 19:50:22 -080070 // lifecycle
71
72 @Override
73 public void onPause() {
74 // FIXME: This removes the active tabs page and resets the menu to
75 // MAIN_MENU. A better solution might be to do this work in onNewIntent
76 // but then we would need to save it in onSaveInstanceState and restore
77 // it in onCreate/onRestoreInstanceState
78 if (mActiveTabsPage != null) {
79 mUiController.removeActiveTabsPage(true);
80 }
81 super.onPause();
82 }
83
84 @Override
85 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080086 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080087 }
88
89 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080090 public void editUrl(boolean clearInput) {
Michael Kolb46f987e2011-04-05 10:39:10 -070091 if (mUseQuickControls) {
92 getTitleBar().setShowProgressOnly(false);
93 }
94 super.editUrl(clearInput);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080095 }
96
97 @Override
Michael Kolb66706532010-12-12 19:50:22 -080098 public boolean onBackKey() {
99 if (mActiveTabsPage != null) {
100 // if tab page is showing, hide it
101 mUiController.removeActiveTabsPage(true);
102 return true;
103 }
104 return super.onBackKey();
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) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800114 hideTitleBar();
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 Reck88a42b72011-03-21 16:30:12 -0700133 captureTab(mActiveTab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700134 super.setActiveTab(tab, true);
135 setActiveTab(tab, true);
136 }
137
138 @Override
139 void setActiveTab(Tab tab, boolean needsAttaching) {
140 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800141 // TabControl.setCurrentTab has been called before this,
142 // so the tab is guaranteed to have a webview
143 if (view == null) {
144 Log.e(LOGTAG, "active tab with no webview detected");
145 return;
146 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700147 // Request focus on the top window.
148 if (mUseQuickControls) {
149 mPieControl.forceToTop(mContentView);
150 view.setScrollListener(null);
151 } else {
152 // check if title bar is already attached by animation
153 if (mTitleBar.getParent() == null) {
154 view.setEmbeddedTitleBar(mTitleBar);
155 }
156 }
Michael Kolb376b5412010-12-15 11:52:57 -0800157 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700158 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800159 } else {
160 revertVoiceTitleBar(tab);
161 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700162 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800163 tab.getTopWindow().requestFocus();
164 }
165
166 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800167 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800168 return mTitleBar;
169 }
170
171 // active tabs page
172
173 @Override
174 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700175 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800176 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
177 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800178 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800179 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
180 mActiveTabsPage.requestFocus();
181 }
182
183 /**
184 * Remove the active tabs page.
185 */
186 @Override
187 public void removeActiveTabsPage() {
188 mContentView.removeView(mActiveTabsPage);
189 mTitleBar.setVisibility(View.VISIBLE);
190 mActiveTabsPage = null;
191 }
192
193 @Override
194 public boolean showsWeb() {
195 return super.showsWeb() && mActiveTabsPage == null;
196 }
197
198 // menu handling callbacks
199
200 @Override
201 public void onOptionsMenuOpened() {
John Reck172160e2011-04-11 17:13:11 -0700202 if (!mUseQuickControls) {
203 mOptionsMenuOpen = true;
204 // options menu opened, show title bar
205 showTitleBar();
206 if (mTitleOverlay == null) {
207 // This assumes that getTitleBar always returns the same View
208 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
209 }
210 mActivity.getWindowManager().addView(mTitleOverlay,
211 mTitleOverlay.getWindowLayoutParams());
John Reckd29abda2011-02-14 17:51:40 -0800212 }
Michael Kolb66706532010-12-12 19:50:22 -0800213 }
214
215 @Override
216 public void onExtendedMenuOpened() {
217 // Switching the menu to expanded view, so hide the
218 // title bar.
219 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800220 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800221 }
222
223 @Override
224 public void onOptionsMenuClosed(boolean inLoad) {
225 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800226 mActivity.getWindowManager().removeView(mTitleOverlay);
227 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800228 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230 }
231
232 @Override
233 public void onExtendedMenuClosed(boolean inLoad) {
234 mExtendedMenuOpen = false;
Michael Kolbfdb70242011-03-24 09:41:11 -0700235 if (!mUseQuickControls) {
236 showTitleBar();
237 }
Michael Kolb66706532010-12-12 19:50:22 -0800238 }
239
240 @Override
241 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800242 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800243 }
244
245 @Override
246 public void onContextMenuClosed(Menu menu, boolean inLoad) {
247 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800248 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250 }
251
252 // action mode callbacks
253
254 @Override
255 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800256 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800257 }
258
Michael Kolba4183062011-01-16 10:43:21 -0800259 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700260 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700261 if (inLoad) {
262 if (mUseQuickControls) {
263 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700264 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700265 showTitleBar();
266 }
267 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700268 }
269
270 @Override
Michael Kolba4183062011-01-16 10:43:21 -0800271 public boolean dispatchKey(int code, KeyEvent event) {
272 return false;
273 }
274
John Reckd29abda2011-02-14 17:51:40 -0800275 static class TouchProxy extends View {
276
277 View mTarget;
278
279 TouchProxy(Context context, View target) {
280 super(context);
281 mTarget = target;
282 }
283
284 @Override
285 public boolean dispatchTouchEvent(MotionEvent event) {
286 return mTarget.dispatchTouchEvent(event);
287 }
288
289 WindowManager.LayoutParams getWindowLayoutParams() {
290 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800291 new WindowManager.LayoutParams(
292 mTarget.getWidth(),
293 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800294 WindowManager.LayoutParams.TYPE_APPLICATION,
295 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
296 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800297 params.gravity = Gravity.TOP | Gravity.LEFT;
298 params.y = mTarget.getTop();
299 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800300 return params;
301 }
302 }
Michael Kolb11d19782011-03-20 10:17:40 -0700303
Michael Kolbfdb70242011-03-24 09:41:11 -0700304 @Override
305 protected void setTitleGravity(int gravity) {
306 if (mUseQuickControls) {
307 FrameLayout.LayoutParams lp =
308 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
309 lp.gravity = gravity;
310 getTitleBar().setLayoutParams(lp);
311 } else {
312 super.setTitleGravity(gravity);
313 }
314 }
315
316 private void setUseQuickControls(boolean useQuickControls) {
317 mUseQuickControls = useQuickControls;
318 getTitleBar().setUseQuickControls(mUseQuickControls);
319 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700320 mPieControl = new PieControl(mActivity, mUiController, this);
321 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700322 WebView web = getWebView();
323 if (web != null) {
324 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700325 }
326 } else {
Michael Kolbfdb70242011-03-24 09:41:11 -0700327 if (mPieControl != null) {
328 mPieControl.removeFromContainer(mContentView);
329 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700330 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700331 if (web != null) {
332 web.setEmbeddedTitleBar(mTitleBar);
333 }
334 setTitleGravity(Gravity.NO_GRAVITY);
335 }
336 }
337
338 @Override
339 public boolean onPrepareOptionsMenu(Menu menu) {
340 if (mUseQuickControls) {
341 menu.setGroupVisible(R.id.NAV_MENU, false);
342 mPieControl.onMenuOpened(menu);
343 return false;
344 } else {
345 return true;
346 }
347 }
348
349 @Override
350 protected void captureTab(final Tab tab) {
351 if (mUseQuickControls) {
352 super.captureTab(tab);
353 } else {
354 captureTab(tab,
355 mActivity.getWindowManager().getDefaultDisplay().getWidth(),
356 (int) mActivity.getResources()
357 .getDimension(R.dimen.tab_view_thumbnail_height));
358 }
359 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700360
Michael Kolb66706532010-12-12 19:50:22 -0800361}