PhoneUi.java revision 5d43ce897758b7cb5deadc1203717b965334784c
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.View;
27import android.webkit.WebView;
28import android.widget.FrameLayout;
29
30/**
31 * Ui for regular phone screen sizes
32 */
33public class PhoneUi extends BaseUi {
34
35    private static final String LOGTAG = "PhoneUi";
36
37    private TitleBarPhone mTitleBar;
38    private ActiveTabsPage mActiveTabsPage;
39    private boolean mUseQuickControls;
40    private PieControl mPieControl;
41    private NavScreen mNavScreen;
42
43    boolean mExtendedMenuOpen;
44    boolean mOptionsMenuOpen;
45    boolean mAnimating;
46
47    /**
48     * @param browser
49     * @param controller
50     */
51    public PhoneUi(Activity browser, UiController controller) {
52        super(browser, controller);
53        mTitleBar = new TitleBarPhone(mActivity, mUiController, this,
54                mContentView);
55        // mTitleBar will be always be shown in the fully loaded mode on
56        // phone
57        mTitleBar.setProgress(100);
58        mActivity.getActionBar().hide();
59        setUseQuickControls(BrowserSettings.getInstance().useQuickControls());
60    }
61
62    @Override
63    public void hideComboView() {
64        super.hideComboView();
65        mActivity.getActionBar().hide();
66    }
67
68    // lifecycle
69
70    @Override
71    public void onPause() {
72        // FIXME: This removes the active tabs page and resets the menu to
73        // MAIN_MENU.  A better solution might be to do this work in onNewIntent
74        // but then we would need to save it in onSaveInstanceState and restore
75        // it in onCreate/onRestoreInstanceState
76        if (mActiveTabsPage != null) {
77            mUiController.removeActiveTabsPage(true);
78        }
79        super.onPause();
80    }
81
82    @Override
83    public void onDestroy() {
84        hideTitleBar();
85    }
86
87    @Override
88    public void editUrl(boolean clearInput) {
89        if (mUseQuickControls) {
90            getTitleBar().setShowProgressOnly(false);
91        }
92        super.editUrl(clearInput);
93    }
94
95    @Override
96    public boolean onBackKey() {
97        if (mActiveTabsPage != null) {
98            // if tab page is showing, hide it
99            mUiController.removeActiveTabsPage(true);
100            return true;
101        } else if (mNavScreen != null) {
102            mNavScreen.close();
103            return true;
104        }
105        return super.onBackKey();
106    }
107
108    @Override
109    public boolean onMenuKey() {
110        if (!isComboViewShowing()) {
111            if (mNavScreen == null) {
112                showNavScreen();
113            } else {
114                mNavScreen.close();
115            }
116            return true;
117        } else {
118            return false;
119        }
120    }
121
122    @Override
123    public boolean dispatchKey(int code, KeyEvent event) {
124        if (!isComboViewShowing()) {
125        }
126        return false;
127    }
128
129    @Override
130    public void onProgressChanged(Tab tab) {
131        if (tab.inForeground()) {
132            int progress = tab.getLoadProgress();
133            mTitleBar.setProgress(progress);
134            if (progress == 100) {
135                if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
136                    suggestHideTitleBar();
137                    if (mUseQuickControls) {
138                        mTitleBar.setShowProgressOnly(false);
139                    }
140                }
141            } else {
142                if (!mOptionsMenuOpen || mExtendedMenuOpen) {
143                    if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
144                        mTitleBar.setShowProgressOnly(true);
145                        setTitleGravity(Gravity.TOP);
146                    }
147                    showTitleBar();
148                }
149            }
150        }
151    }
152
153    @Override
154    public void setActiveTab(final Tab tab) {
155        captureTab(mActiveTab);
156        super.setActiveTab(tab);
157        BrowserWebView view = (BrowserWebView) tab.getWebView();
158        // TabControl.setCurrentTab has been called before this,
159        // so the tab is guaranteed to have a webview
160        if (view == null) {
161            Log.e(LOGTAG, "active tab with no webview detected");
162            return;
163        }
164        // Request focus on the top window.
165        if (mUseQuickControls) {
166            mPieControl.forceToTop(mContentView);
167            view.setScrollListener(null);
168        } else {
169            // check if title bar is already attached by animation
170            if (mTitleBar.getParent() == null) {
171                view.setEmbeddedTitleBar(mTitleBar);
172            }
173            view.setScrollListener(this);
174        }
175        if (tab.isInVoiceSearchMode()) {
176            showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
177        } else {
178            revertVoiceTitleBar(tab);
179        }
180        updateLockIconToLatest(tab);
181        tab.getTopWindow().requestFocus();
182    }
183
184    @Override
185    protected TitleBarBase getTitleBar() {
186        return mTitleBar;
187    }
188
189    // active tabs page
190
191    @Override
192    public void showActiveTabsPage() {
193        captureTab(mActiveTab);
194        mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
195        mTitleBar.setVisibility(View.GONE);
196        hideTitleBar();
197        mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
198        mActiveTabsPage.requestFocus();
199    }
200
201    /**
202     * Remove the active tabs page.
203     */
204    @Override
205    public void removeActiveTabsPage() {
206        mContentView.removeView(mActiveTabsPage);
207        mTitleBar.setVisibility(View.VISIBLE);
208        mActiveTabsPage = null;
209    }
210
211    @Override
212    public void showComboView(boolean startWithHistory, Bundle extras) {
213        if (mNavScreen != null) {
214            hideNavScreen(false);
215        }
216        super.showComboView(startWithHistory, extras);
217    }
218
219    @Override
220    public boolean showsWeb() {
221        return super.showsWeb() && mActiveTabsPage == null;
222    }
223
224    // menu handling callbacks
225
226    @Override
227    public void onContextMenuCreated(Menu menu) {
228        hideTitleBar();
229    }
230
231    @Override
232    public void onContextMenuClosed(Menu menu, boolean inLoad) {
233        if (inLoad) {
234            showTitleBar();
235        }
236    }
237
238    // action mode callbacks
239
240    @Override
241    public void onActionModeStarted(ActionMode mode) {
242        hideTitleBar();
243    }
244
245    @Override
246    public void onActionModeFinished(boolean inLoad) {
247        if (inLoad) {
248            if (mUseQuickControls) {
249                mTitleBar.setShowProgressOnly(true);
250            }
251            showTitleBar();
252        }
253        mActivity.getActionBar().hide();
254    }
255
256    @Override
257    protected void setTitleGravity(int gravity) {
258        if (mUseQuickControls) {
259            FrameLayout.LayoutParams lp =
260                    (FrameLayout.LayoutParams) getTitleBar().getLayoutParams();
261            lp.gravity = gravity;
262            getTitleBar().setLayoutParams(lp);
263        } else {
264            super.setTitleGravity(gravity);
265        }
266    }
267
268    private void setUseQuickControls(boolean useQuickControls) {
269        mUseQuickControls = useQuickControls;
270        getTitleBar().setUseQuickControls(mUseQuickControls);
271        if (useQuickControls) {
272            mPieControl = new PieControl(mActivity, mUiController, this);
273            mPieControl.attachToContainer(mContentView);
274            WebView web = getWebView();
275            if (web != null) {
276                web.setEmbeddedTitleBar(null);
277            }
278        } else {
279            if (mPieControl != null) {
280                mPieControl.removeFromContainer(mContentView);
281            }
282            WebView web = getWebView();
283            if (web != null) {
284                web.setEmbeddedTitleBar(mTitleBar);
285            }
286            setTitleGravity(Gravity.NO_GRAVITY);
287        }
288    }
289
290    @Override
291    protected void captureTab(final Tab tab) {
292        if (tab == null) return;
293        BrowserWebView web = (BrowserWebView) tab.getWebView();
294        if (web != null) {
295            tab.setScreenshot(web.capture());
296        }
297    }
298
299    void showNavScreen() {
300        detachTab(mActiveTab);
301        mNavScreen = new NavScreen(mActivity, mUiController, this);
302        // Add the custom view to its container.
303        mCustomViewContainer.addView(mNavScreen, COVER_SCREEN_PARAMS);
304        mContentView.setVisibility(View.GONE);
305        mCustomViewContainer.setVisibility(View.VISIBLE);
306        mCustomViewContainer.bringToFront();
307    }
308
309    void hideNavScreen(boolean animate) {
310        if (mNavScreen == null) return;
311        Tab tab = mNavScreen.getSelectedTab();
312        mCustomViewContainer.removeView(mNavScreen);
313        mNavScreen = null;
314        mCustomViewContainer.setVisibility(View.GONE);
315        mUiController.setActiveTab(tab);
316        // Show the content view.
317        mContentView.setVisibility(View.VISIBLE);
318    }
319
320}
321