blob: 8dd31d8b4bc52f8431d536a2b6ba9a0ef85a38cb [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolb66706532010-12-12 19:50:22 -080018
Michael Kolb66706532010-12-12 19:50:22 -080019import android.app.ActionBar;
20import android.app.Activity;
John Reck034637c2011-08-11 11:34:44 -070021import android.content.res.Resources;
22import android.graphics.Bitmap;
23import android.graphics.drawable.BitmapDrawable;
24import android.graphics.drawable.Drawable;
25import android.graphics.drawable.LayerDrawable;
26import android.graphics.drawable.PaintDrawable;
Michael Kolbac35bdc2011-01-17 17:06:04 -080027import android.os.Bundle;
Michael Kolbba238702011-03-08 10:40:50 -080028import android.os.Handler;
Michael Kolb376b5412010-12-15 11:52:57 -080029import android.util.Log;
Michael Kolb66706532010-12-12 19:50:22 -080030import android.view.ActionMode;
Michael Kolba4183062011-01-16 10:43:21 -080031import android.view.KeyEvent;
Michael Kolb0d0245f2011-12-05 16:36:05 -080032import android.view.Menu;
33import android.view.MenuItem;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080034import org.codeaurora.swe.WebView;
35
Bijan Amirzada41242f22014-03-21 12:12:18 -070036import com.android.browser.R;
Michael Kolb66706532010-12-12 19:50:22 -080037
38import java.util.List;
39
40/**
41 * Ui for xlarge screen sizes
42 */
Michael Kolbb14ff2f2011-07-01 15:33:56 -070043public class XLargeUi extends BaseUi {
Michael Kolb66706532010-12-12 19:50:22 -080044
45 private static final String LOGTAG = "XLargeUi";
46
John Reck034637c2011-08-11 11:34:44 -070047 private PaintDrawable mFaviconBackground;
48
Michael Kolb376b5412010-12-15 11:52:57 -080049 private ActionBar mActionBar;
Michael Kolb66706532010-12-12 19:50:22 -080050 private TabBar mTabBar;
51
John Reck0f602f32011-07-07 15:38:43 -070052 private NavigationBarTablet mNavBar;
Michael Kolb66706532010-12-12 19:50:22 -080053
Michael Kolbba238702011-03-08 10:40:50 -080054 private Handler mHandler;
Michael Kolb376b5412010-12-15 11:52:57 -080055
Michael Kolb66706532010-12-12 19:50:22 -080056 /**
57 * @param browser
58 * @param controller
59 */
60 public XLargeUi(Activity browser, UiController controller) {
61 super(browser, controller);
Michael Kolbba238702011-03-08 10:40:50 -080062 mHandler = new Handler();
John Reck0f602f32011-07-07 15:38:43 -070063 mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
Michael Kolb66706532010-12-12 19:50:22 -080064 mTabBar = new TabBar(mActivity, mUiController, this);
Michael Kolb376b5412010-12-15 11:52:57 -080065 mActionBar = mActivity.getActionBar();
John Reckb3417f02011-01-14 11:01:05 -080066 setupActionBar();
67 setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
68 }
69
70 private void setupActionBar() {
71 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Michael Kolb376b5412010-12-15 11:52:57 -080072 mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
73 mActionBar.setCustomView(mTabBar);
John Reckb3417f02011-01-14 11:01:05 -080074 }
75
John Reck2bc80422011-06-30 15:11:49 -070076 public void showComboView(ComboViews startWith, Bundle extras) {
77 super.showComboView(startWith, extras);
Michael Kolbac35bdc2011-01-17 17:06:04 -080078 if (mUseQuickControls) {
79 mActionBar.show();
80 }
81 }
82
83 @Override
Michael Kolb0241e752011-07-07 14:58:50 -070084 public void setUseQuickControls(boolean useQuickControls) {
Michael Kolbda580632012-04-16 13:30:28 -070085 super.setUseQuickControls(useQuickControls);
86 checkHideActionBar();
87 if (!useQuickControls) {
88 mActionBar.show();
Michael Kolb376b5412010-12-15 11:52:57 -080089 }
90 mTabBar.setUseQuickControls(mUseQuickControls);
John Reck1cf4b792011-07-26 10:22:22 -070091 // We need to update the tabs with this change
92 for (Tab t : mTabControl.getTabs()) {
93 t.updateShouldCaptureThumbnails();
94 }
Michael Kolb376b5412010-12-15 11:52:57 -080095 }
96
Michael Kolbda580632012-04-16 13:30:28 -070097 private void checkHideActionBar() {
Michael Kolb376b5412010-12-15 11:52:57 -080098 if (mUseQuickControls) {
Michael Kolbeb95db42011-03-03 10:38:40 -080099 mHandler.post(new Runnable() {
100 public void run() {
101 mActionBar.hide();
102 }
103 });
Michael Kolb376b5412010-12-15 11:52:57 -0800104 }
Michael Kolb66706532010-12-12 19:50:22 -0800105 }
106
107 @Override
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000108 public void onResume() {
109 super.onResume();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000110 mNavBar.clearCompletions();
Michael Kolbda580632012-04-16 13:30:28 -0700111 checkHideActionBar();
Narayan Kamath67b8c1b2011-03-09 20:47:52 +0000112 }
113
114 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800115 public void onDestroy() {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800116 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800117 }
118
Michael Kolb66706532010-12-12 19:50:22 -0800119 void stopWebViewScrolling() {
John Reckb9a051b2011-03-18 11:55:48 -0700120 BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800121 if (web != null) {
122 web.stopScroll();
Michael Kolb66706532010-12-12 19:50:22 -0800123 }
124 }
125
Michael Kolb0d0245f2011-12-05 16:36:05 -0800126 @Override
127 public boolean onPrepareOptionsMenu(Menu menu) {
128 MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
129 if (bm != null) {
130 bm.setVisible(false);
131 }
132 return true;
133 }
134
135
Michael Kolb66706532010-12-12 19:50:22 -0800136 // WebView callbacks
137
138 @Override
Michael Kolb66706532010-12-12 19:50:22 -0800139 public void addTab(Tab tab) {
140 mTabBar.onNewTab(tab);
Michael Kolb8814d732011-01-26 11:22:30 -0800141 }
142
143 protected void onAddTabCompleted(Tab tab) {
Michael Kolbda580632012-04-16 13:30:28 -0700144 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800145 }
146
147 @Override
Michael Kolb377ea312011-02-17 14:36:56 -0800148 public void setActiveTab(final Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700149 mTitleBar.cancelTitleBarAnimation(true);
150 mTitleBar.setSkipTitleBarAnimations(true);
John Reck5d43ce82011-06-21 17:16:51 -0700151 super.setActiveTab(tab);
John Reckb9a051b2011-03-18 11:55:48 -0700152 BrowserWebView view = (BrowserWebView) tab.getWebView();
Michael Kolb376b5412010-12-15 11:52:57 -0800153 // TabControl.setCurrentTab has been called before this,
154 // so the tab is guaranteed to have a webview
155 if (view == null) {
156 Log.e(LOGTAG, "active tab with no webview detected");
157 return;
158 }
Michael Kolb66706532010-12-12 19:50:22 -0800159 mTabBar.onSetActiveTab(tab);
Michael Kolb376b5412010-12-15 11:52:57 -0800160 updateLockIconToLatest(tab);
John Reck5d43ce82011-06-21 17:16:51 -0700161 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb66706532010-12-12 19:50:22 -0800162 }
163
164 @Override
165 public void updateTabs(List<Tab> tabs) {
166 mTabBar.updateTabs(tabs);
Michael Kolbda580632012-04-16 13:30:28 -0700167 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800168 }
169
170 @Override
171 public void removeTab(Tab tab) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700172 mTitleBar.cancelTitleBarAnimation(true);
173 mTitleBar.setSkipTitleBarAnimations(true);
Michael Kolb66706532010-12-12 19:50:22 -0800174 super.removeTab(tab);
175 mTabBar.onRemoveTab(tab);
Michael Kolb46f987e2011-04-05 10:39:10 -0700176 mTitleBar.setSkipTitleBarAnimations(false);
Michael Kolb8814d732011-01-26 11:22:30 -0800177 }
178
179 protected void onRemoveTabCompleted(Tab tab) {
Michael Kolbda580632012-04-16 13:30:28 -0700180 checkHideActionBar();
Michael Kolb66706532010-12-12 19:50:22 -0800181 }
182
Michael Kolb376b5412010-12-15 11:52:57 -0800183 int getContentWidth() {
184 if (mContentView != null) {
185 return mContentView.getWidth();
Michael Kolb66706532010-12-12 19:50:22 -0800186 }
187 return 0;
188 }
189
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800190 @Override
Michael Kolb1f9b3562012-04-24 14:38:34 -0700191 public void editUrl(boolean clearInput, boolean forceIME) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700192 if (mUseQuickControls) {
John Reck0f602f32011-07-07 15:38:43 -0700193 mTitleBar.setShowProgressOnly(false);
Michael Kolbd72ea3b2011-01-09 15:56:37 -0800194 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700195 super.editUrl(clearInput, forceIME);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800196 }
197
Michael Kolb66706532010-12-12 19:50:22 -0800198 // action mode callbacks
199
200 @Override
201 public void onActionModeStarted(ActionMode mode) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800202 if (!mTitleBar.isEditingUrl()) {
Michael Kolbb2e91fd2011-07-14 13:47:29 -0700203 // hide the title bar when CAB is shown
Michael Kolb7cdc4902011-02-03 17:54:40 -0800204 hideTitleBar();
Michael Kolb66706532010-12-12 19:50:22 -0800205 }
206 }
207
208 @Override
Michael Kolb376b5412010-12-15 11:52:57 -0800209 public void onActionModeFinished(boolean inLoad) {
Michael Kolbda580632012-04-16 13:30:28 -0700210 checkHideActionBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800211 if (inLoad) {
212 // the titlebar was removed when the CAB was shown
213 // if the page is loading, show it again
Michael Kolb1544f3b2011-03-10 09:06:10 -0800214 if (mUseQuickControls) {
215 mTitleBar.setShowProgressOnly(true);
Michael Kolb376b5412010-12-15 11:52:57 -0800216 }
Michael Kolb7cdc4902011-02-03 17:54:40 -0800217 showTitleBar();
Michael Kolb376b5412010-12-15 11:52:57 -0800218 }
219 }
220
221 @Override
Michael Kolb5a72f182011-01-13 20:35:06 -0800222 protected void updateNavigationState(Tab tab) {
John Reck0f602f32011-07-07 15:38:43 -0700223 mNavBar.updateNavigationState(tab);
Michael Kolb5a72f182011-01-13 20:35:06 -0800224 }
225
226 @Override
John Reck30c714c2010-12-16 17:30:34 -0800227 public void setUrlTitle(Tab tab) {
228 super.setUrlTitle(tab);
229 mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
Michael Kolb66706532010-12-12 19:50:22 -0800230 }
231
232 // Set the favicon in the title bar.
233 @Override
John Reck30c714c2010-12-16 17:30:34 -0800234 public void setFavicon(Tab tab) {
235 super.setFavicon(tab);
236 mTabBar.onFavicon(tab, tab.getFavicon());
Michael Kolb66706532010-12-12 19:50:22 -0800237 }
238
Michael Kolbcfa3af52010-12-14 10:36:11 -0800239 @Override
John Reckd73c5a22010-12-22 10:22:50 -0800240 public void onHideCustomView() {
241 super.onHideCustomView();
Michael Kolbda580632012-04-16 13:30:28 -0700242 checkHideActionBar();
John Reckd73c5a22010-12-22 10:22:50 -0800243 }
Michael Kolba4183062011-01-16 10:43:21 -0800244
245 @Override
246 public boolean dispatchKey(int code, KeyEvent event) {
Michael Kolbdfe99a12011-03-08 11:45:40 -0800247 if (mActiveTab != null) {
248 WebView web = mActiveTab.getWebView();
249 if (event.getAction() == KeyEvent.ACTION_DOWN) {
250 switch (code) {
251 case KeyEvent.KEYCODE_TAB:
252 case KeyEvent.KEYCODE_DPAD_UP:
253 case KeyEvent.KEYCODE_DPAD_LEFT:
254 if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700255 editUrl(false, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800256 return true;
257 }
258 }
259 boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
260 if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
Michael Kolb1f9b3562012-04-24 14:38:34 -0700261 editUrl(true, false);
Michael Kolbdfe99a12011-03-08 11:45:40 -0800262 return mContentView.dispatchKeyEvent(event);
263 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800264 }
Michael Kolba4183062011-01-16 10:43:21 -0800265 }
266 return false;
267 }
268
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800269 private boolean isTypingKey(KeyEvent evt) {
270 return evt.getUnicodeChar() > 0;
271 }
272
273 TabBar getTabBar() {
274 return mTabBar;
275 }
276
John Reck1cf4b792011-07-26 10:22:22 -0700277 @Override
278 public boolean shouldCaptureThumbnails() {
279 return mUseQuickControls;
280 }
281
John Reck034637c2011-08-11 11:34:44 -0700282 private Drawable getFaviconBackground() {
283 if (mFaviconBackground == null) {
284 mFaviconBackground = new PaintDrawable();
285 Resources res = mActivity.getResources();
286 mFaviconBackground.getPaint().setColor(
287 res.getColor(R.color.tabFaviconBackground));
288 mFaviconBackground.setCornerRadius(
289 res.getDimension(R.dimen.tab_favicon_corner_radius));
290 }
291 return mFaviconBackground;
292 }
293
294 @Override
295 public Drawable getFaviconDrawable(Bitmap icon) {
296 Drawable[] array = new Drawable[2];
297 array[0] = getFaviconBackground();
298 if (icon == null) {
299 array[1] = mGenericFavicon;
300 } else {
301 array[1] = new BitmapDrawable(mActivity.getResources(), icon);
302 }
303 LayerDrawable d = new LayerDrawable(array);
304 d.setLayerInset(1, 2, 2, 2, 2);
305 return d;
306 }
307
Michael Kolb66706532010-12-12 19:50:22 -0800308}