blob: 4119c298ceb9834808789f532fb2ed3fcf2580ed [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;
20import 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;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.WindowManager;
30import 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;
40 private TitleBar mFakeTitleBar;
41 private ActiveTabsPage mActiveTabsPage;
42
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);
52 mTitleBar = new TitleBar(mActivity, mUiController);
53 // mTitleBar will be always be shown in the fully loaded mode on
54 // phone
55 mTitleBar.setProgress(100);
56 mFakeTitleBar = new TitleBar(mActivity, mUiController);
57
58 }
59
60 // webview factory
61
62 @Override
63 public WebView createWebView(boolean privateBrowsing) {
64 // Create a new WebView
65 WebView w = new WebView(mActivity, null,
66 android.R.attr.webViewStyle, privateBrowsing);
67 initWebViewSettings(w);
68 return w;
69 }
70
71 @Override
72 public WebView createSubWebView(boolean privateBrowsing) {
73 WebView web = createWebView(privateBrowsing);
74 return web;
75 }
76
77 // lifecycle
78
79 @Override
80 public void onPause() {
81 // FIXME: This removes the active tabs page and resets the menu to
82 // MAIN_MENU. A better solution might be to do this work in onNewIntent
83 // but then we would need to save it in onSaveInstanceState and restore
84 // it in onCreate/onRestoreInstanceState
85 if (mActiveTabsPage != null) {
86 mUiController.removeActiveTabsPage(true);
87 }
88 super.onPause();
89 }
90
91 @Override
92 public void onDestroy() {
93 hideFakeTitleBar();
94 }
95
96 @Override
97 public boolean onBackKey() {
98 if (mActiveTabsPage != null) {
99 // if tab page is showing, hide it
100 mUiController.removeActiveTabsPage(true);
101 return true;
102 }
103 return super.onBackKey();
104 }
105
106 @Override
John Reck30c714c2010-12-16 17:30:34 -0800107 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800108 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800109 int progress = tab.getLoadProgress();
Michael Kolb66706532010-12-12 19:50:22 -0800110 mFakeTitleBar.setProgress(progress);
111 if (progress == 100) {
112 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
113 hideFakeTitleBar();
114 }
115 } else {
116 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
117 showFakeTitleBar();
118 }
119 }
120 }
121 }
122
123 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800124 public void setActiveTab(Tab tab) {
125 super.setActiveTab(tab);
126 WebView view = tab.getWebView();
127 // TabControl.setCurrentTab has been called before this,
128 // so the tab is guaranteed to have a webview
129 if (view == null) {
130 Log.e(LOGTAG, "active tab with no webview detected");
131 return;
132 }
133 view.setEmbeddedTitleBar(getEmbeddedTitleBar());
134 if (tab.isInVoiceSearchMode()) {
135 showVoiceTitleBar(tab.getVoiceDisplayTitle());
136 } else {
137 revertVoiceTitleBar(tab);
138 }
Michael Kolb376b5412010-12-15 11:52:57 -0800139 tab.getTopWindow().requestFocus();
140 }
141
142 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800143 protected void attachFakeTitleBar(WebView mainView) {
144 WindowManager manager = (WindowManager)
145 mActivity.getSystemService(Context.WINDOW_SERVICE);
146
147 // Add the title bar to the window manager so it can receive
148 // touches while the menu is up
149 WindowManager.LayoutParams params =
150 new WindowManager.LayoutParams(
151 ViewGroup.LayoutParams.MATCH_PARENT,
152 ViewGroup.LayoutParams.WRAP_CONTENT,
153 WindowManager.LayoutParams.TYPE_APPLICATION,
154 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
155 PixelFormat.TRANSLUCENT);
156 params.gravity = Gravity.TOP;
157 boolean atTop = mainView.getScrollY() == 0;
158 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
159 manager.addView(mFakeTitleBar, params);
160 }
161
162 @Override
163 protected void hideFakeTitleBar() {
164 if (!isFakeTitleBarShowing()) return;
165 WindowManager.LayoutParams params =
166 (WindowManager.LayoutParams) mFakeTitleBar.getLayoutParams();
167 WebView mainView = mUiController.getCurrentWebView();
168 // Although we decided whether or not to animate based on the
169 // current
170 // scroll position, the scroll position may have changed since the
171 // fake title bar was displayed. Make sure it has the appropriate
172 // animation/lack thereof before removing.
173 params.windowAnimations =
174 mainView != null && mainView.getScrollY() == 0 ?
175 0 : R.style.TitleBar;
176 WindowManager manager = (WindowManager) mActivity
177 .getSystemService(Context.WINDOW_SERVICE);
178 manager.updateViewLayout(mFakeTitleBar, params);
179 manager.removeView(mFakeTitleBar);
180 }
181
182 @Override
183 protected boolean isFakeTitleBarShowing() {
184 return (mFakeTitleBar.getParent() != null);
185 }
186
187 @Override
188 protected TitleBarBase getFakeTitleBar() {
189 return mFakeTitleBar;
190 }
191
192 @Override
193 protected TitleBarBase getEmbeddedTitleBar() {
194 return mTitleBar;
195 }
196
197 // active tabs page
198
199 @Override
200 public void showActiveTabsPage() {
201 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
202 mTitleBar.setVisibility(View.GONE);
203 hideFakeTitleBar();
204 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
205 mActiveTabsPage.requestFocus();
206 }
207
208 /**
209 * Remove the active tabs page.
210 */
211 @Override
212 public void removeActiveTabsPage() {
213 mContentView.removeView(mActiveTabsPage);
214 mTitleBar.setVisibility(View.VISIBLE);
215 mActiveTabsPage = null;
216 }
217
218 @Override
219 public boolean showsWeb() {
220 return super.showsWeb() && mActiveTabsPage == null;
221 }
222
223 // menu handling callbacks
224
225 @Override
226 public void onOptionsMenuOpened() {
227 mOptionsMenuOpen = true;
228 // options menu opened, show fake title bar
229 showFakeTitleBar();
230 }
231
232 @Override
233 public void onExtendedMenuOpened() {
234 // Switching the menu to expanded view, so hide the
235 // title bar.
236 mExtendedMenuOpen = true;
237 hideFakeTitleBar();
238 }
239
240 @Override
241 public void onOptionsMenuClosed(boolean inLoad) {
242 mOptionsMenuOpen = false;
243 if (!inLoad) {
244 hideFakeTitleBar();
245 }
246 }
247
248 @Override
249 public void onExtendedMenuClosed(boolean inLoad) {
250 mExtendedMenuOpen = false;
251 showFakeTitleBar();
252 }
253
254 @Override
255 public void onContextMenuCreated(Menu menu) {
256 hideFakeTitleBar();
257 }
258
259 @Override
260 public void onContextMenuClosed(Menu menu, boolean inLoad) {
261 if (inLoad) {
262 showFakeTitleBar();
263 }
264 }
265
266 // action mode callbacks
267
268 @Override
269 public void onActionModeStarted(ActionMode mode) {
270 hideFakeTitleBar();
271 }
272
Michael Kolba4183062011-01-16 10:43:21 -0800273 @Override
274 public boolean dispatchKey(int code, KeyEvent event) {
275 return false;
276 }
277
Michael Kolb66706532010-12-12 19:50:22 -0800278}