blob: eb7ddc5d8fc28ae6df9507c638c94c986396a64b [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() {
202 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800203 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800204 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800205 if (mTitleOverlay == null) {
206 // This assumes that getTitleBar always returns the same View
207 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
208 }
209 mActivity.getWindowManager().addView(mTitleOverlay,
210 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800211 }
212
213 @Override
214 public void onExtendedMenuOpened() {
215 // Switching the menu to expanded view, so hide the
216 // title bar.
217 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800218 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800219 }
220
221 @Override
222 public void onOptionsMenuClosed(boolean inLoad) {
223 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800224 mActivity.getWindowManager().removeView(mTitleOverlay);
225 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800226 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800227 }
228 }
229
230 @Override
231 public void onExtendedMenuClosed(boolean inLoad) {
232 mExtendedMenuOpen = false;
Michael Kolbfdb70242011-03-24 09:41:11 -0700233 if (!mUseQuickControls) {
234 showTitleBar();
235 }
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
238 @Override
239 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800240 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242
243 @Override
244 public void onContextMenuClosed(Menu menu, boolean inLoad) {
245 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800246 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800247 }
248 }
249
250 // action mode callbacks
251
252 @Override
253 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800254 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800255 }
256
Michael Kolba4183062011-01-16 10:43:21 -0800257 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700258 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700259 if (inLoad) {
260 if (mUseQuickControls) {
261 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700262 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700263 showTitleBar();
264 }
265 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700266 }
267
268 @Override
Michael Kolba4183062011-01-16 10:43:21 -0800269 public boolean dispatchKey(int code, KeyEvent event) {
270 return false;
271 }
272
John Reckd29abda2011-02-14 17:51:40 -0800273 static class TouchProxy extends View {
274
275 View mTarget;
276
277 TouchProxy(Context context, View target) {
278 super(context);
279 mTarget = target;
280 }
281
282 @Override
283 public boolean dispatchTouchEvent(MotionEvent event) {
284 return mTarget.dispatchTouchEvent(event);
285 }
286
287 WindowManager.LayoutParams getWindowLayoutParams() {
288 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800289 new WindowManager.LayoutParams(
290 mTarget.getWidth(),
291 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800292 WindowManager.LayoutParams.TYPE_APPLICATION,
293 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
294 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800295 params.gravity = Gravity.TOP | Gravity.LEFT;
296 params.y = mTarget.getTop();
297 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800298 return params;
299 }
300 }
Michael Kolb11d19782011-03-20 10:17:40 -0700301
Michael Kolbfdb70242011-03-24 09:41:11 -0700302 @Override
303 protected void setTitleGravity(int gravity) {
304 if (mUseQuickControls) {
305 FrameLayout.LayoutParams lp =
306 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
307 lp.gravity = gravity;
308 getTitleBar().setLayoutParams(lp);
309 } else {
310 super.setTitleGravity(gravity);
311 }
312 }
313
314 private void setUseQuickControls(boolean useQuickControls) {
315 mUseQuickControls = useQuickControls;
316 getTitleBar().setUseQuickControls(mUseQuickControls);
317 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700318 mPieControl = new PieControl(mActivity, mUiController, this);
319 mPieControl.attachToContainer(mContentView);
Michael Kolb46f987e2011-04-05 10:39:10 -0700320 WebView web = getWebView();
321 if (web != null) {
322 web.setEmbeddedTitleBar(null);
Michael Kolbfdb70242011-03-24 09:41:11 -0700323 }
324 } else {
325 mActivity.getActionBar().show();
326 if (mPieControl != null) {
327 mPieControl.removeFromContainer(mContentView);
328 }
Michael Kolb46f987e2011-04-05 10:39:10 -0700329 WebView web = getWebView();
Michael Kolbfdb70242011-03-24 09:41:11 -0700330 if (web != null) {
331 web.setEmbeddedTitleBar(mTitleBar);
332 }
333 setTitleGravity(Gravity.NO_GRAVITY);
334 }
335 }
336
337 @Override
338 public boolean onPrepareOptionsMenu(Menu menu) {
339 if (mUseQuickControls) {
340 menu.setGroupVisible(R.id.NAV_MENU, false);
341 mPieControl.onMenuOpened(menu);
342 return false;
343 } else {
344 return true;
345 }
346 }
347
348 @Override
349 protected void captureTab(final Tab tab) {
350 if (mUseQuickControls) {
351 super.captureTab(tab);
352 } else {
353 captureTab(tab,
354 mActivity.getWindowManager().getDefaultDisplay().getWidth(),
355 (int) mActivity.getResources()
356 .getDimension(R.dimen.tab_view_thumbnail_height));
357 }
358 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700359
Michael Kolb66706532010-12-12 19:50:22 -0800360}