1/*
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.ActionBar;
20import android.app.Activity;
21import 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;
27import android.os.Bundle;
28import android.os.Handler;
29import android.util.Log;
30import android.view.ActionMode;
31import android.view.KeyEvent;
32import android.view.Menu;
33import android.view.MenuItem;
34import android.webkit.WebView;
35
36import java.util.List;
37
38/**
39 * Ui for xlarge screen sizes
40 */
41public class XLargeUi extends BaseUi {
42
43    private static final String LOGTAG = "XLargeUi";
44
45    private PaintDrawable mFaviconBackground;
46
47    private ActionBar mActionBar;
48    private TabBar mTabBar;
49
50    private NavigationBarTablet mNavBar;
51
52    private Handler mHandler;
53
54    /**
55     * @param browser
56     * @param controller
57     */
58    public XLargeUi(Activity browser, UiController controller) {
59        super(browser, controller);
60        mHandler = new Handler();
61        mNavBar = (NavigationBarTablet) mTitleBar.getNavigationBar();
62        mTabBar = new TabBar(mActivity, mUiController, this);
63        mActionBar = mActivity.getActionBar();
64        setupActionBar();
65        setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
66    }
67
68    private void setupActionBar() {
69        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
70        mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
71        mActionBar.setCustomView(mTabBar);
72    }
73
74    public void showComboView(ComboViews startWith, Bundle extras) {
75        super.showComboView(startWith, extras);
76        if (mUseQuickControls) {
77            mActionBar.show();
78        }
79    }
80
81    @Override
82    public void setUseQuickControls(boolean useQuickControls) {
83        super.setUseQuickControls(useQuickControls);
84        checkHideActionBar();
85        if (!useQuickControls) {
86            mActionBar.show();
87        }
88        mTabBar.setUseQuickControls(mUseQuickControls);
89        // We need to update the tabs with this change
90        for (Tab t : mTabControl.getTabs()) {
91            t.updateShouldCaptureThumbnails();
92        }
93    }
94
95    private void checkHideActionBar() {
96        if (mUseQuickControls) {
97            mHandler.post(new Runnable() {
98                public void run() {
99                    mActionBar.hide();
100                }
101            });
102        }
103    }
104
105    @Override
106    public void onResume() {
107        super.onResume();
108        mNavBar.clearCompletions();
109        checkHideActionBar();
110    }
111
112    @Override
113    public void onDestroy() {
114        hideTitleBar();
115    }
116
117    void stopWebViewScrolling() {
118        BrowserWebView web = (BrowserWebView) mUiController.getCurrentWebView();
119    }
120
121    @Override
122    public boolean onPrepareOptionsMenu(Menu menu) {
123        MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
124        if (bm != null) {
125            bm.setVisible(false);
126        }
127        return true;
128    }
129
130
131    // WebView callbacks
132
133    @Override
134    public void addTab(Tab tab) {
135        mTabBar.onNewTab(tab);
136    }
137
138    protected void onAddTabCompleted(Tab tab) {
139        checkHideActionBar();
140    }
141
142    @Override
143    public void setActiveTab(final Tab tab) {
144        mTitleBar.cancelTitleBarAnimation(true);
145        mTitleBar.setSkipTitleBarAnimations(true);
146        super.setActiveTab(tab);
147        BrowserWebView view = (BrowserWebView) tab.getWebView();
148        // TabControl.setCurrentTab has been called before this,
149        // so the tab is guaranteed to have a webview
150        if (view == null) {
151            Log.e(LOGTAG, "active tab with no webview detected");
152            return;
153        }
154        mTabBar.onSetActiveTab(tab);
155        updateLockIconToLatest(tab);
156        mTitleBar.setSkipTitleBarAnimations(false);
157    }
158
159    @Override
160    public void updateTabs(List<Tab> tabs) {
161        mTabBar.updateTabs(tabs);
162        checkHideActionBar();
163    }
164
165    @Override
166    public void removeTab(Tab tab) {
167        mTitleBar.cancelTitleBarAnimation(true);
168        mTitleBar.setSkipTitleBarAnimations(true);
169        super.removeTab(tab);
170        mTabBar.onRemoveTab(tab);
171        mTitleBar.setSkipTitleBarAnimations(false);
172    }
173
174    protected void onRemoveTabCompleted(Tab tab) {
175        checkHideActionBar();
176    }
177
178    int getContentWidth() {
179        if (mContentView != null) {
180            return mContentView.getWidth();
181        }
182        return 0;
183    }
184
185    @Override
186    public void editUrl(boolean clearInput, boolean forceIME) {
187        if (mUseQuickControls) {
188            mTitleBar.setShowProgressOnly(false);
189        }
190        super.editUrl(clearInput, forceIME);
191    }
192
193    // action mode callbacks
194
195    @Override
196    public void onActionModeStarted(ActionMode mode) {
197        if (!mTitleBar.isEditingUrl()) {
198            // hide the title bar when CAB is shown
199            hideTitleBar();
200        }
201    }
202
203    @Override
204    public void onActionModeFinished(boolean inLoad) {
205        checkHideActionBar();
206        if (inLoad) {
207            // the titlebar was removed when the CAB was shown
208            // if the page is loading, show it again
209            if (mUseQuickControls) {
210                mTitleBar.setShowProgressOnly(true);
211            }
212            showTitleBar();
213        }
214    }
215
216    @Override
217    protected void updateNavigationState(Tab tab) {
218        mNavBar.updateNavigationState(tab);
219    }
220
221    @Override
222    public void setUrlTitle(Tab tab) {
223        super.setUrlTitle(tab);
224        mTabBar.onUrlAndTitle(tab, tab.getUrl(), tab.getTitle());
225    }
226
227    // Set the favicon in the title bar.
228    @Override
229    public void setFavicon(Tab tab) {
230        super.setFavicon(tab);
231        mTabBar.onFavicon(tab, tab.getFavicon());
232    }
233
234    @Override
235    public void onHideCustomView() {
236        super.onHideCustomView();
237        checkHideActionBar();
238    }
239
240    @Override
241    public boolean dispatchKey(int code, KeyEvent event) {
242        if (mActiveTab != null) {
243            WebView web = mActiveTab.getWebView();
244            if (event.getAction() == KeyEvent.ACTION_DOWN) {
245                switch (code) {
246                    case KeyEvent.KEYCODE_TAB:
247                    case KeyEvent.KEYCODE_DPAD_UP:
248                    case KeyEvent.KEYCODE_DPAD_LEFT:
249                        if ((web != null) && web.hasFocus() && !mTitleBar.hasFocus()) {
250                            editUrl(false, false);
251                            return true;
252                        }
253                }
254                boolean ctrl = event.hasModifiers(KeyEvent.META_CTRL_ON);
255                if (!ctrl && isTypingKey(event) && !mTitleBar.isEditingUrl()) {
256                    editUrl(true, false);
257                    return mContentView.dispatchKeyEvent(event);
258                }
259            }
260        }
261        return false;
262    }
263
264    private boolean isTypingKey(KeyEvent evt) {
265        return evt.getUnicodeChar() > 0;
266    }
267
268    TabBar getTabBar() {
269        return mTabBar;
270    }
271
272    @Override
273    public boolean shouldCaptureThumbnails() {
274        return mUseQuickControls;
275    }
276
277    private Drawable getFaviconBackground() {
278        if (mFaviconBackground == null) {
279            mFaviconBackground = new PaintDrawable();
280            Resources res = mActivity.getResources();
281            mFaviconBackground.getPaint().setColor(
282                    res.getColor(R.color.tabFaviconBackground));
283            mFaviconBackground.setCornerRadius(
284                    res.getDimension(R.dimen.tab_favicon_corner_radius));
285        }
286        return mFaviconBackground;
287    }
288
289    @Override
290    public Drawable getFaviconDrawable(Bitmap icon) {
291        Drawable[] array = new Drawable[2];
292        array[0] = getFaviconBackground();
293        if (icon == null) {
294            array[1] = mGenericFavicon;
295        } else {
296            array[1] = new BitmapDrawable(mActivity.getResources(), icon);
297        }
298        LayerDrawable d = new LayerDrawable(array);
299        d.setLayerInset(1, 2, 2, 2, 2);
300        return d;
301    }
302
303}
304