OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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; | 18 package org.adblockplus.android; |
19 | 19 |
20 import java.io.IOException; | 20 import java.io.IOException; |
21 import java.net.InetAddress; | 21 import java.net.InetAddress; |
22 import java.net.ServerSocket; | 22 import java.net.ServerSocket; |
23 import java.net.UnknownHostException; | 23 import java.net.UnknownHostException; |
24 import java.util.Properties; | 24 import java.util.Properties; |
25 | 25 |
26 import org.adblockplus.android.configurators.ProxyConfigurator; | 26 import org.adblockplus.android.configurators.ProxyConfigurator; |
27 import org.adblockplus.android.configurators.ProxyConfigurators; | 27 import org.adblockplus.android.configurators.ProxyConfigurators; |
28 import org.adblockplus.android.configurators.ProxyRegistrationType; | 28 import org.adblockplus.android.configurators.ProxyRegistrationType; |
29 import org.adblockplus.libadblockplus.Notification.Type; | |
30 import org.apache.commons.lang.StringUtils; | 29 import org.apache.commons.lang.StringUtils; |
31 | 30 |
32 import sunlabs.brazil.server.Server; | 31 import sunlabs.brazil.server.Server; |
33 import sunlabs.brazil.util.Base64; | 32 import sunlabs.brazil.util.Base64; |
34 import android.annotation.SuppressLint; | 33 import android.annotation.SuppressLint; |
35 import android.app.Notification; | 34 import android.app.Notification; |
36 import android.app.NotificationManager; | 35 import android.app.NotificationManager; |
37 import android.app.PendingIntent; | 36 import android.app.PendingIntent; |
38 import android.app.Service; | 37 import android.app.Service; |
39 import android.content.BroadcastReceiver; | 38 import android.content.BroadcastReceiver; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 boolean hideIcon; | 84 boolean hideIcon; |
86 | 85 |
87 protected ProxyServer proxy = null; | 86 protected ProxyServer proxy = null; |
88 | 87 |
89 protected int port; | 88 protected int port; |
90 | 89 |
91 private final Properties proxyConfiguration = new Properties(); | 90 private final Properties proxyConfiguration = new Properties(); |
92 | 91 |
93 private ProxyConfigurator proxyConfigurator = null; | 92 private ProxyConfigurator proxyConfigurator = null; |
94 | 93 |
95 private NotificationWatcher notificationWatcher = null; | |
96 | |
97 @SuppressLint("NewApi") | 94 @SuppressLint("NewApi") |
98 @Override | 95 @Override |
99 public void onCreate() | 96 public void onCreate() |
100 { | 97 { |
101 super.onCreate(); | 98 super.onCreate(); |
102 | 99 |
103 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() | 100 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() |
104 .permitAll() | 101 .permitAll() |
105 .penaltyLog() | 102 .penaltyLog() |
106 .build()); | 103 .build()); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 } | 215 } |
219 | 216 |
220 prefs.registerOnSharedPreferenceChangeListener(this); | 217 prefs.registerOnSharedPreferenceChangeListener(this); |
221 | 218 |
222 // Lock service | 219 // Lock service |
223 hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), getResources(
).getBoolean(R.bool.def_hideicon)); | 220 hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), getResources(
).getBoolean(R.bool.def_hideicon)); |
224 startForeground(AdblockPlus.ONGOING_NOTIFICATION_ID, getNotification()); | 221 startForeground(AdblockPlus.ONGOING_NOTIFICATION_ID, getNotification()); |
225 | 222 |
226 sendStateChangedBroadcast(); | 223 sendStateChangedBroadcast(); |
227 | 224 |
228 this.startNotificationWatcher(); | |
229 | |
230 Log.i(TAG, "Service started"); | 225 Log.i(TAG, "Service started"); |
231 } | 226 } |
232 | 227 |
233 @Override | 228 @Override |
234 public int onStartCommand(final Intent intent, final int flags, final int star
tId) | 229 public int onStartCommand(final Intent intent, final int flags, final int star
tId) |
235 { | 230 { |
236 return START_STICKY; | 231 return START_STICKY; |
237 } | 232 } |
238 | 233 |
239 @Override | 234 @Override |
240 public void onDestroy() | 235 public void onDestroy() |
241 { | 236 { |
242 super.onDestroy(); | 237 super.onDestroy(); |
243 | 238 |
244 this.stopNotificationWatcher(); | |
245 | |
246 unregisterReceiver(this.proxyReceiver); | 239 unregisterReceiver(this.proxyReceiver); |
247 unregisterReceiver(this.proxyStateChangedReceiver); | 240 unregisterReceiver(this.proxyStateChangedReceiver); |
248 | 241 |
249 if (this.proxyConfigurator != null) | 242 if (this.proxyConfigurator != null) |
250 { | 243 { |
251 this.proxyConfigurator.unregisterProxy(); | 244 this.proxyConfigurator.unregisterProxy(); |
252 this.proxyConfigurator.shutdown(); | 245 this.proxyConfigurator.shutdown(); |
253 } | 246 } |
254 | 247 |
255 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)
); | 248 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)
); |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 | 588 |
596 @Override | 589 @Override |
597 public void log(final int level, final Object obj, final String message) | 590 public void log(final int level, final Object obj, final String message) |
598 { | 591 { |
599 if (level <= logLevel) | 592 if (level <= logLevel) |
600 { | 593 { |
601 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); | 594 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); |
602 } | 595 } |
603 } | 596 } |
604 } | 597 } |
605 | |
606 private void startNotificationWatcher() | |
607 { | |
608 this.stopNotificationWatcher(); | |
609 | |
610 this.notificationWatcher = new NotificationWatcher(this); | |
611 | |
612 final Thread thread = new Thread(this.notificationWatcher); | |
613 thread.setDaemon(true); | |
614 thread.start(); | |
615 } | |
616 | |
617 private void stopNotificationWatcher() | |
618 { | |
619 if (this.notificationWatcher != null) | |
620 { | |
621 this.notificationWatcher.stop(); | |
622 this.notificationWatcher = null; | |
623 } | |
624 } | |
625 | |
626 private static class NotificationWatcher implements Runnable | |
627 { | |
628 public static final int DEFAULT_POLL_DELAY = 3 * 60; /* seconds */ | |
629 public static final int DEFAULT_POLL_INTERVAL = 0; /* seconds */ | |
630 | |
631 private final ProxyService proxyService; | |
632 private final int pollDelay; | |
633 private final int pollInterval; | |
634 | |
635 volatile boolean running = true; | |
636 | |
637 public NotificationWatcher(final ProxyService proxyService, final int pollDe
lay, | |
638 final int pollInterval) | |
639 { | |
640 this.proxyService = proxyService; | |
641 this.pollDelay = Math.max(0, pollDelay); | |
642 this.pollInterval = Math.max(0, pollInterval); | |
643 } | |
644 | |
645 public NotificationWatcher(final ProxyService proxyService) | |
646 { | |
647 this(proxyService, DEFAULT_POLL_DELAY, DEFAULT_POLL_INTERVAL); | |
648 } | |
649 | |
650 protected void stop() | |
651 { | |
652 this.running = false; | |
653 } | |
654 | |
655 @Override | |
656 public void run() | |
657 { | |
658 if (this.pollDelay == 0) | |
659 { | |
660 this.running = false; | |
661 return; | |
662 } | |
663 | |
664 long nextPoll = System.currentTimeMillis() + 1000L * this.pollDelay; | |
665 | |
666 while (this.running) | |
667 { | |
668 try | |
669 { | |
670 Thread.sleep(250); | |
671 } | |
672 catch (InterruptedException ex) | |
673 { | |
674 break; | |
675 } | |
676 | |
677 if (System.currentTimeMillis() >= nextPoll && this.running) | |
678 { | |
679 try | |
680 { | |
681 Log.d(TAG, "Polling for notifications"); | |
682 org.adblockplus.libadblockplus.Notification notification = | |
683 AdblockPlus.getApplication().getNextNotificationToShow(); | |
684 | |
685 while (notification != null | |
686 && (notification.getType() == Type.INVALID || notification.getTy
pe() == Type.QUESTION)) | |
687 { | |
688 notification = AdblockPlus.getApplication().getNextNotificationToS
how(); | |
689 } | |
690 | |
691 if (notification != null) | |
692 { | |
693 final NotificationManager notificationManager = (NotificationManag
er) this.proxyService | |
694 .getSystemService(NOTIFICATION_SERVICE); | |
695 | |
696 notificationManager.notify(AdblockPlus.SERVER_NOTIFICATION_ID, | |
697 new NotificationCompat.Builder(this.proxyService.getApplicatio
nContext()) | |
698 .setSmallIcon(R.drawable.ic_stat_blocking) | |
699 .setContentTitle(notification.getTitle()) | |
700 .setContentText(notification.getMessageString()) | |
701 .getNotification()); | |
702 } | |
703 } | |
704 catch (Exception ex) | |
705 { | |
706 Log.e(TAG, "Polling for notifications failed: " + ex.getMessage(), e
x); | |
707 } | |
708 | |
709 if (this.pollInterval == 0) | |
710 { | |
711 this.running = false; | |
712 } | |
713 else | |
714 { | |
715 nextPoll = System.currentTimeMillis() + 1000L * this.pollInterval; | |
716 } | |
717 } | |
718 } | |
719 } | |
720 } | |
721 } | 598 } |
OLD | NEW |