OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.android.configurators; |
| 19 |
| 20 import java.net.InetAddress; |
| 21 |
| 22 import org.adblockplus.android.compat.CompatibilityException; |
| 23 import org.adblockplus.android.compat.LinkProperties; |
| 24 import org.adblockplus.android.compat.ProxyProperties; |
| 25 |
| 26 import android.content.BroadcastReceiver; |
| 27 import android.content.Context; |
| 28 import android.content.Intent; |
| 29 import android.content.IntentFilter; |
| 30 import android.net.ConnectivityManager; |
| 31 import android.net.NetworkInfo; |
| 32 import android.os.Parcelable; |
| 33 |
| 34 /** |
| 35 * Proxy registrator setting native proxy using {@code android.net.LinkPropertie
s} via reflection. |
| 36 */ |
| 37 public class NativeProxyConfigurator implements ProxyConfigurator |
| 38 { |
| 39 private final Context context; |
| 40 private final WiFiChangeReceiver wiFiChangeReceiver; |
| 41 private ProxyProperties proxyProperties; |
| 42 private boolean isRegistered = false; |
| 43 |
| 44 public NativeProxyConfigurator(final Context context) |
| 45 { |
| 46 this.context = context; |
| 47 this.wiFiChangeReceiver = new WiFiChangeReceiver(this); |
| 48 } |
| 49 |
| 50 /** |
| 51 * Reliably checks for setHttpProxy hack using {@code android.net.wifi.LINK_CO
NFIGURATION_CHANGED}. |
| 52 * |
| 53 * @param context |
| 54 * The context used for querying the {@code ConnectivityManager} |
| 55 * @return {@code true} if we can set a WiFi proxy using this method |
| 56 */ |
| 57 public static boolean canUse(final Context context) |
| 58 { |
| 59 try |
| 60 { |
| 61 final ConnectivityManager conMan = (ConnectivityManager) context.getSystem
Service(Context.CONNECTIVITY_SERVICE); |
| 62 final NetworkInfo ni = conMan.getActiveNetworkInfo(); |
| 63 |
| 64 final Object lp; |
| 65 |
| 66 // Check if we're currently running on WiFi |
| 67 if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI) |
| 68 { |
| 69 // I'm using reflection directly here to keep this method self-contained
. |
| 70 lp = conMan.getClass() |
| 71 .getMethod("getActiveLinkProperties") |
| 72 .invoke(conMan); |
| 73 } |
| 74 else |
| 75 // We're not running on WiFi so get the last used WiFi link properties |
| 76 { |
| 77 // I'm using reflection directly here to keep this method self-contained
. |
| 78 lp = conMan.getClass() |
| 79 .getMethod("getLinkProperties", int.class) |
| 80 .invoke(conMan, ConnectivityManager.TYPE_WIFI); |
| 81 } |
| 82 |
| 83 if (lp == null) |
| 84 { |
| 85 // Is this even possible? |
| 86 throw new IllegalStateException("No WiFi?"); |
| 87 } |
| 88 |
| 89 context.sendBroadcast( |
| 90 new Intent("android.net.wifi.LINK_CONFIGURATION_CHANGED") |
| 91 .putExtra("linkProperties", (Parcelable) lp)); |
| 92 } |
| 93 catch (final Throwable t) |
| 94 { |
| 95 return false; |
| 96 } |
| 97 |
| 98 return true; |
| 99 } |
| 100 |
| 101 @Override |
| 102 public boolean initialize() |
| 103 { |
| 104 if (canUse(this.context)) |
| 105 { |
| 106 this.context.registerReceiver(this.wiFiChangeReceiver, new IntentFilter(Co
nnectivityManager.CONNECTIVITY_ACTION)); |
| 107 return true; |
| 108 } |
| 109 |
| 110 return false; |
| 111 } |
| 112 |
| 113 private boolean sendIntent(final LinkProperties lp, final ProxyProperties prox
yProperties) |
| 114 { |
| 115 boolean success = true; |
| 116 |
| 117 final ConnectivityManager conMan = (ConnectivityManager) context.getSystemSe
rvice(Context.CONNECTIVITY_SERVICE); |
| 118 final NetworkInfo ni = conMan.getActiveNetworkInfo(); |
| 119 |
| 120 if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI) |
| 121 { |
| 122 final Intent intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHAN
GED"); |
| 123 |
| 124 if (lp.isValid()) |
| 125 { |
| 126 try |
| 127 { |
| 128 lp.setHttpProxy(proxyProperties); |
| 129 intent.putExtra("linkProperties", (Parcelable) lp.getLinkProperties())
; |
| 130 context.sendBroadcast(intent); |
| 131 } |
| 132 catch (final Exception e) |
| 133 { |
| 134 // Catch all, again |
| 135 success = false; |
| 136 } |
| 137 } |
| 138 } |
| 139 |
| 140 return success; |
| 141 } |
| 142 |
| 143 private boolean registerProxy(final ProxyProperties proxyProperties) |
| 144 { |
| 145 try |
| 146 { |
| 147 final ConnectivityManager conMan = (ConnectivityManager) context.getSystem
Service(Context.CONNECTIVITY_SERVICE); |
| 148 |
| 149 return this.sendIntent(LinkProperties.getActiveLinkProperties(conMan), pro
xyProperties); |
| 150 } |
| 151 catch (final CompatibilityException e) |
| 152 { |
| 153 return false; |
| 154 } |
| 155 } |
| 156 |
| 157 private boolean reRegisterProxy() |
| 158 { |
| 159 return this.registerProxy(this.proxyProperties); |
| 160 } |
| 161 |
| 162 @Override |
| 163 public boolean registerProxy(final InetAddress address, final int port) |
| 164 { |
| 165 this.proxyProperties = new ProxyProperties(address.getHostName(), port, ""); |
| 166 |
| 167 return this.isRegistered = this.registerProxy(this.proxyProperties); |
| 168 } |
| 169 |
| 170 @Override |
| 171 public void unregisterProxy() |
| 172 { |
| 173 this.proxyProperties = null; |
| 174 this.isRegistered = false; |
| 175 this.registerProxy(this.proxyProperties); |
| 176 } |
| 177 |
| 178 @Override |
| 179 public void shutdown() |
| 180 { |
| 181 this.context.unregisterReceiver(this.wiFiChangeReceiver); |
| 182 } |
| 183 |
| 184 @Override |
| 185 public ProxyRegistrationType getType() |
| 186 { |
| 187 return ProxyRegistrationType.NATIVE; |
| 188 } |
| 189 |
| 190 @Override |
| 191 public boolean isRegistered() |
| 192 { |
| 193 return this.isRegistered; |
| 194 } |
| 195 |
| 196 @Override |
| 197 public boolean isSticky() |
| 198 { |
| 199 return false; |
| 200 } |
| 201 |
| 202 @Override |
| 203 public String toString() |
| 204 { |
| 205 return "[ProxyConfigurator: " + this.getType() + "]"; |
| 206 } |
| 207 |
| 208 private final static class WiFiChangeReceiver extends BroadcastReceiver |
| 209 { |
| 210 private final NativeProxyConfigurator configurator; |
| 211 |
| 212 private WiFiChangeReceiver(final NativeProxyConfigurator configurator) |
| 213 { |
| 214 this.configurator = configurator; |
| 215 } |
| 216 |
| 217 @Override |
| 218 public void onReceive(final Context context, final Intent intent) |
| 219 { |
| 220 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) |
| 221 { |
| 222 final ConnectivityManager connectivityManager = (ConnectivityManager) co
ntext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 223 final NetworkInfo ni = connectivityManager.getActiveNetworkInfo(); |
| 224 |
| 225 if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI && ni.is
Available() && ni.isConnected()) |
| 226 { |
| 227 this.configurator.reRegisterProxy(); |
| 228 } |
| 229 } |
| 230 } |
| 231 } |
| 232 } |
OLD | NEW |