blob: efd6c433d016f0c084c6d3e8a49ff402d08fddfe [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;
John Reck88a42b72011-03-21 16:30:12 -070021import android.graphics.Bitmap;
Michael Kolbb0868f12011-02-14 14:36:13 -080022import android.graphics.PixelFormat;
John Reck6cda7b12011-03-18 15:57:13 -070023import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080024import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080025import android.view.ActionMode;
John Reck88a42b72011-03-21 16:30:12 -070026import android.view.Display;
Michael Kolb66706532010-12-12 19:50:22 -080027import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080028import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080029import android.view.Menu;
John Reckd29abda2011-02-14 17:51:40 -080030import android.view.MotionEvent;
Michael Kolb66706532010-12-12 19:50:22 -080031import android.view.View;
Michael Kolbb0868f12011-02-14 14:36:13 -080032import android.view.WindowManager;
Michael Kolb66706532010-12-12 19:50:22 -080033import android.webkit.WebView;
34
35/**
36 * Ui for regular phone screen sizes
37 */
38public class PhoneUi extends BaseUi {
39
40 private static final String LOGTAG = "PhoneUi";
41
Michael Kolb11d19782011-03-20 10:17:40 -070042 private TitleBarPhone mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080043 private ActiveTabsPage mActiveTabsPage;
John Reckd29abda2011-02-14 17:51:40 -080044 private TouchProxy mTitleOverlay;
Michael Kolb66706532010-12-12 19:50:22 -080045
46 boolean mExtendedMenuOpen;
47 boolean mOptionsMenuOpen;
48
49 /**
50 * @param browser
51 * @param controller
52 */
53 public PhoneUi(Activity browser, UiController controller) {
54 super(browser, controller);
Michael Kolb11d19782011-03-20 10:17:40 -070055 mTitleBar = new TitleBarPhone(mActivity, mUiController, this);
Michael Kolb66706532010-12-12 19:50:22 -080056 // mTitleBar will be always be shown in the fully loaded mode on
57 // phone
58 mTitleBar.setProgress(100);
John Reck285ef042011-02-11 15:44:17 -080059 mActivity.getActionBar().hide();
60 }
Michael Kolb66706532010-12-12 19:50:22 -080061
John Reck285ef042011-02-11 15:44:17 -080062 @Override
63 public void hideComboView() {
64 super.hideComboView();
65 mActivity.getActionBar().hide();
Michael Kolb66706532010-12-12 19:50:22 -080066 }
67
Michael Kolb66706532010-12-12 19:50:22 -080068 // lifecycle
69
70 @Override
71 public void onPause() {
72 // FIXME: This removes the active tabs page and resets the menu to
73 // MAIN_MENU. A better solution might be to do this work in onNewIntent
74 // but then we would need to save it in onSaveInstanceState and restore
75 // it in onCreate/onRestoreInstanceState
76 if (mActiveTabsPage != null) {
77 mUiController.removeActiveTabsPage(true);
78 }
79 super.onPause();
80 }
81
82 @Override
83 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080084 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080085 }
86
87 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080088 public void editUrl(boolean clearInput) {
89 String url = getActiveTab().getUrl();
90 mUiController.startSearch(url);
91 }
92
93 @Override
Michael Kolb66706532010-12-12 19:50:22 -080094 public boolean onBackKey() {
95 if (mActiveTabsPage != null) {
96 // if tab page is showing, hide it
97 mUiController.removeActiveTabsPage(true);
98 return true;
99 }
100 return super.onBackKey();
101 }
102
103 @Override
John Reck30c714c2010-12-16 17:30:34 -0800104 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800105 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800106 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800107 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800108 if (progress == 100) {
109 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800110 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800111 }
112 } else {
113 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800114 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800115 }
116 }
117 }
118 }
119
120 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800121 public void setActiveTab(Tab tab) {
John Reck88a42b72011-03-21 16:30:12 -0700122 captureTab(mActiveTab);
Michael Kolb376b5412010-12-15 11:52:57 -0800123 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 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800131 view.setEmbeddedTitleBar(getTitleBar());
Michael Kolb376b5412010-12-15 11:52:57 -0800132 if (tab.isInVoiceSearchMode()) {
Michael Kolb11d19782011-03-20 10:17:40 -0700133 showVoiceTitleBar(tab.getVoiceDisplayTitle(),
134 tab.getVoiceSearchResults());
Michael Kolb376b5412010-12-15 11:52:57 -0800135 } else {
136 revertVoiceTitleBar(tab);
137 }
Michael Kolb376b5412010-12-15 11:52:57 -0800138 tab.getTopWindow().requestFocus();
139 }
140
John Reck88a42b72011-03-21 16:30:12 -0700141 public void captureTab(final Tab tab) {
142 if (tab == null) return;
143 if (tab.getWebView() == null) return;
144
145 Display display = mActivity.getWindowManager().getDefaultDisplay();
146 float height = mActivity.getResources()
147 .getDimension(R.dimen.tab_view_thumbnail_height);
148 Bitmap sshot = Controller.createScreenshot(tab,
149 display.getWidth(), (int) height);
150 tab.setScreenshot(sshot);
151 }
152
Michael Kolb376b5412010-12-15 11:52:57 -0800153 @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() {
John Reck88a42b72011-03-21 16:30:12 -0700178 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800179 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
180 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800181 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800182 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
183 mActiveTabsPage.requestFocus();
184 }
185
186 /**
187 * Remove the active tabs page.
188 */
189 @Override
190 public void removeActiveTabsPage() {
191 mContentView.removeView(mActiveTabsPage);
192 mTitleBar.setVisibility(View.VISIBLE);
193 mActiveTabsPage = null;
194 }
195
196 @Override
197 public boolean showsWeb() {
198 return super.showsWeb() && mActiveTabsPage == null;
199 }
200
201 // menu handling callbacks
202
203 @Override
204 public void onOptionsMenuOpened() {
205 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800206 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800207 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800208 if (mTitleOverlay == null) {
209 // This assumes that getTitleBar always returns the same View
210 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
211 }
212 mActivity.getWindowManager().addView(mTitleOverlay,
213 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800214 }
215
216 @Override
217 public void onExtendedMenuOpened() {
218 // Switching the menu to expanded view, so hide the
219 // title bar.
220 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800221 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800222 }
223
224 @Override
225 public void onOptionsMenuClosed(boolean inLoad) {
226 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800227 mActivity.getWindowManager().removeView(mTitleOverlay);
228 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800229 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231 }
232
233 @Override
234 public void onExtendedMenuClosed(boolean inLoad) {
235 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800236 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800237 }
238
239 @Override
240 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800241 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800242 }
243
244 @Override
245 public void onContextMenuClosed(Menu menu, boolean inLoad) {
246 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800247 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800248 }
249 }
250
251 // action mode callbacks
252
253 @Override
254 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800255 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800256 }
257
Michael Kolba4183062011-01-16 10:43:21 -0800258 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700259 public void onActionModeFinished(boolean inLoad) {
260 // TODO: Remove once b/4136071 is fixed
261 new Handler().post(new Runnable() {
262 @Override
263 public void run() {
264 mActivity.getActionBar().hide();
265 }
266 });
267 }
268
269 @Override
Michael Kolba4183062011-01-16 10:43:21 -0800270 public boolean dispatchKey(int code, KeyEvent event) {
271 return false;
272 }
273
John Reckd29abda2011-02-14 17:51:40 -0800274 static class TouchProxy extends View {
275
276 View mTarget;
277
278 TouchProxy(Context context, View target) {
279 super(context);
280 mTarget = target;
281 }
282
283 @Override
284 public boolean dispatchTouchEvent(MotionEvent event) {
285 return mTarget.dispatchTouchEvent(event);
286 }
287
288 WindowManager.LayoutParams getWindowLayoutParams() {
289 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800290 new WindowManager.LayoutParams(
291 mTarget.getWidth(),
292 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800293 WindowManager.LayoutParams.TYPE_APPLICATION,
294 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
295 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800296 params.gravity = Gravity.TOP | Gravity.LEFT;
297 params.y = mTarget.getTop();
298 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800299 return params;
300 }
301 }
Michael Kolb11d19782011-03-20 10:17:40 -0700302
Michael Kolb66706532010-12-12 19:50:22 -0800303}