blob: e51962e6ff70945a0f1a1c7615ec52295b4200d1 [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.ViewGroup;
30import android.view.WindowManager;
Michael Kolb66706532010-12-12 19:50:22 -080031import android.webkit.WebView;
32
33/**
34 * Ui for regular phone screen sizes
35 */
36public class PhoneUi extends BaseUi {
37
38 private static final String LOGTAG = "PhoneUi";
39
40 private TitleBar mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080041 private ActiveTabsPage mActiveTabsPage;
John Reckd29abda2011-02-14 17:51:40 -080042 private TouchProxy mTitleOverlay;
Michael Kolb66706532010-12-12 19:50:22 -080043
44 boolean mExtendedMenuOpen;
45 boolean mOptionsMenuOpen;
46
47 /**
48 * @param browser
49 * @param controller
50 */
51 public PhoneUi(Activity browser, UiController controller) {
52 super(browser, controller);
53 mTitleBar = new TitleBar(mActivity, mUiController);
54 // mTitleBar will be always be shown in the fully loaded mode on
55 // phone
56 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080057 mActivity.getActionBar().hide();
58 }
Michael Kolb66706532010-12-12 19:50:22 -080059
John Reck285ef042011-02-11 15:44:17 -080060 @Override
61 public void hideComboView() {
62 super.hideComboView();
63 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080064 }
65
66 // webview factory
67
68 @Override
69 public WebView createWebView(boolean privateBrowsing) {
70 // Create a new WebView
71 WebView w = new WebView(mActivity, null,
72 android.R.attr.webViewStyle, privateBrowsing);
73 initWebViewSettings(w);
74 return w;
75 }
76
77 @Override
78 public WebView createSubWebView(boolean privateBrowsing) {
79 WebView web = createWebView(privateBrowsing);
80 return web;
81 }
82
83 // lifecycle
84
85 @Override
86 public void onPause() {
87 // FIXME: This removes the active tabs page and resets the menu to
88 // MAIN_MENU. A better solution might be to do this work in onNewIntent
89 // but then we would need to save it in onSaveInstanceState and restore
90 // it in onCreate/onRestoreInstanceState
91 if (mActiveTabsPage != null) {
92 mUiController.removeActiveTabsPage(true);
93 }
94 super.onPause();
95 }
96
97 @Override
98 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080099 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800100 }
101
102 @Override
103 public boolean onBackKey() {
104 if (mActiveTabsPage != null) {
105 // if tab page is showing, hide it
106 mUiController.removeActiveTabsPage(true);
107 return true;
108 }
109 return super.onBackKey();
110 }
111
112 @Override
John Reck30c714c2010-12-16 17:30:34 -0800113 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800114 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800115 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800116 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800117 if (progress == 100) {
118 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800119 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800120 }
121 } else {
122 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800123 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800124 }
125 }
126 }
127 }
128
129 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800130 public void setActiveTab(Tab tab) {
131 super.setActiveTab(tab);
132 WebView view = tab.getWebView();
133 // 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 Kolb7cdc4902011-02-03 17:54:40 -0800139 view.setEmbeddedTitleBar(getTitleBar());
Michael Kolb376b5412010-12-15 11:52:57 -0800140 if (tab.isInVoiceSearchMode()) {
141 showVoiceTitleBar(tab.getVoiceDisplayTitle());
142 } else {
143 revertVoiceTitleBar(tab);
144 }
Michael Kolb376b5412010-12-15 11:52:57 -0800145 tab.getTopWindow().requestFocus();
146 }
147
Michael Kolbb0868f12011-02-14 14:36:13 -0800148 private void attachTitleBar(WebView mainView) {
149 if (mainView != null) {
150 mainView.setEmbeddedTitleBar(null);
151 }
152 WindowManager manager = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
153 // Add the title bar to the window manager so it can receive
154 // touches while the menu is up
155 WindowManager.LayoutParams params =
156 new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
157 ViewGroup.LayoutParams.WRAP_CONTENT,
158 WindowManager.LayoutParams.TYPE_APPLICATION,
159 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
160 PixelFormat.OPAQUE);
161 params.gravity = Gravity.TOP;
162 boolean atTop = mainView.getScrollY() == 0;
163 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
164 manager.addView(mTitleBar, params);
165 super.showTitleBar();
166 }
167
Michael Kolb376b5412010-12-15 11:52:57 -0800168 @Override
John Reckd29abda2011-02-14 17:51:40 -0800169 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800170 if (canShowTitleBar()) {
Michael Kolbb0868f12011-02-14 14:36:13 -0800171 WebView mainView = mUiController.getCurrentWebView();
172 attachTitleBar(mainView);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800173 super.showTitleBar();
174 }
Michael Kolb66706532010-12-12 19:50:22 -0800175 }
176
177 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800178 protected void hideTitleBar() {
179 if (isTitleBarShowing()) {
Michael Kolbb0868f12011-02-14 14:36:13 -0800180 WindowManager.LayoutParams params =
181 (WindowManager.LayoutParams) mTitleBar.getLayoutParams();
182 WebView mainView = mUiController.getCurrentWebView();
183 // Although we decided whether or not to animate based on the
184 // current
185 // scroll position, the scroll position may have changed since the
186 // fake title bar was displayed. Make sure it has the appropriate
187 // animation/lack thereof before removing.
188 params.windowAnimations =
189 mainView != null && mainView.getScrollY() == 0 ? 0 : R.style.TitleBar;
190 WindowManager manager =
191 (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
192 manager.updateViewLayout(mTitleBar, params);
193 manager.removeView(mTitleBar);
194 if (mainView != null) {
195 mainView.setEmbeddedTitleBar(mTitleBar);
196 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800197 super.hideTitleBar();
198 }
Michael Kolb66706532010-12-12 19:50:22 -0800199 }
200
201 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800202 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800203 return mTitleBar;
204 }
205
206 // active tabs page
207
208 @Override
209 public void showActiveTabsPage() {
210 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
211 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800212 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800213 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
214 mActiveTabsPage.requestFocus();
215 }
216
217 /**
218 * Remove the active tabs page.
219 */
220 @Override
221 public void removeActiveTabsPage() {
222 mContentView.removeView(mActiveTabsPage);
223 mTitleBar.setVisibility(View.VISIBLE);
224 mActiveTabsPage = null;
225 }
226
227 @Override
228 public boolean showsWeb() {
229 return super.showsWeb() && mActiveTabsPage == null;
230 }
231
232 // menu handling callbacks
233
234 @Override
235 public void onOptionsMenuOpened() {
236 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800237 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800238 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800239 if (mTitleOverlay == null) {
240 // This assumes that getTitleBar always returns the same View
241 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
242 }
243 mActivity.getWindowManager().addView(mTitleOverlay,
244 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800245 }
246
247 @Override
248 public void onExtendedMenuOpened() {
249 // Switching the menu to expanded view, so hide the
250 // title bar.
251 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800252 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800253 }
254
255 @Override
256 public void onOptionsMenuClosed(boolean inLoad) {
257 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800258 mActivity.getWindowManager().removeView(mTitleOverlay);
259 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800260 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800261 }
262 }
263
264 @Override
265 public void onExtendedMenuClosed(boolean inLoad) {
266 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800267 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800268 }
269
270 @Override
271 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800272 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800273 }
274
275 @Override
276 public void onContextMenuClosed(Menu menu, boolean inLoad) {
277 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800278 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800279 }
280 }
281
282 // action mode callbacks
283
284 @Override
285 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800286 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800287 }
288
Michael Kolba4183062011-01-16 10:43:21 -0800289 @Override
290 public boolean dispatchKey(int code, KeyEvent event) {
291 return false;
292 }
293
John Reckd29abda2011-02-14 17:51:40 -0800294 static class TouchProxy extends View {
295
296 View mTarget;
297
298 TouchProxy(Context context, View target) {
299 super(context);
300 mTarget = target;
301 }
302
303 @Override
304 public boolean dispatchTouchEvent(MotionEvent event) {
305 return mTarget.dispatchTouchEvent(event);
306 }
307
308 WindowManager.LayoutParams getWindowLayoutParams() {
309 WindowManager.LayoutParams params =
310 new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
311 ViewGroup.LayoutParams.WRAP_CONTENT,
312 WindowManager.LayoutParams.TYPE_APPLICATION,
313 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
314 PixelFormat.TRANSPARENT);
315 params.gravity = Gravity.TOP;
316 return params;
317 }
318 }
Michael Kolb66706532010-12-12 19:50:22 -0800319}