mirror of
https://github.com/M66B/NetGuard.git
synced 2025-02-24 07:10:50 +00:00
Check notification permissions
This commit is contained in:
parent
36bdbc4dcf
commit
e9d3a2d385
3 changed files with 35 additions and 15 deletions
|
@ -175,7 +175,7 @@ public class DownloadTask extends AsyncTask<Object, Integer, Object> {
|
|||
builder.setCategory(NotificationCompat.CATEGORY_STATUS)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
||||
|
||||
if (Util.canNotify(context))
|
||||
NotificationManagerCompat.from(context).notify(ServiceSinkhole.NOTIFY_DOWNLOAD, builder.build());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1128,10 +1128,12 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
startForeground(NOTIFY_TRAFFIC, builder.build());
|
||||
state = State.stats;
|
||||
Log.d(TAG, "Start foreground state=" + state.toString());
|
||||
} else
|
||||
} else {
|
||||
if (Util.canNotify(ServiceSinkhole.this))
|
||||
NotificationManagerCompat.from(ServiceSinkhole.this).notify(NOTIFY_TRAFFIC, builder.build());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<InetAddress> getDns(Context context) {
|
||||
List<InetAddress> listDns = new ArrayList<>();
|
||||
|
@ -2376,15 +2378,17 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
builder.addAction(oAction);
|
||||
|
||||
// Show notification
|
||||
if (internet)
|
||||
if (internet) {
|
||||
if (Util.canNotify(this))
|
||||
NotificationManagerCompat.from(this).notify(uid, builder.build());
|
||||
else {
|
||||
} else {
|
||||
NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
||||
expanded.bigText(getString(R.string.msg_installed_n));
|
||||
else
|
||||
expanded.bigText(getString(R.string.msg_installed, name));
|
||||
expanded.setSummaryText(getString(R.string.title_internet));
|
||||
if (Util.canNotify(this))
|
||||
NotificationManagerCompat.from(this).notify(uid, expanded.build());
|
||||
}
|
||||
|
||||
|
@ -2860,6 +2864,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
// Update notification
|
||||
Notification notification = getEnforcingNotification(allowed, total - allowed, mapHostsBlocked.size());
|
||||
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
if (Util.canNotify(this))
|
||||
nm.notify(NOTIFY_ENFORCING, notification);
|
||||
}
|
||||
|
||||
|
@ -2912,6 +2917,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
|
||||
notification.bigText(getString(R.string.msg_revoked));
|
||||
|
||||
if (Util.canNotify(this))
|
||||
NotificationManagerCompat.from(this).notify(NOTIFY_DISABLED, notification.build());
|
||||
}
|
||||
|
||||
|
@ -2938,6 +2944,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
|
||||
notification.bigText(getString(R.string.msg_always_on_lockdown));
|
||||
|
||||
if (Util.canNotify(this))
|
||||
NotificationManagerCompat.from(this).notify(NOTIFY_LOCKDOWN, notification.build());
|
||||
}
|
||||
|
||||
|
@ -2968,6 +2975,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
|
||||
notification.bigText(getString(R.string.msg_autostart));
|
||||
|
||||
if (Util.canNotify(this))
|
||||
NotificationManagerCompat.from(this).notify(NOTIFY_AUTOSTART, notification.build());
|
||||
}
|
||||
|
||||
|
@ -2994,6 +3002,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
notification.bigText(getString(R.string.msg_error, message));
|
||||
notification.setSummaryText(message);
|
||||
|
||||
if (Util.canNotify(this))
|
||||
NotificationManagerCompat.from(this).notify(NOTIFY_ERROR, notification.build());
|
||||
}
|
||||
|
||||
|
@ -3079,6 +3088,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
}
|
||||
}
|
||||
|
||||
if (Util.canNotify(this))
|
||||
NotificationManagerCompat.from(this).notify(uid + 10000, notification.build());
|
||||
}
|
||||
|
||||
|
@ -3101,6 +3111,7 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
|
|||
builder.setCategory(NotificationCompat.CATEGORY_STATUS)
|
||||
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
||||
|
||||
if (Util.canNotify(this))
|
||||
NotificationManagerCompat.from(this).notify(NOTIFY_UPDATE, builder.build());
|
||||
}
|
||||
|
||||
|
|
|
@ -53,6 +53,7 @@ import android.view.View;
|
|||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.net.ConnectivityManagerCompat;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
|
@ -844,6 +845,14 @@ public class Util {
|
|||
return (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
|
||||
}
|
||||
|
||||
public static boolean canNotify(Context context) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
|
||||
return true;
|
||||
else
|
||||
return (ActivityCompat.checkSelfPermission(context,
|
||||
Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED);
|
||||
}
|
||||
|
||||
private static StringBuilder getTrafficLog(Context context) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
|
Loading…
Reference in a new issue