Change PacProcessor to Android Service

This switches the PacProcessor over to an Android Service.  The service
is bound and unbound by the PacManager, which also adds it to the
ServiceManager, allowing for Context-Free access by the PacProxySelector
in all DVMs.

bug:10182711
Change-Id: Id1ff7660be56e8976cdcccd76e041feb47a17a61
diff --git a/core/java/android/net/PacProxySelector.java b/core/java/android/net/PacProxySelector.java
index d3ce2e5..b674324 100644
--- a/core/java/android/net/PacProxySelector.java
+++ b/core/java/android/net/PacProxySelector.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 
 package android.net;
 
@@ -25,19 +40,25 @@
     private static final String TAG = "PacProxySelector";
     public static final String PROXY_SERVICE = "com.android.net.IProxyService";
     private IProxyService mProxyService;
+    private final List<Proxy> mDefaultList;
 
     public PacProxySelector() {
         mProxyService = IProxyService.Stub.asInterface(
                 ServiceManager.getService(PROXY_SERVICE));
         if (mProxyService == null) {
             // Added because of b10267814 where mako is restarting.
-            Log.e(TAG, "PackManager: no proxy service");
+            Log.e(TAG, "PacManager: no proxy service");
         }
+        mDefaultList = Lists.newArrayList(java.net.Proxy.NO_PROXY);
     }
 
     @Override
     public List<Proxy> select(URI uri) {
         if (mProxyService == null) {
+            mProxyService = IProxyService.Stub.asInterface(
+                    ServiceManager.getService(PROXY_SERVICE));
+        }
+        if (mProxyService == null) {
             Log.e(TAG, "select: no proxy service return NO_PROXY");
             return Lists.newArrayList(java.net.Proxy.NO_PROXY);
         }
@@ -53,6 +74,9 @@
         } catch (RemoteException e) {
             e.printStackTrace();
         }
+        if (response == null) {
+            return mDefaultList;
+        }
 
         return parseResponse(response);
     }
diff --git a/core/java/android/net/Proxy.java b/core/java/android/net/Proxy.java
index 5b38f57..c3e1438 100644
--- a/core/java/android/net/Proxy.java
+++ b/core/java/android/net/Proxy.java
@@ -48,7 +48,6 @@
     private static final String TAG = "Proxy";
 
     private static final ProxySelector sDefaultProxySelector;
-    private static PacProxySelector sPacProxySelector;
 
     /**
      * Used to notify an app that's caching the default connection proxy
@@ -352,11 +351,8 @@
             System.clearProperty("http.nonProxyHosts");
             System.clearProperty("https.nonProxyHosts");
         }
-        if ((pacFileUrl != null) && !TextUtils.isEmpty(pacFileUrl)) {
-            if (sPacProxySelector == null) {
-                sPacProxySelector = new PacProxySelector();
-            }
-            ProxySelector.setDefault(sPacProxySelector);
+        if (!TextUtils.isEmpty(pacFileUrl)) {
+            ProxySelector.setDefault(new PacProxySelector());
         } else {
             ProxySelector.setDefault(sDefaultProxySelector);
         }
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 1363e3c..813b55f 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -218,6 +218,7 @@
     <protected-broadcast android:name="android.location.GPS_ENABLED_CHANGE" />
     <protected-broadcast android:name="android.location.PROVIDERS_CHANGED" />
     <protected-broadcast android:name="android.location.GPS_FIX_CHANGE" />
+    <protected-broadcast android:name="android.net.proxy.PAC_REFRESH" />
 
     <!-- ====================================== -->
     <!-- Permissions for things that cost money -->