WebRefiner UX changes

- Reworded the message shown in site specific
  settings.

Change-Id: I39cce5e6386f38018d6cbfc0b785cf295f1dcb55
diff --git a/res/values/strings.xml b/res/values/strings.xml
index a4cce68..17c2208 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -406,8 +406,26 @@
     <string name="pref_web_refiner_enabled">Web Refiner</string>
     <!-- Settings summary for the WebRefiner -->
     <string name="pref_web_refiner_enabled_summary">Block advertisements and tracking</string>
-    <string name="pref_web_refiner_blocked">Blocked</string>
-    <string name="pref_web_refiner_advertisements">distractions</string>
+    <plurals name="pref_web_refiner_advertisements">
+        <item quantity="one">%d advertisement</item>
+        <item quantity="other">%d advertisements</item>
+    </plurals>
+    <plurals name="pref_web_refiner_trackers">
+        <item quantity="one">%d tracker</item>
+        <item quantity="other">%d trackers</item>
+    </plurals>
+    <plurals name="pref_web_refiner_malware">
+        <item quantity="one">%d malware threat</item>
+        <item quantity="other">%d malware threats</item>
+    </plurals>
+    <string-array name="pref_web_refiner_message">
+        <item>"Blocked %s. "</item>
+        <item>"Blocked %1$s and %2$s. "</item>
+        <item>"Blocked %1$s, %2$s and $3$s. "</item>
+    </string-array>
+    <string name="pref_valid_cert">Site has a valid SSL certificate.</string>
+    <string name="pref_invalid_cert">Site has an invalid SSL certificate.</string>
+    <string name="pref_warning_cert">Site SSL certificate has warnings.</string>
     <!-- Label for option that when clicked opens the AutoFill settings screen. Also used as the title of that AutoFill Settings screen. [CHAR-LIMIT=32] -->
     <string name="pref_autofill_profile_editor">Auto-fill text</string>
     <!-- Summary for the AutoFill Settings preference [CHAR-LIMIT=none] -->
diff --git a/res/xml/privacy_and_security_preferences.xml b/res/xml/privacy_and_security_preferences.xml
index 45e80f2..89c40a9 100644
--- a/res/xml/privacy_and_security_preferences.xml
+++ b/res/xml/privacy_and_security_preferences.xml
@@ -125,6 +125,7 @@
     </PreferenceCategory>
 
     <PreferenceCategory
+        android:key="default_site_settings"
         android:layout="@layout/swe_preference_category"
         android:title="@string/pref_default_site_settings_title">
         <SwitchPreference
diff --git a/res/xml/site_specific_preferences.xml b/res/xml/site_specific_preferences.xml
index 836bb34..0df9446 100644
--- a/res/xml/site_specific_preferences.xml
+++ b/res/xml/site_specific_preferences.xml
@@ -54,7 +54,7 @@
     <PreferenceCategory
         android:title="@string/pref_site_settings_title"
         android:layout="@layout/swe_preference_category"
-        android:key="reset_default">
+        android:key="site_pref_list">
 
         <SwitchPreference
             android:layout="@layout/swe_preference"
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 6ee8e29..60f13d5 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -140,6 +140,7 @@
         // Intialize Web Refiner only once
         final WebRefiner refiner = WebRefiner.getInstance();
         if (refiner != null) {
+            mPrefs.edit().putBoolean(PREF_WEB_REFINER, true).apply();
             refiner.setDefaultPermission(PermissionsServiceFactory.getDefaultPermissions(
                             PermissionsServiceFactory.PermissionType.WEBREFINER));
 
@@ -174,6 +175,8 @@
                     }
                 }
             );
+        } else {
+            mPrefs.edit().putBoolean(PREF_WEB_REFINER, false).apply();
         }
 
         mAutofillHandler = new AutofillHandler(mContext);
diff --git a/src/com/android/browser/NavigationBarBase.java b/src/com/android/browser/NavigationBarBase.java
index c8984cf..c032872 100644
--- a/src/com/android/browser/NavigationBarBase.java
+++ b/src/com/android/browser/NavigationBarBase.java
@@ -44,7 +44,6 @@
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.View.OnFocusChangeListener;
-import android.view.ViewConfiguration;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.PopupMenu;
@@ -282,16 +281,37 @@
 
     protected void showSiteSpecificSettings() {
         WebView wv = mUiController.getCurrentTopWebView();
-        int count = 0;
+        int ads = 0;
+        int tracker = 0;
+        int malware = 0;
 
-        if (wv != null && WebRefiner.getInstance() != null) {
-            count = WebRefiner.getInstance().getBlockedURLCount(wv);
+        WebRefiner webRefiner = WebRefiner.getInstance();
+        if (wv != null &&  webRefiner != null) {
+            WebRefiner.PageInfo pageInfo = webRefiner.getPageInfo(wv);
+            if (pageInfo != null) {
+                for (WebRefiner.MatchedURLInfo urlInfo : pageInfo.mMatchedURLInfoList) {
+                    switch (urlInfo.mMatchedFilterCategory) {
+                        case WebRefiner.RuleSet.CATEGORY_ADS:
+                            ads++;
+                            break;
+                        case WebRefiner.RuleSet.CATEGORY_TRACKERS:
+                            tracker++;
+                            break;
+                        case WebRefiner.RuleSet.CATEGORY_MALWARE_DOMAINS:
+                            malware++;
+                            break;
+                    }
+                }
+            }
         }
 
         Bundle bundle = new Bundle();
         bundle.putCharSequence(SiteSpecificPreferencesFragment.EXTRA_SITE,
                 mUiController.getCurrentTab().getUrl());
-        bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_INFO, count);
+
+        bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_ADS_INFO, ads);
+        bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_TRACKER_INFO, tracker);
+        bundle.putInt(SiteSpecificPreferencesFragment.EXTRA_WEB_REFINER_MALWARE_INFO, malware);
 
         bundle.putParcelable(SiteSpecificPreferencesFragment.EXTRA_SECURITY_CERT,
                 SslCertificate.saveState(wv.getCertificate()));
diff --git a/src/com/android/browser/PreferenceKeys.java b/src/com/android/browser/PreferenceKeys.java
index 7c7c286..d4280a6 100644
--- a/src/com/android/browser/PreferenceKeys.java
+++ b/src/com/android/browser/PreferenceKeys.java
@@ -99,6 +99,7 @@
     static final String PREF_SAVE_FORMDATA = "save_formdata";
     static final String PREF_SHOW_SECURITY_WARNINGS = "show_security_warnings";
     static final String PREF_DO_NOT_TRACK = "do_not_track";
+    static final String PREF_WEB_REFINER = "web_refiner";
 
     // ----------------------
     // Keys for bandwidth_preferences.xml
diff --git a/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java b/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
index 6d22f48..e586cb5 100644
--- a/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
+++ b/src/com/android/browser/preferences/PrivacySecurityPreferencesFragment.java
@@ -17,6 +17,7 @@
 package com.android.browser.preferences;
 
 import com.android.browser.BrowserLocationSwitchPreference;
+import com.android.browser.BrowserSettings;
 import com.android.browser.PreferenceKeys;
 import com.android.browser.R;
 import com.android.browser.mdm.DoNotTrackRestriction;
@@ -31,6 +32,7 @@
 import android.content.Intent;
 import android.os.Bundle;
 import android.preference.Preference;
+import android.preference.PreferenceCategory;
 import android.preference.PreferenceScreen;
 import android.preference.TwoStatePreference;
 
@@ -60,13 +62,22 @@
 
         readAndShowPermission("camera", PermissionsServiceFactory.PermissionType.VIDEO);
 
-        // since webrefiner and distracting_contents are paradoxes
-        // the value needs to be flipped
         Preference pref = findPreference("distracting_contents");
-        pref.setOnPreferenceChangeListener(this);
-        showPermission(pref,
-            !PermissionsServiceFactory.getDefaultPermissions(
-                PermissionsServiceFactory.PermissionType.WEBREFINER));
+        if (!BrowserSettings.getInstance().getPreferences()
+                .getBoolean(PreferenceKeys.PREF_WEB_REFINER, false)) {
+            PreferenceCategory category =
+                    (PreferenceCategory) findPreference("default_site_settings");
+            if (category != null) {
+                category.removePreference(pref);
+            }
+        } else {
+            // since webrefiner and distracting_contents are paradoxes
+            // the value needs to be flipped
+            pref.setOnPreferenceChangeListener(this);
+            showPermission(pref,
+                    !PermissionsServiceFactory.getDefaultPermissions(
+                            PermissionsServiceFactory.PermissionType.WEBREFINER));
+        }
 
         readAndShowPermission("popup_windows", PermissionsServiceFactory.PermissionType.POPUP);
 
diff --git a/src/com/android/browser/preferences/SiteSpecificPreferencesFragment.java b/src/com/android/browser/preferences/SiteSpecificPreferencesFragment.java
index 7859422..a67d96d 100644
--- a/src/com/android/browser/preferences/SiteSpecificPreferencesFragment.java
+++ b/src/com/android/browser/preferences/SiteSpecificPreferencesFragment.java
@@ -40,9 +40,9 @@
 import android.net.http.SslCertificate;
 import android.net.http.SslError;
 import android.os.Bundle;
-import android.os.Parcelable;
 import android.preference.ListPreference;
 import android.preference.Preference;
+import android.preference.PreferenceCategory;
 import android.preference.PreferenceScreen;
 import android.preference.TwoStatePreference;
 import android.text.TextUtils;
@@ -53,6 +53,7 @@
 
 import com.android.browser.BrowserLocationListPreference;
 import com.android.browser.BrowserLocationSwitchPreference;
+import com.android.browser.BrowserSettings;
 import com.android.browser.NavigationBarBase;
 import com.android.browser.PageDialogsHandler;
 import com.android.browser.PreferenceKeys;
@@ -64,8 +65,10 @@
 
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.text.Normalizer;
 import java.util.Arrays;
 import java.util.EnumMap;
+import java.util.Formatter;
 import java.util.List;
 import java.util.Map;
 
@@ -75,7 +78,9 @@
     public static final String EXTRA_SITE = "website";
     public static final String EXTRA_ORIGIN = "website_origin";
     public static final String EXTRA_FAVICON = "website_favicon";
-    public static final String EXTRA_WEB_REFINER_INFO = "website_refiner_info";
+    public static final String EXTRA_WEB_REFINER_ADS_INFO = "website_refiner_ads_info";
+    public static final String EXTRA_WEB_REFINER_TRACKER_INFO = "website_refiner_tracker_info";
+    public static final String EXTRA_WEB_REFINER_MALWARE_INFO = "website_refiner_malware_info";
     public static final String EXTRA_SECURITY_CERT = "website_security_cert";
     public static final String EXTRA_SECURITY_CERT_ERR = "website_security_cert_err";
 
@@ -228,6 +233,44 @@
             }
         );
 
+        if (!BrowserSettings.getInstance().getPreferences()
+                .getBoolean(PreferenceKeys.PREF_WEB_REFINER, false)) {
+            PreferenceCategory category = (PreferenceCategory) findPreference("site_pref_list");
+            if (category != null) {
+                Preference pref = findPreference("distracting_contents");
+                category.removePreference(pref);
+            }
+        }
+
+        int ads = args.getInt(EXTRA_WEB_REFINER_ADS_INFO, 0);
+        String[] strings = new String[3];
+        int index = 0;
+
+        if (ads > 0) {
+            strings[index++] = getResources().getQuantityString(
+                    R.plurals.pref_web_refiner_advertisements, ads, ads);
+        }
+
+        int trackers = args.getInt(EXTRA_WEB_REFINER_TRACKER_INFO, 0);
+        if (trackers > 0) {
+            strings[index++] = getResources().getQuantityString(
+                    R.plurals.pref_web_refiner_trackers, trackers, trackers);
+
+        }
+
+        int malware = args.getInt(EXTRA_WEB_REFINER_MALWARE_INFO, 0);
+        if (malware > 0) {
+            strings[index++] = getResources().getQuantityString(
+                    R.plurals.pref_web_refiner_malware, malware, malware);
+        }
+
+        if (index > 0) {
+            String[] formats = getResources().getStringArray(R.array.pref_web_refiner_message);
+            Formatter formatter = new Formatter();
+            formatter.format(formats[index - 1], strings[0], strings[1], strings[2]);
+            mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.INFO, formatter.toString());
+        }
+
         Bundle parcel = args.getParcelable(EXTRA_SECURITY_CERT);
         mSslCert = (parcel != null) ? SslCertificate.restoreState(parcel) : null;
 
@@ -241,54 +284,47 @@
 
             if (certErrors == 0) {
                 mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.INFO,
-                        "Valid SSL Certificate. ");
+                        getString(R.string.pref_valid_cert));
             } else {
                 mSslError = new SslError(-1, mSslCert, mOriginText);
 
                 if ((certErrors & (1 << SslError.SSL_DATE_INVALID)) != 0) {
                     mSslError.addError(SslError.SSL_DATE_INVALID);
                     mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.ERROR,
-                            "Invalid SSL Certificate. ");
+                            getString(R.string.pref_invalid_cert));
                 }
 
                 if ((certErrors & (1 << SslError.SSL_EXPIRED)) != 0) {
                     mSslError.addError(SslError.SSL_EXPIRED);
                     mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.ERROR,
-                            "Invalid SSL Certificate. ");
+                            getString(R.string.pref_invalid_cert));
                 }
 
                 if ((certErrors & (1 << SslError.SSL_IDMISMATCH)) != 0) {
                     mSslError.addError(SslError.SSL_IDMISMATCH);
                     mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.ERROR,
-                            "Invalid SSL Certificate. ");
+                            getString(R.string.pref_invalid_cert));
                 }
 
                 if ((certErrors & (1 << SslError.SSL_INVALID)) != 0) {
                     mSslError.addError(SslError.SSL_INVALID);
                     mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.ERROR,
-                            "Invalid SSL Certificate. ");
+                            getString(R.string.pref_invalid_cert));
                 }
 
                 if ((certErrors & (1 << SslError.SSL_NOTYETVALID)) != 0) {
                     mSslError.addError(SslError.SSL_NOTYETVALID);
                     mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.WARNING,
-                            "SSL Certificate warnings. ");
+                            getString(R.string.pref_warning_cert));
                 }
 
                 if ((certErrors & (1 << SslError.SSL_UNTRUSTED)) != 0) {
                     mSslError.addError(SslError.SSL_UNTRUSTED);
                     mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.WARNING,
-                            "SSL Certificate warnings. ");
+                            getString(R.string.pref_warning_cert));
                 }
             }
         }
-
-        int adBlocks = args.getInt(EXTRA_WEB_REFINER_INFO, 0);
-        if (adBlocks > 0) {
-            mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.INFO,
-                     getString(R.string.pref_web_refiner_blocked) + " " + adBlocks + " " +
-                            getString(R.string.pref_web_refiner_advertisements));
-        }
     }
 
     private void setActionBarTitle(String url) {
@@ -441,14 +477,16 @@
             mSecurityViews.appendText(SiteSecurityViewFactory.ViewType.WARNING, warningText);
         }
 
-        permission = showPermission("distracting_contents",
-                PermissionsServiceFactory.PermissionType.WEBREFINER,
-                R.string.pref_security_allowed, R.string.pref_security_not_allowed);
         pref = findPreference("distracting_contents");
-        if (permission == PermissionsServiceFactory.Permission.BLOCK) {
-            ((TwoStatePreference) pref).setChecked(true);
-        } else {
-            ((TwoStatePreference) pref).setChecked(false);
+        if (pref != null) {
+            permission = showPermission("distracting_contents",
+                    PermissionsServiceFactory.PermissionType.WEBREFINER,
+                    R.string.pref_security_allowed, R.string.pref_security_not_allowed);
+            if (permission == PermissionsServiceFactory.Permission.BLOCK) {
+                ((TwoStatePreference) pref).setChecked(true);
+            } else {
+                ((TwoStatePreference) pref).setChecked(false);
+            }
         }
 
         showPermission("popup_windows", PermissionsServiceFactory.PermissionType.POPUP,