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

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

Issue 4705284891082752: Proxy configurators (Closed)
Patch Set: FIXME removed Created Aug. 19, 2014, 12:58 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 import java.util.concurrent.Semaphore;
22 import java.util.concurrent.locks.ReentrantLock;
23
24 import org.adblockplus.android.BridgeCommand;
25 import org.adblockplus.android.ConfigurationActivity;
26 import org.adblockplus.android.ProxyService;
27 import org.adblockplus.android.R;
28 import org.adblockplus.android.compat.ProxyProperties;
29 import org.adblockplus.brazil.RequestHandler;
30
31 import android.app.NotificationManager;
32 import android.app.PendingIntent;
33 import android.content.Context;
34 import android.content.Intent;
35 import android.os.Handler;
36 import android.support.v4.app.NotificationCompat;
37
38 /**
39 * A dummy registrator only holding callbacks and checks.
40 */
41 public class ManualProxyConfigurator implements ProxyConfigurator
42 {
43 final Context context;
44 private ProxyProperties proxyProperties = null;
45 private static final int NOTRAFFIC_NOTIFICATION_ID = R.string.proxysettings_na me;
46 private static final int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes
47 private volatile boolean isRegistered = false;
48 private NoTrafficWorker noTrafficWorker = null;
49 private final ReentrantLock noTrafficAccessLock = new ReentrantLock();
50 private final Handler uiHandler;
51
52 public ManualProxyConfigurator(final Context context)
53 {
54 this.uiHandler = new Handler();
55 this.context = context;
56 }
57
58 @Override
59 public boolean initialize()
60 {
61 return true;
62 }
63
64 @Override
65 public boolean registerProxy(final InetAddress address, final int port)
66 {
67 this.proxyProperties = new ProxyProperties(address.getHostName(), port, "");
68 this.startNoTrafficCheck();
69 return true;
70 }
71
72 @Override
73 public void unregisterProxy()
74 {
75 this.isRegistered = false;
76 this.abortNoTrafficCheck();
77 }
78
79 @Override
80 public void shutdown()
81 {
82 this.removeErrorNotification();
83 }
84
85 @Override
86 public ProxyRegistrationType getType()
87 {
88 return ProxyRegistrationType.MANUAL;
89 }
90
91 @Override
92 public boolean isRegistered()
93 {
94 return this.isRegistered;
95 }
96
97 @Override
98 public boolean isSticky()
99 {
100 return true;
101 }
102
103 @Override
104 public String toString()
105 {
106 return "[ProxyConfigurator: " + this.getType() + "]";
107 }
108
109 private void removeErrorNotification()
110 {
111 final NotificationManager notificationManager =
112 (NotificationManager) this.context.getSystemService(Context.NOTIFICATION _SERVICE);
113 notificationManager.cancel(NOTRAFFIC_NOTIFICATION_ID);
114 }
115
116 private void startNoTrafficCheck()
117 {
118 this.noTrafficAccessLock.lock();
119 try
120 {
121 if (this.noTrafficWorker == null)
122 {
123 this.noTrafficWorker = new NoTrafficWorker(this);
124 final Thread t = new Thread(this.noTrafficWorker);
125 t.setDaemon(true);
126 t.start();
127 }
128 }
129 finally
130 {
131 this.noTrafficAccessLock.unlock();
132 }
133 }
134
135 private void abortNoTrafficCheck()
136 {
137 this.noTrafficAccessLock.lock();
138 try
139 {
140 if (this.noTrafficWorker != null)
141 {
142 this.noTrafficWorker.stop();
143 }
144 }
145 finally
146 {
147 this.noTrafficWorker = null;
148 this.noTrafficAccessLock.unlock();
149 }
150 }
151
152 private synchronized void trafficReceived()
153 {
154 this.isRegistered = true;
155 this.abortNoTrafficCheck();
156
157 this.uiHandler.post(new Runnable()
158 {
159 @Override
160 public void run()
161 {
162 ManualProxyConfigurator.this.removeErrorNotification();
163 ProxyService.sendBridgeCommand(ManualProxyConfigurator.this.context, Bri dgeCommand.STATE_CHANGED);
164 }
165 });
166 }
167
168 private synchronized void noTrafficReceived()
169 {
170 this.isRegistered = false;
171 this.abortNoTrafficCheck();
172
173 this.uiHandler.post(new Runnable()
174 {
175 @Override
176 public void run()
177 {
178 final Context context = ManualProxyConfigurator.this.context;
179 // Show warning notification
180 final Intent intent =
181 new Intent(context, ConfigurationActivity.class)
182 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
183 .putExtra("port", ManualProxyConfigurator.this.proxyProperties.g etPort());
184
185 final PendingIntent contentIntent =
186 PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPD ATE_CURRENT);
187
188 final NotificationCompat.Builder builder =
189 new NotificationCompat.Builder(context)
190 .setSmallIcon(R.drawable.ic_stat_warning)
191 .setWhen(System.currentTimeMillis())
192 .setAutoCancel(true)
193 .setContentIntent(contentIntent)
194 .setContentTitle(context.getText(R.string.app_name))
195 .setContentText(context.getText(R.string.notif_notraffic));
196
197 final NotificationManager notificationManager = (NotificationManager) co ntext.getSystemService(Context.NOTIFICATION_SERVICE);
198 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, builder.getNotific ation());
199
200 ProxyService.sendBridgeCommand(context, BridgeCommand.STATE_CHANGED);
201 }
202 });
203 }
204
205 private static class NoTrafficWorker implements Runnable
206 {
207 private volatile boolean running = true;
208 private final Semaphore finished = new Semaphore(1);
209 private final ManualProxyConfigurator manualProxyConfigurator;
210
211 public NoTrafficWorker(final ManualProxyConfigurator manualProxyConfigurator )
212 {
213 this.manualProxyConfigurator = manualProxyConfigurator;
214 this.finished.acquireUninterruptibly();
215 }
216
217 @Override
218 public void run()
219 {
220 try
221 {
222 final long endTime = System.currentTimeMillis() + NO_TRAFFIC_TIMEOUT;
223
224 final long blockedStart = RequestHandler.getBlockedRequestCount();
225 final long unblockedStart = RequestHandler.getUnblockedRequestCount();
226
227 while (this.running)
228 {
229 try
230 {
231 if (System.currentTimeMillis() >= endTime)
232 {
233 this.running = false;
234 this.manualProxyConfigurator.noTrafficReceived();
235 break;
236 }
237
238 if (RequestHandler.getBlockedRequestCount() != blockedStart
239 || RequestHandler.getUnblockedRequestCount() != unblockedStart)
240 {
241 this.running = false;
242 this.manualProxyConfigurator.trafficReceived();
243 break;
244 }
245
246 Thread.sleep(100);
247 }
248 catch (final Throwable t)
249 {
250 // Swallow everything to keep this thread alive at all cost
251 }
252 }
253 }
254 finally
255 {
256 this.finished.release();
257 }
258 }
259
260 public synchronized void stop()
261 {
262 if (this.running)
263 {
264 this.running = false;
265 this.finished.acquireUninterruptibly();
266 }
267 }
268 }
269 }
OLDNEW

Powered by Google App Engine
This is Rietveld