Enable platform notifications for chrome http stack

Change-Id: I386aef25010c95b15fb6a8edc96e59e538540306
diff --git a/core/java/android/webkit/JniUtil.java b/core/java/android/webkit/JniUtil.java
index 62b415c..b5d4933 100644
--- a/core/java/android/webkit/JniUtil.java
+++ b/core/java/android/webkit/JniUtil.java
@@ -48,6 +48,12 @@
         initialized = true;
     }
 
+    protected static synchronized Context getContext() {
+        if (!initialized)
+            return null;
+        return sContext;
+    }
+
     /**
      * Called by JNI. Gets the application's database directory, excluding the trailing slash.
      * @return String The application's database directory
diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java
index 0504532..7a0c3a0 100644
--- a/core/java/android/webkit/WebView.java
+++ b/core/java/android/webkit/WebView.java
@@ -822,6 +822,8 @@
 
     private WebViewCore.AutoFillData mAutoFillData;
 
+    private static boolean sNotificationsEnabled = true;
+
     /**
      * URI scheme for telephone number
      */
@@ -1024,24 +1026,38 @@
     }
 
     /*
-     * A variable to track if there is a receiver added for PROXY_CHANGE_ACTION
+     * Receiver for PROXY_CHANGE_ACTION, will be null when it is not added handling broadcasts.
      */
-    private static boolean sProxyReceiverAdded;
+    private static ProxyReceiver sProxyReceiver;
 
+    /*
+     * @param context This method expects this to be a valid context
+     */
     private static synchronized void setupProxyListener(Context context) {
-        if (sProxyReceiverAdded) {
+        if (sProxyReceiver != null || sNotificationsEnabled == false) {
             return;
         }
         IntentFilter filter = new IntentFilter();
         filter.addAction(Proxy.PROXY_CHANGE_ACTION);
+        sProxyReceiver = new ProxyReceiver();
         Intent currentProxy = context.getApplicationContext().registerReceiver(
-                new ProxyReceiver(), filter);
-        sProxyReceiverAdded = true;
+                sProxyReceiver, filter);
         if (currentProxy != null) {
             handleProxyBroadcast(currentProxy);
         }
     }
 
+    /*
+     * @param context This method expects this to be a valid context
+     */
+    private static synchronized void disableProxyListener(Context context) {
+        if (sProxyReceiver == null)
+            return;
+
+        context.getApplicationContext().unregisterReceiver(sProxyReceiver);
+        sProxyReceiver = null;
+    }
+
     private static void handleProxyBroadcast(Intent intent) {
         ProxyProperties proxyProperties = (ProxyProperties)intent.getExtra(Proxy.EXTRA_PROXY_INFO);
         if (proxyProperties == null || proxyProperties.getHost() == null) {
@@ -1530,19 +1546,32 @@
 
     /**
      * Enables platform notifications of data state and proxy changes.
+     * Notifications are enabled by default.
      */
     public static void enablePlatformNotifications() {
         checkThread();
-        Network.enablePlatformNotifications();
+        synchronized (WebView.class) {
+            Network.enablePlatformNotifications();
+            sNotificationsEnabled = true;
+            Context context = JniUtil.getContext();
+            if (context != null)
+                setupProxyListener(context);
+        }
     }
 
     /**
-     * If platform notifications are enabled, this should be called
-     * from the Activity's onPause() or onStop().
+     * Disables platform notifications of data state and proxy changes.
+     * Notifications are enabled by default.
      */
     public static void disablePlatformNotifications() {
         checkThread();
-        Network.disablePlatformNotifications();
+        synchronized (WebView.class) {
+            Network.disablePlatformNotifications();
+            sNotificationsEnabled = false;
+            Context context = JniUtil.getContext();
+            if (context != null)
+                disableProxyListener(context);
+        }
     }
 
     /**