PhoneUi.java revision 8ee633fd62f94cd66c85c2904232d7c9e204cc9c
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.Activity;
20import android.os.Bundle;
21import android.util.Log;
22import android.view.ActionMode;
23import android.view.Gravity;
24import android.view.KeyEvent;
25import android.view.Menu;
26import android.view.MenuItem;
27import android.view.View;
28import android.view.accessibility.AccessibilityEvent;
29import android.webkit.WebView;
30import android.widget.FrameLayout;
31
32import com.android.browser.UrlInputView.StateListener;
33
34/**
35 * Ui for regular phone screen sizes
36 */
37public class PhoneUi extends BaseUi {
38
39    private static final String LOGTAG = "PhoneUi";
40
41    private PieControlPhone mPieControl;
42    private NavScreen mNavScreen;
43    private NavigationBarPhone mNavigationBar;
44
45    boolean mExtendedMenuOpen;
46    boolean mOptionsMenuOpen;
47    boolean mAnimating;
48
49    /**
50     * @param browser
51     * @param controller
52     */
53    public PhoneUi(Activity browser, UiController controller) {
54        super(browser, controller);
55        mActivity.getActionBar().hide();
56        setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
57        mNavigationBar = (NavigationBarPhone) mTitleBar.getNavigationBar();
58    }
59
60    @Override
61    public void onDestroy() {
62        hideTitleBar();
63    }
64
65    @Override
66    public void editUrl(boolean clearInput) {
67        if (mUseQuickControls) {
68            mTitleBar.setShowProgressOnly(false);
69        }
70        super.editUrl(clearInput);
71    }
72
73    @Override
74    public boolean onBackKey() {
75        if (mNavScreen != null) {
76            mNavScreen.close();
77            return true;
78        }
79        return super.onBackKey();
80    }
81
82    @Override
83    public boolean dispatchKey(int code, KeyEvent event) {
84        return false;
85    }
86
87    @Override
88    public void onProgressChanged(Tab tab) {
89        if (tab.inForeground()) {
90            int progress = tab.getLoadProgress();
91            mTitleBar.setProgress(progress);
92            if (progress == 100) {
93                if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
94                    suggestHideTitleBar();
95                    if (mUseQuickControls) {
96                        mTitleBar.setShowProgressOnly(false);
97                    }
98                }
99            } else {
100                if (!mOptionsMenuOpen || mExtendedMenuOpen) {
101                    if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
102                        mTitleBar.setShowProgressOnly(true);
103                        setTitleGravity(Gravity.TOP);
104                    }
105                    showTitleBar();
106                }
107            }
108        }
109    }
110
111    @Override
112    public void setActiveTab(final Tab tab) {
113        mTitleBar.cancelTitleBarAnimation(true);
114        mTitleBar.setSkipTitleBarAnimations(true);
115        super.setActiveTab(tab);
116        BrowserWebView view = (BrowserWebView) tab.getWebView();
117        // TabControl.setCurrentTab has been called before this,
118        // so the tab is guaranteed to have a webview
119        if (view == null) {
120            Log.e(LOGTAG, "active tab with no webview detected");
121            return;
122        }
123        // Request focus on the top window.
124        if (mUseQuickControls) {
125            mPieControl.forceToTop(mContentView);
126        } else {
127            // check if title bar is already attached by animation
128            if (mTitleBar.getParent() == null) {
129                view.setEmbeddedTitleBar(mTitleBar);
130            }
131        }
132        if (tab.isInVoiceSearchMode()) {
133            showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
134        } else {
135            revertVoiceTitleBar(tab);
136        }
137        // update nav bar state
138        mNavigationBar.onStateChanged(StateListener.STATE_NORMAL);
139        updateLockIconToLatest(tab);
140        tab.getTopWindow().requestFocus();
141        mTitleBar.setSkipTitleBarAnimations(false);
142    }
143
144    /**
145     * Suggest to the UI that the title bar can be hidden. The UI will then
146     * decide whether or not to hide based off a number of factors, such
147     * as if the user is editing the URL bar or if the page is loading
148     */
149    @Override
150    public void suggestHideTitleBar() {
151        if (!mNavigationBar.isMenuShowing()) {
152            super.suggestHideTitleBar();
153        }
154    }
155
156    @Override
157    public void showComboView(ComboViews startWith, Bundle extras) {
158        if (mNavScreen != null) {
159            hideNavScreen(false);
160        }
161        super.showComboView(startWith, extras);
162    }
163
164    // menu handling callbacks
165
166    @Override
167    public boolean onPrepareOptionsMenu(Menu menu) {
168        updateMenuState(mActiveTab, menu);
169        return true;
170    }
171
172    @Override
173    public void updateMenuState(Tab tab, Menu menu) {
174        menu.setGroupVisible(R.id.NAV_MENU, (mNavScreen == null));
175        MenuItem bm = menu.findItem(R.id.bookmarks_menu_id);
176        if (bm != null) {
177            bm.setVisible(mNavScreen == null);
178        }
179        MenuItem nt = menu.findItem(R.id.new_tab_menu_id);
180        if (nt != null) {
181            nt.setVisible(mNavScreen == null);
182        }
183        MenuItem find = menu.findItem(R.id.find_menu_id);
184        if (find != null) {
185            find.setVisible(((tab != null) && !tab.isSnapshot()));
186        }
187        MenuItem abm = menu.findItem(R.id.add_bookmark_menu_id);
188        if (abm != null) {
189            abm.setVisible((tab != null) && !tab.isSnapshot());
190        }
191    }
192
193    @Override
194    public boolean onOptionsItemSelected(MenuItem item) {
195        if (mNavScreen != null) {
196            hideNavScreen(false);
197        }
198        return false;
199    }
200
201    @Override
202    public void onContextMenuCreated(Menu menu) {
203        hideTitleBar();
204    }
205
206    @Override
207    public void onContextMenuClosed(Menu menu, boolean inLoad) {
208        if (inLoad) {
209            showTitleBar();
210        }
211    }
212
213    // action mode callbacks
214
215    @Override
216    public void onActionModeStarted(ActionMode mode) {
217        if (!isEditingUrl()) {
218            hideTitleBar();
219        }
220    }
221
222    @Override
223    public void onActionModeFinished(boolean inLoad) {
224        if (inLoad) {
225            if (mUseQuickControls) {
226                mTitleBar.setShowProgressOnly(true);
227            }
228            showTitleBar();
229        }
230        mActivity.getActionBar().hide();
231    }
232
233    @Override
234    protected void setTitleGravity(int gravity) {
235        if (mUseQuickControls) {
236            FrameLayout.LayoutParams lp =
237                    (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
238            lp.gravity = gravity;
239            mTitleBar.setLayoutParams(lp);
240        } else {
241            super.setTitleGravity(gravity);
242        }
243    }
244
245    @Override
246    public void setUseQuickControls(boolean useQuickControls) {
247        mUseQuickControls = useQuickControls;
248        mTitleBar.setUseQuickControls(mUseQuickControls);
249        if (useQuickControls) {
250            mPieControl = new PieControlPhone(mActivity, mUiController, this);
251            mPieControl.attachToContainer(mContentView);
252            WebView web = getWebView();
253            if (web != null) {
254                web.setEmbeddedTitleBar(null);
255                // don't show url bar on scrolling
256                web.setOnTouchListener(null);
257            }
258        } else {
259            if (mPieControl != null) {
260                mPieControl.removeFromContainer(mContentView);
261            }
262            WebView web = getWebView();
263            if (web != null) {
264                web.setEmbeddedTitleBar(mTitleBar);
265                // show url bar on scrolling
266                web.setOnTouchListener(this);
267            }
268            setTitleGravity(Gravity.NO_GRAVITY);
269        }
270    }
271
272    void showNavScreen() {
273        mActiveTab.capture();
274        detachTab(mActiveTab);
275        mNavScreen = new NavScreen(mActivity, mUiController, this);
276        // Add the custom view to its container.
277        mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
278        mContentView.setVisibility(View.GONE);
279        mCustomViewContainer.setVisibility(View.VISIBLE);
280        mCustomViewContainer.bringToFront();
281        // notify accessibility manager about the screen change
282        mNavScreen.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
283        mTabControl.setOnThumbnailUpdatedListener(mNavScreen);
284    }
285
286    void hideNavScreen(boolean animate) {
287        if (mNavScreen == null) return;
288        mTabControl.setOnThumbnailUpdatedListener(null);
289        Tab tab = mNavScreen.getSelectedTab();
290        mCustomViewContainer.removeView(mNavScreen);
291        mNavScreen = null;
292        mCustomViewContainer.setVisibility(View.GONE);
293        mUiController.setActiveTab(tab);
294        // Show the content view.
295        mContentView.setVisibility(View.VISIBLE);
296    }
297
298    @Override
299    public boolean needsRestoreAllTabs() {
300        return false;
301    }
302
303    public void toggleNavScreen() {
304        if (mNavScreen == null) {
305            showNavScreen();
306        } else {
307            hideNavScreen(false);
308        }
309    }
310
311    @Override
312    public boolean shouldCaptureThumbnails() {
313        return true;
314    }
315
316}
317