Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/org/adblockplus/android/configurators/ReflectionProxyConfigurator.java

Issue 4705284891082752: Proxy configurators (Closed)
Patch Set: Created Aug. 11, 2014, 12:36 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 using {@code android.net.LinkProperties} via reflection.
36 *
37 * @author rjeschke
38 */
39 public class ReflectionProxyConfigurator implements ProxyConfigurator
Felix Dahlke 2014/08/19 09:06:00 Maybe we should rather call this "NativeProxyConfi
René Jeschke 2014/08/19 10:41:33 k, why not ;-)
40 {
41 private final Context context;
42 private final WiFiChangeReceiver wiFiChangeReceiver;
43 private ProxyProperties proxyProperties = null;
44
45 public ReflectionProxyConfigurator(final Context context)
46 {
47 this.context = context;
48 this.wiFiChangeReceiver = new WiFiChangeReceiver(this);
49 }
50
51 /**
52 * Reliably checks for setHttpProxy hack using {@code android.net.wifi.LINK_CO NFIGURATION_CHANGED}.
53 *
54 * @param context
55 * The context used for querying the {@code ConnectivityManager}
56 * @return {@code true} if we can set a WiFi proxy using this method
57 */
58 public static boolean canUse(final Context context)
59 {
60 try
61 {
62 final ConnectivityManager conMan = (ConnectivityManager) context.getSystem Service(Context.CONNECTIVITY_SERVICE);
63 final NetworkInfo ni = conMan.getActiveNetworkInfo();
64
65 final Object lp;
66
67 // Check if we're currently running on WiFi
68 if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI)
69 {
70 // I'm using reflection directly here to keep this method self-contained .
71 lp = conMan.getClass()
72 .getMethod("getActiveLinkProperties")
73 .invoke(conMan);
74 }
75 else
76 // We're not running on WiFi so get the last used WiFi link properties
77 {
78 // I'm using reflection directly here to keep this method self-contained .
79 lp = conMan.getClass()
80 .getMethod("getLinkProperties", int.class)
81 .invoke(conMan, ConnectivityManager.TYPE_WIFI);
82 }
83
84 if (lp == null)
85 {
86 // Is this even possible?
87 throw new IllegalStateException("No WiFi?");
88 }
89
90 context.sendBroadcast(
91 new Intent("android.net.wifi.LINK_CONFIGURATION_CHANGED")
92 .putExtra("linkProperties", (Parcelable) lp));
93 }
94 catch (final Throwable t)
95 {
96 return false;
97 }
98
99 return true;
100 }
101
102 @Override
103 public boolean initialize()
104 {
105 if (canUse(this.context))
106 {
107 this.context.registerReceiver(this.wiFiChangeReceiver, new IntentFilter(Co nnectivityManager.CONNECTIVITY_ACTION));
108 return true;
109 }
110
111 return false;
112 }
113
114 private boolean sendIntent(final LinkProperties lp, final ProxyProperties prox yProperties)
115 {
116 boolean success = true;
117
118 final ConnectivityManager conMan = (ConnectivityManager) context.getSystemSe rvice(Context.CONNECTIVITY_SERVICE);
119 final NetworkInfo ni = conMan.getActiveNetworkInfo();
120
121 if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI)
122 {
123 final Intent intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHAN GED");
124
125 if (lp.isValid())
126 {
127 try
128 {
129 lp.setHttpProxy(proxyProperties);
130 intent.putExtra("linkProperties", (Parcelable) lp.getLinkProperties()) ;
131 context.sendBroadcast(intent);
132 }
133 catch (final Exception e)
134 {
135 // Catch all, again
136 success = false;
137 }
138 }
139 }
140
141 return success;
142 }
143
144 private boolean registerProxy(final ProxyProperties proxyProperties)
145 {
146 try
147 {
148 final ConnectivityManager conMan = (ConnectivityManager) context.getSystem Service(Context.CONNECTIVITY_SERVICE);
149
150 return this.sendIntent(LinkProperties.getActiveLinkProperties(conMan), pro xyProperties);
151 }
152 catch (final CompatibilityException e)
153 {
154 return false;
155 }
156 }
157
158 private boolean reRegisterProxy()
159 {
160 return this.registerProxy(this.proxyProperties);
161 }
162
163 @Override
164 public boolean registerProxy(final InetAddress address, final int port)
165 {
166 this.proxyProperties = new ProxyProperties(address.getHostName(), port, "");
167
168 return this.registerProxy(this.proxyProperties);
169 }
170
171 @Override
172 public void unregisterProxy()
173 {
174 this.proxyProperties = null;
175
176 this.registerProxy(this.proxyProperties);
177 }
178
179 @Override
180 public void shutdown()
181 {
182 this.context.unregisterReceiver(this.wiFiChangeReceiver);
183 }
184
185 @Override
186 public ProxyRegistrationType getType()
187 {
188 return ProxyRegistrationType.REFLECTION;
189 }
190
191 @Override
192 public boolean isRegistered()
193 {
194 // TODO Auto-generated method stub
195 return false;
196 }
197
198 @Override
199 public boolean isSticky()
200 {
201 return false;
202 }
203
204 @Override
205 public String toString()
206 {
207 return "[ProxyConfigurator: " + this.getType() + "]";
208 }
209
210 private final static class WiFiChangeReceiver extends BroadcastReceiver
211 {
212 private final ReflectionProxyConfigurator configurator;
213
214 private WiFiChangeReceiver(final ReflectionProxyConfigurator configurator)
215 {
216 this.configurator = configurator;
217 }
218
219 @Override
220 public void onReceive(final Context context, final Intent intent)
221 {
222 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
223 {
224 final ConnectivityManager connectivityManager = (ConnectivityManager) co ntext.getSystemService(Context.CONNECTIVITY_SERVICE);
225 final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
226
227 if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI && ni.is Available() && ni.isConnected())
228 {
229 this.configurator.reRegisterProxy();
230 }
231 }
232 }
233 }
234 }
OLDNEW

Powered by Google App Engine
This is Rietveld