blob: f1939e431cf061c729f67fc90516f00e7e712bed [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);
John Reck92026732011-02-15 10:12:30 -080052 mTitleBar = new TitleBar(mActivity, mUiController, this);
Michael Kolb66706532010-12-12 19:50:22 -080053 // 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
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800102 public void editUrl(boolean clearInput) {
103 String url = getActiveTab().getUrl();
104 mUiController.startSearch(url);
105 }
106
107 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800108 public boolean onBackKey() {
109 if (mActiveTabsPage != null) {
110 // if tab page is showing, hide it
111 mUiController.removeActiveTabsPage(true);
112 return true;
113 }
114 return super.onBackKey();
115 }
116
117 @Override
John Reck30c714c2010-12-16 17:30:34 -0800118 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800119 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800120 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800121 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800122 if (progress == 100) {
123 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800124 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800125 }
126 } else {
127 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800128 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800129 }
130 }
131 }
132 }
133
134 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800135 public void setActiveTab(Tab tab) {
136 super.setActiveTab(tab);
137 WebView view = tab.getWebView();
138 // TabControl.setCurrentTab has been called before this,
139 // so the tab is guaranteed to have a webview
140 if (view == null) {
141 Log.e(LOGTAG, "active tab with no webview detected");
142 return;
143 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800144 view.setEmbeddedTitleBar(getTitleBar());
Michael Kolb376b5412010-12-15 11:52:57 -0800145 if (tab.isInVoiceSearchMode()) {
146 showVoiceTitleBar(tab.getVoiceDisplayTitle());
147 } else {
148 revertVoiceTitleBar(tab);
149 }
Michael Kolb376b5412010-12-15 11:52:57 -0800150 tab.getTopWindow().requestFocus();
151 }
152
153 @Override
John Reckd29abda2011-02-14 17:51:40 -0800154 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800155 if (canShowTitleBar()) {
John Reck539937b2011-02-14 18:18:39 -0800156 setTitleGravity(Gravity.TOP);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800157 super.showTitleBar();
158 }
Michael Kolb66706532010-12-12 19:50:22 -0800159 }
160
161 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800162 protected void hideTitleBar() {
163 if (isTitleBarShowing()) {
John Reck539937b2011-02-14 18:18:39 -0800164 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800165 super.hideTitleBar();
166 }
Michael Kolb66706532010-12-12 19:50:22 -0800167 }
168
169 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800170 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800171 return mTitleBar;
172 }
173
174 // active tabs page
175
176 @Override
177 public void showActiveTabsPage() {
178 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
179 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800180 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800181 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
182 mActiveTabsPage.requestFocus();
183 }
184
185 /**
186 * Remove the active tabs page.
187 */
188 @Override
189 public void removeActiveTabsPage() {
190 mContentView.removeView(mActiveTabsPage);
191 mTitleBar.setVisibility(View.VISIBLE);
192 mActiveTabsPage = null;
193 }
194
195 @Override
196 public boolean showsWeb() {
197 return super.showsWeb() && mActiveTabsPage == null;
198 }
199
200 // menu handling callbacks
201
202 @Override
203 public void onOptionsMenuOpened() {
204 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800205 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800206 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800207 if (mTitleOverlay == null) {
208 // This assumes that getTitleBar always returns the same View
209 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
210 }
211 mActivity.getWindowManager().addView(mTitleOverlay,
212 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800213 }
214
215 @Override
216 public void onExtendedMenuOpened() {
217 // Switching the menu to expanded view, so hide the
218 // title bar.
219 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800220 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800221 }
222
223 @Override
224 public void onOptionsMenuClosed(boolean inLoad) {
225 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800226 mActivity.getWindowManager().removeView(mTitleOverlay);
227 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800228 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800229 }
230 }
231
232 @Override
233 public void onExtendedMenuClosed(boolean inLoad) {
234 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800235 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800236 }
237
238 @Override
239 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800240 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800241 }
242
243 @Override
244 public void onContextMenuClosed(Menu menu, boolean inLoad) {
245 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800246 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800247 }
248 }
249
250 // action mode callbacks
251
252 @Override
253 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800254 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800255 }
256
Michael Kolba4183062011-01-16 10:43:21 -0800257 @Override
258 public boolean dispatchKey(int code, KeyEvent event) {
259 return false;
260 }
261
John Reckd29abda2011-02-14 17:51:40 -0800262 static class TouchProxy extends View {
263
264 View mTarget;
265
266 TouchProxy(Context context, View target) {
267 super(context);
268 mTarget = target;
269 }
270
271 @Override
272 public boolean dispatchTouchEvent(MotionEvent event) {
273 return mTarget.dispatchTouchEvent(event);
274 }
275
276 WindowManager.LayoutParams getWindowLayoutParams() {
277 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800278 new WindowManager.LayoutParams(
279 mTarget.getWidth(),
280 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800281 WindowManager.LayoutParams.TYPE_APPLICATION,
282 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
283 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800284 params.gravity = Gravity.TOP | Gravity.LEFT;
285 params.y = mTarget.getTop();
286 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800287 return params;
288 }
289 }
Michael Kolb66706532010-12-12 19:50:22 -0800290}