blob: 42e2aff46629c9d716e324a6a5fc8719749ad7c7 [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 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) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800112 hideTitleBar();
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 Reck88a42b72011-03-21 16:30:12 -0700131 captureTab(mActiveTab);
Michael Kolbfdb70242011-03-24 09:41:11 -0700132 super.setActiveTab(tab, true);
133 setActiveTab(tab, true);
134 }
135
136 @Override
137 void setActiveTab(Tab tab, boolean needsAttaching) {
138 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800139 // TabControl.setCurrentTab has been called before this,
140 // so the tab is guaranteed to have a webview
141 if (view == null) {
142 Log.e(LOGTAG, "active tab with no webview detected");
143 return;
144 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700145 // Request focus on the top window.
146 if (mUseQuickControls) {
147 mPieControl.forceToTop(mContentView);
148 view.setScrollListener(null);
149 } else {
150 // check if title bar is already attached by animation
151 if (mTitleBar.getParent() == null) {
152 view.setEmbeddedTitleBar(mTitleBar);
153 }
154 }
Michael Kolb376b5412010-12-15 11:52:57 -0800155 if (tab.isInVoiceSearchMode()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700156 showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800157 } else {
158 revertVoiceTitleBar(tab);
159 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700160 updateLockIconToLatest(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800161 tab.getTopWindow().requestFocus();
162 }
163
164 @Override
John Reckd29abda2011-02-14 17:51:40 -0800165 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800166 if (canShowTitleBar()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700167 if (mUseQuickControls) {
168 mContentView.addView(mTitleBar);
169 } else {
170 setTitleGravity(Gravity.TOP);
171 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800172 super.showTitleBar();
173 }
Michael Kolb66706532010-12-12 19:50:22 -0800174 }
175
176 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800177 protected void hideTitleBar() {
178 if (isTitleBarShowing()) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700179 if (mUseQuickControls) {
180 mContentView.removeView(mTitleBar);
181 } else {
182 setTitleGravity(Gravity.NO_GRAVITY);
183 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800184 super.hideTitleBar();
185 }
Michael Kolb66706532010-12-12 19:50:22 -0800186 }
187
188 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800189 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800190 return mTitleBar;
191 }
192
193 // active tabs page
194
195 @Override
196 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700197 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800198 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
199 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800201 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
202 mActiveTabsPage.requestFocus();
203 }
204
205 /**
206 * Remove the active tabs page.
207 */
208 @Override
209 public void removeActiveTabsPage() {
210 mContentView.removeView(mActiveTabsPage);
211 mTitleBar.setVisibility(View.VISIBLE);
212 mActiveTabsPage = null;
213 }
214
215 @Override
216 public boolean showsWeb() {
217 return super.showsWeb() && mActiveTabsPage == null;
218 }
219
220 // menu handling callbacks
221
222 @Override
223 public void onOptionsMenuOpened() {
224 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800225 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800226 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800227 if (mTitleOverlay == null) {
228 // This assumes that getTitleBar always returns the same View
229 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
230 }
231 mActivity.getWindowManager().addView(mTitleOverlay,
232 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800233 }
234
235 @Override
236 public void onExtendedMenuOpened() {
237 // Switching the menu to expanded view, so hide the
238 // title bar.
239 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800240 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242
243 @Override
244 public void onOptionsMenuClosed(boolean inLoad) {
245 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800246 mActivity.getWindowManager().removeView(mTitleOverlay);
247 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800248 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250 }
251
252 @Override
253 public void onExtendedMenuClosed(boolean inLoad) {
254 mExtendedMenuOpen = false;
Michael Kolbfdb70242011-03-24 09:41:11 -0700255 if (!mUseQuickControls) {
256 showTitleBar();
257 }
Michael Kolb66706532010-12-12 19:50:22 -0800258 }
259
260 @Override
261 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800262 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800263 }
264
265 @Override
266 public void onContextMenuClosed(Menu menu, boolean inLoad) {
267 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800268 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800269 }
270 }
271
272 // action mode callbacks
273
274 @Override
275 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800276 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800277 }
278
Michael Kolba4183062011-01-16 10:43:21 -0800279 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700280 public void onActionModeFinished(boolean inLoad) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700281 if (inLoad) {
282 if (mUseQuickControls) {
283 mTitleBar.setShowProgressOnly(true);
John Reck6cda7b12011-03-18 15:57:13 -0700284 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700285 showTitleBar();
286 }
287 mActivity.getActionBar().hide();
John Reck6cda7b12011-03-18 15:57:13 -0700288 }
289
290 @Override
Michael Kolba4183062011-01-16 10:43:21 -0800291 public boolean dispatchKey(int code, KeyEvent event) {
292 return false;
293 }
294
John Reckd29abda2011-02-14 17:51:40 -0800295 static class TouchProxy extends View {
296
297 View mTarget;
298
299 TouchProxy(Context context, View target) {
300 super(context);
301 mTarget = target;
302 }
303
304 @Override
305 public boolean dispatchTouchEvent(MotionEvent event) {
306 return mTarget.dispatchTouchEvent(event);
307 }
308
309 WindowManager.LayoutParams getWindowLayoutParams() {
310 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800311 new WindowManager.LayoutParams(
312 mTarget.getWidth(),
313 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800314 WindowManager.LayoutParams.TYPE_APPLICATION,
315 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
316 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800317 params.gravity = Gravity.TOP | Gravity.LEFT;
318 params.y = mTarget.getTop();
319 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800320 return params;
321 }
322 }
Michael Kolb11d19782011-03-20 10:17:40 -0700323
Michael Kolbfdb70242011-03-24 09:41:11 -0700324 @Override
325 protected void setTitleGravity(int gravity) {
326 if (mUseQuickControls) {
327 FrameLayout.LayoutParams lp =
328 (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
329 lp.gravity = gravity;
330 getTitleBar().setLayoutParams(lp);
331 } else {
332 super.setTitleGravity(gravity);
333 }
334 }
335
336 private void setUseQuickControls(boolean useQuickControls) {
337 mUseQuickControls = useQuickControls;
338 getTitleBar().setUseQuickControls(mUseQuickControls);
339 if (useQuickControls) {
Michael Kolbfdb70242011-03-24 09:41:11 -0700340 mPieControl = new PieControl(mActivity, mUiController, this);
341 mPieControl.attachToContainer(mContentView);
342 Tab tab = getActiveTab();
343 if ((tab != null) && (tab.getWebView() != null)) {
344 tab.getWebView().setEmbeddedTitleBar(null);
345 }
346 } else {
347 mActivity.getActionBar().show();
348 if (mPieControl != null) {
349 mPieControl.removeFromContainer(mContentView);
350 }
351 WebView web = mTabControl.getCurrentWebView();
352 if (web != null) {
353 web.setEmbeddedTitleBar(mTitleBar);
354 }
355 setTitleGravity(Gravity.NO_GRAVITY);
356 }
357 }
358
359 @Override
360 public boolean onPrepareOptionsMenu(Menu menu) {
361 if (mUseQuickControls) {
362 menu.setGroupVisible(R.id.NAV_MENU, false);
363 mPieControl.onMenuOpened(menu);
364 return false;
365 } else {
366 return true;
367 }
368 }
369
370 @Override
371 protected void captureTab(final Tab tab) {
372 if (mUseQuickControls) {
373 super.captureTab(tab);
374 } else {
375 captureTab(tab,
376 mActivity.getWindowManager().getDefaultDisplay().getWidth(),
377 (int) mActivity.getResources()
378 .getDimension(R.dimen.tab_view_thumbnail_height));
379 }
380 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700381
382 @Override
383 void showTitleBarAndEdit() {
384 mTitleBar.setShowProgressOnly(false);
385 super.showTitleBarAndEdit();
386 }
387
Michael Kolb66706532010-12-12 19:50:22 -0800388}