blob: 60ca705bfcf936d8b1dd832f5cfd9fc76a54679e [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;
27import android.view.View;
Michael Kolbb0868f12011-02-14 14:36:13 -080028import android.view.ViewGroup;
29import 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;
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);
John Reck285ef042011-02-11 15:44:17 -080055 mActivity.getActionBar().hide();
56 }
Michael Kolb66706532010-12-12 19:50:22 -080057
John Reck285ef042011-02-11 15:44:17 -080058 @Override
59 public void hideComboView() {
60 super.hideComboView();
61 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080062 }
63
64 // webview factory
65
66 @Override
67 public WebView createWebView(boolean privateBrowsing) {
68 // Create a new WebView
69 WebView w = new WebView(mActivity, null,
70 android.R.attr.webViewStyle, privateBrowsing);
71 initWebViewSettings(w);
72 return w;
73 }
74
75 @Override
76 public WebView createSubWebView(boolean privateBrowsing) {
77 WebView web = createWebView(privateBrowsing);
78 return web;
79 }
80
81 // lifecycle
82
83 @Override
84 public void onPause() {
85 // FIXME: This removes the active tabs page and resets the menu to
86 // MAIN_MENU. A better solution might be to do this work in onNewIntent
87 // but then we would need to save it in onSaveInstanceState and restore
88 // it in onCreate/onRestoreInstanceState
89 if (mActiveTabsPage != null) {
90 mUiController.removeActiveTabsPage(true);
91 }
92 super.onPause();
93 }
94
95 @Override
96 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080097 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080098 }
99
100 @Override
101 public boolean onBackKey() {
102 if (mActiveTabsPage != null) {
103 // if tab page is showing, hide it
104 mUiController.removeActiveTabsPage(true);
105 return true;
106 }
107 return super.onBackKey();
108 }
109
110 @Override
John Reck30c714c2010-12-16 17:30:34 -0800111 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800112 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800113 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800114 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800115 if (progress == 100) {
116 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800117 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800118 }
119 } else {
120 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800121 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800122 }
123 }
124 }
125 }
126
127 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800128 public void setActiveTab(Tab tab) {
129 super.setActiveTab(tab);
130 WebView view = tab.getWebView();
131 // TabControl.setCurrentTab has been called before this,
132 // so the tab is guaranteed to have a webview
133 if (view == null) {
134 Log.e(LOGTAG, "active tab with no webview detected");
135 return;
136 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800137 view.setEmbeddedTitleBar(getTitleBar());
Michael Kolb376b5412010-12-15 11:52:57 -0800138 if (tab.isInVoiceSearchMode()) {
139 showVoiceTitleBar(tab.getVoiceDisplayTitle());
140 } else {
141 revertVoiceTitleBar(tab);
142 }
Michael Kolb376b5412010-12-15 11:52:57 -0800143 tab.getTopWindow().requestFocus();
144 }
145
Michael Kolbb0868f12011-02-14 14:36:13 -0800146 private void attachTitleBar(WebView mainView) {
147 if (mainView != null) {
148 mainView.setEmbeddedTitleBar(null);
149 }
150 WindowManager manager = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
151 // Add the title bar to the window manager so it can receive
152 // touches while the menu is up
153 WindowManager.LayoutParams params =
154 new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
155 ViewGroup.LayoutParams.WRAP_CONTENT,
156 WindowManager.LayoutParams.TYPE_APPLICATION,
157 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
158 PixelFormat.OPAQUE);
159 params.gravity = Gravity.TOP;
160 boolean atTop = mainView.getScrollY() == 0;
161 params.windowAnimations = atTop ? 0 : R.style.TitleBar;
162 manager.addView(mTitleBar, params);
163 super.showTitleBar();
164 }
165
Michael Kolb376b5412010-12-15 11:52:57 -0800166 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800167 void showTitleBar() {
168 if (canShowTitleBar()) {
Michael Kolbb0868f12011-02-14 14:36:13 -0800169 WebView mainView = mUiController.getCurrentWebView();
170 attachTitleBar(mainView);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800171 super.showTitleBar();
172 }
Michael Kolb66706532010-12-12 19:50:22 -0800173 }
174
175 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800176 protected void hideTitleBar() {
177 if (isTitleBarShowing()) {
Michael Kolbb0868f12011-02-14 14:36:13 -0800178 WindowManager.LayoutParams params =
179 (WindowManager.LayoutParams) mTitleBar.getLayoutParams();
180 WebView mainView = mUiController.getCurrentWebView();
181 // Although we decided whether or not to animate based on the
182 // current
183 // scroll position, the scroll position may have changed since the
184 // fake title bar was displayed. Make sure it has the appropriate
185 // animation/lack thereof before removing.
186 params.windowAnimations =
187 mainView != null && mainView.getScrollY() == 0 ? 0 : R.style.TitleBar;
188 WindowManager manager =
189 (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
190 manager.updateViewLayout(mTitleBar, params);
191 manager.removeView(mTitleBar);
192 if (mainView != null) {
193 mainView.setEmbeddedTitleBar(mTitleBar);
194 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800195 super.hideTitleBar();
196 }
Michael Kolb66706532010-12-12 19:50:22 -0800197 }
198
199 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800201 return mTitleBar;
202 }
203
204 // active tabs page
205
206 @Override
207 public void showActiveTabsPage() {
208 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
209 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800210 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800211 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
212 mActiveTabsPage.requestFocus();
213 }
214
215 /**
216 * Remove the active tabs page.
217 */
218 @Override
219 public void removeActiveTabsPage() {
220 mContentView.removeView(mActiveTabsPage);
221 mTitleBar.setVisibility(View.VISIBLE);
222 mActiveTabsPage = null;
223 }
224
225 @Override
226 public boolean showsWeb() {
227 return super.showsWeb() && mActiveTabsPage == null;
228 }
229
230 // menu handling callbacks
231
232 @Override
233 public void onOptionsMenuOpened() {
234 mOptionsMenuOpen = true;
235 // options menu opened, show fake title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800236 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800237 }
238
239 @Override
240 public void onExtendedMenuOpened() {
241 // Switching the menu to expanded view, so hide the
242 // title bar.
243 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800244 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800245 }
246
247 @Override
248 public void onOptionsMenuClosed(boolean inLoad) {
249 mOptionsMenuOpen = false;
250 if (!inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800251 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800252 }
253 }
254
255 @Override
256 public void onExtendedMenuClosed(boolean inLoad) {
257 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800258 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800259 }
260
261 @Override
262 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800263 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800264 }
265
266 @Override
267 public void onContextMenuClosed(Menu menu, boolean inLoad) {
268 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800269 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800270 }
271 }
272
273 // action mode callbacks
274
275 @Override
276 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800277 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800278 }
279
Michael Kolba4183062011-01-16 10:43:21 -0800280 @Override
281 public boolean dispatchKey(int code, KeyEvent event) {
282 return false;
283 }
284
Michael Kolb66706532010-12-12 19:50:22 -0800285}