blob: dd68e85b5884e3010508e11f093cd132463fed63 [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;
John Reck6cda7b12011-03-18 15:57:13 -070022import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080023import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080024import android.view.ActionMode;
25import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080026import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080027import android.view.Menu;
John Reckd29abda2011-02-14 17:51:40 -080028import android.view.MotionEvent;
Michael Kolb66706532010-12-12 19:50:22 -080029import android.view.View;
Michael Kolbb0868f12011-02-14 14:36:13 -080030import 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);
John Reck92026732011-02-15 10:12:30 -080053 mTitleBar = new TitleBar(mActivity, mUiController, this);
Michael Kolb66706532010-12-12 19:50:22 -080054 // 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
Michael Kolb66706532010-12-12 19:50:22 -080066 // lifecycle
67
68 @Override
69 public void onPause() {
70 // FIXME: This removes the active tabs page and resets the menu to
71 // MAIN_MENU. A better solution might be to do this work in onNewIntent
72 // but then we would need to save it in onSaveInstanceState and restore
73 // it in onCreate/onRestoreInstanceState
74 if (mActiveTabsPage != null) {
75 mUiController.removeActiveTabsPage(true);
76 }
77 super.onPause();
78 }
79
80 @Override
81 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080082 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080083 }
84
85 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080086 public void editUrl(boolean clearInput) {
87 String url = getActiveTab().getUrl();
88 mUiController.startSearch(url);
89 }
90
91 @Override
Michael Kolb66706532010-12-12 19:50:22 -080092 public boolean onBackKey() {
93 if (mActiveTabsPage != null) {
94 // if tab page is showing, hide it
95 mUiController.removeActiveTabsPage(true);
96 return true;
97 }
98 return super.onBackKey();
99 }
100
101 @Override
John Reck30c714c2010-12-16 17:30:34 -0800102 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800103 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800104 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800105 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800106 if (progress == 100) {
107 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800108 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800109 }
110 } else {
111 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800112 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800113 }
114 }
115 }
116 }
117
118 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800119 public void setActiveTab(Tab tab) {
120 super.setActiveTab(tab);
121 WebView view = tab.getWebView();
122 // TabControl.setCurrentTab has been called before this,
123 // so the tab is guaranteed to have a webview
124 if (view == null) {
125 Log.e(LOGTAG, "active tab with no webview detected");
126 return;
127 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800128 view.setEmbeddedTitleBar(getTitleBar());
Michael Kolb376b5412010-12-15 11:52:57 -0800129 if (tab.isInVoiceSearchMode()) {
130 showVoiceTitleBar(tab.getVoiceDisplayTitle());
131 } else {
132 revertVoiceTitleBar(tab);
133 }
Michael Kolb376b5412010-12-15 11:52:57 -0800134 tab.getTopWindow().requestFocus();
135 }
136
137 @Override
John Reckd29abda2011-02-14 17:51:40 -0800138 protected void showTitleBar() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800139 if (canShowTitleBar()) {
John Reck539937b2011-02-14 18:18:39 -0800140 setTitleGravity(Gravity.TOP);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800141 super.showTitleBar();
142 }
Michael Kolb66706532010-12-12 19:50:22 -0800143 }
144
145 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800146 protected void hideTitleBar() {
147 if (isTitleBarShowing()) {
John Reck539937b2011-02-14 18:18:39 -0800148 setTitleGravity(Gravity.NO_GRAVITY);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800149 super.hideTitleBar();
150 }
Michael Kolb66706532010-12-12 19:50:22 -0800151 }
152
153 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800154 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800155 return mTitleBar;
156 }
157
158 // active tabs page
159
160 @Override
161 public void showActiveTabsPage() {
162 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
163 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800164 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800165 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
166 mActiveTabsPage.requestFocus();
167 }
168
169 /**
170 * Remove the active tabs page.
171 */
172 @Override
173 public void removeActiveTabsPage() {
174 mContentView.removeView(mActiveTabsPage);
175 mTitleBar.setVisibility(View.VISIBLE);
176 mActiveTabsPage = null;
177 }
178
179 @Override
180 public boolean showsWeb() {
181 return super.showsWeb() && mActiveTabsPage == null;
182 }
183
184 // menu handling callbacks
185
186 @Override
187 public void onOptionsMenuOpened() {
188 mOptionsMenuOpen = true;
John Reckd29abda2011-02-14 17:51:40 -0800189 // options menu opened, show title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800190 showTitleBar();
John Reckd29abda2011-02-14 17:51:40 -0800191 if (mTitleOverlay == null) {
192 // This assumes that getTitleBar always returns the same View
193 mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
194 }
195 mActivity.getWindowManager().addView(mTitleOverlay,
196 mTitleOverlay.getWindowLayoutParams());
Michael Kolb66706532010-12-12 19:50:22 -0800197 }
198
199 @Override
200 public void onExtendedMenuOpened() {
201 // Switching the menu to expanded view, so hide the
202 // title bar.
203 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800204 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800205 }
206
207 @Override
208 public void onOptionsMenuClosed(boolean inLoad) {
209 mOptionsMenuOpen = false;
John Reckd29abda2011-02-14 17:51:40 -0800210 mActivity.getWindowManager().removeView(mTitleOverlay);
211 if (!inLoad && !getTitleBar().hasFocus()) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800212 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800213 }
214 }
215
216 @Override
217 public void onExtendedMenuClosed(boolean inLoad) {
218 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800219 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800220 }
221
222 @Override
223 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800224 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800225 }
226
227 @Override
228 public void onContextMenuClosed(Menu menu, boolean inLoad) {
229 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800230 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800231 }
232 }
233
234 // action mode callbacks
235
236 @Override
237 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800238 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800239 }
240
Michael Kolba4183062011-01-16 10:43:21 -0800241 @Override
John Reck6cda7b12011-03-18 15:57:13 -0700242 public void onActionModeFinished(boolean inLoad) {
243 // TODO: Remove once b/4136071 is fixed
244 new Handler().post(new Runnable() {
245 @Override
246 public void run() {
247 mActivity.getActionBar().hide();
248 }
249 });
250 }
251
252 @Override
Michael Kolba4183062011-01-16 10:43:21 -0800253 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 =
John Reck539937b2011-02-14 18:18:39 -0800273 new WindowManager.LayoutParams(
274 mTarget.getWidth(),
275 mTarget.getHeight(),
John Reckd29abda2011-02-14 17:51:40 -0800276 WindowManager.LayoutParams.TYPE_APPLICATION,
277 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
278 PixelFormat.TRANSPARENT);
John Reck539937b2011-02-14 18:18:39 -0800279 params.gravity = Gravity.TOP | Gravity.LEFT;
280 params.y = mTarget.getTop();
281 params.x = mTarget.getLeft();
John Reckd29abda2011-02-14 17:51:40 -0800282 return params;
283 }
284 }
Michael Kolb66706532010-12-12 19:50:22 -0800285}