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

Powered by Google App Engine
This is Rietveld