| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 package org.adblockplus.android.configurators; | 18 package org.adblockplus.android.configurators; |
| 19 | 19 |
| 20 import java.net.InetAddress; | 20 import java.net.InetAddress; |
| 21 import java.util.concurrent.Semaphore; | 21 import java.util.concurrent.Semaphore; |
| 22 import java.util.concurrent.locks.ReentrantLock; | 22 import java.util.concurrent.locks.ReentrantLock; |
| 23 | 23 |
| 24 import org.adblockplus.android.BridgeCommand; | |
| 25 import org.adblockplus.android.ConfigurationActivity; | 24 import org.adblockplus.android.ConfigurationActivity; |
| 26 import org.adblockplus.android.ProxyService; | 25 import org.adblockplus.android.ProxyService; |
| 27 import org.adblockplus.android.R; | 26 import org.adblockplus.android.R; |
| 28 import org.adblockplus.android.compat.ProxyProperties; | 27 import org.adblockplus.android.compat.ProxyProperties; |
| 29 import org.adblockplus.brazil.RequestHandler; | 28 import org.adblockplus.brazil.RequestHandler; |
| 30 | 29 |
| 31 import android.app.NotificationManager; | 30 import android.app.NotificationManager; |
| 32 import android.app.PendingIntent; | 31 import android.app.PendingIntent; |
| 33 import android.content.Context; | 32 import android.content.Context; |
| 34 import android.content.Intent; | 33 import android.content.Intent; |
| 35 import android.os.Handler; | 34 import android.os.Handler; |
| 36 import android.support.v4.app.NotificationCompat; | 35 import android.support.v4.app.NotificationCompat; |
| 37 | 36 |
| 38 /** | 37 /** |
| 39 * A dummy registrator only holding callbacks and checks. | 38 * A dummy registrator only holding callbacks and checks. |
| 40 * | |
| 41 * @author rjeschke | |
| 42 */ | 39 */ |
| 43 public class ManualProxyConfigurator implements ProxyConfigurator | 40 public class ManualProxyConfigurator implements ProxyConfigurator |
| 44 { | 41 { |
| 45 final Context context; | 42 final Context context; |
| 46 private ProxyProperties proxyProperties = null; | 43 private ProxyProperties proxyProperties = null; |
| 47 private static final int NOTRAFFIC_NOTIFICATION_ID = R.string.proxysettings_na
me; | 44 private static final int NOTRAFFIC_NOTIFICATION_ID = R.string.proxysettings_na
me; |
| 48 private static final int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes | 45 private static final int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes |
| 49 private volatile boolean isRegistered = false; | 46 private volatile boolean isRegistered = false; |
| 50 private NoTrafficWorker noTrafficWorker = null; | 47 private NoTrafficWorker noTrafficWorker = null; |
| 51 private ReentrantLock noTrafficAccessLock = new ReentrantLock(); | 48 private final ReentrantLock noTrafficAccessLock = new ReentrantLock(); |
| 52 private final Handler uiHandler; | 49 private final Handler uiHandler; |
| 53 | 50 |
| 54 public ManualProxyConfigurator(final Context context) | 51 public ManualProxyConfigurator(final Context context) |
| 55 { | 52 { |
| 56 this.uiHandler = new Handler(); | 53 this.uiHandler = new Handler(); |
| 57 this.context = context; | 54 this.context = context; |
| 58 } | 55 } |
| 59 | 56 |
| 60 @Override | 57 @Override |
| 61 public boolean initialize() | 58 public boolean initialize() |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 { | 152 { |
| 156 this.isRegistered = true; | 153 this.isRegistered = true; |
| 157 this.abortNoTrafficCheck(); | 154 this.abortNoTrafficCheck(); |
| 158 | 155 |
| 159 this.uiHandler.post(new Runnable() | 156 this.uiHandler.post(new Runnable() |
| 160 { | 157 { |
| 161 @Override | 158 @Override |
| 162 public void run() | 159 public void run() |
| 163 { | 160 { |
| 164 ManualProxyConfigurator.this.removeErrorNotification(); | 161 ManualProxyConfigurator.this.removeErrorNotification(); |
| 165 ProxyService.sendBridgeCommand(ManualProxyConfigurator.this.context, Bri
dgeCommand.STATE_CHANGED); | 162 ManualProxyConfigurator.this.context.sendBroadcast(new Intent(ProxyServi
ce.PROXY_STATE_CHANGED_ACTION)); |
| 166 } | 163 } |
| 167 }); | 164 }); |
| 168 } | 165 } |
| 169 | 166 |
| 170 private synchronized void noTrafficReceived() | 167 private synchronized void noTrafficReceived() |
| 171 { | 168 { |
| 172 this.isRegistered = false; | 169 this.isRegistered = false; |
| 173 this.abortNoTrafficCheck(); | 170 this.abortNoTrafficCheck(); |
| 174 | 171 |
| 175 this.uiHandler.post(new Runnable() | 172 this.uiHandler.post(new Runnable() |
| (...skipping 16 matching lines...) Expand all Loading... |
| 192 .setSmallIcon(R.drawable.ic_stat_warning) | 189 .setSmallIcon(R.drawable.ic_stat_warning) |
| 193 .setWhen(System.currentTimeMillis()) | 190 .setWhen(System.currentTimeMillis()) |
| 194 .setAutoCancel(true) | 191 .setAutoCancel(true) |
| 195 .setContentIntent(contentIntent) | 192 .setContentIntent(contentIntent) |
| 196 .setContentTitle(context.getText(R.string.app_name)) | 193 .setContentTitle(context.getText(R.string.app_name)) |
| 197 .setContentText(context.getText(R.string.notif_notraffic)); | 194 .setContentText(context.getText(R.string.notif_notraffic)); |
| 198 | 195 |
| 199 final NotificationManager notificationManager = (NotificationManager) co
ntext.getSystemService(Context.NOTIFICATION_SERVICE); | 196 final NotificationManager notificationManager = (NotificationManager) co
ntext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 200 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, builder.getNotific
ation()); | 197 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, builder.getNotific
ation()); |
| 201 | 198 |
| 202 ProxyService.sendBridgeCommand(context, BridgeCommand.STATE_CHANGED); | 199 context.sendBroadcast(new Intent(ProxyService.PROXY_STATE_CHANGED_ACTION
)); |
| 203 } | 200 } |
| 204 }); | 201 }); |
| 205 } | 202 } |
| 206 | 203 |
| 207 private static class NoTrafficWorker implements Runnable | 204 private static class NoTrafficWorker implements Runnable |
| 208 { | 205 { |
| 209 private volatile boolean running = true; | 206 private volatile boolean running = true; |
| 210 private final Semaphore finished = new Semaphore(1); | 207 private final Semaphore finished = new Semaphore(1); |
| 211 private final ManualProxyConfigurator manualProxyConfigurator; | 208 private final ManualProxyConfigurator manualProxyConfigurator; |
| 212 | 209 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 240 if (RequestHandler.getBlockedRequestCount() != blockedStart | 237 if (RequestHandler.getBlockedRequestCount() != blockedStart |
| 241 || RequestHandler.getUnblockedRequestCount() != unblockedStart) | 238 || RequestHandler.getUnblockedRequestCount() != unblockedStart) |
| 242 { | 239 { |
| 243 this.running = false; | 240 this.running = false; |
| 244 this.manualProxyConfigurator.trafficReceived(); | 241 this.manualProxyConfigurator.trafficReceived(); |
| 245 break; | 242 break; |
| 246 } | 243 } |
| 247 | 244 |
| 248 Thread.sleep(100); | 245 Thread.sleep(100); |
| 249 } | 246 } |
| 250 catch (Throwable t) | 247 catch (final Throwable t) |
| 251 { | 248 { |
| 252 // Swallow everything to keep this thread alive at all cost | 249 // Swallow everything to keep this thread alive at all cost |
| 253 } | 250 } |
| 254 } | 251 } |
| 255 } | 252 } |
| 256 finally | 253 finally |
| 257 { | 254 { |
| 258 this.finished.release(); | 255 this.finished.release(); |
| 259 } | 256 } |
| 260 } | 257 } |
| 261 | 258 |
| 262 public synchronized void stop() | 259 public synchronized void stop() |
| 263 { | 260 { |
| 264 if (this.running) | 261 if (this.running) |
| 265 { | 262 { |
| 266 this.running = false; | 263 this.running = false; |
| 267 this.finished.acquireUninterruptibly(); | 264 this.finished.acquireUninterruptibly(); |
| 268 } | 265 } |
| 269 } | 266 } |
| 270 } | 267 } |
| 271 } | 268 } |
| LEFT | RIGHT |