blob: cfe735129afc698ff90c2bdabd6f9ce7a93c4947 [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.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;
John Reckd29abda2011-02-14 17:51:40 -080041 private TouchProxy mTitleOverlay;
Michael Kolb66706532010-12-12 19:50:22 -080042
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);
John Reck285ef042011-02-11 15:44:17 -080056 mActivity.getActionBar().hide();
57 }
Michael Kolb66706532010-12-12 19:50:22 -080058
John Reck285ef042011-02-11 15:44:17 -080059 @Override
60 public void hideComboView() {
61 super.hideComboView();
62 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080063 }
64
65 // webview factory
66
67 @Override
68 public WebView createWebView(boolean privateBrowsing) {
69 // Create a new WebView
70 WebView w = new WebView(mActivity, null,
71 android.R.attr.webViewStyle, privateBrowsing);
72 initWebViewSettings(w);
73 return w;
74 }
75
76 @Override
77 public WebView createSubWebView(boolean privateBrowsing) {
78 WebView web = createWebView(privateBrowsing);
79 return web;
80 }
81
82 // lifecycle
83
84 @Override
85 public void onPause() {
86 // FIXME: This removes the active tabs page and resets the menu to
87 // MAIN_MENU. A better solution might be to do this work in onNewIntent
88 // but then we would need to save it in onSaveInstanceState and restore
89 // it in onCreate/onRestoreInstanceState
90 if (mActiveTabsPage != null) {
91 mUiController.removeActiveTabsPage(true);
92 }
93 super.onPause();
94 }
95
96 @Override
97 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080098 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080099 }
100
101 @Override
102 public boolean onBackKey() {
103 if (mActiveTabsPage != null) {
104 // if tab page is showing, hide it
105 mUiController.removeActiveTabsPage(true);
106 return true;
107 }
108 return super.onBackKey();
109 }
110
111 @Override
John Reck30c714c2010-12-16 17:30:34 -0800112 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800113 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800114 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800115 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800116 if (progress == 100) {
117 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800118 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800119 }
120 } else {
121 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800122 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800123 }
124 }
125 }
126 }
127
128 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800129 public void setActiveTab(Tab tab) {
130 super.setActiveTab(tab);
131 WebView view = tab.getWebView();
132 // TabControl.setCurrentTab has been called before this,
133 // so the tab is guaranteed to have a webview
134 if (view == null) {
135 Log.e(LOGTAG, "active tab with no webview detected");
136 return;
137 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800138 view.setEmbeddedTitleBar(getTitleBar());
Michael Kolb376b5412010-12-15 11:52:57 -0800139 if (tab.isInVoiceSearchMode()) {
140 showVoiceTitleBar(tab.getVoiceDisplayTitle());
141 } else {
142 revertVoiceTitleBar(tab);
143 }
Michael Kolb376b5412010-12-15 11:52:57 -0800144 tab.getTopWindow().requestFocus();
145 }
146
147 @Override
John Reckd29abda2011-02-14 17:51:40 -0800148 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800149 if (canShowTitleBar()) {
John Reck539937b2011-02-14 18:18:39 -0800150 setTitleGravity(Gravity.TOP);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800151 super.showTitleBar();
152 }
Michael Kolb66706532010-12-12 19:50:22 -0800153 }
154
155 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800156 protected void hideTitleBar() {
157 if (isTitleBarShowing()) {
John Reck539937b2011-02-14 18:18:39 -0800158 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800159 super.hideTitleBar();
160 }
Michael Kolb66706532010-12-12 19:50:22 -0800161 }
162
163 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800164 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800165 return mTitleBar;
166 }
167
168 // active tabs page
169
170 @Override
171 public void showActiveTabsPage() {
172 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
173 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800174 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800175 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
176 mActiveTabsPage.requestFocus();
177 }
178
179 /**
180 * Remove the active tabs page.
181 */
182 @Override
183 public void removeActiveTabsPage() {
184 mContentView.removeView(mActiveTabsPage);
185 mTitleBar.setVisibility(View.VISIBLE);
186 mActiveTabsPage = null;
187 }
188
189 @Override
190 public boolean showsWeb() {
191 return super.showsWeb() && mActiveTabsPage == null;
192 }
193
194 // menu handling callbacks
195
196 @Override
197 public void onOptionsMenuOpened() {
198 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800199 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800200 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800201 if (mTitleOverlay == null) {
202 // This assumes that getTitleBar always returns the same View
203 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
204 }
205 mActivity.getWindowManager().addView(mTitleOverlay,
206 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800207 }
208
209 @Override
210 public void onExtendedMenuOpened() {
211 // Switching the menu to expanded view, so hide the
212 // title bar.
213 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800214 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800215 }
216
217 @Override
218 public void onOptionsMenuClosed(boolean inLoad) {
219 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800220 mActivity.getWindowManager().removeView(mTitleOverlay);
221 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800222 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800223 }
224 }
225
226 @Override
227 public void onExtendedMenuClosed(boolean inLoad) {
228 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800229 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231
232 @Override
233 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800234 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800235 }
236
237 @Override
238 public void onContextMenuClosed(Menu menu, boolean inLoad) {
239 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800240 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242 }
243
244 // action mode callbacks
245
246 @Override
247 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800248 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800249 }
250
Michael Kolba4183062011-01-16 10:43:21 -0800251 @Override
252 public boolean dispatchKey(int code, KeyEvent event) {
253 return false;
254 }
255
John Reckd29abda2011-02-14 17:51:40 -0800256 static class TouchProxy extends View {
257
258 View mTarget;
259
260 TouchProxy(Context context, View target) {
261 super(context);
262 mTarget = target;
263 }
264
265 @Override
266 public boolean dispatchTouchEvent(MotionEvent event) {
267 return mTarget.dispatchTouchEvent(event);
268 }
269
270 WindowManager.LayoutParams getWindowLayoutParams() {
271 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800272 new WindowManager.LayoutParams(
273 mTarget.getWidth(),
274 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800275 WindowManager.LayoutParams.TYPE_APPLICATION,
276 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
277 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800278 params.gravity = Gravity.TOP | Gravity.LEFT;
279 params.y = mTarget.getTop();
280 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800281 return params;
282 }
283 }
Michael Kolb66706532010-12-12 19:50:22 -0800284}