SWE WebRefiner integration

Change-Id: I85bcb0e75b3942affa2cb1fe0d07b005a8560ad6
diff --git a/assets/web_refiner/web_refiner.conf b/assets/web_refiner/web_refiner.conf
new file mode 100644
index 0000000..4500593
--- /dev/null
+++ b/assets/web_refiner/web_refiner.conf
@@ -0,0 +1,18 @@
+{
+  "version": "1.0",
+  "update": {},
+  "subscriptions": [
+    {
+      "name"            : "AdFilter",
+      "format"          : "WebRefiner 1.0",
+      "url"             : "None",
+      "expiration"      : "5 days"
+    },
+    {
+      "name"            : "PrivacyFilter",
+      "format"          : "WebRefiner 1.0",
+      "url"             : "None",
+      "expiration"      : "5 days"
+    }
+  ]
+}
diff --git a/res/drawable/rounded_rectangle_background.xml b/res/drawable/rounded_rectangle_background.xml
new file mode 100644
index 0000000..b521143
--- /dev/null
+++ b/res/drawable/rounded_rectangle_background.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    * Copyright (c) 2015, The Linux Foundation. All rights reserved.
+    *
+    * Redistribution and use in source and binary forms, with or without
+    * modification, are permitted provided that the following conditions are
+    * met:
+    * * Redistributions of source code must retain the above copyright
+    * notice, this list of conditions and the following disclaimer.
+    * * Redistributions in binary form must reproduce the above
+    * copyright notice, this list of conditions and the following
+    * disclaimer in the documentation and/or other materials provided
+    * with the distribution.
+    * * Neither the name of The Linux Foundation nor the names of its
+    * contributors may be used to endorse or promote products derived
+    * from this software without specific prior written permission.
+    *
+    * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+    * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+    * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+    * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+    * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+    * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+    * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+    *
+ -->
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+
+    <solid android:color="#ffa10404"></solid>
+    <corners android:radius="2dp"></corners>
+    <stroke  android:width="1dp" android:color="@color/black"></stroke>
+
+</shape>
diff --git a/res/layout/page_info.xml b/res/layout/page_info.xml
index e3b117c..3d4bf99 100644
--- a/res/layout/page_info.xml
+++ b/res/layout/page_info.xml
@@ -43,16 +43,6 @@
             android:orientation="vertical">
 
             <TextView
-                android:id="@+id/address_header"
-                android:layout_height="wrap_content"
-                android:layout_width="wrap_content"
-                android:text="@string/page_info_address"
-                android:gravity="start"
-                android:layout_marginStart="20dip"
-                android:layout_marginEnd="20dip"
-                android:layout_marginBottom="10dip" />
-
-            <TextView
                 android:id="@+id/address"
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
@@ -63,6 +53,46 @@
 
         </TableLayout>
 
+        <TableLayout
+            android:id="@+id/web_refiner_info"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:orientation="vertical">
+
+        <TextView
+            android:id="@+id/web_refiner_title"
+            android:layout_height="wrap_content"
+            android:layout_width="wrap_content"
+            android:textStyle="bold"
+            android:layout_marginStart="20dip"
+            android:layout_marginEnd="20dip"
+            android:layout_marginTop="12dip"
+            android:text="WebRefiner"
+            android:layout_marginBottom="12dip" />
+
+            <TextView
+                android:id="@+id/web_refiner_status"
+                android:layout_height="wrap_content"
+                android:layout_width="wrap_content"
+                android:gravity="start"
+                android:text="Enabled on this site"
+                android:layout_marginStart="20dip"
+                android:layout_marginEnd="20dip"
+                android:layout_marginBottom="12dip" />
+
+            <TextView
+                android:id="@+id/web_refiner_blocked_status"
+                android:layout_height="wrap_content"
+                android:layout_width="wrap_content"
+                android:gravity="start"
+                android:text="99 URLs blocked on this page"
+                android:layout_marginStart="20dip"
+                android:layout_marginEnd="20dip"
+                android:layout_marginBottom="12dip" />
+
+        </TableLayout>
+
+
     </LinearLayout>
 
 </ScrollView>
diff --git a/res/layout/title_bar_nav.xml b/res/layout/title_bar_nav.xml
index 142b73b..83ef6bd 100644
--- a/res/layout/title_bar_nav.xml
+++ b/res/layout/title_bar_nav.xml
@@ -73,6 +73,21 @@
                 android:layout_height="32dip"
                 android:layout_gravity="center"
                 android:visibility="gone" />
+
+            <TextView
+                android:id="@+id/notification_counter"
+                android:layout_marginTop="6dip"
+                android:layout_width="15dp"
+                android:layout_height="15dp"
+                android:layout_gravity="top|end"
+                android:background="@drawable/rounded_rectangle_background"
+                android:textColor="#FFFFFF"
+                android:textSize="10sp"
+                android:gravity="center"
+                android:text="99"
+                android:textStyle="bold"
+                android:visibility="gone" />
+
         </FrameLayout>
 
         <ImageView
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 17a061e..fdfcc9d 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -389,6 +389,10 @@
     <string name="pref_autofill_enabled">Form auto-fill</string>
     <!-- Settings summary for the form AutoFill feature. [CHAR-LIMIT=none] -->
     <string name="pref_autofill_enabled_summary">Fill out web forms with a single touch</string>
+    <!-- Checkbox setting for enabling/disabling WebRefiner feature -->
+    <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>
     <!-- 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/general_preferences.xml b/res/xml/general_preferences.xml
index 8f5817f..662ef30 100644
--- a/res/xml/general_preferences.xml
+++ b/res/xml/general_preferences.xml
@@ -48,6 +48,12 @@
             android:summary="@string/pref_autofill_enabled_summary"
             android:defaultValue="true" />
 
+        <CheckBoxPreference
+            android:key="web_refiner_enabled"
+            android:title="@string/pref_web_refiner_enabled"
+            android:summary="@string/pref_web_refiner_enabled_summary"
+            android:defaultValue="true" />
+
         <PreferenceScreen
             android:key="autofill_profile"
             android:title="@string/pref_autofill_profile_editor"
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 8cd2a08..36f28b1 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -48,6 +48,7 @@
 import org.codeaurora.swe.AutoFillProfile;
 import org.codeaurora.swe.CookieManager;
 import org.codeaurora.swe.GeolocationPermissions;
+import org.codeaurora.swe.WebRefiner;
 import org.codeaurora.swe.WebSettings.LayoutAlgorithm;
 import org.codeaurora.swe.WebSettings.PluginState;
 import org.codeaurora.swe.WebSettings.TextSize;
@@ -377,6 +378,9 @@
             }
         } else if (PREF_LINK_PREFETCH.equals(key)) {
             updateConnectionType();
+        } else if (PREF_WEB_REFINER_ENABLED.equals(key)) {
+            if (WebRefiner.isInitialized())
+                WebRefiner.getInstance().setRulesEnabled(WebRefiner.CATEGORY_ALL, isWebRefinerEnabled());
         }
     }
 
@@ -783,6 +787,14 @@
         mPrefs.edit().putBoolean(PREF_POWERSAVE_ENABLED, value).apply();
     }
 
+    public boolean isWebRefinerEnabled() {
+        return mPrefs.getBoolean(PREF_WEB_REFINER_ENABLED, true);
+    }
+
+    public void setWebRefinerEnabled(boolean value) {
+        mPrefs.edit().putBoolean(PREF_WEB_REFINER_ENABLED, value).apply();
+    }
+
     // -----------------------------
     // getter/setters for debug_preferences.xml
     // -----------------------------
diff --git a/src/com/android/browser/NavigationBarPhone.java b/src/com/android/browser/NavigationBarPhone.java
index 3226800..cb6f7fc 100644
--- a/src/com/android/browser/NavigationBarPhone.java
+++ b/src/com/android/browser/NavigationBarPhone.java
@@ -18,9 +18,13 @@
 import android.content.Context;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
+import android.os.Handler;
+import android.os.Message;
 import android.util.AttributeSet;
 import android.util.TypedValue;
 import android.view.View;
+
+import org.codeaurora.swe.WebRefiner;
 import org.codeaurora.swe.WebView;
 import org.codeaurora.swe.util.Activator;
 import org.codeaurora.swe.util.Observable;
@@ -47,6 +51,10 @@
     private float mTabSwitcherInitialTextSize = 0;
     private float mTabSwitcherCompressedTextSize = 0;
 
+    private static final int MSG_UPDATE_NOTIFICATION_COUNTER = 4242;
+    private static final int NOTIFICATION_COUNTER_UPDATE_DELAY = 3000;
+    private TextView mNotificationCounter;
+    private Handler mHandler;
 
     public NavigationBarPhone(Context context) {
         super(context);
@@ -89,6 +97,26 @@
             mTabSwitcherInitialTextSize = mTabText.getTextSize();
             mTabSwitcherCompressedTextSize = (float) (mTabSwitcherInitialTextSize / 1.2);
         }
+
+        mNotificationCounter = (TextView) findViewById(R.id.notification_counter);
+        mHandler = new Handler() {
+            @Override
+            public void handleMessage(Message m) {
+                switch (m.what) {
+                    case MSG_UPDATE_NOTIFICATION_COUNTER:
+                        WebView wv = mUiController.getCurrentTopWebView();
+                        if (wv != null && WebRefiner.isInitialized()) {
+                            int count = WebRefiner.getInstance().getBlockedURLCount(wv);
+                            if (count > 0) {
+                                mNotificationCounter.setText(String.valueOf(count));
+                                mNotificationCounter.setVisibility(View.VISIBLE);
+                            }
+                        }
+                        mHandler.sendEmptyMessageDelayed(MSG_UPDATE_NOTIFICATION_COUNTER, NOTIFICATION_COUNTER_UPDATE_DELAY);
+                    break;
+                }
+            }
+        };
     }
 
     @Override
@@ -113,21 +141,24 @@
     @Override
     public void onProgressStarted() {
         super.onProgressStarted();
-        if (mStopButton.getDrawable() != mStopDrawable) {
+        /*if (mStopButton.getDrawable() != mStopDrawable) {
             mStopButton.setImageDrawable(mStopDrawable);
             mStopButton.setContentDescription(mStopDescription);
             if (mStopButton.getVisibility() != View.VISIBLE) {
                 mComboIcon.setVisibility(View.GONE);
                 mStopButton.setVisibility(View.VISIBLE);
             }
-        }
+        }*/
+        mNotificationCounter.setVisibility(View.INVISIBLE);
+        mHandler.removeMessages(MSG_UPDATE_NOTIFICATION_COUNTER);
+        mHandler.sendEmptyMessageDelayed(MSG_UPDATE_NOTIFICATION_COUNTER, NOTIFICATION_COUNTER_UPDATE_DELAY);
     }
 
     @Override
     public void onProgressStopped() {
         super.onProgressStopped();
-        mStopButton.setImageDrawable(mRefreshDrawable);
-        mStopButton.setContentDescription(mRefreshDescription);
+        //mStopButton.setImageDrawable(mRefreshDrawable);
+        //mStopButton.setContentDescription(mRefreshDescription);
         if (!isEditingUrl()) {
             mComboIcon.setVisibility(View.VISIBLE);
         }
diff --git a/src/com/android/browser/PageDialogsHandler.java b/src/com/android/browser/PageDialogsHandler.java
index e027e80..b92add9 100644
--- a/src/com/android/browser/PageDialogsHandler.java
+++ b/src/com/android/browser/PageDialogsHandler.java
@@ -27,6 +27,7 @@
 import android.view.View;
 import org.codeaurora.swe.HttpAuthHandler;
 import org.codeaurora.swe.SslErrorHandler;
+import org.codeaurora.swe.WebRefiner;
 import org.codeaurora.swe.WebView;
 
 import com.android.browser.reflect.ReflectHelper;
@@ -161,6 +162,15 @@
         ((TextView) pageInfoView.findViewById(R.id.address)).setText(url);
         ((TextView) pageInfoView.findViewById(R.id.title)).setText(title);
 
+        if (WebRefiner.isInitialized() && view != null) {
+            (pageInfoView.findViewById(R.id.web_refiner_info)).setVisibility(View.VISIBLE);
+            int count = WebRefiner.getInstance().getBlockedURLCount(view);
+            String msg = String.valueOf(count) + " requests blocked on this page";
+            ((TextView) pageInfoView.findViewById(R.id.web_refiner_blocked_status)).setText(msg);
+        } else {
+            (pageInfoView.findViewById(R.id.web_refiner_info)).setVisibility(View.INVISIBLE);
+        }
+
         mPageInfoView = tab;
         mPageInfoFromShowSSLCertificateOnError = fromShowSSLCertificateOnError;
         mUrlCertificateOnError = urlCertificateOnError;
diff --git a/src/com/android/browser/PreferenceKeys.java b/src/com/android/browser/PreferenceKeys.java
index b0c384a..2df0ceb 100644
--- a/src/com/android/browser/PreferenceKeys.java
+++ b/src/com/android/browser/PreferenceKeys.java
@@ -59,6 +59,7 @@
     // ----------------------
     static final String PREF_AUTOFILL_ENABLED = "autofill_enabled";
     static final String PREF_AUTOFILL_PROFILE = "autofill_profile";
+    static final String PREF_WEB_REFINER_ENABLED = "web_refiner_enabled";
     static final String PREF_HOMEPAGE = "homepage";
     static final String PREF_POWERSAVE_ENABLED = "powersave_enabled";
     static final String PREF_SYNC_WITH_CHROME = "sync_with_chrome";
diff --git a/swe_android_browser.gypi b/swe_android_browser.gypi
index 0a6dca5..d056eb8 100644
--- a/swe_android_browser.gypi
+++ b/swe_android_browser.gypi
@@ -12,6 +12,7 @@
         '<@(libnetxt_dependencies)',
         '<@(libsta_dependencies)',
         '<@(libsweadrenoext_dependencies)',
+        '<@(web_refiner_dependencies)',
         #'android-support-v13',
       ],
       'variables': {
@@ -42,6 +43,7 @@
           '<@(libnetxt_native_libs)',
           '<@(libsta_native_libs)',
           '<@(libsweadrenoext_native_libs)',
+          '<@(web_refiner_native_libs)',
         ],
         'additional_input_paths': [
           '<@(chrome_android_pak_output_resources)',
@@ -66,6 +68,12 @@
             '<(assets_dir)/wml/swe_wml.css',
           ],
         },
+        {
+          'destination': '<(PRODUCT_DIR)/swe_android_browser_apk/assets/web_refiner',
+          'files': [
+            '<(assets_dir)/web_refiner/web_refiner.conf',
+          ],
+        },
       ],
       'includes': [ '../../build/java_apk.gypi' ],
     },
diff --git a/swe_res.gypi b/swe_res.gypi
index 271400b..dd1ae7e 100644
--- a/swe_res.gypi
+++ b/swe_res.gypi
@@ -43,6 +43,12 @@
             '<(PRODUCT_DIR)/swe_android_browser_apk/assets/wml/swe_wml.css',
           ],
         },
+        {
+          'destination': '<(PRODUCT_DIR)/swe_android_browser_apk/swe_res/assets/web_refiner',
+          'files': [
+            '<(PRODUCT_DIR)/swe_android_browser_apk/assets/web_refiner/web_refiner.conf',
+          ],
+        },
 
         #ui res
         {