Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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; | |
29 import org.apache.commons.lang.StringUtils; | 30 import org.apache.commons.lang.StringUtils; |
30 | 31 |
31 import sunlabs.brazil.server.Server; | 32 import sunlabs.brazil.server.Server; |
32 import sunlabs.brazil.util.Base64; | 33 import sunlabs.brazil.util.Base64; |
33 import android.annotation.SuppressLint; | 34 import android.annotation.SuppressLint; |
34 import android.app.Notification; | 35 import android.app.Notification; |
35 import android.app.NotificationManager; | 36 import android.app.NotificationManager; |
36 import android.app.PendingIntent; | 37 import android.app.PendingIntent; |
37 import android.app.Service; | 38 import android.app.Service; |
38 import android.content.BroadcastReceiver; | 39 import android.content.BroadcastReceiver; |
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
615 | 616 |
616 private void stopNotificationWatcher() | 617 private void stopNotificationWatcher() |
617 { | 618 { |
618 if (this.notificationWatcher != null) | 619 if (this.notificationWatcher != null) |
619 { | 620 { |
620 this.notificationWatcher.stop(); | 621 this.notificationWatcher.stop(); |
621 this.notificationWatcher = null; | 622 this.notificationWatcher = null; |
622 } | 623 } |
623 } | 624 } |
624 | 625 |
625 private static class NotificationWatcher implements Runnable | 626 private static class NotificationWatcher implements Runnable |
Felix Dahlke
2015/02/18 12:49:56
This seems to be way too much code for what we wan
René Jeschke
2015/02/18 13:13:02
AlarmManager: Why use a system-wide, scheduling se
Felix Dahlke
2015/02/18 14:10:07
Well, if this doesn't work reliably on the EDT, fa
| |
626 { | 627 { |
627 public static final int DEFAULT_FIRST_POLL_INTERVAL = 3 * 60; /* seconds */ | 628 public static final int DEFAULT_POLL_DELAY = 3 * 60; /* seconds */ |
628 public static final int DEFAULT_NEXT_POLL_INTERVALS = 0; /* seconds */ | 629 public static final int DEFAULT_POLL_INTERVAL = 0; /* seconds */ |
629 | 630 |
630 private final ProxyService proxyService; | 631 private final ProxyService proxyService; |
631 private final int firstPollInterval; | 632 private final int pollDelay; |
Felix Dahlke
2015/02/18 14:10:07
How about initialDelay? It's not really an interva
René Jeschke
2015/02/18 14:45:20
Done.
| |
632 private final int nextPollIntervals; | 633 private final int pollInterval; |
Felix Dahlke
2015/02/18 14:10:07
Assuming firstPollInterval is renamed, this could
René Jeschke
2015/02/18 14:45:20
Done.
| |
633 | 634 |
634 volatile boolean running = true; | 635 volatile boolean running = true; |
635 | 636 |
636 public NotificationWatcher(final ProxyService proxyService, final int firstP ollInterval, | 637 public NotificationWatcher(final ProxyService proxyService, final int pollDe lay, |
637 final int nextPollIntervals) | 638 final int pollInterval) |
638 { | 639 { |
639 this.proxyService = proxyService; | 640 this.proxyService = proxyService; |
640 this.firstPollInterval = Math.max(0, firstPollInterval); | 641 this.pollDelay = Math.max(0, pollDelay); |
641 this.nextPollIntervals = Math.max(0, nextPollIntervals); | 642 this.pollInterval = Math.max(0, pollInterval); |
642 } | 643 } |
643 | 644 |
644 public NotificationWatcher(final ProxyService proxyService) | 645 public NotificationWatcher(final ProxyService proxyService) |
645 { | 646 { |
646 this(proxyService, DEFAULT_FIRST_POLL_INTERVAL, DEFAULT_NEXT_POLL_INTERVAL S); | 647 this(proxyService, DEFAULT_POLL_DELAY, DEFAULT_POLL_INTERVAL); |
647 } | 648 } |
648 | 649 |
649 protected void stop() | 650 protected void stop() |
650 { | 651 { |
651 this.running = false; | 652 this.running = false; |
652 } | 653 } |
653 | 654 |
654 @Override | 655 @Override |
655 public void run() | 656 public void run() |
656 { | 657 { |
657 if (this.firstPollInterval == 0) | 658 if (this.pollDelay == 0) |
658 { | 659 { |
659 this.running = false; | 660 this.running = false; |
660 return; | 661 return; |
661 } | 662 } |
662 | 663 |
663 long nextPoll = System.currentTimeMillis() + 1000L * this.firstPollInterva l; | 664 long nextPoll = System.currentTimeMillis() + 1000L * this.pollDelay; |
664 | 665 |
665 while (this.running) | 666 while (this.running) |
666 { | 667 { |
667 try | 668 try |
668 { | 669 { |
669 Thread.sleep(250); | 670 Thread.sleep(250); |
670 } | 671 } |
671 catch (InterruptedException ex) | 672 catch (InterruptedException ex) |
672 { | 673 { |
673 break; | 674 break; |
674 } | 675 } |
675 | 676 |
676 if (System.currentTimeMillis() >= nextPoll && this.running) | 677 if (System.currentTimeMillis() >= nextPoll && this.running) |
677 { | 678 { |
678 try | 679 try |
679 { | 680 { |
680 Log.d(TAG, "Polling for notifications"); | 681 Log.d(TAG, "Polling for notifications"); |
681 org.adblockplus.libadblockplus.Notification notification = AdblockPl us.getApplication() | 682 org.adblockplus.libadblockplus.Notification notification = |
682 .getNotificationToShow(); | 683 AdblockPlus.getApplication().getNextNotificationToShow(); |
683 | 684 |
684 int idx = 0; | 685 while (notification != null |
685 while (notification != null) | 686 && (notification.getType() == Type.INVALID || notification.getTy pe() == Type.QUESTION)) |
Felix Dahlke
2015/02/18 14:10:07
We should actually just show a single notification
René Jeschke
2015/02/18 14:45:20
It is guaranteed that there aren't any two notific
Felix Dahlke
2015/02/18 14:55:04
There can be an arbitrary number of notifications
| |
686 { | 687 { |
687 Notification notif = new NotificationCompat.Builder( | 688 notification = AdblockPlus.getApplication().getNextNotificationToS how(); |
688 this.proxyService.getApplicationContext()) | 689 } |
Felix Dahlke
2015/02/18 14:10:07
Shouldn't we pass the app's context here rather th
René Jeschke
2015/02/18 14:45:20
I still am working on the service separation. The
| |
689 .setSmallIcon(R.drawable.ic_stat_blocking) | 690 |
690 .setContentTitle(notification.getTitle()) | 691 if (notification != null) |
691 .setContentText(notification.getMessageString()) | 692 { |
692 .getNotification(); | |
693 | |
694 final NotificationManager notificationManager = (NotificationManag er) this.proxyService | 693 final NotificationManager notificationManager = (NotificationManag er) this.proxyService |
695 .getSystemService(NOTIFICATION_SERVICE); | 694 .getSystemService(NOTIFICATION_SERVICE); |
696 notificationManager.notify(AdblockPlus.GROUPED_NOTIFICATION_ID + i dx, notif); | 695 |
697 | 696 notificationManager.notify(AdblockPlus.SERVER_NOTIFICATION_ID, |
698 notification.markAsShown(); | 697 new NotificationCompat.Builder(this.proxyService.getApplicatio nContext()) |
Felix Dahlke
2015/02/18 14:10:07
We actually only need to call this for question no
René Jeschke
2015/02/18 14:45:20
Ah, Ok. I thought this has something to do with 'f
| |
699 | 698 .setSmallIcon(R.drawable.ic_stat_blocking) |
700 idx++; | 699 .setContentTitle(notification.getTitle()) |
701 notification = AdblockPlus.getApplication().getNotificationToShow( ); | 700 .setContentText(notification.getMessageString()) |
701 .getNotification()); | |
702 } | 702 } |
703 | |
704 Log.d(TAG, "Received " + idx + " new notifications"); | |
705 } | 703 } |
706 catch (Exception ex) | 704 catch (Exception ex) |
707 { | 705 { |
708 Log.e(TAG, "Polling for notifications failed: " + ex.getMessage(), e x); | 706 Log.e(TAG, "Polling for notifications failed: " + ex.getMessage(), e x); |
709 } | 707 } |
710 | 708 |
711 if (this.nextPollIntervals == 0) | 709 if (this.pollInterval == 0) |
712 { | 710 { |
713 this.running = false; | 711 this.running = false; |
714 } | 712 } |
715 else | 713 else |
716 { | 714 { |
717 nextPoll = System.currentTimeMillis() + 1000L * this.nextPollInterva ls; | 715 nextPoll = System.currentTimeMillis() + 1000L * this.pollInterval; |
718 } | 716 } |
719 } | 717 } |
720 } | 718 } |
721 } | 719 } |
722 } | 720 } |
723 } | 721 } |
LEFT | RIGHT |