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

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

Issue 4705284891082752: Proxy configurators (Closed)
Patch Set: Last batch of review issues Created Aug. 24, 2014, 11:52 a.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 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 // We're using reflection directly here to keep this method self-contain ed.
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 // We're using reflection directly here to keep this method self-contain ed.
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 final ConnectivityManager conMan = (ConnectivityManager) context.getSystemSe rvice(Context.CONNECTIVITY_SERVICE);
116 final NetworkInfo ni = conMan.getActiveNetworkInfo();
117
118 if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI)
119 {
120 final Intent intent = new Intent("android.net.wifi.LINK_CONFIGURATION_CHAN GED");
121
122 if (lp.isValid())
123 {
124 try
125 {
126 lp.setHttpProxy(proxyProperties);
127 intent.putExtra("linkProperties", (Parcelable) lp.getLinkProperties()) ;
128 context.sendBroadcast(intent);
129 }
130 catch (final Exception e)
131 {
132 // Catch all, again
133 return false;
134 }
135 }
136 }
137
138 return true;
139 }
140
141 private boolean registerProxy(final ProxyProperties proxyProperties)
142 {
143 try
144 {
145 final ConnectivityManager conMan = (ConnectivityManager) context.getSystem Service(Context.CONNECTIVITY_SERVICE);
146
147 return this.sendIntent(LinkProperties.getActiveLinkProperties(conMan), pro xyProperties);
148 }
149 catch (final CompatibilityException e)
150 {
151 return false;
152 }
153 }
154
155 private boolean reRegisterProxy()
156 {
157 return this.registerProxy(this.proxyProperties);
158 }
159
160 @Override
161 public boolean registerProxy(final InetAddress address, final int port)
162 {
163 this.proxyProperties = new ProxyProperties(address.getHostName(), port, "");
164
165 return this.isRegistered = this.registerProxy(this.proxyProperties);
166 }
167
168 @Override
169 public void unregisterProxy()
170 {
171 this.proxyProperties = null;
172 this.isRegistered = false;
173 this.registerProxy(this.proxyProperties);
174 }
175
176 @Override
177 public void shutdown()
178 {
179 this.context.unregisterReceiver(this.wiFiChangeReceiver);
180 }
181
182 @Override
183 public ProxyRegistrationType getType()
184 {
185 return ProxyRegistrationType.NATIVE;
186 }
187
188 @Override
189 public boolean isRegistered()
190 {
191 return this.isRegistered;
192 }
193
194 @Override
195 public boolean isSticky()
196 {
197 return false;
198 }
199
200 @Override
201 public String toString()
202 {
203 return "[ProxyConfigurator: " + this.getType() + "]";
204 }
205
206 private final static class WiFiChangeReceiver extends BroadcastReceiver
207 {
208 private final NativeProxyConfigurator configurator;
209
210 private WiFiChangeReceiver(final NativeProxyConfigurator configurator)
211 {
212 this.configurator = configurator;
213 }
214
215 @Override
216 public void onReceive(final Context context, final Intent intent)
217 {
218 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction()))
219 {
220 final ConnectivityManager connectivityManager = (ConnectivityManager) co ntext.getSystemService(Context.CONNECTIVITY_SERVICE);
221 final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
222
223 if (ni != null && ni.getType() == ConnectivityManager.TYPE_WIFI && ni.is Available() && ni.isConnected())
224 {
225 this.configurator.reRegisterProxy();
226 }
227 }
228 }
229 }
230 }
OLDNEW

Powered by Google App Engine
This is Rietveld