How to enable Safe Search in my Android browser

大城市里の小女人 提交于 2019-12-22 04:18:12

问题


Requirement

I have requirement in my browser to enable/disable safe search while browsing.

On Google Safe Search Page

Block explicit results on Google using SafeSearch

You can filter explicit search results on Google, like pornography, with the SafeSearch setting. SafeSearch isn’t 100% accurate. But it can help you avoid explicit and inappropriate search results on your phone, tablet, or computer.

As you know when safe search is turned on (like google chrome), then user does not see results of many threat types, infected pages, social engineering pages, pornography etc. and block explicit images, videos, and websites from Google Search results

This is available on Google platforms like chrome, google app etc. So I got this task but can I enable it in my browser?

Resources Found

I got hints from safe search api, but I don't know if it is helpful for me, because if I use this, I can do two things,

  • Either I need to call api every-time user open a website (Lookup api)
  • Or I can download all url data. (Update api)

Problem

  1. How can I filter Google search results in my WebView? Is there some way provided by search engines like www.google.com , www.bing.com etc.
  2. If above is not possible, is only way to call safe browsing lookup api and perform blocking/ warning when malware, infection found?

Can any friend give me any hints, if it is possible?


回答1:


If you look into the developer site it is clearly mentioned that

If your app targets Android 7.1 (API level 25) or lower, you can opt your WebView objects out of checking URLs against Google Safe Browsing's list of unsafe websites by adding the following element to your app’s manifest file:

<manifest>
<application>
    <meta-data android:name="android.webkit.WebView.EnableSafeBrowsing"
               android:value="false" />
    ...
 </application>
</manifest>

For Android 8.0 it is clearly mentioned that

While the default value of EnableSafeBrowsing is true, there are occasional cases when you may want to only enable Safe Browsing conditionally or disable it. Android 8.0 (API level 26) and higher support using setSafeBrowsingEnabled(). Apps compiling at lower API levels cannot use setSafeBrowsingEnabled() and should change the value of EnableSafeBrowsing in the manifest to false to disable the feature for all instances of WebView.

If you target Android 8.1 (API level 27) or higher, you can define programmatically how your app responds to a known threat:

  • You can control whether your app reports known threats to Safe Browsing.
  • You can have your app automatically perform a particular action—such as going back to safety—each time it encounters a URL that's classified as a known threat.

Please look into the below sample code, it show how you can instruct your app's instances of WebView to always go back to safety after encountering a known threat:

MyWebActivity.java

private WebView mSuperSafeWebView;
private boolean mSafeBrowsingIsInitialized;

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);

  mSuperSafeWebView = new WebView(this);
  mSuperSafeWebView.setWebViewClient(new MyWebViewClient());
  mSafeBrowsingIsInitialized = false;

  mSuperSafeWebView.startSafeBrowsing(this, new ValueCallback<Boolean>() {
    @Override
    public void onReceiveValue(Boolean success) {
        mSafeBrowsingIsInitialized = true;
        if (!success) {
            Log.e("MY_APP_TAG", "Unable to initialize Safe Browsing!");
          }
      }
   });
}

For enable or disable Safe Browsing. Use the following method.

mSuperSafeWebView.getSettings().setSafeBrowsingEnabled(true);

MyWebViewClient.java

public class MyWebViewClient extends WebViewClient {
   // Automatically go "back to safety" when attempting to load a website that
   // Google has identified as a known threat. An instance of WebView calls
   // this method only after Safe Browsing is initialized, so there's no
   // conditional logic needed here.
   @Override
   public void onSafeBrowsingHit(WebView view, WebResourceRequest request,
        int threatType, SafeBrowsingResponse callback) {
    // The "true" argument indicates that your app reports incidents like
    // this one to Safe Browsing.
    callback.backToSafety(true);
    Toast.makeText(view.getContext(), "Unsafe web page blocked.",
            Toast.LENGTH_LONG).show();
   }
}

Please take look If you want to know about the WebView security version by version.




回答2:


Add on to @Andy Developer answer, you can perform an Android version checking, if it is less than API 27, you could use the Safe Browsing API.

To ensure your browser the best user experience, I would suggest you to implement the Update API and setup a local database. This is the common approach for browsers you can find in Play Store nowadays.

From the Documentation:

Update API (v4)

The Update API lets your client applications download encrypted versions of the Safe Browsing lists for local, client-side checks of URLs.

The Update API is designed for clients that require high frequency, low-latency verdicts. Several web browsers and software platforms use this API to protect large sets of users.

If you are concerned about the privacy of the queried URLs or the latency induced by a network request, use the Update API.

By doing this you can offer this features for both lower end user and high end user.

Note: WebView have implemented Safe Browsing API in native, so it is just the same. If you want to implement Update API only instead of the answer proposed by @Andy Developer, it is still recommendable as it provides uniformity.




回答3:


Definitely we can use the Googles Safe Browsing API for this requirement, the purpose of this APIs is just for that.

So, the scenario can be one like this:
  • we could have an URL Checker engine in background
  • it uses a queue which contains the URLs to be checked and uses Googles Safe Browsing API to check
  • the results could be call-backed to the main thread to safe the browsing data

Good luck )



来源:https://stackoverflow.com/questions/52307138/how-to-enable-safe-search-in-my-android-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!