com.ronnywu.browserview.view.Lv2GoBackBrowserView.java Source code

Java tutorial

Introduction

Here is the source code for com.ronnywu.browserview.view.Lv2GoBackBrowserView.java

Source

/*
 * Copyright (C) 2016 RonnyWu.Pe@Gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ronnywu.browserview.view;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.webkit.WebBackForwardList;

import com.ronnywu.browserview.client.Lv2GoBackWebViewClient;

/**
 *  {@link Lv1BaseBrowserView},,?,,??,,
 * ,?.
 * <p/>
 * Version 2.0 ?,???.APP .
 *
 * @author RonnyWu.Pe@Gmail.com
 * @version 2.0 2016-06-16 ,1.0 2016-06-16
 */
public class Lv2GoBackBrowserView extends Lv1BaseBrowserView {

    private BrowserGoBackListener browserGoBackListener;

    /**
     * URL
     */
    private ArrayMap<String, String> jumpingUrls = null;

    /**
     * .
     *
     * @param context .
     */
    public Lv2GoBackBrowserView(Context context) {
        super(context);
    }

    /**
     * , .
     *
     * @param context .
     * @param attrs   .
     */
    public Lv2GoBackBrowserView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * , , .
     *
     * @param context      .
     * @param attrs        .
     * @param defStyleAttr .
     */
    public Lv2GoBackBrowserView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * , , , ?.
     *
     * @param context      .
     * @param attrs        .
     * @param defStyleAttr .
     * @param defStyleRes  ?.
     */
    public Lv2GoBackBrowserView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * , , , ??, 
     *
     * @param context         .
     * @param attrs           .
     * @param defStyleAttr    .
     * @param privateBrowsing ??.
     */
    public Lv2GoBackBrowserView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(context, attrs, defStyleAttr, privateBrowsing);
    }

    /**
     * ?, , .?.
     *
     * @param keyCode ?.
     * @param event   .
     * @return ??.
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return backPressed(keyCode, event);
    }

    /**
     * onBackPressed ???,?? onBackPressed,onKeyDown.
     *  onBackPressed , -1 , null .
     *
     * @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;
            }
        }
    }

    /**
     * ??
     *
     * @param jumpingDispose ??
     * @return ?
     */
    private boolean doJupingDispose(String jumpingDispose) {
        if (jumpingDispose.equals("close")) {
            // ?
            return false;
        } else if (jumpingDispose.equals("back")) {
            // ??,?2
            goBackOrForward(-2);
            return true;
        } else {
            // ????
            return false;
        }
    }

    /**
     * ???.
     */
    @Override
    public void initBrowserClient() {
        webViewClient = new Lv2GoBackWebViewClient();
        super.initBrowserClient();
    }

    /**
     * ???.
     *
     * @return ??.
     */
    public BrowserGoBackListener getBrowserGoBackListener() {
        return browserGoBackListener;
    }

    /**
     * ??.
     *
     * @param browserGoBackListener ??.
     */
    public void setBrowserGoBackListener(BrowserGoBackListener browserGoBackListener) {
        this.browserGoBackListener = browserGoBackListener;
    }

    /**
     * ?URL?(??)
     */
    public ArrayMap<String, String> getJumpingUrls() {
        return jumpingUrls;
    }

    /**
     * ?????(??)<br/>
     * <p/>
     * H5??,????,??<br/>
     * ,?URL????,???<br/>
     * <p/>
     * ??:<br/>
     * 1. "close":?<br/>
     * 2. "back":??(?2)<br/>
     * <p/>
     * ?URL:
     * 1. URL?:?
     * 2. ?: ??scheme"regex:",???
     * <p/>
     * :<br/>
     * 1. "back"?????<br/>
     * <p/>
     * : ( A <- B <- C <- D ) ??D?,?,C???,C??,2,<br/>
     * B,?B?
     *
     * @param jumpingUrls map?, KEY ?URL(2??) , VALUE ??
     */
    public void setJumpingUrls(ArrayMap<String, String> jumpingUrls) {
        this.jumpingUrls = jumpingUrls;
    }

    /**
     * ?,?.
     * ??.
     */
    public interface BrowserGoBackListener {

        /**
         *  True ?.
         *  False ???,?.
         *
         * @param url          URL ?.
         * @param webSiteTitle Title.
         * @return ?.
         */
        boolean canGoBack(String url, String webSiteTitle);
    }

    public void setInterveneListener(Lv2GoBackWebViewClient.UrlLoadingInterveneListener interveneListener) {
        ((Lv2GoBackWebViewClient) webViewClient).setInterveneListener(interveneListener);
    }
}