Example usage for android.webkit WebBackForwardList getItemAtIndex

List of usage examples for android.webkit WebBackForwardList getItemAtIndex

Introduction

In this page you can find the example usage for android.webkit WebBackForwardList getItemAtIndex.

Prototype

public abstract WebHistoryItem getItemAtIndex(int index);

Source Link

Document

Get the history item at the given index.

Usage

From source file:com.example.administrator.mywebviewdrawsign.SysWebView.java

public boolean startOfHistory() {
    WebBackForwardList currentList = this.copyBackForwardList();
    WebHistoryItem item = currentList.getItemAtIndex(0);
    String url = item.getUrl();/*  ww  w  .  j  a  v  a 2  s. c om*/
    String currentUrl = this.getUrl();
    LogUtils.d("The current URL is: " + currentUrl);
    LogUtils.d("The URL at item 0 is:" + url);
    return currentUrl.equals(url);
}

From source file:com.robandjen.comicsapp.FullscreenActivity.java

@Override
public void onBackPressed() {
    ComicsWebView wv = (ComicsWebView) findViewById(R.id.fullscreen_content);
    if (wv.canGoBack()) {

        //I couldn't remove every URL from the list, so stop when I hit
        //about:blank, I'm all the way back then
        WebBackForwardList bdl = wv.copyBackForwardList();
        final int cur = bdl.getCurrentIndex() - 1;
        if (cur >= 0) {
            WebHistoryItem whi = bdl.getItemAtIndex(cur);
            String url = whi.getUrl();
            if (!url.equals("about:blank")) {
                wv.goBack();/*from   w ww  . j  av  a  2 s  .c  o  m*/
            }
        }

    }

}

From source file:com.ronnywu.browserview.view.Lv2GoBackBrowserView.java

/**
 * onBackPressed ???,?? onBackPressed,onKeyDown.
 *  onBackPressed , -1 , null .//from   w w  w. j a  va  2 s. com
 *
 * @param keyCode code
 * @return ?.
 */
public boolean backPressed(int keyCode, KeyEvent event) {
    if (keyCode != KeyEvent.KEYCODE_BACK) {
        // ??,???,
        return super.onKeyDown(keyCode, event);
    }
    WebBackForwardList webBackForwardList = copyBackForwardList();
    int currentIndex = webBackForwardList.getCurrentIndex();
    if (browserGoBackListener != null) {
        int goBackSize = 0;
        if (currentIndex <= 0) {
            return false;
        }
        for (int i = currentIndex; i > 0; i--) {
            goBackSize--;
            // ?0,??, false,?.
            if (goBackSize + currentIndex < 0) {
                return false;
            }
            String url = webBackForwardList.getItemAtIndex(i - 1).getUrl();
            String title = webBackForwardList.getItemAtIndex(i - 1).getTitle();
            if (browserGoBackListener.canGoBack(url, title)) {
                goBackOrForward(goBackSize);
                break;
            }
        }
        return true;
    } else {
        if (currentIndex <= 0) {
            return false;
        }
        String goBackUrl = webBackForwardList.getItemAtIndex(currentIndex - 1).getUrl();
        if (jumpingUrls != null && jumpingUrls.size() > 0) {
            for (ArrayMap.Entry<String, String> entry : jumpingUrls.entrySet()) {
                // ?URL?,??
                String jumpingUrl = entry.getKey();
                // ?URL?????2
                String jumpingDispose = entry.getValue();
                // regex: ?scheme
                if (jumpingUrl.startsWith("regex:")) {
                    // ??
                    if (goBackUrl.matches(jumpingUrl.replace("regex:", ""))) {
                        // switch,??java6??
                        // Switch??
                        // noinspection IfCanBeSwitch
                        return doJupingDispose(jumpingDispose);
                    }
                    // ????,???
                } else {
                    // URL???
                    if (jumpingUrl.equals(goBackUrl)) {
                        // switch,??java6??
                        // noinspection IfCanBeSwitch
                        return doJupingDispose(jumpingDispose);
                    }
                    // URL?????,???
                }
            }
        }

        // ?,???,?

        if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == -1) && canGoBack() && currentIndex > 0) {
            goBack();
            return true;
        } else {
            return false;
        }
    }
}

From source file:org.apache.cordova.AndroidWebView.java

public boolean startOfHistory() {
    WebBackForwardList currentList = this.copyBackForwardList();
    WebHistoryItem item = currentList.getItemAtIndex(0);
    if (item != null) { // Null-fence in case they haven't called loadUrl yet (CB-2458)
        String url = item.getUrl();
        String currentUrl = this.getUrl();
        LOG.d(TAG, "The current URL is: " + currentUrl);
        LOG.d(TAG, "The URL at item 0 is: " + url);
        return currentUrl.equals(url);
    }/*from  w w  w.  j  a v a  2s.c o  m*/
    return false;
}

From source file:org.apache.cordova.AndroidWebView.java

public void printBackForwardList() {
    WebBackForwardList currentList = this.copyBackForwardList();
    int currentSize = currentList.getSize();
    for (int i = 0; i < currentSize; ++i) {
        WebHistoryItem item = currentList.getItemAtIndex(i);
        String url = item.getUrl();
        LOG.d(TAG, "The URL at index: " + Integer.toString(i) + " is " + url);
    }/*  w w w .  j  av  a  2  s  .  co  m*/
}

From source file:dev.memento.MementoBrowser.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebview.canGoBack()) {

        // Get the previous URL to update our internal copy 

        WebBackForwardList list = mWebview.copyBackForwardList();
        int curr = list.getCurrentIndex();
        WebHistoryItem item = list.getItemAtIndex(curr - 1);
        Bitmap favicon = item.getFavicon();

        if (favicon == null)
            Log.d(LOG_TAG, "No favicon in WebHistoryItem - null");
        else/*from ww w . j  a  v a2 s.  c  om*/
            Log.d(LOG_TAG, "WebHistoryItem favicon W = " + favicon.getWidth());

        mOriginalUrl = item.getUrl();
        Log.d(LOG_TAG, "GO BACK TO " + mOriginalUrl);

        mWebview.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:dev.memento.MainActivity.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    // Back button goes back to previous page or memento
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebview.canGoBack()) {

        // Get the previous URL to update our internal copy 

        WebBackForwardList list = mWebview.copyBackForwardList();
        int curr = list.getCurrentIndex();
        WebHistoryItem item = list.getItemAtIndex(curr - 1);
        String url = item.getUrl();

        if (Log.LOG)
            Log.d(LOG_TAG, "GO BACK TO " + url);

        mWebview.goBack();//from   w w w  .jav  a2s. c  o  m

        // Reset to today because it might be confusing to set to other dates
        setChosenDate(mToday);

        // Just in case
        mMementoProgressBar.setVisibility(View.GONE);

        if (Log.LOG)
            Log.d(LOG_TAG, "mWebHistory size = " + mWebHistory.size());
        if (mWebHistory.isEmpty()) {
            if (Log.LOG)
                Log.e(LOG_TAG, "Stack maintaining page dates is empty!");
        } else {
            ExWebHistoryItem item2 = mWebHistory.pop(); // Remove current
            if (Log.LOG)
                Log.d(LOG_TAG, "!! Removed item " + item2 + " from stack");

            if (!mWebHistory.isEmpty()) {
                item2 = mWebHistory.peek(); // Previous
                if (Log.LOG)
                    Log.d(LOG_TAG, "!! Top item is " + item2);
            }

            mCurrentMemento = item2.getMemento();
            if (Log.LOG)
                Log.d(LOG_TAG, "mCurrentMemento = " + mCurrentMemento);
            if (mCurrentMemento == null)
                mDateDisplayed = mToday;
            else
                mDateDisplayed = mCurrentMemento.getDateTime();

            if (Log.LOG)
                Log.d(LOG_TAG, "!! Back to date " + mDateDisplayed);
            refreshDisplayedDate();

            // Indicate the user pressed the back button
            mUserPressedBack = true;

            // Should be able to press if it's not today
            mNowButton.setEnabled(mToday.compareDateTo(mDateDisplayed) != 0);

            //if (mCurrentMemento != null && !mCurrentMemento.getUrl().equals(url) && 
            //      !url.equals(mOriginalUrl)) {
            if (!mOriginalUrl.equals(item2.getOriginalUrl())) {
                mOriginalUrl = item2.getOriginalUrl();
                if (Log.LOG)
                    Log.d(LOG_TAG, "** Clearing list of old mementos");
                if (Log.LOG)
                    Log.d(LOG_TAG, "mOriginalUrl = " + mOriginalUrl);
                mMementos.clear();
            }
        }

        return true;
    }
    return super.onKeyDown(keyCode, event);
}

From source file:android.webkit.cts.WebViewTest.java

private boolean checkWebBackForwardList(WebBackForwardList list, String currUrl, int currIndex, int size) {
    return (list != null) && (list.getSize() == size) && (list.getCurrentIndex() == currIndex)
            && list.getItemAtIndex(currIndex).getUrl().equals(currUrl);
}

From source file:android.webkit.cts.WebViewTest.java

@UiThreadTest
public void testSaveAndRestoreState() throws Throwable {
    if (!NullWebViewUtils.isWebViewAvailable()) {
        return;// w ww  .ja  va2s .  c  o  m
    }
    // nothing to save
    assertNull(mWebView.saveState(new Bundle()));

    startWebServer(false);
    String url1 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL1);
    String url2 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL2);
    String url3 = mWebServer.getAssetUrl(TestHtmlConstants.HTML_URL3);

    // make a history list
    mOnUiThread.loadUrlAndWaitForCompletion(url1);
    pollingCheckWebBackForwardList(url1, 0, 1);
    mOnUiThread.loadUrlAndWaitForCompletion(url2);
    pollingCheckWebBackForwardList(url2, 1, 2);
    mOnUiThread.loadUrlAndWaitForCompletion(url3);
    pollingCheckWebBackForwardList(url3, 2, 3);

    // save the list
    Bundle bundle = new Bundle();
    WebBackForwardList saveList = mWebView.saveState(bundle);
    assertNotNull(saveList);
    assertEquals(3, saveList.getSize());
    assertEquals(2, saveList.getCurrentIndex());
    assertEquals(url1, saveList.getItemAtIndex(0).getUrl());
    assertEquals(url2, saveList.getItemAtIndex(1).getUrl());
    assertEquals(url3, saveList.getItemAtIndex(2).getUrl());

    // change the content to a new "blank" web view without history
    final WebView newWebView = new WebView(getActivity());

    WebBackForwardList copyListBeforeRestore = newWebView.copyBackForwardList();
    assertNotNull(copyListBeforeRestore);
    assertEquals(0, copyListBeforeRestore.getSize());

    // restore the list
    final WebBackForwardList restoreList = newWebView.restoreState(bundle);
    assertNotNull(restoreList);
    assertEquals(3, restoreList.getSize());
    assertEquals(2, saveList.getCurrentIndex());

    // wait for the list items to get inflated
    new PollingCheck(TEST_TIMEOUT) {
        @Override
        protected boolean check() {
            return restoreList.getItemAtIndex(0).getUrl() != null
                    && restoreList.getItemAtIndex(1).getUrl() != null
                    && restoreList.getItemAtIndex(2).getUrl() != null;
        }
    }.run();
    assertEquals(url1, restoreList.getItemAtIndex(0).getUrl());
    assertEquals(url2, restoreList.getItemAtIndex(1).getUrl());
    assertEquals(url3, restoreList.getItemAtIndex(2).getUrl());

    WebBackForwardList copyListAfterRestore = newWebView.copyBackForwardList();
    assertNotNull(copyListAfterRestore);
    assertEquals(3, copyListAfterRestore.getSize());
    assertEquals(2, copyListAfterRestore.getCurrentIndex());
    assertEquals(url1, copyListAfterRestore.getItemAtIndex(0).getUrl());
    assertEquals(url2, copyListAfterRestore.getItemAtIndex(1).getUrl());
    assertEquals(url3, copyListAfterRestore.getItemAtIndex(2).getUrl());
}