blob: 9cc5a2fc1158c0a98920a67123cd8ff353d5cbc8 [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;
John Reck6cda7b12011-03-18 15:57:13 -070022import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080023import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080024import android.view.ActionMode;
25import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080026import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080027import android.view.Menu;
John Reckd29abda2011-02-14 17:51:40 -080028import android.view.MotionEvent;
Michael Kolb66706532010-12-12 19:50:22 -080029import android.view.View;
Michael Kolbb0868f12011-02-14 14:36:13 -080030import android.view.WindowManager;
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
34/**
35 * Ui for regular phone screen sizes
36 */
37public class PhoneUi extends BaseUi {
38
39 private static final String LOGTAG = "PhoneUi";
40
Michael Kolb11d19782011-03-20 10:17:40 -070041 private TitleBarPhone mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080042 private ActiveTabsPage mActiveTabsPage;
John Reckd29abda2011-02-14 17:51:40 -080043 private TouchProxy mTitleOverlay;
Michael Kolbfdb70242011-03-24 09:41:11 -070044 private boolean mUseQuickControls;
45 private PieControl mPieControl;
Michael Kolb66706532010-12-12 19:50:22 -080046
47 boolean mExtendedMenuOpen;
48 boolean mOptionsMenuOpen;
49
50 /**
51 * @param browser
52 * @param controller
53 */
54 public PhoneUi(Activity browser, UiController controller) {
55 super(browser, controller);
Michael Kolb11d19782011-03-20 10:17:40 -070056 mTitleBar = new TitleBarPhone(mActivity, mUiController, this);
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) {
91 String url = getActiveTab().getUrl();
92 mUiController.startSearch(url);
93 }
94
95 @Override
Michael Kolb66706532010-12-12 19:50:22 -080096 public boolean onBackKey() {
97 if (mActiveTabsPage != null) {
98 // if tab page is showing, hide it
99 mUiController.removeActiveTabsPage(true);
100 return true;
101 }
102 return super.onBackKey();
103 }
104
105 @Override
John Reck30c714c2010-12-16 17:30:34 -0800106 public void onProgressChanged(Tab tab) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700107 if (mUseQuickControls) return;
Michael Kolb66706532010-12-12 19:50:22 -0800108 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800109 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800110 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800111 if (progress == 100) {
112 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800113 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800114 }
115 } else {
116 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800117 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800118 }
119 }
120 }
121 }
122
123 @Override
Michael Kolbfdb70242011-03-24 09:41:11 -0700124 public void setActiveTab(final Tab tab) {
John Reck88a42b72011-03-21 16:30:12 -0700125 captureTab(mActiveTab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700126 super.setActiveTab(tab, true);
127 setActiveTab(tab, true);
128 }
129
130 @Override
131 void setActiveTab(Tab tab, boolean needsAttaching) {
132 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);
142 view.setScrollListener(null);
143 } else {
144 // check if title bar is already attached by animation
145 if (mTitleBar.getParent() == null) {
146 view.setEmbeddedTitleBar(mTitleBar);
147 }
148 }
Michael Kolb376b5412010-12-15 11:52:57 -0800149 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700150 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800151 } else {
152 revertVoiceTitleBar(tab);
153 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700154 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800155 tab.getTopWindow().requestFocus();
156 }
157
158 @Override
John Reckd29abda2011-02-14 17:51:40 -0800159 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800160 if (canShowTitleBar()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700161 if (mUseQuickControls) {
162 mContentView.addView(mTitleBar);
163 } else {
164 setTitleGravity(Gravity.TOP);
165 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800166 super.showTitleBar();
167 }
Michael Kolb66706532010-12-12 19:50:22 -0800168 }
169
170 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800171 protected void hideTitleBar() {
172 if (isTitleBarShowing()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700173 if (mUseQuickControls) {
174 mContentView.removeView(mTitleBar);
175 } else {
176 setTitleGravity(Gravity.NO_GRAVITY);
177 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800178 super.hideTitleBar();
179 }
Michael Kolb66706532010-12-12 19:50:22 -0800180 }
181
182 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800183 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800184 return mTitleBar;
185 }
186
187 // active tabs page
188
189 @Override
190 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700191 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800192 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
193 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800194 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800195 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
196 mActiveTabsPage.requestFocus();
197 }
198
199 /**
200 * Remove the active tabs page.
201 */
202 @Override
203 public void removeActiveTabsPage() {
204 mContentView.removeView(mActiveTabsPage);
205 mTitleBar.setVisibility(View.VISIBLE);
206 mActiveTabsPage = null;
207 }
208
209 @Override
210 public boolean showsWeb() {
211 return super.showsWeb() && mActiveTabsPage == null;
212 }
213
214 // menu handling callbacks
215
216 @Override
217 public void onOptionsMenuOpened() {
218 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800219 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800220 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800221 if (mTitleOverlay == null) {
222 // This assumes that getTitleBar always returns the same View
223 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
224 }
225 mActivity.getWindowManager().addView(mTitleOverlay,
226 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800227 }
228
229 @Override
230 public void onExtendedMenuOpened() {
231 // Switching the menu to expanded view, so hide the
232 // title bar.
233 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800234 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800235 }
236
237 @Override
238 public void onOptionsMenuClosed(boolean inLoad) {
239 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800240 mActivity.getWindowManager().removeView(mTitleOverlay);
241 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800242 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800243 }
244 }
245
246 @Override
247 public void onExtendedMenuClosed(boolean inLoad) {
248 mExtendedMenuOpen = false;
Michael Kolbfdb70242011-03-24 09:41:11 -0700249 if (!mUseQuickControls) {
250 showTitleBar();
251 }
Michael Kolb66706532010-12-12 19:50:22 -0800252 }
253
254 @Override
255 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800256 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800257 }
258
259 @Override
260 public void onContextMenuClosed(Menu menu, boolean inLoad) {
261 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800262 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800263 }
264 }
265
266 // action mode callbacks
267
268 @Override
269 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800270 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800271 }
272
Michael Kolba4183062011-01-16 10:43:21 -0800273 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700274 public void onActionModeFinished(boolean inLoad) {
275 // TODO: Remove once b/4136071 is fixed
276 new Handler().post(new Runnable() {
277 @Override
278 public void run() {
279 mActivity.getActionBar().hide();
280 }
281 });
282 }
283
284 @Override
Michael Kolba4183062011-01-16 10:43:21 -0800285 public boolean dispatchKey(int code, KeyEvent event) {
286 return false;
287 }
288
John Reckd29abda2011-02-14 17:51:40 -0800289 static class TouchProxy extends View {
290
291 View mTarget;
292
293 TouchProxy(Context context, View target) {
294 super(context);
295 mTarget = target;
296 }
297
298 @Override
299 public boolean dispatchTouchEvent(MotionEvent event) {
300 return mTarget.dispatchTouchEvent(event);
301 }
302
303 WindowManager.LayoutParams getWindowLayoutParams() {
304 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800305 new WindowManager.LayoutParams(
306 mTarget.getWidth(),
307 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800308 WindowManager.LayoutParams.TYPE_APPLICATION,
309 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
310 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800311 params.gravity = Gravity.TOP | Gravity.LEFT;
312 params.y = mTarget.getTop();
313 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800314 return params;
315 }
316 }
Michael Kolb11d19782011-03-20 10:17:40 -0700317
Michael Kolbfdb70242011-03-24 09:41:11 -0700318 @Override
319 protected void setTitleGravity(int gravity) {
320 if (mUseQuickControls) {
321 FrameLayout.LayoutParams lp =
322 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
323 lp.gravity = gravity;
324 getTitleBar().setLayoutParams(lp);
325 } else {
326 super.setTitleGravity(gravity);
327 }
328 }
329
330 private void setUseQuickControls(boolean useQuickControls) {
331 mUseQuickControls = useQuickControls;
332 getTitleBar().setUseQuickControls(mUseQuickControls);
333 if (useQuickControls) {
334// checkTabCount();
335 mPieControl = new PieControl(mActivity, mUiController, this);
336 mPieControl.attachToContainer(mContentView);
337 Tab tab = getActiveTab();
338 if ((tab != null) && (tab.getWebView() != null)) {
339 tab.getWebView().setEmbeddedTitleBar(null);
340 }
341 } else {
342 mActivity.getActionBar().show();
343 if (mPieControl != null) {
344 mPieControl.removeFromContainer(mContentView);
345 }
346 WebView web = mTabControl.getCurrentWebView();
347 if (web != null) {
348 web.setEmbeddedTitleBar(mTitleBar);
349 }
350 setTitleGravity(Gravity.NO_GRAVITY);
351 }
352 }
353
354 @Override
355 public boolean onPrepareOptionsMenu(Menu menu) {
356 if (mUseQuickControls) {
357 menu.setGroupVisible(R.id.NAV_MENU, false);
358 mPieControl.onMenuOpened(menu);
359 return false;
360 } else {
361 return true;
362 }
363 }
364
365 @Override
366 protected void captureTab(final Tab tab) {
367 if (mUseQuickControls) {
368 super.captureTab(tab);
369 } else {
370 captureTab(tab,
371 mActivity.getWindowManager().getDefaultDisplay().getWidth(),
372 (int) mActivity.getResources()
373 .getDimension(R.dimen.tab_view_thumbnail_height));
374 }
375 }
Michael Kolb66706532010-12-12 19:50:22 -0800376}