blob: 9c3c912089b38fa4ef1a1ac88fa45eeb86d5a8e4 [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
42 private TitleBar 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);
John Reck92026732011-02-15 10:12:30 -080055 mTitleBar = new TitleBar(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()) {
133 showVoiceTitleBar(tab.getVoiceDisplayTitle());
134 } else {
135 revertVoiceTitleBar(tab);
136 }
Michael Kolb376b5412010-12-15 11:52:57 -0800137 tab.getTopWindow().requestFocus();
138 }
139
John Reck88a42b72011-03-21 16:30:12 -0700140 public void captureTab(final Tab tab) {
141 if (tab == null) return;
142 if (tab.getWebView() == null) return;
143
144 Display display = mActivity.getWindowManager().getDefaultDisplay();
145 float height = mActivity.getResources()
146 .getDimension(R.dimen.tab_view_thumbnail_height);
147 Bitmap sshot = Controller.createScreenshot(tab,
148 display.getWidth(), (int) height);
149 tab.setScreenshot(sshot);
150 }
151
Michael Kolb376b5412010-12-15 11:52:57 -0800152 @Override
John Reckd29abda2011-02-14 17:51:40 -0800153 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800154 if (canShowTitleBar()) {
John Reck539937b2011-02-14 18:18:39 -0800155 setTitleGravity(Gravity.TOP);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800156 super.showTitleBar();
157 }
Michael Kolb66706532010-12-12 19:50:22 -0800158 }
159
160 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800161 protected void hideTitleBar() {
162 if (isTitleBarShowing()) {
John Reck539937b2011-02-14 18:18:39 -0800163 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800164 super.hideTitleBar();
165 }
Michael Kolb66706532010-12-12 19:50:22 -0800166 }
167
168 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800169 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800170 return mTitleBar;
171 }
172
173 // active tabs page
174
175 @Override
176 public void showActiveTabsPage() {
John Reck88a42b72011-03-21 16:30:12 -0700177 captureTab(mActiveTab);
Michael Kolb66706532010-12-12 19:50:22 -0800178 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
John Reck6cda7b12011-03-18 15:57:13 -0700258 public void onActionModeFinished(boolean inLoad) {
259 // TODO: Remove once b/4136071 is fixed
260 new Handler().post(new Runnable() {
261 @Override
262 public void run() {
263 mActivity.getActionBar().hide();
264 }
265 });
266 }
267
268 @Override
Michael Kolba4183062011-01-16 10:43:21 -0800269 public boolean dispatchKey(int code, KeyEvent event) {
270 return false;
271 }
272
John Reckd29abda2011-02-14 17:51:40 -0800273 static class TouchProxy extends View {
274
275 View mTarget;
276
277 TouchProxy(Context context, View target) {
278 super(context);
279 mTarget = target;
280 }
281
282 @Override
283 public boolean dispatchTouchEvent(MotionEvent event) {
284 return mTarget.dispatchTouchEvent(event);
285 }
286
287 WindowManager.LayoutParams getWindowLayoutParams() {
288 WindowManager.LayoutParams params =
John Reck539937b2011-02-14 18:18:39 -0800289 new WindowManager.LayoutParams(
290 mTarget.getWidth(),
291 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800292 WindowManager.LayoutParams.TYPE_APPLICATION,
293 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
294 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800295 params.gravity = Gravity.TOP | Gravity.LEFT;
296 params.y = mTarget.getTop();
297 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800298 return params;
299 }
300 }
Michael Kolb66706532010-12-12 19:50:22 -0800301}