Check notification permissions

This commit is contained in:
M66B 2023-09-21 14:53:41 +02:00
parent 36bdbc4dcf
commit e9d3a2d385
3 changed files with 35 additions and 15 deletions

View File

@ -175,7 +175,7 @@ public class DownloadTask extends AsyncTask<Object, Integer, Object> {
builder.setCategory(NotificationCompat.CATEGORY_STATUS) builder.setCategory(NotificationCompat.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_SECRET); .setVisibility(NotificationCompat.VISIBILITY_SECRET);
NotificationManagerCompat.from(context).notify(ServiceSinkhole.NOTIFY_DOWNLOAD, builder.build()); if (Util.canNotify(context))
NotificationManagerCompat.from(context).notify(ServiceSinkhole.NOTIFY_DOWNLOAD, builder.build());
} }
} }

View File

@ -1128,8 +1128,10 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
startForeground(NOTIFY_TRAFFIC, builder.build()); startForeground(NOTIFY_TRAFFIC, builder.build());
state = State.stats; state = State.stats;
Log.d(TAG, "Start foreground state=" + state.toString()); Log.d(TAG, "Start foreground state=" + state.toString());
} else } else {
NotificationManagerCompat.from(ServiceSinkhole.this).notify(NOTIFY_TRAFFIC, builder.build()); if (Util.canNotify(ServiceSinkhole.this))
NotificationManagerCompat.from(ServiceSinkhole.this).notify(NOTIFY_TRAFFIC, builder.build());
}
} }
} }
@ -2376,16 +2378,18 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
builder.addAction(oAction); builder.addAction(oAction);
// Show notification // Show notification
if (internet) if (internet) {
NotificationManagerCompat.from(this).notify(uid, builder.build()); if (Util.canNotify(this))
else { NotificationManagerCompat.from(this).notify(uid, builder.build());
} else {
NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder); NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
expanded.bigText(getString(R.string.msg_installed_n)); expanded.bigText(getString(R.string.msg_installed_n));
else else
expanded.bigText(getString(R.string.msg_installed, name)); expanded.bigText(getString(R.string.msg_installed, name));
expanded.setSummaryText(getString(R.string.title_internet)); expanded.setSummaryText(getString(R.string.title_internet));
NotificationManagerCompat.from(this).notify(uid, expanded.build()); if (Util.canNotify(this))
NotificationManagerCompat.from(this).notify(uid, expanded.build());
} }
} catch (PackageManager.NameNotFoundException ex) { } catch (PackageManager.NameNotFoundException ex) {
@ -2860,7 +2864,8 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
// Update notification // Update notification
Notification notification = getEnforcingNotification(allowed, total - allowed, mapHostsBlocked.size()); Notification notification = getEnforcingNotification(allowed, total - allowed, mapHostsBlocked.size());
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(NOTIFY_ENFORCING, notification); if (Util.canNotify(this))
nm.notify(NOTIFY_ENFORCING, notification);
} }
private Notification getWaitingNotification() { private Notification getWaitingNotification() {
@ -2912,7 +2917,8 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder); NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
notification.bigText(getString(R.string.msg_revoked)); notification.bigText(getString(R.string.msg_revoked));
NotificationManagerCompat.from(this).notify(NOTIFY_DISABLED, notification.build()); if (Util.canNotify(this))
NotificationManagerCompat.from(this).notify(NOTIFY_DISABLED, notification.build());
} }
private void showLockdownNotification() { private void showLockdownNotification() {
@ -2938,7 +2944,8 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder); NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
notification.bigText(getString(R.string.msg_always_on_lockdown)); notification.bigText(getString(R.string.msg_always_on_lockdown));
NotificationManagerCompat.from(this).notify(NOTIFY_LOCKDOWN, notification.build()); if (Util.canNotify(this))
NotificationManagerCompat.from(this).notify(NOTIFY_LOCKDOWN, notification.build());
} }
private void removeLockdownNotification() { private void removeLockdownNotification() {
@ -2968,7 +2975,8 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder); NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
notification.bigText(getString(R.string.msg_autostart)); notification.bigText(getString(R.string.msg_autostart));
NotificationManagerCompat.from(this).notify(NOTIFY_AUTOSTART, notification.build()); if (Util.canNotify(this))
NotificationManagerCompat.from(this).notify(NOTIFY_AUTOSTART, notification.build());
} }
private void showErrorNotification(String message) { private void showErrorNotification(String message) {
@ -2994,7 +3002,8 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
notification.bigText(getString(R.string.msg_error, message)); notification.bigText(getString(R.string.msg_error, message));
notification.setSummaryText(message); notification.setSummaryText(message);
NotificationManagerCompat.from(this).notify(NOTIFY_ERROR, notification.build()); if (Util.canNotify(this))
NotificationManagerCompat.from(this).notify(NOTIFY_ERROR, notification.build());
} }
private void showAccessNotification(int uid) { private void showAccessNotification(int uid) {
@ -3079,7 +3088,8 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
} }
} }
NotificationManagerCompat.from(this).notify(uid + 10000, notification.build()); if (Util.canNotify(this))
NotificationManagerCompat.from(this).notify(uid + 10000, notification.build());
} }
private void showUpdateNotification(String name, String url) { private void showUpdateNotification(String name, String url) {
@ -3101,7 +3111,8 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS
builder.setCategory(NotificationCompat.CATEGORY_STATUS) builder.setCategory(NotificationCompat.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_SECRET); .setVisibility(NotificationCompat.VISIBILITY_SECRET);
NotificationManagerCompat.from(this).notify(NOTIFY_UPDATE, builder.build()); if (Util.canNotify(this))
NotificationManagerCompat.from(this).notify(NOTIFY_UPDATE, builder.build());
} }
private void removeWarningNotifications() { private void removeWarningNotifications() {

View File

@ -53,6 +53,7 @@ import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.net.ConnectivityManagerCompat; import androidx.core.net.ConnectivityManagerCompat;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
@ -844,6 +845,14 @@ public class Util {
return (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED); 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) { private static StringBuilder getTrafficLog(Context context) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();