blob: 290ddf640478e608edb3e517f31b303ea6e7aa7b [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;
31
32/**
33 * Ui for regular phone screen sizes
34 */
35public class PhoneUi extends BaseUi {
36
37 private static final String LOGTAG = "PhoneUi";
38
39 private TitleBar mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080040 private ActiveTabsPage mActiveTabsPage;
John Reckd29abda2011-02-14 17:51:40 -080041 private TouchProxy mTitleOverlay;
Michael Kolb66706532010-12-12 19:50:22 -080042
43 boolean mExtendedMenuOpen;
44 boolean mOptionsMenuOpen;
45
46 /**
47 * @param browser
48 * @param controller
49 */
50 public PhoneUi(Activity browser, UiController controller) {
51 super(browser, controller);
John Reck92026732011-02-15 10:12:30 -080052 mTitleBar = new TitleBar(mActivity, mUiController, this);
Michael Kolb66706532010-12-12 19:50:22 -080053 // mTitleBar will be always be shown in the fully loaded mode on
54 // phone
55 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080056 mActivity.getActionBar().hide();
57 }
Michael Kolb66706532010-12-12 19:50:22 -080058
John Reck285ef042011-02-11 15:44:17 -080059 @Override
60 public void hideComboView() {
61 super.hideComboView();
62 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080063 }
64
Michael Kolb66706532010-12-12 19:50:22 -080065 // lifecycle
66
67 @Override
68 public void onPause() {
69 // FIXME: This removes the active tabs page and resets the menu to
70 // MAIN_MENU. A better solution might be to do this work in onNewIntent
71 // but then we would need to save it in onSaveInstanceState and restore
72 // it in onCreate/onRestoreInstanceState
73 if (mActiveTabsPage != null) {
74 mUiController.removeActiveTabsPage(true);
75 }
76 super.onPause();
77 }
78
79 @Override
80 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080081 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080082 }
83
84 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080085 public void editUrl(boolean clearInput) {
86 String url = getActiveTab().getUrl();
87 mUiController.startSearch(url);
88 }
89
90 @Override
Michael Kolb66706532010-12-12 19:50:22 -080091 public boolean onBackKey() {
92 if (mActiveTabsPage != null) {
93 // if tab page is showing, hide it
94 mUiController.removeActiveTabsPage(true);
95 return true;
96 }
97 return super.onBackKey();
98 }
99
100 @Override
John Reck30c714c2010-12-16 17:30:34 -0800101 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800102 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800103 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800104 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800105 if (progress == 100) {
106 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800107 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800108 }
109 } else {
110 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800111 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800112 }
113 }
114 }
115 }
116
117 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800118 public void setActiveTab(Tab tab) {
119 super.setActiveTab(tab);
120 WebView view = tab.getWebView();
121 // TabControl.setCurrentTab has been called before this,
122 // so the tab is guaranteed to have a webview
123 if (view == null) {
124 Log.e(LOGTAG, "active tab with no webview detected");
125 return;
126 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800127 view.setEmbeddedTitleBar(getTitleBar());
Michael Kolb376b5412010-12-15 11:52:57 -0800128 if (tab.isInVoiceSearchMode()) {
129 showVoiceTitleBar(tab.getVoiceDisplayTitle());
130 } else {
131 revertVoiceTitleBar(tab);
132 }
Michael Kolb376b5412010-12-15 11:52:57 -0800133 tab.getTopWindow().requestFocus();
134 }
135
136 @Override
John Reckd29abda2011-02-14 17:51:40 -0800137 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800138 if (canShowTitleBar()) {
John Reck539937b2011-02-14 18:18:39 -0800139 setTitleGravity(Gravity.TOP);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800140 super.showTitleBar();
141 }
Michael Kolb66706532010-12-12 19:50:22 -0800142 }
143
144 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800145 protected void hideTitleBar() {
146 if (isTitleBarShowing()) {
John Reck539937b2011-02-14 18:18:39 -0800147 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800148 super.hideTitleBar();
149 }
Michael Kolb66706532010-12-12 19:50:22 -0800150 }
151
152 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800153 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800154 return mTitleBar;
155 }
156
157 // active tabs page
158
159 @Override
160 public void showActiveTabsPage() {
161 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
162 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800163 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800164 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
165 mActiveTabsPage.requestFocus();
166 }
167
168 /**
169 * Remove the active tabs page.
170 */
171 @Override
172 public void removeActiveTabsPage() {
173 mContentView.removeView(mActiveTabsPage);
174 mTitleBar.setVisibility(View.VISIBLE);
175 mActiveTabsPage = null;
176 }
177
178 @Override
179 public boolean showsWeb() {
180 return super.showsWeb() && mActiveTabsPage == null;
181 }
182
183 // menu handling callbacks
184
185 @Override
186 public void onOptionsMenuOpened() {
187 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800188 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800189 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800190 if (mTitleOverlay == null) {
191 // This assumes that getTitleBar always returns the same View
192 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
193 }
194 mActivity.getWindowManager().addView(mTitleOverlay,
195 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800196 }
197
198 @Override
199 public void onExtendedMenuOpened() {
200 // Switching the menu to expanded view, so hide the
201 // title bar.
202 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800203 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800204 }
205
206 @Override
207 public void onOptionsMenuClosed(boolean inLoad) {
208 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800209 mActivity.getWindowManager().removeView(mTitleOverlay);
210 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800211 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800212 }
213 }
214
215 @Override
216 public void onExtendedMenuClosed(boolean inLoad) {
217 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800218 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800219 }
220
221 @Override
222 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800223 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800224 }
225
226 @Override
227 public void onContextMenuClosed(Menu menu, boolean inLoad) {
228 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800229 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231 }
232
233 // action mode callbacks
234
235 @Override
236 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800237 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800238 }
239
Michael Kolba4183062011-01-16 10:43:21 -0800240 @Override
241 public boolean dispatchKey(int code, KeyEvent event) {
242 return false;
243 }
244
John Reckd29abda2011-02-14 17:51:40 -0800245 static class TouchProxy extends View {
246
247 View mTarget;
248
249 TouchProxy(Context context, View target) {
250 super(context);
251 mTarget = target;
252 }
253
254 @Override
255 public boolean dispatchTouchEvent(MotionEvent event) {
256 return mTarget.dispatchTouchEvent(event);
257 }
258
259 WindowManager.LayoutParams getWindowLayoutParams() {
260 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800261 new WindowManager.LayoutParams(
262 mTarget.getWidth(),
263 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800264 WindowManager.LayoutParams.TYPE_APPLICATION,
265 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
266 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800267 params.gravity = Gravity.TOP | Gravity.LEFT;
268 params.y = mTarget.getTop();
269 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800270 return params;
271 }
272 }
Michael Kolb66706532010-12-12 19:50:22 -0800273}