Code example for WebView

Methods: setMapTrackballToArrowKeyssetWebChromeClientsetWebViewClient, setDownloadListener, setOnCreateContextMenuListener

0
    /** 
     * Creates a new WebView and registers it with the global settings. 
     */ 
    private WebView createNewWebView() {
        // Create a new WebView 
        WebView w = new WebView(mActivity);
        w.setMapTrackballToArrowKeys(false); // use trackball directly
        // Enable the built-in zoom 
        //w.getSettings().setBuiltInZoomControls(false); 
 
        // Add this WebView to the settings observer list and update the 
        // settings 
        final BrowserSettings s = BrowserSettings.getInstance();
        s.addObserver(w.getSettings()).update(s, null);
        return w;
    } 
 
    /** 
     * Put the current tab in the background and set newTab as the current tab. 
     * @param newTab The new tab. If newTab is null, the current tab is not 
     *               set. 
     */ 
    boolean setCurrentTab(Tab newTab) {
        return setCurrentTab(newTab, false);
    } 
 
    /** 
     * If force is true, this method skips the check for newTab == current. 
     */ 
    private boolean setCurrentTab(Tab newTab, boolean force) {
        Tab current = getTab(mCurrentTab);
        if (current == newTab && !force) {
            return true; 
        } 
        if (current != null) {
            // Remove the current WebView and the container of the subwindow 
            putTabInBackground(current);
        } 
 
        if (newTab == null) {
            return false; 
        } 
 
        // Move the newTab to the end of the queue 
        int index = mTabQueue.indexOf(newTab);
        if (index != -1) {
            mTabQueue.remove(index);
        } 
        mTabQueue.add(newTab);
 
        WebView mainView;
        WebView subView;
 
        // Display the new current tab 
        mCurrentTab = mTabs.indexOf(newTab);
        mainView = newTab.mMainView;
        boolean needRestore = (mainView == null);
        if (needRestore) {
            // Same work as in createNewTab() except don't do new Tab() 
            newTab.mMainView = mainView = createNewWebView();
        } 
        mainView.setWebViewClient(mActivity.getWebViewClient());
        mainView.setWebChromeClient(mActivity.getWebChromeClient());
        mainView.setOnCreateContextMenuListener(mActivity);
        mainView.setDownloadListener(mActivity);
        // Add the subwindow if it exists 
        if (newTab.mSubViewContainer != null) {
            subView = newTab.mSubView;
            subView.setWebViewClient(newTab.mSubViewClient);
            subView.setWebChromeClient(newTab.mSubViewChromeClient);
            subView.setOnCreateContextMenuListener(mActivity);
            subView.setDownloadListener(mActivity);
        } 
        if (needRestore) {
            // Have to finish setCurrentTab work before calling restoreState 
            if (!restoreState(newTab.mSavedState, newTab)) {
                mainView.loadUrl(BrowserSettings.getInstance().getHomePage());
            } 
        } 
        return true; 
    } 
 
    /* 
     * Put the tab in the background using all the empty/background clients. 
     */ 
    private void putTabInBackground(Tab t) {
        WebView mainView = t.mMainView;
        // Set an empty callback so that default actions are not triggered. 
        mainView.setWebViewClient(mEmptyClient);
        mainView.setWebChromeClient(mBackgroundChromeClient);
        mainView.setOnCreateContextMenuListener(null);
        // Leave the DownloadManager attached so that downloads can start in 
        // a non-active window. This can happen when going to a site that does 
        // a redirect after a period of time. The user could have switched to 
        // another tab while waiting for the download to start. 
        mainView.setDownloadListener(mActivity);
        WebView subView = t.mSubView;
        if (subView != null) {
            // Set an empty callback so that default actions are not triggered. 
            subView.setWebViewClient(mEmptyClient);