Search in sources :

Example 6 with WebHistoryItem

use of android.webkit.WebHistoryItem 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 7 with WebHistoryItem

use of android.webkit.WebHistoryItem 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)

Example 8 with WebHistoryItem

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

the class WebViewDebugOverlay method updateViewTreeList.

private void updateViewTreeList(View rootView, int level) {
    if (!(rootView instanceof ViewGroup)) {
        return;
    }
    StringBuilder builder = new StringBuilder();
    int count = 0;
    while (count < level) {
        if (count < level - 1) {
            builder.append("  ");
            count += 1;
        } else if (count == level - 1) {
            builder.append("|-");
            count += 2;
        } else {
            builder.append("--");
            count += 2;
        }
    }
    builder.append(rootView.getClass().getSimpleName());
    if (rootView instanceof WebView) {
        WebHistoryItem item = ((WebView) rootView).copyBackForwardList().getCurrentItem();
        if (item != null) {
            builder.append("(").append(item.getOriginalUrl()).append(")");
        }
    }
    insertText(builder.toString(), Color.LTGRAY, viewTreeList);
    if (rootView == viewTreeList) {
        return;
    }
    ViewGroup viewGroup = (ViewGroup) rootView;
    for (int i = 0; i < viewGroup.getChildCount(); ++i) {
        updateViewTreeList(viewGroup.getChildAt(i), level + 1);
    }
}
Also used : WebHistoryItem(android.webkit.WebHistoryItem) ViewGroup(android.view.ViewGroup) WebView(android.webkit.WebView) SuppressLint(android.annotation.SuppressLint)

Aggregations

WebHistoryItem (android.webkit.WebHistoryItem)8 WebBackForwardList (android.webkit.WebBackForwardList)7 SuppressLint (android.annotation.SuppressLint)3 WebView (android.webkit.WebView)3 TabView (org.mozilla.focus.tabs.TabView)2 ViewGroup (android.view.ViewGroup)1