diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/NotificationHelper.java b/app/src/main/java/dummydomain/yetanothercallblocker/NotificationHelper.java index 623ae6c..b99c992 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/NotificationHelper.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/NotificationHelper.java @@ -25,6 +25,7 @@ public class NotificationHelper { private static final String NOTIFICATION_TAG_INCOMING_CALL = "incomingCallNotification"; private static final String NOTIFICATION_TAG_BLOCKED_CALL = "blockedCallNotification"; + private static final int NOTIFICATION_ID_INCOMING_CALL = 1; private static final int NOTIFICATION_ID_BLOCKED_CALL = 2; public static final int NOTIFICATION_ID_TASKS = 3; @@ -41,11 +42,19 @@ public class NotificationHelper { private static final String CHANNEL_ID_BLOCKED_INFO = "blocked_info"; public static final String CHANNEL_ID_TASKS = "tasks"; + public static void notify(Context context, int id, Notification notification) { + NotificationManagerCompat.from(context).notify(id, notification); + } + + public static void notify(Context context, String tag, int id, Notification notification) { + NotificationManagerCompat.from(context).notify(tag, id, notification); + } + public static void showIncomingCallNotification(Context context, NumberInfo numberInfo) { Notification notification = createIncomingCallNotification(context, numberInfo); String tag = numberInfo.number != null ? NOTIFICATION_TAG_INCOMING_CALL + numberInfo.number : null; - NotificationManagerCompat.from(context).notify(tag, NOTIFICATION_ID_INCOMING_CALL, notification); + notify(context, tag, NOTIFICATION_ID_INCOMING_CALL, notification); } public static void hideIncomingCallNotification(Context context, String number) { @@ -57,7 +66,20 @@ public class NotificationHelper { Notification notification = createBlockedCallNotification(context, numberInfo); String tag = numberInfo.number != null ? NOTIFICATION_TAG_BLOCKED_CALL + numberInfo.number : null; // TODO: handle repeating - NotificationManagerCompat.from(context).notify(tag, NOTIFICATION_ID_BLOCKED_CALL, notification); + notify(context, tag, NOTIFICATION_ID_BLOCKED_CALL, notification); + } + + public static Notification createServiceNotification(Context context, String title) { + if (title == null) title = context.getString(R.string.notification_background_operation); + + Intent intent = new Intent(context, MainActivity.class); + PendingIntent contentIntent = PendingIntent.getActivity( + context, 0, intent, 0); + + return new NotificationCompat.Builder(context, CHANNEL_ID_TASKS) + .setSmallIcon(R.drawable.ic_file_download_black_24dp) + .setContentIntent(contentIntent) + .setContentTitle(title).build(); } private static Notification createIncomingCallNotification(Context context, NumberInfo numberInfo) { diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/work/TaskService.java b/app/src/main/java/dummydomain/yetanothercallblocker/work/TaskService.java index 724f746..d6017bd 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/work/TaskService.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/work/TaskService.java @@ -8,8 +8,6 @@ import android.os.Process; import android.text.TextUtils; import androidx.annotation.Nullable; -import androidx.core.app.NotificationCompat; -import androidx.core.app.NotificationManagerCompat; import androidx.core.content.ContextCompat; import org.slf4j.Logger; @@ -78,23 +76,12 @@ public class TaskService extends IntentService { } private Notification createNotification(String title) { - if (title == null) title = getString(R.string.notification_background_operation); - - NotificationCompat.Builder builder = new NotificationCompat.Builder( - getApplicationContext(), NotificationHelper.CHANNEL_ID_TASKS) - .setSmallIcon(R.drawable.ic_file_download_black_24dp); - - builder.setContentTitle(title); - - return builder.build(); + return NotificationHelper.createServiceNotification(getApplicationContext(), title); } private void updateNotification(String title) { - NotificationManagerCompat notificationManager = NotificationManagerCompat - .from(getApplicationContext()); - - notificationManager.notify(NotificationHelper.NOTIFICATION_ID_TASKS, - createNotification(title)); + NotificationHelper.notify(getApplicationContext(), + NotificationHelper.NOTIFICATION_ID_TASKS, createNotification(title)); } private void downloadMainDb() {