Code example for Handler

Methods: removeMessagessendMessageDelayed, obtain

0
        if (web != null) {
            web.setTitleBarGravity(gravity);
        } 
    } 
 
    @Override 
    public void showVoiceTitleBar(String title, List<String> results) {
        mNavigationBar.setInVoiceMode(true, results);
        mNavigationBar.setDisplayTitle(title);
    } 
 
    @Override 
    public void revertVoiceTitleBar(Tab tab) {
        mNavigationBar.setInVoiceMode(false, null);
        String url = tab.getUrl();
        mNavigationBar.setDisplayTitle(url);
    } 
 
    @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 showCustomView(View view, int requestedOrientation,
            WebChromeClient.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();
        mFullscreenContainer = new FullscreenHolder(mActivity);
        mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
        decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
        mCustomView = view;
        setFullscreen(true); 
        mCustomViewCallback = callback;
        mActivity.setRequestedOrientation(requestedOrientation);
    } 
 
    @Override 
    public void onHideCustomView() { 
        if (mCustomView == null)
            return; 
        setFullscreen(false); 
        FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
        decor.removeView(mFullscreenContainer);
        mFullscreenContainer = null;
        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 void showAutoLogin(Tab tab) {
        updateAutoLogin(tab, true);
    } 
 
    @Override 
    public void hideAutoLogin(Tab tab) {
        updateAutoLogin(tab, true);
    } 
 
    // ------------------------------------------------------------------------- 
 
    protected void updateNavigationState(Tab tab) {
    } 
 
    protected void updateAutoLogin(Tab tab, boolean animate) {
        mTitleBar.updateAutoLogin(tab, animate);
    } 
 
    /** 
     * Update the lock icon to correspond to our latest state. 
     */ 
    protected void updateLockIconToLatest(Tab t) {
        if (t != null && t.inForeground()) {
            updateLockIconImage(t.getSecurityState());
        } 
    } 
 
    /** 
     * Updates the lock-icon image in the title-bar. 
     */ 
    private void updateLockIconImage(SecurityState securityState) {
        Drawable d = null;
        if (securityState == SecurityState.SECURITY_STATE_SECURE) {
            d = mLockIconSecure;
        } else if (securityState == SecurityState.SECURITY_STATE_MIXED
                || securityState == SecurityState.SECURITY_STATE_BAD_CERTIFICATE) {
            // TODO: It would be good to have different icons for insecure vs mixed content. 
            // See http://b/5403800 
            d = mLockIconMixed;
        } 
        mNavigationBar.setLock(d);
    } 
 
    protected void setUrlTitle(Tab tab) {
        String url = tab.getUrl();
        String title = tab.getTitle();
        if (TextUtils.isEmpty(title)) {
            title = url;
        } 
        if (tab.isInVoiceSearchMode()) return;
        if (tab.inForeground()) {
            mNavigationBar.setDisplayTitle(url);
        } 
    } 
 
    // Set the favicon in the title bar. 
    protected void setFavicon(Tab tab) {
        if (tab.inForeground()) {
            Bitmap icon = tab.getFavicon();
            mNavigationBar.setFavicon(icon);
        } 
    } 
 
    @Override 
    public void onActionModeFinished(boolean inLoad) {
    } 
 
    // 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) {
    } 
 
    // error console 
 
    @Override 
    public void setShouldShowErrorConsole(Tab tab, boolean flag) {
        if (tab == null) return;
        ErrorConsoleView errorConsole = tab.getErrorConsole(true);
        if (flag) {
            // Setting the show state of the console will cause it's the layout 
            // to be inflated. 
            if (errorConsole.numberOfErrors() > 0) {
                errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
            } else { 
                errorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
            } 
            if (errorConsole.getParent() != null) {
                mErrorConsoleContainer.removeView(errorConsole);
            } 
            // Now we can add it to the main view. 
            mErrorConsoleContainer.addView(errorConsole,
                    new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT));
        } else { 
            mErrorConsoleContainer.removeView(errorConsole);
        } 
    } 
 
    // ------------------------------------------------------------------------- 
    // 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; 
        } 
    } 
 
    protected Menu getMenu() {
        MenuBuilder menu = new MenuBuilder(mActivity);
        mActivity.getMenuInflater().inflate(R.menu.browser, menu);
        return menu;
    } 
 
    public void setFullscreen(boolean enabled) {
        Window win = mActivity.getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        if (enabled) {
            winParams.flags |=  bits;
        } else { 
            winParams.flags &= ~bits;
            if (mCustomView != null) {
                mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            } else { 
                mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
            } 
        } 
        win.setAttributes(winParams);
    } 
 
    public Drawable getFaviconDrawable(Bitmap icon) {
        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] = mGenericFavicon;
        } else { 
            array[2] = new BitmapDrawable(icon);
        } 
        LayerDrawable d = new LayerDrawable(array);
        d.setLayerInset(1, 1, 1, 1, 1);
        d.setLayerInset(2, 2, 2, 2, 2);
        return d;
    } 
 
    public boolean isLoading() { 
        return mActiveTab != null ? mActiveTab.inPageLoad() : false;
    } 
 
    /** 
     * Suggest to the UI that the title bar can be hidden. The UI will then 
     * decide whether or not to hide based off a number of factors, such 
     * as if the user is editing the URL bar or if the page is loading 
     */ 
    public void suggestHideTitleBar() { 
        if (!isLoading() && !isEditingUrl() && !mTitleBar.wantsToBeVisible()
                && !mNavigationBar.isMenuShowing()) {
            hideTitleBar(); 
        } 
    } 
 
    protected final void showTitleBarForDuration() { 
        showTitleBarForDuration(HIDE_TITLEBAR_DELAY);
    } 
 
    protected final void showTitleBarForDuration(long duration) {
        showTitleBar(); 
        Message msg = Message.obtain(mHandler, MSG_HIDE_TITLEBAR);
        mHandler.sendMessageDelayed(msg, duration);
    } 
 
    protected Handler mHandler = new Handler() {
 
        @Override 
        public void handleMessage(Message msg) { 
            if (msg.what == MSG_HIDE_TITLEBAR) { 
                suggestHideTitleBar(); 
            } 
            BaseUi.this.handleMessage(msg); 
        } 
    }; 
 
    protected void handleMessage(Message msg) {}
 
    @Override 
    public void showWeb(boolean animate) {
        mUiController.hideCustomView();
    } 
 
    static class FullscreenHolder extends FrameLayout {