Android 13 限制原生浏览器(Browser)访问某个网址或只允许访问某个网址

介绍

客户需要只允许访问某些网站或限制某几个网站的访问。

修改

首先我们先限制浏览器加载页面的地方,这里我们实际上只是限制了网址中的关键字访问,例如www.baidu.com 我在数组中屏蔽了关键字baidu 检测到baidu的字样就return true掉;正常屏蔽网址我觉得应该是屏蔽对应的IP才对,但是我看一些网址IP是有多个的,而且感觉有时候会变动,所以采用了网址字符串屏蔽的方式,shouldOverrideUrlLoading方法返回true时就是不继续加载网页。

目前我这种写法是限制这三个网址的访问,如何是只允许这三个网址访问就是把boolean isContains = false;然后下面的isContains = true;即可,后面输入栏的也如此。

路径:Browser/src/com/android/browser/Tab.java

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            /// M: workaround for v74 WebView issue.
            mDownloadUrl = url;
            //*/soda water. add website white list
            String UrlArrays[] = {"taobao","douyin","baidu"};
            if(UrlArrays != null){
                boolean isContains = true;
                for(String tmp:UrlArrays){
                         Log.d("soda water1",url);
                         Log.d("soda water2",tmp);
                     if(url.contains(tmp)){
                         isContains = false;
                         Log.d("soda water3","yes");
                     }
                 }
                if(!isContains)return true;
            } else {
                Log.d("soda water4","2no");
                return true;
            }
           //*/
            if (!mDisableOverrideUrlLoading && mInForeground) {
                return mWebViewController.shouldOverrideUrlLoading(Tab.this,
                        view, url);
            } else {
                return false;
            }
        }

这里限制后确实无法访问网页,但有时候在输入栏输入后点推荐的会加载出第一屏,所以在输入处在限制一下。

路径:Browser/src/com/android/browser/UrlInputView.java

    private void finishInput(String url, String extra, String source) {
        if (DEBUG) {
            Log.d(TAG, "UrlInputView.finishInput()--->url = " + url
                    + ", extra = " + extra + ", source = " + source);
        }
	    //*/soda water. Student URL white list
        String UrlArrays[] = {"taobao","douyin","baidu"};
            if(UrlArrays != null){
            boolean isContains = true;
                for(String tmp:UrlArrays){
                     Log.d("soda water1",url);
                     Log.d("soda water2",tmp);
                   if(url.contains(tmp)){
                     isContains = false;
                     Log.d("soda water3","yes");
                     }
                 }
                if(!isContains)return;
            }else{
            Log.d("soda water4","2no");
                return;
            }
       //*/

        mNeedsUpdate = true;
        dismissDropDown();
        mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
        if (TextUtils.isEmpty(url)) {
            mListener.onDismiss();
        } else {
            if (mIncognitoMode && isSearch(url)) {
                // To prevent logging, intercept this request
                // TODO: This is a quick hack, refactor this
                SearchEngine searchEngine = BrowserSettings.getInstance()
                        .getSearchEngine();
                if (searchEngine == null) return;
                com.mediatek.common.search.SearchEngine engineInfo = SearchEngines
                        .getSearchEngineInfo(mContext, searchEngine.getName());
                if (engineInfo == null) return;
                url = engineInfo.getSearchUriForQuery(url);
                // mLister.onAction can take it from here without logging
            }
            mListener.onAction(url, extra, source);
        }
    }

你可能感兴趣的:(Android,13,android)