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