| OLD | NEW | 
| (Empty) |  | 
 |    1 package org.adblockplus.android; | 
 |    2  | 
 |    3 import java.lang.reflect.Constructor; | 
 |    4 import java.lang.reflect.Method; | 
 |    5  | 
 |    6 import android.content.Context; | 
 |    7 import android.content.Intent; | 
 |    8 import android.net.ConnectivityManager; | 
 |    9 import android.net.NetworkInfo; | 
 |   10 import android.os.Parcelable; | 
 |   11 import android.util.Log; | 
 |   12  | 
 |   13 public class ProxySettings | 
 |   14 { | 
 |   15   private static final String TAG = "ProxySettings"; | 
 |   16    | 
 |   17   /** | 
 |   18    * Reads system proxy settings on Android 3.1+ using Java reflection. | 
 |   19    *  | 
 |   20    * @return string array of host, port and exclusion list | 
 |   21    */ | 
 |   22   public static String[] getUserProxy(Context context) | 
 |   23   { | 
 |   24     Method method = null; | 
 |   25     try | 
 |   26     { | 
 |   27       /* | 
 |   28        * ProxyProperties proxyProperties = ConnectivityManager.getProxy(); | 
 |   29        */ | 
 |   30       method = ConnectivityManager.class.getMethod("getProxy"); | 
 |   31     } | 
 |   32     catch (NoSuchMethodException e) | 
 |   33     { | 
 |   34       // This is normal situation for pre-ICS devices | 
 |   35       return null; | 
 |   36     } | 
 |   37     catch (Exception e) | 
 |   38     { | 
 |   39       // This should not happen | 
 |   40       Log.e(TAG, "getProxy failure", e); | 
 |   41       return null; | 
 |   42     } | 
 |   43  | 
 |   44     try | 
 |   45     { | 
 |   46       ConnectivityManager connectivityManager = (ConnectivityManager) context.ge
     tSystemService(Context.CONNECTIVITY_SERVICE); | 
 |   47       Object pp = method.invoke(connectivityManager); | 
 |   48       if (pp == null) | 
 |   49         return null; | 
 |   50  | 
 |   51       return getUserProxy(pp); | 
 |   52     } | 
 |   53     catch (Exception e) | 
 |   54     { | 
 |   55       // This should not happen | 
 |   56       Log.e(TAG, "getProxy failure", e); | 
 |   57       return null; | 
 |   58     } | 
 |   59   } | 
 |   60  | 
 |   61   /** | 
 |   62    * Reads system proxy settings on Android 3.1+ using Java reflection. | 
 |   63    *  | 
 |   64    * @param pp | 
 |   65    *          ProxyProperties object | 
 |   66    * @return string array of host, port and exclusion list | 
 |   67    * @throws Exception | 
 |   68    */ | 
 |   69   protected static String[] getUserProxy(Object pp) throws Exception | 
 |   70   { | 
 |   71     String[] userProxy = new String[3]; | 
 |   72  | 
 |   73     String className = "android.net.ProxyProperties"; | 
 |   74     Class<?> c = Class.forName(className); | 
 |   75     Method method; | 
 |   76  | 
 |   77     /* | 
 |   78      * String proxyHost = pp.getHost() | 
 |   79      */ | 
 |   80     method = c.getMethod("getHost"); | 
 |   81     userProxy[0] = (String) method.invoke(pp); | 
 |   82  | 
 |   83     /* | 
 |   84      * int proxyPort = pp.getPort(); | 
 |   85      */ | 
 |   86     method = c.getMethod("getPort"); | 
 |   87     userProxy[1] = String.valueOf((Integer) method.invoke(pp)); | 
 |   88  | 
 |   89     /* | 
 |   90      * String proxyEL = pp.getExclusionList() | 
 |   91      */ | 
 |   92     method = c.getMethod("getExclusionList"); | 
 |   93     userProxy[2] = (String) method.invoke(pp); | 
 |   94  | 
 |   95     if (userProxy[0] != null) | 
 |   96       return userProxy; | 
 |   97     else | 
 |   98       return null; | 
 |   99   } | 
 |  100  | 
 |  101   /** | 
 |  102    * Tries to set local proxy in system settings via native call on Android 3.1+ | 
 |  103    * devices using Java reflection. | 
 |  104    *  | 
 |  105    * @return true if device supports native proxy setting | 
 |  106    */ | 
 |  107   public static boolean setConnectionProxy(Context context, String host, int por
     t, String excl) | 
 |  108   { | 
 |  109     Method method = null; | 
 |  110     try | 
 |  111     { | 
 |  112       /* | 
 |  113        * android.net.LinkProperties lp = | 
 |  114        * ConnectivityManager.getActiveLinkProperties(); | 
 |  115        */ | 
 |  116       method = ConnectivityManager.class.getMethod("getActiveLinkProperties"); | 
 |  117     } | 
 |  118     catch (NoSuchMethodException e) | 
 |  119     { | 
 |  120       // This is normal situation for pre-ICS devices | 
 |  121       return false; | 
 |  122     } | 
 |  123     catch (Exception e) | 
 |  124     { | 
 |  125       // This should not happen | 
 |  126       Log.e(TAG, "setHttpProxy failure", e); | 
 |  127       return false; | 
 |  128     } | 
 |  129     try | 
 |  130     { | 
 |  131       ConnectivityManager connectivityManager = (ConnectivityManager) context.ge
     tSystemService(Context.CONNECTIVITY_SERVICE); | 
 |  132       Object lp = method.invoke(connectivityManager); | 
 |  133       if (lp == null) // There is no active link now, but device has native prox
     y support | 
 |  134         return true; | 
 |  135        | 
 |  136       String className = "android.net.ProxyProperties"; | 
 |  137       Class<?> c = Class.forName(className); | 
 |  138       method = lp.getClass().getMethod("setHttpProxy", c); | 
 |  139       if (host != null) | 
 |  140       { | 
 |  141         /* | 
 |  142          * ProxyProperties pp = new ProxyProperties(host, port, excl); | 
 |  143          */ | 
 |  144         Class<?>[] parameter = new Class[] {String.class, int.class, String.clas
     s}; | 
 |  145         Object args[] = new Object[3]; | 
 |  146         args[0] = host; | 
 |  147         args[1] = Integer.valueOf(port); | 
 |  148         args[2] = excl; | 
 |  149         Constructor<?> cons = c.getConstructor(parameter); | 
 |  150         Object pp = cons.newInstance(args); | 
 |  151         /* | 
 |  152          * lp.setHttpProxy(pp); | 
 |  153          */ | 
 |  154         method.invoke(lp, pp); | 
 |  155       } | 
 |  156       else | 
 |  157       { | 
 |  158         /* | 
 |  159          * lp.setHttpProxy(null); | 
 |  160          */ | 
 |  161         method.invoke(lp, new Object[] {null}); | 
 |  162       } | 
 |  163  | 
 |  164       Intent intent = null; | 
 |  165       NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); | 
 |  166       switch (ni.getType()) | 
 |  167       { | 
 |  168         case ConnectivityManager.TYPE_WIFI: | 
 |  169           intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHANGED"); | 
 |  170           break; | 
 |  171         case ConnectivityManager.TYPE_MOBILE: | 
 |  172           // TODO We leave it here for future, it does not work now | 
 |  173           // intent = new Intent("android.intent.action.ANY_DATA_STATE"); | 
 |  174           break; | 
 |  175       } | 
 |  176       if (intent != null) | 
 |  177       { | 
 |  178         if (lp != null) | 
 |  179         { | 
 |  180           intent.putExtra("linkProperties", (Parcelable) lp); | 
 |  181         } | 
 |  182         context.sendBroadcast(intent); | 
 |  183       } | 
 |  184  | 
 |  185       return true; | 
 |  186     } | 
 |  187     catch (Exception e) | 
 |  188     { | 
 |  189       // This should not happen | 
 |  190       Log.e(TAG, "setHttpProxy failure", e); | 
 |  191       return false; | 
 |  192     } | 
 |  193   } | 
 |  194 } | 
| OLD | NEW |