From d6595f7f3ba53da24a6a4151b17dbb66b2bf62a4 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Wed, 1 Jul 2020 08:33:53 +0200 Subject: [PATCH] Improve notifications * Add color * Don't use DNS 66 as notification title. The app name is already shown in notifications as default. When I minimize the running notification, it said "DNS 66 * DNS 66" before. Now it says "DNS 66 * Active" * Chain calls to the same builder object --- .../jak_linux/dns66/NotificationChannels.java | 3 ++- .../dns66/db/RuleDatabaseUpdateTask.java | 19 ++++++++++--------- .../org/jak_linux/dns66/vpn/AdVpnService.java | 7 +++++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/jak_linux/dns66/NotificationChannels.java b/app/src/main/java/org/jak_linux/dns66/NotificationChannels.java index 4ca94573..d739df4b 100644 --- a/app/src/main/java/org/jak_linux/dns66/NotificationChannels.java +++ b/app/src/main/java/org/jak_linux/dns66/NotificationChannels.java @@ -25,8 +25,9 @@ public class NotificationChannels { public static void onCreate(Context context) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || notificationManager == null) { return; + } notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(GROUP_SERVICE, context.getString(R.string.notifications_group_service))); notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(GROUP_UPDATE, context.getString(R.string.notifications_group_updates))); diff --git a/app/src/main/java/org/jak_linux/dns66/db/RuleDatabaseUpdateTask.java b/app/src/main/java/org/jak_linux/dns66/db/RuleDatabaseUpdateTask.java index 6eaa22ca..8353d87f 100644 --- a/app/src/main/java/org/jak_linux/dns66/db/RuleDatabaseUpdateTask.java +++ b/app/src/main/java/org/jak_linux/dns66/db/RuleDatabaseUpdateTask.java @@ -17,6 +17,8 @@ import android.os.AsyncTask; import androidx.annotation.NonNull; import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; + import android.util.Log; import org.jak_linux.dns66.Configuration; @@ -63,12 +65,11 @@ public RuleDatabaseUpdateTask(Context context, Configuration configuration, bool private void setupNotificationBuilder() { notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - notificationBuilder = new NotificationCompat.Builder(context, NotificationChannels.UPDATE_STATUS); - notificationBuilder.setContentTitle(context.getString(R.string.updating_hostfiles)) - .setContentText(context.getString(R.string.updating_hostfiles)) - .setSmallIcon(R.drawable.ic_refresh); - - notificationBuilder.setProgress(configuration.hosts.items.size(), 0, false); + notificationBuilder = new NotificationCompat.Builder(context, NotificationChannels.UPDATE_STATUS) + .setContentTitle(context.getString(R.string.updating_hostfiles)) + .setSmallIcon(R.drawable.ic_refresh) + .setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark)) + .setProgress(configuration.hosts.items.size(), 0, false); } @Override @@ -151,9 +152,9 @@ private synchronized void updateProgressNotification() { } if (notificationBuilder != null) { - notificationBuilder.setProgress(pending.size() + done.size(), done.size(), false); - notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(builder.toString())); - notificationBuilder.setContentText(context.getString(R.string.updating_n_host_files, pending.size())); + notificationBuilder.setProgress(pending.size() + done.size(), done.size(), false) + .setStyle(new NotificationCompat.BigTextStyle().bigText(builder.toString())) + .setContentText(context.getString(R.string.updating_n_host_files, pending.size())); notificationManager.notify(UPDATE_NOTIFICATION_ID, notificationBuilder.build()); } } diff --git a/app/src/main/java/org/jak_linux/dns66/vpn/AdVpnService.java b/app/src/main/java/org/jak_linux/dns66/vpn/AdVpnService.java index 2efa0435..4d3f0c6b 100644 --- a/app/src/main/java/org/jak_linux/dns66/vpn/AdVpnService.java +++ b/app/src/main/java/org/jak_linux/dns66/vpn/AdVpnService.java @@ -28,6 +28,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; +import androidx.core.content.ContextCompat; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import android.util.Log; @@ -119,7 +120,8 @@ public void onCreate() { notificationBuilder.addAction(R.drawable.ic_pause_black_24dp, getString(R.string.notification_action_pause), PendingIntent.getService(this, REQUEST_CODE_PAUSE, new Intent(this, AdVpnService.class) - .putExtra("COMMAND", Command.PAUSE.ordinal()), 0)); + .putExtra("COMMAND", Command.PAUSE.ordinal()), 0)) + .setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)); } public static void checkStartVpnOnBoot(Context context) { @@ -197,6 +199,7 @@ private void pauseVpn() { notificationManager.notify(NOTIFICATION_ID_STATE, new NotificationCompat.Builder(this, NotificationChannels.SERVICE_PAUSED) .setSmallIcon(R.drawable.ic_state_deny) // TODO: Notification icon .setPriority(Notification.PRIORITY_LOW) + .setColor(ContextCompat.getColor(this, R.color.colorPrimaryDark)) .setContentTitle(getString(R.string.notification_paused_title)) .setContentText(getString(R.string.notification_paused_text)) .setContentIntent(PendingIntent.getService(this, REQUEST_CODE_START, getResumeIntent(this), PendingIntent.FLAG_ONE_SHOT)) @@ -206,7 +209,7 @@ private void pauseVpn() { private void updateVpnStatus(int status) { vpnStatus = status; int notificationTextId = vpnStatusToTextId(status); - notificationBuilder.setContentText(getString(notificationTextId)); + notificationBuilder.setContentTitle(getString(notificationTextId)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O || FileHelper.loadCurrentSettings(getApplicationContext()).showNotification) startForeground(NOTIFICATION_ID_STATE, notificationBuilder.build());