blob: 7463ca5dc92c013446cf56ae68f0d13d1aca6b75 [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 Kolb376b5412010-12-15 11:52:57 -080020import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080021import android.view.ActionMode;
22import android.view.Gravity;
Michael Kolba4183062011-01-16 10:43:21 -080023import android.view.KeyEvent;
Michael Kolb66706532010-12-12 19:50:22 -080024import android.view.Menu;
25import android.view.View;
Michael Kolb66706532010-12-12 19:50:22 -080026import android.webkit.WebView;
27
28/**
29 * Ui for regular phone screen sizes
30 */
31public class PhoneUi extends BaseUi {
32
33 private static final String LOGTAG = "PhoneUi";
34
35 private TitleBar mTitleBar;
Michael Kolb66706532010-12-12 19:50:22 -080036 private ActiveTabsPage mActiveTabsPage;
37
38 boolean mExtendedMenuOpen;
39 boolean mOptionsMenuOpen;
40
41 /**
42 * @param browser
43 * @param controller
44 */
45 public PhoneUi(Activity browser, UiController controller) {
46 super(browser, controller);
47 mTitleBar = new TitleBar(mActivity, mUiController);
48 // mTitleBar will be always be shown in the fully loaded mode on
49 // phone
50 mTitleBar.setProgress(100);
Michael Kolb66706532010-12-12 19:50:22 -080051
52 }
53
54 // webview factory
55
56 @Override
57 public WebView createWebView(boolean privateBrowsing) {
58 // Create a new WebView
59 WebView w = new WebView(mActivity, null,
60 android.R.attr.webViewStyle, privateBrowsing);
61 initWebViewSettings(w);
62 return w;
63 }
64
65 @Override
66 public WebView createSubWebView(boolean privateBrowsing) {
67 WebView web = createWebView(privateBrowsing);
68 return web;
69 }
70
71 // lifecycle
72
73 @Override
74 public void onPause() {
75 // FIXME: This removes the active tabs page and resets the menu to
76 // MAIN_MENU. A better solution might be to do this work in onNewIntent
77 // but then we would need to save it in onSaveInstanceState and restore
78 // it in onCreate/onRestoreInstanceState
79 if (mActiveTabsPage != null) {
80 mUiController.removeActiveTabsPage(true);
81 }
82 super.onPause();
83 }
84
85 @Override
86 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -080087 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -080088 }
89
90 @Override
91 public boolean onBackKey() {
92 if (mActiveTabsPage != null) {
93 // if tab page is showing, hide it
94 mUiController.removeActiveTabsPage(true);
95 return true;
96 }
97 return super.onBackKey();
98 }
99
100 @Override
John Reck30c714c2010-12-16 17:30:34 -0800101 public void onProgressChanged(Tab tab) {
Michael Kolb66706532010-12-12 19:50:22 -0800102 if (tab.inForeground()) {
John Reck30c714c2010-12-16 17:30:34 -0800103 int progress = tab.getLoadProgress();
Michael Kolb7cdc4902011-02-03 17:54:40 -0800104 mTitleBar.setProgress(progress);
Michael Kolb66706532010-12-12 19:50:22 -0800105 if (progress == 100) {
106 if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800107 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800108 }
109 } else {
110 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800111 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800112 }
113 }
114 }
115 }
116
117 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800118 public void setActiveTab(Tab tab) {
119 super.setActiveTab(tab);
120 WebView view = tab.getWebView();
121 // TabControl.setCurrentTab has been called before this,
122 // so the tab is guaranteed to have a webview
123 if (view == null) {
124 Log.e(LOGTAG, "active tab with no webview detected");
125 return;
126 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800127 view.setEmbeddedTitleBar(getTitleBar());
Michael Kolb376b5412010-12-15 11:52:57 -0800128 if (tab.isInVoiceSearchMode()) {
129 showVoiceTitleBar(tab.getVoiceDisplayTitle());
130 } else {
131 revertVoiceTitleBar(tab);
132 }
Michael Kolb376b5412010-12-15 11:52:57 -0800133 tab.getTopWindow().requestFocus();
134 }
135
136 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800137 void showTitleBar() {
138 if (canShowTitleBar()) {
139 setTitleGravity(Gravity.TOP);
140 super.showTitleBar();
141 }
Michael Kolb66706532010-12-12 19:50:22 -0800142 }
143
144 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800145 protected void hideTitleBar() {
146 if (isTitleBarShowing()) {
147 setTitleGravity(Gravity.NO_GRAVITY);
148 super.hideTitleBar();
149 }
Michael Kolb66706532010-12-12 19:50:22 -0800150 }
151
152 @Override
Michael Kolb7cdc4902011-02-03 17:54:40 -0800153 protected TitleBarBase getTitleBar() {
Michael Kolb66706532010-12-12 19:50:22 -0800154 return mTitleBar;
155 }
156
157 // active tabs page
158
159 @Override
160 public void showActiveTabsPage() {
161 mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
162 mTitleBar.setVisibility(View.GONE);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800163 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800164 mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
165 mActiveTabsPage.requestFocus();
166 }
167
168 /**
169 * Remove the active tabs page.
170 */
171 @Override
172 public void removeActiveTabsPage() {
173 mContentView.removeView(mActiveTabsPage);
174 mTitleBar.setVisibility(View.VISIBLE);
175 mActiveTabsPage = null;
176 }
177
178 @Override
179 public boolean showsWeb() {
180 return super.showsWeb() && mActiveTabsPage == null;
181 }
182
183 // menu handling callbacks
184
185 @Override
186 public void onOptionsMenuOpened() {
187 mOptionsMenuOpen = true;
188 // options menu opened, show fake title bar
Michael Kolb7cdc4902011-02-03 17:54:40 -0800189 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800190 }
191
192 @Override
193 public void onExtendedMenuOpened() {
194 // Switching the menu to expanded view, so hide the
195 // title bar.
196 mExtendedMenuOpen = true;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800197 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800198 }
199
200 @Override
201 public void onOptionsMenuClosed(boolean inLoad) {
202 mOptionsMenuOpen = false;
203 if (!inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800204 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800205 }
206 }
207
208 @Override
209 public void onExtendedMenuClosed(boolean inLoad) {
210 mExtendedMenuOpen = false;
Michael Kolb7cdc4902011-02-03 17:54:40 -0800211 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800212 }
213
214 @Override
215 public void onContextMenuCreated(Menu menu) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800216 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800217 }
218
219 @Override
220 public void onContextMenuClosed(Menu menu, boolean inLoad) {
221 if (inLoad) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800222 showTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800223 }
224 }
225
226 // action mode callbacks
227
228 @Override
229 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800230 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800231 }
232
Michael Kolba4183062011-01-16 10:43:21 -0800233 @Override
234 public boolean dispatchKey(int code, KeyEvent event) {
235 return false;
236 }
237
Michael Kolb66706532010-12-12 19:50:22 -0800238}