mirror of https://github.com/M66B/NetGuard.git
Add Notification, which shows which ads are blocking and how many times those ads are blocking.
This commit is contained in:
parent
941c9bc044
commit
a4619175c1
|
@ -72,5 +72,10 @@ public class ApplicationEx extends Application {
|
|||
NotificationChannel access = new NotificationChannel("access", getString(R.string.channel_access), NotificationManager.IMPORTANCE_DEFAULT);
|
||||
access.setSound(null, Notification.AUDIO_ATTRIBUTES_DEFAULT);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,6 +93,8 @@ import java.net.Socket;
|
|||
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;
|
||||
|
@ -142,6 +144,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<>();
|
||||
|
@ -189,7 +192,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
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";
|
||||
|
@ -1852,10 +1855,77 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
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_ad_block_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) {
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 1.0 KiB |
Binary file not shown.
After Width: | Height: | Size: 703 B |
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
Binary file not shown.
After Width: | Height: | Size: 3.3 KiB |
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue