I have requirement in my browser to enable/disable safe search while browsing.
On Google Safe Search Page
Block
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.
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:
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.
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:
Good luck )