SWE WebRefiner integration

Change-Id: I85bcb0e75b3942affa2cb1fe0d07b005a8560ad6
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";