Search in sources :

Example 16 with WebBackForwardList

use of android.webkit.WebBackForwardList in project robolectric by robolectric.

the class ShadowWebViewTest method shouldSaveAndRestoreHistoryList_noPushedEntries.

@Test
public void shouldSaveAndRestoreHistoryList_noPushedEntries() {
    webView.loadUrl("foo1.bar");
    Bundle outState = new Bundle();
    webView.saveState(outState);
    WebView newWebView = new WebView(ApplicationProvider.getApplicationContext());
    WebBackForwardList historyList = newWebView.restoreState(outState);
    assertThat(newWebView.canGoBack()).isFalse();
    assertThat(newWebView.canGoForward()).isFalse();
    assertThat(newWebView.getUrl()).isNull();
    assertThat(historyList).isNull();
}
Also used : Bundle(android.os.Bundle) WebView(android.webkit.WebView) WebBackForwardList(android.webkit.WebBackForwardList) Test(org.junit.Test)

Example 17 with WebBackForwardList

use of android.webkit.WebBackForwardList in project phonegap-facebook-plugin by Wizcorp.

the class CordovaWebView method startOfHistory.

// Can Go Back is BROKEN!
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);
    }
    return false;
}
Also used : WebHistoryItem(android.webkit.WebHistoryItem) WebBackForwardList(android.webkit.WebBackForwardList)

Example 18 with WebBackForwardList

use of android.webkit.WebBackForwardList in project Rocket by mozilla-tw.

the class WebkitView method restoreViewState.

@Override
public void restoreViewState(Bundle savedInstanceState) {
    // We need to have a different method name because restoreState() returns
    // a WebBackForwardList, and we can't overload with different return types:
    final WebBackForwardList backForwardList = restoreState(savedInstanceState);
    // Pages are only added to the back/forward list when loading finishes. If a new page is
    // loading when the Activity is paused/killed, then that page won't be in the list,
    // and needs to be restored separately to the history list. We detect this by checking
    // whether the last fully loaded page (getCurrentItem()) matches the last page that the
    // WebView was actively loading (which was retrieved during saveViewState():
    // WebView.getUrl() always returns the currently loading or loaded page).
    // If the app is paused/killed before the initial page finished loading, then the entire
    // list will be null - so we need to additionally check whether the list even exists.
    final String desiredURL = savedInstanceState.getString(KEY_CURRENTURL);
    // in saveViewState. In any cases we can not get desiredURL, no need to load it.
    if (TextUtils.isEmpty(desiredURL)) {
        return;
    }
    webViewClient.notifyCurrentURL(desiredURL);
    WebHistoryItem currentItem;
    if (backForwardList != null && (currentItem = backForwardList.getCurrentItem()) != null) {
        String latestHistoryUrl = currentItem.getUrl();
        if (desiredURL.equals(latestHistoryUrl)) {
            // The last url WebView saved is consistent with what we saved.
            reload();
        } else {
            // noinspection StatementWithEmptyBody, for clearer logic and comment
            if (UrlUtils.isInternalErrorURL(latestHistoryUrl) && desiredURL.equals(getUrl())) {
            // Case 1: What we get from WebHistoryItem:
            // WebHistoryItem#getOriginalUrl(): error-url
            // WebHistoryItem#getUrl(): error-url
            // 
            // However, after WebView restores this error-url, WebView#getUrl() will instead
            // return a virtual url given when we called loadDataWithBaseUrl(..., virtualUrl)
            // 
            // What we get from WebView after error-url is restored:
            // WebView#getOriginalUrl(): error-url
            // WebView#getUrl(): virtual-url
            // The condition here means "WebView and us agree the last page is <virtual-url>,
            // which however, ends up become an error page.
            } else {
                // Case 2, we have more up-to-date url than WebView, load it
                loadUrl(desiredURL);
            }
        }
    } else {
        // WebView saved nothing, so directly load what we recorded
        loadUrl(desiredURL);
    }
}
Also used : WebHistoryItem(android.webkit.WebHistoryItem) WebBackForwardList(android.webkit.WebBackForwardList)

Example 19 with WebBackForwardList

use of android.webkit.WebBackForwardList in project Rocket by mozilla-tw.

the class WebViewDebugOverlay method updateHistory.

public void updateHistory() {
    if (isEnable()) {
        backForwardList.removeAllViews();
        WebBackForwardList list = this.webView.copyBackForwardList();
        int size = list.getSize();
        int curr = list.getCurrentIndex();
        if (curr < 0) {
            return;
        }
        insertHistory("size:" + size + ", curr:" + curr, Color.WHITE);
        int first = curr - 2;
        int last = curr + 2;
        if (first < 0) {
            last -= first;
            first = 0;
        }
        if (last >= size) {
            first = Math.max(0, first - (last - size + 1));
            last = size - 1;
        }
        if (first != 0) {
            insertHistory("...", Color.WHITE);
        }
        for (int i = first; i <= last; ++i) {
            WebHistoryItem item = list.getItemAtIndex(i);
            String line = item.getOriginalUrl().replaceAll("https://", "");
            String line2 = item.getUrl().replaceAll("https://", "");
            int color;
            if (list.getCurrentIndex() == i) {
                color = UrlUtils.isInternalErrorURL(item.getOriginalUrl()) || UrlUtils.isInternalErrorURL(item.getUrl()) ? Color.RED : Color.GREEN;
            } else {
                color = Color.LTGRAY;
            }
            insertHistory(i + ": " + line, color);
            if (!line.equals(line2)) {
                insertHistory("-> " + line2, color);
            }
            insertDivider(backForwardList);
        }
        updateViewTree();
    }
}
Also used : WebHistoryItem(android.webkit.WebHistoryItem) WebBackForwardList(android.webkit.WebBackForwardList) SuppressLint(android.annotation.SuppressLint)

Aggregations

WebBackForwardList (android.webkit.WebBackForwardList)19 Test (org.junit.Test)10 WebHistoryItem (android.webkit.WebHistoryItem)7 Bundle (android.os.Bundle)6 WebView (android.webkit.WebView)5 SuppressLint (android.annotation.SuppressLint)2 TabView (org.mozilla.focus.tabs.TabView)2