PhoneUi.java revision dc2ee1bfb50b7bec0cf3215e3d298b246dc71101
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.graphics.PixelFormat;
22import android.util.Log;
23import android.view.ActionMode;
24import android.view.Gravity;
25import android.view.KeyEvent;
26import android.view.Menu;
27import android.view.MotionEvent;
28import android.view.View;
29import android.view.WindowManager;
30import android.webkit.WebView;
31
32/**
33 * Ui for regular phone screen sizes
34 */
35public class PhoneUi extends BaseUi {
36
37    private static final String LOGTAG = "PhoneUi";
38
39    private TitleBar mTitleBar;
40    private ActiveTabsPage mActiveTabsPage;
41    private TouchProxy mTitleOverlay;
42
43    boolean mExtendedMenuOpen;
44    boolean mOptionsMenuOpen;
45
46    /**
47     * @param browser
48     * @param controller
49     */
50    public PhoneUi(Activity browser, UiController controller) {
51        super(browser, controller);
52        mTitleBar = new TitleBar(mActivity, mUiController, this);
53        // mTitleBar will be always be shown in the fully loaded mode on
54        // phone
55        mTitleBar.setProgress(100);
56        mActivity.getActionBar().hide();
57    }
58
59    @Override
60    public void hideComboView() {
61        super.hideComboView();
62        mActivity.getActionBar().hide();
63    }
64
65    // webview factory
66
67    @Override
68    public WebView createWebView(boolean privateBrowsing) {
69        // Create a new WebView
70        WebView w = new WebView(mActivity, null,
71                android.R.attr.webViewStyle, privateBrowsing);
72        initWebViewSettings(w);
73        return w;
74    }
75
76    @Override
77    public WebView createSubWebView(boolean privateBrowsing) {
78        WebView web = createWebView(privateBrowsing);
79        return web;
80    }
81
82    // lifecycle
83
84    @Override
85    public void onPause() {
86        // FIXME: This removes the active tabs page and resets the menu to
87        // MAIN_MENU.  A better solution might be to do this work in onNewIntent
88        // but then we would need to save it in onSaveInstanceState and restore
89        // it in onCreate/onRestoreInstanceState
90        if (mActiveTabsPage != null) {
91            mUiController.removeActiveTabsPage(true);
92        }
93        super.onPause();
94    }
95
96    @Override
97    public void onDestroy() {
98        hideTitleBar();
99    }
100
101    @Override
102    public void editUrl(boolean clearInput) {
103        String url = getActiveTab().getUrl();
104        mUiController.startSearch(url);
105    }
106
107    @Override
108    public boolean onBackKey() {
109        if (mActiveTabsPage != null) {
110            // if tab page is showing, hide it
111            mUiController.removeActiveTabsPage(true);
112            return true;
113        }
114        return super.onBackKey();
115    }
116
117    @Override
118    public void onProgressChanged(Tab tab) {
119        if (tab.inForeground()) {
120            int progress = tab.getLoadProgress();
121            mTitleBar.setProgress(progress);
122            if (progress == 100) {
123                if (!mOptionsMenuOpen || !mExtendedMenuOpen) {
124                    hideTitleBar();
125                }
126            } else {
127                if (!mOptionsMenuOpen || mExtendedMenuOpen) {
128                    showTitleBar();
129                }
130            }
131        }
132    }
133
134    @Override
135    public void setActiveTab(Tab tab) {
136        super.setActiveTab(tab);
137        WebView view = tab.getWebView();
138        // TabControl.setCurrentTab has been called before this,
139        // so the tab is guaranteed to have a webview
140        if (view == null) {
141            Log.e(LOGTAG, "active tab with no webview detected");
142            return;
143        }
144        view.setEmbeddedTitleBar(getTitleBar());
145        if (tab.isInVoiceSearchMode()) {
146            showVoiceTitleBar(tab.getVoiceDisplayTitle());
147        } else {
148            revertVoiceTitleBar(tab);
149        }
150        tab.getTopWindow().requestFocus();
151    }
152
153    @Override
154    protected void showTitleBar() {
155        if (canShowTitleBar()) {
156            setTitleGravity(Gravity.TOP);
157            super.showTitleBar();
158        }
159    }
160
161    @Override
162    protected void hideTitleBar() {
163        if (isTitleBarShowing()) {
164            setTitleGravity(Gravity.NO_GRAVITY);
165            super.hideTitleBar();
166        }
167    }
168
169    @Override
170    protected TitleBarBase getTitleBar() {
171        return mTitleBar;
172    }
173
174    // active tabs page
175
176    @Override
177    public void showActiveTabsPage() {
178        mActiveTabsPage = new ActiveTabsPage(mActivity, mUiController);
179        mTitleBar.setVisibility(View.GONE);
180        hideTitleBar();
181        mContentView.addView(mActiveTabsPage, COVER_SCREEN_PARAMS);
182        mActiveTabsPage.requestFocus();
183    }
184
185    /**
186     * Remove the active tabs page.
187     */
188    @Override
189    public void removeActiveTabsPage() {
190        mContentView.removeView(mActiveTabsPage);
191        mTitleBar.setVisibility(View.VISIBLE);
192        mActiveTabsPage = null;
193    }
194
195    @Override
196    public boolean showsWeb() {
197        return super.showsWeb() && mActiveTabsPage == null;
198    }
199
200    // menu handling callbacks
201
202    @Override
203    public void onOptionsMenuOpened() {
204        mOptionsMenuOpen = true;
205        // options menu opened, show title bar
206        showTitleBar();
207        if (mTitleOverlay == null) {
208            // This assumes that getTitleBar always returns the same View
209            mTitleOverlay = new TouchProxy(mActivity, getTitleBar());
210        }
211        mActivity.getWindowManager().addView(mTitleOverlay,
212                mTitleOverlay.getWindowLayoutParams());
213    }
214
215    @Override
216    public void onExtendedMenuOpened() {
217        // Switching the menu to expanded view, so hide the
218        // title bar.
219        mExtendedMenuOpen = true;
220        hideTitleBar();
221    }
222
223    @Override
224    public void onOptionsMenuClosed(boolean inLoad) {
225        mOptionsMenuOpen = false;
226        mActivity.getWindowManager().removeView(mTitleOverlay);
227        if (!inLoad && !getTitleBar().hasFocus()) {
228            hideTitleBar();
229        }
230    }
231
232    @Override
233    public void onExtendedMenuClosed(boolean inLoad) {
234        mExtendedMenuOpen = false;
235        showTitleBar();
236    }
237
238    @Override
239    public void onContextMenuCreated(Menu menu) {
240        hideTitleBar();
241    }
242
243    @Override
244    public void onContextMenuClosed(Menu menu, boolean inLoad) {
245        if (inLoad) {
246            showTitleBar();
247        }
248    }
249
250    // action mode callbacks
251
252    @Override
253    public void onActionModeStarted(ActionMode mode) {
254        hideTitleBar();
255    }
256
257    @Override
258    public boolean dispatchKey(int code, KeyEvent event) {
259        return false;
260    }
261
262    static class TouchProxy extends View {
263
264        View mTarget;
265
266        TouchProxy(Context context, View target) {
267            super(context);
268            mTarget = target;
269        }
270
271        @Override
272        public boolean dispatchTouchEvent(MotionEvent event) {
273            return mTarget.dispatchTouchEvent(event);
274        }
275
276        WindowManager.LayoutParams getWindowLayoutParams() {
277            WindowManager.LayoutParams params =
278                new WindowManager.LayoutParams(
279                        mTarget.getWidth(),
280                        mTarget.getHeight(),
281                        WindowManager.LayoutParams.TYPE_APPLICATION,
282                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
283                        PixelFormat.TRANSPARENT);
284            params.gravity = Gravity.TOP | Gravity.LEFT;
285            params.y = mTarget.getTop();
286            params.x = mTarget.getLeft();
287            return params;
288        }
289    }
290}
291