blob: e35e6249fc660699b1ac08229525477110cf6ed4 [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;
25import android.view.Menu;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.WindowManager;
29import android.webkit.WebView;
30
31/**
32 * Ui for regular phone screen sizes
33 */
34public class PhoneUi extends BaseUi {
35
36 private static final String LOGTAG = "PhoneUi";
37
38 private TitleBar mTitleBar;
39 private TitleBar mFakeTitleBar;
40 private ActiveTabsPage mActiveTabsPage;
41
42 boolean mExtendedMenuOpen;
43 boolean mOptionsMenuOpen;
44
45 /**
46 * @param browser
47 * @param controller
48 */
49 public PhoneUi(Activity browser, UiController controller) {
50 super(browser, controller);
51 mTitleBar = new TitleBar(mActivity, mUiController);
52 // mTitleBar will be always be shown in the fully loaded mode on
53 // phone
54 mTitleBar.setProgress(100);
55 mFakeTitleBar = new TitleBar(mActivity, mUiController);
56
57 }
58
59 // webview factory
60
61 @Override
62 public WebView createWebView(boolean privateBrowsing) {
63 // Create a new WebView
64 WebView w = new WebView(mActivity, null,
65 android.R.attr.webViewStyle, privateBrowsing);
66 initWebViewSettings(w);
67 return w;
68 }
69
70 @Override
71 public WebView createSubWebView(boolean privateBrowsing) {
72 WebView web = createWebView(privateBrowsing);
73 return web;
74 }
75
76 // lifecycle
77
78 @Override
79 public void onPause() {
80 // FIXME: This removes the active tabs page and resets the menu to
81 // MAIN_MENU. A better solution might be to do this work in onNewIntent
82 // but then we would need to save it in onSaveInstanceState and restore
83 // it in onCreate/onRestoreInstanceState
84 if (mActiveTabsPage != null) {
85 mUiController.removeActiveTabsPage(true);
86 }
87 super.onPause();
88 }
89
90 @Override
91 public void onDestroy() {
92 hideFakeTitleBar();
93 }
94
95 @Override
96 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
106 public void onProgressChanged(Tab tab, int progress) {
107 if (tab.inForeground()) {
108 mFakeTitleBar.setProgress(progress);
109 if (progress == 100) {
110 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
111 hideFakeTitleBar();
112 }
113 } else {
114 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
115 showFakeTitleBar();
116 }
117 }
118 }
119 }
120
121 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800122 public void setActiveTab(Tab tab) {
123 super.setActiveTab(tab);
124 WebView view = tab.getWebView();
125 // TabControl.setCurrentTab has been called before this,
126 // so the tab is guaranteed to have a webview
127 if (view == null) {
128 Log.e(LOGTAG, "active tab with no webview detected");
129 return;
130 }
131 view.setEmbeddedTitleBar(getEmbeddedTitleBar());
132 if (tab.isInVoiceSearchMode()) {
133 showVoiceTitleBar(tab.getVoiceDisplayTitle());
134 } else {
135 revertVoiceTitleBar(tab);
136 }
137 resetTitleIconAndProgress(tab);
138 updateLockIconToLatest(tab);
139 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
273}