summaryrefslogtreecommitdiffstats
path: root/src/src/com/android/browser/BaseUi.java
blob: f4e7d164e92adcb091e0de86d458e9f66a5f7eb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.content.res.TypedArray;

import org.codeaurora.swe.BrowserCommandLine;
import org.codeaurora.swe.WebView;

import java.util.List;

/**
 * UI interface definitions
 */
public abstract class BaseUi implements UI {

    protected static final boolean ENABLE_BORDER_AROUND_FAVICON = false;

    protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS =
        new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);

    protected static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER =
        new FrameLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT,
        Gravity.CENTER);

    Activity mActivity;
    UiController mUiController;
    TabControl mTabControl;
    protected Tab mActiveTab;
    private InputMethodManager mInputManager;

    private Drawable mGenericFavicon;

    protected FrameLayout mContentView;
    protected FrameLayout mCustomViewContainer;

    private View mCustomView;
    private CustomViewCallback mCustomViewCallback;
    private int mOriginalOrientation;

    private Toast mStopToast;

    // the default <video> poster
    private Bitmap mDefaultVideoPoster;
    // the video progress view
    private View mVideoProgressView;

    private final View mDecorView;

    private boolean mActivityPaused;
    protected TitleBar mTitleBar;
    private NavigationBarBase mNavigationBar;
    private boolean mBlockFocusAnimations;
    private boolean mFullscreenModeLocked;

    private EdgeSwipeController mEdgeSwipeController;
    private EdgeSwipeSettings mEdgeSwipeSettings;

    public BaseUi(Activity browser, UiController controller) {
        mActivity = browser;
        mUiController = controller;
        mTabControl = controller.getTabControl();
        mInputManager = (InputMethodManager)
                browser.getSystemService(Activity.INPUT_METHOD_SERVICE);
        // This assumes that the top-level root of our layout has the 'android.R.id.content' id
        // it's used in place of setContentView because we're attaching a <merge> here.
        FrameLayout frameLayout = (FrameLayout) mActivity.getWindow()
                .getDecorView().findViewById(android.R.id.content);
        LayoutInflater.from(mActivity)
                .inflate(R.layout.custom_screen, frameLayout);
        mContentView = (FrameLayout) frameLayout.findViewById(
                R.id.main_content);
        mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
                R.id.fullscreen_custom_content);
        setFullscreen(BrowserSettings.getInstance().useFullscreen());
        mTitleBar = new TitleBar(mActivity, mUiController, this,
                mContentView);
        mTitleBar.setProgress(100);
        mNavigationBar = mTitleBar.getNavigationBar();

        // install system ui visibility listeners
        mDecorView = mActivity.getWindow().getDecorView();
        mDecorView.setOnSystemUiVisibilityChangeListener(mSystemUiVisibilityChangeListener);
        mFullscreenModeLocked = false;
    }

    private View.OnSystemUiVisibilityChangeListener mSystemUiVisibilityChangeListener =
            new View.OnSystemUiVisibilityChangeListener() {
                @Override
                public void onSystemUiVisibilityChange(int visFlags) {
                    final boolean lostFullscreen = (visFlags & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0;
                    if (lostFullscreen)
                        setFullscreen(BrowserSettings.getInstance().useFullscreen());
                }
            };

    private void cancelStopToast() {
        if (mStopToast != null) {
            mStopToast.cancel();
            mStopToast = null;
        }
    }

    protected Drawable getGenericFavicon() {
        if (mGenericFavicon == null) {
            mGenericFavicon = mActivity.getResources().getDrawable(R.drawable.ic_deco_favicon_normal);
        }
        return mGenericFavicon;
    }

    // lifecycle

    public void onPause() {
        if (isCustomViewShowing()) {
            onHideCustomView();
        }
        if (mTabControl.getCurrentTab() != null) {
            mTabControl.getCurrentTab().exitFullscreen();
        }
        cancelStopToast();
        mActivityPaused = true;
    }

    public void onResume() {
        mActivityPaused = false;
        // check if we exited without setting active tab
        // b: 5188145
        setFullscreen(BrowserSettings.getInstance().useFullscreen());
        final Tab ct = mTabControl.getCurrentTab();
        if (ct != null) {
            setActiveTab(ct);
        }
        mTitleBar.onResume();
    }

    protected boolean isActivityPaused() {
        return mActivityPaused;
    }

    public void onConfigurationChanged(Configuration config) {
        if (mEdgeSwipeController != null) {
            mEdgeSwipeController.onConfigurationChanged();
        }
        if (mEdgeSwipeSettings != null) {
            mEdgeSwipeSettings.onConfigurationChanged();
        }
    }

    public Activity getActivity() {
        return mActivity;
    }

    // key handling

    @Override
    public boolean onBackKey() {
        if (mCustomView != null) {
            mUiController.hideCustomView();
            return true;
        } else if ((mTabControl.getCurrentTab() != null) &&
                (mTabControl.getCurrentTab().exitFullscreen())) {
            return true;
        }
        return false;
    }

    public boolean isFullScreen() {
        if (mTabControl.getCurrentTab() != null)
            return mTabControl.getCurrentTab().isTabFullScreen();
        return false;
    }

    @Override
    public boolean onMenuKey() {
        return false;
    }

    // Tab callbacks
    @Override
    public void onTabDataChanged(Tab tab) {
        setUrlTitle(tab);
        updateTabSecurityState(tab);
        updateNavigationState(tab);
        mTitleBar.onTabDataChanged(tab);
        mNavigationBar.onTabDataChanged(tab);
        onProgressChanged(tab);
    }

    @Override
    public void onProgressChanged(Tab tab) {
        int progress = tab.getLoadProgress();
        if (tab.inForeground()) {
            if (tab.inPageLoad()) {
                mTitleBar.setProgress(progress);
            } else {
                mTitleBar.setProgress(100);
            }
        }
    }

    @Override
    public void bookmarkedStatusHasChanged(Tab tab) {
        if (tab.inForeground()) {
            boolean isBookmark = tab.isBookmarkedSite();
            mNavigationBar.setCurrentUrlIsBookmark(isBookmark);
        }
    }

    @Override
    public void onPageStopped(Tab tab) {
        cancelStopToast();
        if (tab.inForeground()) {
            mStopToast = Toast
                    .makeText(mActivity, R.string.stopping, Toast.LENGTH_SHORT);
            mStopToast.show();
        }
    }

    @Override
    public boolean needsRestoreAllTabs() {
        return true;
    }

    @Override
    public void addTab(Tab tab) {
    }

    public void cancelNavScreenRequest(){
    }

    public void setActiveTab(final Tab tab) {
        if (tab == null) return;
        Tab tabToRemove = null;
        Tab tabToWaitFor = null;

        // block unnecessary focus change animations during tab switch
        mBlockFocusAnimations = true;
        if ((tab != mActiveTab) && (mActiveTab != null)) {
            tabToRemove = mActiveTab;
            WebView web = mActiveTab.getWebView();
            if (web != null) {
                web.setOnTouchListener(null);
            }
        }
        mActiveTab = tab;

        BrowserWebView web = (BrowserWebView) mActiveTab.getWebView();
        attachTabToContentView(tab);
        if (web != null) {
            // Request focus on the top window.
            web.setTitleBar(mTitleBar);
            mTitleBar.onScrollChanged();
            tabToWaitFor = mActiveTab;
        }
        mTitleBar.bringToFront();
        tab.getTopWindow().requestFocus();
        onTabDataChanged(tab);
        setFavicon(tab);
        onProgressChanged(tab);
        mNavigationBar.setIncognitoMode(tab.isPrivateBrowsingEnabled());
        mBlockFocusAnimations = false;

        scheduleRemoveTab(tabToRemove, tabToWaitFor);

        updateTabSecurityState(tab);
    }

    Tab mTabToRemove = null;
    Tab mTabToWaitFor = null;
    int mNumRemoveTries = 0;
    Runnable mRunnable = null;

    protected void scheduleRemoveTab(Tab tabToRemove, Tab tabToWaitFor) {

        if(tabToWaitFor == mTabToRemove) {
            if (mRunnable != null) {
                mTitleBar.removeCallbacks(mRunnable);
            }
            mTabToRemove = null;
            mTabToWaitFor = null;
            mRunnable = null;
            return;
        }

        //remove previously scehduled tab
        if (mTabToRemove != null) {
            if (mRunnable != null)
               mTitleBar.removeCallbacks(mRunnable);
            removeTabFromContentView(mTabToRemove);
            mTabToRemove.performPostponedDestroy();
            mRunnable = null;
        }
        mTabToRemove = tabToRemove;
        mTabToWaitFor = tabToWaitFor;
        mNumRemoveTries = 0;

        if (mTabToRemove != null) {
            mTabToRemove.postponeDestroy();
            tryRemoveTab();
        }
    }

    protected void tryRemoveTab() {
        mNumRemoveTries++;
        // Ensure the webview is still valid
        if (mNumRemoveTries < 20 && mTabToWaitFor.getWebView() != null) {
            if (!mTabToWaitFor.getWebView().isReady()) {
                if (mRunnable == null) {
                    mRunnable = new Runnable() {
                        public void run() {
                            tryRemoveTab();
                        }
                    };
                }
                /*if the new tab is still not ready, wait another 2 frames
                  before trying again.  1 frame for the tab to render the first
                  frame, another 1 frame to make sure the swap is done*/
                mTitleBar.postDelayed(mRunnable, 33);
                return;
            }
        }
        if (mTabToRemove != null) {
            if (mRunnable != null)
                mTitleBar.removeCallbacks(mRunnable);
            removeTabFromContentView(mTabToRemove);
            mTabToRemove.performPostponedDestroy();
            mRunnable = null;
        }
        mTabToRemove = null;
        mTabToWaitFor = null;
    }

    Tab getActiveTab() {
        return mActiveTab;
    }

    @Override
    public void updateTabs(List<Tab> tabs) {
    }

    @Override
    public void removeTab(Tab tab) {
        if (mActiveTab == tab) {
            removeTabFromContentView(tab);
            mActiveTab = null;
        }
    }

    @Override
    public void detachTab(Tab tab) {
        removeTabFromContentView(tab);
    }

    @Override
    public void attachTab(Tab tab) {
        attachTabToContentView(tab);
    }

    protected void attachTabToContentView(Tab tab) {
        if ((tab == null) || (tab.getWebView() == null)) {
            return;
        }
        View container = tab.getViewContainer();
        WebView mainView  = tab.getWebView();
        // Attach the WebView to the container and then attach the
        // container to the content view.
        FrameLayout wrapper =
                (FrameLayout) container.findViewById(R.id.webview_wrapper);
        ViewGroup parentView = (ViewGroup)mainView.getView().getParent();

        if (wrapper != parentView) {
            // clean up old view before attaching new view
            // this helping in fixing issues such touch event
            // getting triggered on old view instead of new one
            if (parentView != null) {
                parentView.removeView(mainView.getView());
            }
            wrapper.addView(mainView.getView());
        }
        ViewGroup parent = (ViewGroup) container.getParent();
        if (parent != mContentView) {
            if (parent != null) {
                parent.removeView(container);
            }
            mContentView.addView(container, COVER_SCREEN_PARAMS);
        }

        refreshEdgeSwipeController(container);

        mUiController.attachSubWindow(tab);
    }

    public void refreshEdgeSwipeController(View container) {
        if (isUiLowPowerMode()) {
            return;
        }

        if (mEdgeSwipeController != null) {
            mEdgeSwipeController.cleanup();
        }

        String action = BrowserSettings.getInstance().getEdgeSwipeAction();

        if (mEdgeSwipeSettings != null)  {
            mEdgeSwipeSettings.cleanup();
        }
        mEdgeSwipeSettings = null;

        if (action.equalsIgnoreCase(
                mActivity.getResources().getString(R.string.value_temporal_edge_swipe))) {
            mEdgeSwipeController = new EdgeSwipeController(
                    container,
                    R.id.stationary_navview,
                    R.id.sliding_navview,
                    R.id.sliding_navview_shadow,
                    R.id.navview_opacity,
                    R.id.webview_wrapper,
                    R.id.draggable_mainframe,
                    this);
        } else if (action.equalsIgnoreCase(
                mActivity.getResources().getString(R.string.value_unknown_edge_swipe))) {
            mEdgeSwipeSettings = new EdgeSwipeSettings(
                    container,
                    R.id.stationary_navview,
                    R.id.edge_sliding_settings,
                    R.id.sliding_navview_shadow,
                    R.id.webview_wrapper,
                    R.id.draggable_mainframe,
                    this);
        } else {
            DraggableFrameLayout draggableView = (DraggableFrameLayout)
                    container.findViewById(R.id.draggable_mainframe);
            draggableView.setDragHelper(null);
        }
    }

    private void removeTabFromContentView(Tab tab) {
        hideTitleBar();
        // Remove the container that contains the main WebView.
        WebView mainView = tab.getWebView();
        View container = tab.getViewContainer();
        if (mainView == null) {
            return;
        }
        // Remove the container from the content and then remove the
        // WebView from the container. This will trigger a focus change
        // needed by WebView.
        FrameLayout wrapper =
                (FrameLayout) container.findViewById(R.id.webview_wrapper);
        wrapper.removeView(mainView.getView());
        mContentView.removeView(container);
        mUiController.endActionMode();
        mUiController.removeSubWindow(tab);
    }

    @Override
    public void onSetWebView(Tab tab, WebView webView) {
        View container = tab.getViewContainer();
        if (container == null) {
            // The tab consists of a container view, which contains the main
            // WebView, as well as any other UI elements associated with the tab.
            container = mActivity.getLayoutInflater().inflate(R.layout.tab,
                    mContentView, false);
            tab.setViewContainer(container);
        }
        if (tab.getWebView() != webView) {
            // Just remove the old one.
            FrameLayout wrapper =
                    (FrameLayout) container.findViewById(R.id.webview_wrapper);
            wrapper.removeView(tab.getWebView());
        }
    }

    /**
     * create a sub window container and webview for the tab
     * Note: this methods operates through side-effects for now
     * it sets both the subView and subViewContainer for the given tab
     * @param tab tab to create the sub window for
     * @param subView webview to be set as a subwindow for the tab
     */
    @Override
    public void createSubWindow(Tab tab, WebView subView) {
        View subViewContainer = mActivity.getLayoutInflater().inflate(
                R.layout.browser_subwindow, null);
        ViewGroup inner = (ViewGroup) subViewContainer
                .findViewById(R.id.inner_container);
        inner.addView(subView, new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        final ImageButton cancel = (ImageButton) subViewContainer
                .findViewById(R.id.subwindow_close);
        final WebView cancelSubView = subView;
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BrowserWebView) cancelSubView).getWebChromeClient().onCloseWindow(cancelSubView);
            }
        });
        tab.setSubWebView(subView);
        tab.setSubViewContainer(subViewContainer);
    }

    /**
     * Remove the sub window from the content view.
     */
    @Override
    public void removeSubWindow(View subviewContainer) {
        mContentView.removeView(subviewContainer);
        mUiController.endActionMode();
    }

    /**
     * Attach the sub window to the content view.
     */
    @Override
    public void attachSubWindow(View container) {
        if (container.getParent() != null) {
            // already attached, remove first
            ((ViewGroup) container.getParent()).removeView(container);
        }
        mContentView.addView(container, COVER_SCREEN_PARAMS);
    }

    protected void refreshWebView() {
        WebView web = getWebView();
        if (web != null) {
            web.invalidate();
        }
    }

    public void editUrl(boolean clearInput, boolean forceIME) {
        if (mUiController.isInCustomActionMode()) {
            mUiController.endActionMode();
        }
        showTitleBar();
        if ((getActiveTab() != null) && !getActiveTab().isSnapshot()) {
            mNavigationBar.startEditingUrl(clearInput, forceIME);
        }
    }

    boolean canShowTitleBar() {
        return !isTitleBarShowing()
                && !isActivityPaused()
                && (getActiveTab() != null)
                && (getWebView() != null)
                && !mUiController.isInCustomActionMode();
    }

    protected void showTitleBar() {
        if (canShowTitleBar()) {
            mTitleBar.showTopControls(false);
        }
    }

    protected void hideTitleBar() {
        if (mTitleBar.isShowing()) {
            mTitleBar.enableTopControls(false);
        }
    }

    protected boolean isTitleBarShowing() {
        return mTitleBar.isShowing();
    }

    public boolean isEditingUrl() {
        return mTitleBar.isEditingUrl();
    }

    public void stopEditingUrl() {
        mTitleBar.getNavigationBar().stopEditingUrl();
    }

    public TitleBar getTitleBar() {
        return mTitleBar;
    }

    @Override
    public void showComboView(ComboViews startingView, Bundle extras) {
        Intent intent = new Intent(mActivity, ComboViewActivity.class);
        intent.putExtra(ComboViewActivity.EXTRA_INITIAL_VIEW, startingView.name());
        intent.putExtra(ComboViewActivity.EXTRA_COMBO_ARGS, extras);
        Tab t = getActiveTab();
        if (t != null) {
            intent.putExtra(ComboViewActivity.EXTRA_CURRENT_URL, t.getUrl());
        }
        mActivity.startActivityForResult(intent, Controller.COMBO_VIEW);
    }

    @Override
    public void hideComboView() {
    }

    @Override
    public void showCustomView(View view, int requestedOrientation,
            CustomViewCallback callback) {
        // if a view already exists then immediately terminate the new one
        if (mCustomView != null) {
            callback.onCustomViewHidden();
            return;
        }
        mOriginalOrientation = mActivity.getRequestedOrientation();
        FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
        decor.addView(view, COVER_SCREEN_PARAMS);
        mCustomView = view;
        showFullscreen(true);
        ((BrowserWebView) getWebView()).setVisibility(View.INVISIBLE);
        mCustomViewCallback = callback;
        mActivity.setRequestedOrientation(requestedOrientation);
    }

    @Override
    public void onHideCustomView() {
        ((BrowserWebView) getWebView()).setVisibility(View.VISIBLE);
        if (mCustomView == null)
            return;
        showFullscreen(false);
        FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
        decor.removeView(mCustomView);
        mCustomView = null;
        mCustomViewCallback.onCustomViewHidden();
        // Show the content view.
        mActivity.setRequestedOrientation(mOriginalOrientation);
    }

    @Override
    public boolean isCustomViewShowing() {
        return mCustomView != null;
    }

    protected void dismissIME() {
        if (mInputManager.isActive()) {
            mInputManager.hideSoftInputFromWindow(mContentView.getWindowToken(),
                    0);
        }
    }

    @Override
    public boolean isWebShowing() {
        return mCustomView == null;
    }

    @Override
    public boolean isComboViewShowing() {
        return false;
    }

    public static boolean isUiLowPowerMode() {
        return BrowserCommandLine.hasSwitch("ui-low-power-mode")
            || BrowserSettings.getInstance().isPowerSaveModeEnabled()
            || BrowserSettings.getInstance().isDisablePerfFeatures();
    }

    // -------------------------------------------------------------------------

    protected void updateNavigationState(Tab tab) {
    }

    /**
     * Update the lock icon to correspond to our latest state.
     */
    private void updateTabSecurityState(Tab t) {
        if (t != null && t.inForeground()) {
            mNavigationBar.setSecurityState(t.getSecurityState());
            setUrlTitle(t);
        }
    }

    protected void setUrlTitle(Tab tab) {
        String url = tab.getUrl();
        String title = tab.getTitle();
        if (TextUtils.isEmpty(title)) {
            title = url;
        }
        if (tab.inForeground()) {
            mNavigationBar.setDisplayTitle(title, url);
        }
    }

    // Set the favicon in the title bar.
    public void setFavicon(Tab tab) {
        mNavigationBar.showCurrentFavicon(tab);
    }

    // active tabs page

    public void showActiveTabsPage() {
    }

    /**
     * Remove the active tabs page.
     */
    public void removeActiveTabsPage() {
    }

    // menu handling callbacks

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public void updateMenuState(Tab tab, Menu menu) {
    }

    @Override
    public void onOptionsMenuOpened() {
    }

    @Override
    public void onExtendedMenuOpened() {
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return false;
    }

    @Override
    public void onOptionsMenuClosed(boolean inLoad) {
    }

    @Override
    public void onExtendedMenuClosed(boolean inLoad) {
    }

    @Override
    public void onContextMenuCreated(Menu menu) {
    }

    @Override
    public void onContextMenuClosed(Menu menu, boolean inLoad) {
    }

    // -------------------------------------------------------------------------
    // Helper function for WebChromeClient
    // -------------------------------------------------------------------------

    @Override
    public Bitmap getDefaultVideoPoster() {
        if (mDefaultVideoPoster == null) {
            mDefaultVideoPoster = BitmapFactory.decodeResource(
                    mActivity.getResources(), R.drawable.default_video_poster);
        }
        return mDefaultVideoPoster;
    }

    @Override
    public View getVideoLoadingProgressView() {
        if (mVideoProgressView == null) {
            LayoutInflater inflater = LayoutInflater.from(mActivity);
            mVideoProgressView = inflater.inflate(
                    R.layout.video_loading_progress, null);
        }
        return mVideoProgressView;
    }

    @Override
    public void showMaxTabsWarning() {
        Toast warning = Toast.makeText(mActivity,
                mActivity.getString(R.string.max_tabs_warning),
                Toast.LENGTH_SHORT);
        warning.show();
    }

    protected WebView getWebView() {

        if (mActiveTab != null) {
            return mActiveTab.getWebView();
        } else {
            return null;
        }
    }

    public void forceDisableFullscreenMode(boolean disabled) {
        mFullscreenModeLocked = false;
        setFullscreen(!disabled);
        mFullscreenModeLocked = disabled;
    }

    public void setFullscreen(boolean enabled) {
        if (mFullscreenModeLocked)
            return;

        Window win = mActivity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        final int fullscreenImmersiveSetting =
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

        if (mCustomView != null) {
            mCustomView.setSystemUiVisibility(enabled ?
                    fullscreenImmersiveSetting : View.SYSTEM_UI_FLAG_VISIBLE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            mContentView.setSystemUiVisibility(enabled ?
                    fullscreenImmersiveSetting  : View.SYSTEM_UI_FLAG_VISIBLE);
        } else {
            mContentView.setSystemUiVisibility(enabled ?
                    View.SYSTEM_UI_FLAG_LOW_PROFILE  : View.SYSTEM_UI_FLAG_VISIBLE);
        }
        if (enabled) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }

        win.setAttributes(winParams);
    }

    //make full screen by showing/hiding topbar and system status bar
    public void showFullscreen(boolean fullScreen) {
        ImageView shadow = (ImageView) mActivity.findViewById(R.id.titleBar_dropShadow);

        //Hide/show system ui bar as needed
        if (!BrowserSettings.getInstance().useFullscreen())
            setFullscreen(fullScreen);
        //Hide/show topbar as needed
        if (getWebView() != null) {
            BrowserWebView bwv = (BrowserWebView) getWebView();
            if (fullScreen) {
                // hide titlebar
                mTitleBar.hideTopControls(true);
                //Hide the Titlebar DropShadow
                shadow.setVisibility(View.GONE);
            } else {
                // show titlebar
                mTitleBar.showTopControls(false);
                //Show the Titlebar DropShadow
                shadow.setVisibility(View.VISIBLE);
                // enable auto hide titlebar
                if (!mTitleBar.isFixed())
                    mTitleBar.enableTopControls(false);
            }
        }
    }

    public void translateTitleBar(float topControlsOffsetYPix) {
        if (mTitleBar == null || mTitleBar.isFixed())
            return;
        if (!mInActionMode) {
            if (topControlsOffsetYPix != 0.0) {
                mTitleBar.setEnabled(false);
            } else {
                mTitleBar.setEnabled(true);
            }
            float currentY = mTitleBar.getTranslationY();
            float height = mNavigationBar.getHeight();
            if ((height + currentY) <= 0 && (height + topControlsOffsetYPix) > 0) {
                mTitleBar.requestLayout();
            } else if ((height + topControlsOffsetYPix) <= 0) {
                topControlsOffsetYPix -= 1;
                mTitleBar.getParent().requestTransparentRegion(mTitleBar);
            }
            // This was done to get HTML5 fullscreen API to work with fixed mode since
            // topcontrols are used to implement HTML5 fullscreen
            mTitleBar.setTranslationY(topControlsOffsetYPix);
        }
    }

    public Drawable getFaviconDrawable(Bitmap icon) {
        if (ENABLE_BORDER_AROUND_FAVICON) {
            Drawable[] array = new Drawable[3];
            array[0] = new PaintDrawable(Color.BLACK);
            PaintDrawable p = new PaintDrawable(Color.WHITE);
            array[1] = p;
            if (icon == null) {
                array[2] = getGenericFavicon();
            } else {
                array[2] = new BitmapDrawable(mActivity.getResources(), icon);
            }
            LayerDrawable d = new LayerDrawable(array);
            d.setLayerInset(1, 1, 1, 1, 1);
            d.setLayerInset(2, 2, 2, 2, 2);
            return d;
        }
        return icon == null ? getGenericFavicon() : new BitmapDrawable(mActivity.getResources(), icon);
    }

    public boolean isLoading() {
        return mActiveTab != null ? mActiveTab.inPageLoad() : false;
    }

    protected void setMenuItemVisibility(Menu menu, int id,
                                         boolean visibility) {
        MenuItem item = menu.findItem(id);
        if (item != null) {
            item.setVisible(visibility);
        }
    }

    protected Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            BaseUi.this.handleMessage(msg);
        }
    };

    protected void handleMessage(Message msg) {}

    @Override
    public void showWeb(boolean animate) {
        mUiController.hideCustomView();
    }

    public void setContentViewMarginTop(int margin) {
        FrameLayout.LayoutParams params =
                (FrameLayout.LayoutParams) mContentView.getLayoutParams();
        if (params.topMargin != margin) {
            params.topMargin = margin;
            mContentView.setLayoutParams(params);
        }
    }

    @Override
    public boolean blockFocusAnimations() {
        return mBlockFocusAnimations;
    }

    @Override
    public void onVoiceResult(String result) {
        mNavigationBar.onVoiceResult(result);
    }

    protected UiController getUiController() {
        return mUiController;
    }

    boolean mInActionMode = false;
    private float getActionModeHeight() {
        TypedArray actionBarSizeTypedArray = mActivity.obtainStyledAttributes(
                    new int[] { android.R.attr.actionBarSize });
        float size = actionBarSizeTypedArray.getDimension(0, 0f);
        actionBarSizeTypedArray.recycle();
        return size;
    }


    @Override
    public void onActionModeStarted(ActionMode mode) {
        mInActionMode = true;

        if (mTitleBar.isFixed()) {
            int fixedTbarHeight = mTitleBar.calculateEmbeddedHeight();
            setContentViewMarginTop(fixedTbarHeight);
        } else {
            mTitleBar.setTranslationY(getActionModeHeight());
        }
    }

    @Override
    public void onActionModeFinished(boolean inLoad) {
        mInActionMode = false;
        if (mTitleBar.isFixed()) {
            setContentViewMarginTop(0);
        } else {
            mTitleBar.setTranslationY(0);
        }
    }

    @Override
    public boolean shouldCaptureThumbnails() {
        return true;
    }
}