Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / net / android / java / src / org / chromium / net / ProxyChangeListener.java
index ce9aabb..7b57396 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Copyright 2012 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
@@ -9,22 +9,26 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.Proxy;
-
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
+import android.os.Build;
+import android.util.Log;
 
 import org.chromium.base.CalledByNative;
 import org.chromium.base.JNINamespace;
 import org.chromium.base.NativeClassQualifiedName;
 
-// This class partners with native ProxyConfigServiceAndroid to listen for
-// proxy change notifications from Android.
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * This class partners with native ProxyConfigServiceAndroid to listen for
+ * proxy change notifications from Android.
+ */
 @JNINamespace("net")
 public class ProxyChangeListener {
     private static final String TAG = "ProxyChangeListener";
     private static boolean sEnabled = true;
 
-    private int mNativePtr;
+    private long mNativePtr;
     private Context mContext;
     private ProxyReceiver mProxyReceiver;
     private Delegate mDelegate;
@@ -36,7 +40,7 @@ public class ProxyChangeListener {
         }
         public final String mHost;
         public final int mPort;
-    };
+    }
 
     public interface Delegate {
         public void proxySettingsChanged();
@@ -55,17 +59,17 @@ public class ProxyChangeListener {
     }
 
     @CalledByNative
-    static public ProxyChangeListener create(Context context) {
+    public static ProxyChangeListener create(Context context) {
         return new ProxyChangeListener(context);
     }
 
     @CalledByNative
-    static public String getProperty(String property) {
+    public static String getProperty(String property) {
         return System.getProperty(property);
     }
 
     @CalledByNative
-    public void start(int nativePtr) {
+    public void start(long nativePtr) {
         assert mNativePtr == 0;
         mNativePtr = nativePtr;
         registerReceiver();
@@ -90,34 +94,44 @@ public class ProxyChangeListener {
         // tne Android SDK, so we have to use reflection to get at it and invoke
         // methods on it. If we fail, return an empty proxy config (meaning
         // 'direct').
-        // TODO(ellyjones): once android.net.ProxyProperties is exported,
-        // rewrite this.
+        // TODO(sgurun): once android.net.ProxyInfo is public, rewrite this.
         private ProxyConfig extractNewProxy(Intent intent) {
             try {
-                final String CLASS_NAME = "android.net.ProxyProperties";
                 final String GET_HOST_NAME = "getHost";
                 final String GET_PORT_NAME = "getPort";
                 Object props = intent.getExtras().get("proxy");
                 if (props == null) {
                     return null;
                 }
-                Class cls = Class.forName(CLASS_NAME);
+                String className;
+                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
+                    className = "android.net.ProxyProperties";
+                } else {
+                    className = "android.net.ProxyInfo";
+                }
+
+                Class<?> cls = Class.forName(className);
                 Method getHostMethod = cls.getDeclaredMethod(GET_HOST_NAME);
                 Method getPortMethod = cls.getDeclaredMethod(GET_PORT_NAME);
 
-                String host = (String)getHostMethod.invoke(props);
-                int port = (Integer)getPortMethod.invoke(props);
+                String host = (String) getHostMethod.invoke(props);
+                int port = (Integer) getPortMethod.invoke(props);
 
                 return new ProxyConfig(host, port);
             } catch (ClassNotFoundException ex) {
+                Log.e(TAG, "Using no proxy configuration due to exception:" + ex);
                 return null;
             } catch (NoSuchMethodException ex) {
+                Log.e(TAG, "Using no proxy configuration due to exception:" + ex);
                 return null;
             } catch (IllegalAccessException ex) {
+                Log.e(TAG, "Using no proxy configuration due to exception:" + ex);
                 return null;
             } catch (InvocationTargetException ex) {
+                Log.e(TAG, "Using no proxy configuration due to exception:" + ex);
                 return null;
             } catch (NullPointerException ex) {
+                Log.e(TAG, "Using no proxy configuration due to exception:" + ex);
                 return null;
             }
         }
@@ -128,10 +142,10 @@ public class ProxyChangeListener {
             return;
         }
         if (mDelegate != null) {
-          mDelegate.proxySettingsChanged();
+            mDelegate.proxySettingsChanged();
         }
         if (mNativePtr == 0) {
-          return;
+            return;
         }
         // Note that this code currently runs on a MESSAGE_LOOP_UI thread, but
         // the C++ code must run the callbacks on the network thread.
@@ -164,9 +178,9 @@ public class ProxyChangeListener {
      * See net/proxy/proxy_config_service_android.cc
      */
     @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate")
-    private native void nativeProxySettingsChangedTo(int nativePtr,
+    private native void nativeProxySettingsChangedTo(long nativePtr,
                                                      String host,
                                                      int port);
     @NativeClassQualifiedName("ProxyConfigServiceAndroid::JNIDelegate")
-    private native void nativeProxySettingsChanged(int nativePtr);
+    private native void nativeProxySettingsChanged(long nativePtr);
 }