Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show blocked ads notification #540

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/java/eu/faircode/netguard/ApplicationEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,10 @@ private void createNotificationChannels() {
access.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
access.setBypassDnd(true);
nm.createNotificationChannel(access);

NotificationChannel block = new NotificationChannel("block", getString(R.string.channel_block), NotificationManager.IMPORTANCE_DEFAULT);
block.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
block.setBypassDnd(true);
nm.createNotificationChannel(block);
}
}
72 changes: 71 additions & 1 deletion app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
import java.net.URL;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -143,6 +145,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
private boolean temporarilyStopped = false;

private long last_hosts_modified = 0;
private HashMap<String, Long> mapAdsBlocked = new HashMap<>();
private Map<String, Boolean> mapHostsBlocked = new HashMap<>();
private Map<Integer, Boolean> mapUidAllowed = new HashMap<>();
private Map<Integer, Integer> mapUidKnown = new HashMap<>();
Expand Down Expand Up @@ -191,7 +194,7 @@ public enum Command {run, start, reload, stop, stats, set, householding, watchdo
private static volatile PowerManager.WakeLock wlInstance = null;

private ExecutorService executor = Executors.newCachedThreadPool();

private String previouslyBlockedDomain = "";
private static final String ACTION_HOUSE_HOLDING = "eu.faircode.netguard.HOUSE_HOLDING";
private static final String ACTION_SCREEN_OFF_DELAYED = "eu.faircode.netguard.SCREEN_OFF_DELAYED";
private static final String ACTION_WATCHDOG = "eu.faircode.netguard.WATCHDOG";
Expand Down Expand Up @@ -1883,10 +1886,77 @@ private void dnsResolved(ResourceRecord rr) {
private boolean isDomainBlocked(String name) {
lock.readLock().lock();
boolean blocked = (mapHostsBlocked.containsKey(name) && mapHostsBlocked.get(name));
if (blocked) {
calculateBlockedAds(name);
}
lock.readLock().unlock();
return blocked;
}

private void calculateBlockedAds(String blockedDomain) {
long noOfTimes = 1;
if (mapAdsBlocked.containsKey(blockedDomain)) {
noOfTimes = mapAdsBlocked.get(blockedDomain);
noOfTimes++;
}
mapAdsBlocked.put(blockedDomain, noOfTimes);
if (previouslyBlockedDomain.equals(blockedDomain)) {
return;
}
previouslyBlockedDomain = blockedDomain;
showBlockedAdsNotification(blockedDomain + " : " + format(noOfTimes) + " times.");
}


private String format(double value) {
int power;
String suffix = " kmbt";
String formattedNumber = "";
NumberFormat formatter = new DecimalFormat("#,###.#");
power = (int) StrictMath.log10(value);
value = value / (Math.pow(10, (power / 3) * 3));
formattedNumber = formatter.format(value);
formattedNumber = formattedNumber + suffix.charAt(power / 3);
return formattedNumber.length() > 4 ? formattedNumber.replaceAll("\\.[0-9]+", "") : formattedNumber;
}

private void showBlockedAdsNotification(String blockedDomain) {
String title = "Blocked Ads";
Intent main = new Intent(ServiceSinkhole.this, ActivityMain.class);
PendingIntent pi = PendingIntent.getActivity(ServiceSinkhole.this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);

TypedValue tv = new TypedValue();
getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "block");

builder.setSmallIcon(R.drawable.ic_security_white_24dp)
.setGroup("BlockingAttampt")
.setContentIntent(pi)
.setColor(tv.data)
.setOngoing(true)
.setAutoCancel(true);

builder.setContentTitle(title)
.setContentText(blockedDomain);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
builder.setCategory(NotificationCompat.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_SECRET);

NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
notification.addLine(blockedDomain);
else {
String sname = getString(R.string.msg_access, blockedDomain);
int pos = sname.indexOf(blockedDomain);
Spannable sp = new SpannableString(sname);
sp.setSpan(new StyleSpan(Typeface.BOLD), pos, pos + blockedDomain.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
notification.addLine(sp);
}
NotificationManagerCompat.from(this).notify(0, notification.build());
}


// Called from native code
@TargetApi(Build.VERSION_CODES.Q)
private int getUidQ(int version, int protocol, String saddr, int sport, String daddr, int dport) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<string name="channel_foreground">Running services</string>
<string name="channel_notify">General notifications</string>
<string name="channel_access">Access notifications</string>
<string name="channel_block">Blocked Ads notifications</string>

<string name="menu_search">Search for app</string>
<string name="menu_filter">Filter apps</string>
Expand Down