blob: a9ae25a016da680f2b281d4628a8d8d45e15fc75 [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;
John Reckd29abda2011-02-14 17:51:40 -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;
John Reckd29abda2011-02-14 17:51:40 -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
148 @Override
John Reckd29abda2011-02-14 17:51:40 -0800149 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800150 if (canShowTitleBar()) {
151 setTitleGravity(Gravity.TOP);
152 super.showTitleBar();
153 }
Michael Kolb66706532010-12-12 19:50:22 -0800154 }
155
156 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800157 protected void hideTitleBar() {
158 if (isTitleBarShowing()) {
159 setTitleGravity(Gravity.NO_GRAVITY);
160 super.hideTitleBar();
161 }
Michael Kolb66706532010-12-12 19:50:22 -0800162 }
163
164 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800165 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800166 return mTitleBar;
167 }
168
169 // active tabs page
170
171 @Override
172 public void showActiveTabsPage() {
173 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
174 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800175 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800176 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
177 mActiveTabsPage.requestFocus();
178 }
179
180 /**
181 * Remove the active tabs page.
182 */
183 @Override
184 public void removeActiveTabsPage() {
185 mContentView.removeView(mActiveTabsPage);
186 mTitleBar.setVisibility(View.VISIBLE);
187 mActiveTabsPage = null;
188 }
189
190 @Override
191 public boolean showsWeb() {
192 return super.showsWeb() && mActiveTabsPage == null;
193 }
194
195 // menu handling callbacks
196
197 @Override
198 public void onOptionsMenuOpened() {
199 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800200 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800201 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800202 if (mTitleOverlay == null) {
203 // This assumes that getTitleBar always returns the same View
204 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
205 }
206 mActivity.getWindowManager().addView(mTitleOverlay,
207 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800208 }
209
210 @Override
211 public void onExtendedMenuOpened() {
212 // Switching the menu to expanded view, so hide the
213 // title bar.
214 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800215 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800216 }
217
218 @Override
219 public void onOptionsMenuClosed(boolean inLoad) {
220 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800221 mActivity.getWindowManager().removeView(mTitleOverlay);
222 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800223 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800224 }
225 }
226
227 @Override
228 public void onExtendedMenuClosed(boolean inLoad) {
229 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800230 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800231 }
232
233 @Override
234 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
238 @Override
239 public void onContextMenuClosed(Menu menu, boolean inLoad) {
240 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800241 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800242 }
243 }
244
245 // action mode callbacks
246
247 @Override
248 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800249 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800250 }
251
Michael Kolba4183062011-01-16 10:43:21 -0800252 @Override
253 public boolean dispatchKey(int code, KeyEvent event) {
254 return false;
255 }
256
John Reckd29abda2011-02-14 17:51:40 -0800257 static class TouchProxy extends View {
258
259 View mTarget;
260
261 TouchProxy(Context context, View target) {
262 super(context);
263 mTarget = target;
264 }
265
266 @Override
267 public boolean dispatchTouchEvent(MotionEvent event) {
268 return mTarget.dispatchTouchEvent(event);
269 }
270
271 WindowManager.LayoutParams getWindowLayoutParams() {
272 WindowManager.LayoutParams params =
273 new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
274 ViewGroup.LayoutParams.WRAP_CONTENT,
275 WindowManager.LayoutParams.TYPE_APPLICATION,
276 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
277 PixelFormat.TRANSPARENT);
278 params.gravity = Gravity.TOP;
279 return params;
280 }
281 }
Michael Kolb66706532010-12-12 19:50:22 -0800282}