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