From 29f36b883a6ab1799aefbfff4b8458882a4d6db1 Mon Sep 17 00:00:00 2001 From: xynngh Date: Wed, 9 Sep 2020 16:18:28 +0400 Subject: [PATCH] Extract NotificationService --- .../NotificationService.java | 27 +++++++++++++++++++ .../PhoneStateHandler.java | 23 +++++----------- .../yetanothercallblocker/data/Config.java | 7 ++++- .../data/YacbHolder.java | 14 ++++++++++ 4 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/dummydomain/yetanothercallblocker/NotificationService.java diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/NotificationService.java b/app/src/main/java/dummydomain/yetanothercallblocker/NotificationService.java new file mode 100644 index 0000000..a35c0e6 --- /dev/null +++ b/app/src/main/java/dummydomain/yetanothercallblocker/NotificationService.java @@ -0,0 +1,27 @@ +package dummydomain.yetanothercallblocker; + +import android.content.Context; + +import dummydomain.yetanothercallblocker.data.NumberInfo; + +public class NotificationService { + + private final Context context; + + public NotificationService(Context context) { + this.context = context; + } + + public void startCallIndication(NumberInfo numberInfo) { + NotificationHelper.showIncomingCallNotification(context, numberInfo); + } + + public void stopAllCallsIndication() { + NotificationHelper.hideIncomingCallNotification(context); + } + + public void notifyCallBlocked(NumberInfo numberInfo) { + NotificationHelper.showBlockedCallNotification(context, numberInfo); + } + +} diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/PhoneStateHandler.java b/app/src/main/java/dummydomain/yetanothercallblocker/PhoneStateHandler.java index 43b581e..7b91db5 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/PhoneStateHandler.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/PhoneStateHandler.java @@ -19,12 +19,15 @@ public class PhoneStateHandler { private final Settings settings; private final NumberInfoService numberInfoService; + private final NotificationService notificationService; private boolean isOffHook; - public PhoneStateHandler(Settings settings, NumberInfoService numberInfoService) { + public PhoneStateHandler(Settings settings, NumberInfoService numberInfoService, + NotificationService notificationService) { this.settings = settings; this.numberInfoService = numberInfoService; + this.notificationService = notificationService; } public void onRinging(Context context, String phoneNumber) { @@ -53,7 +56,7 @@ public class PhoneStateHandler { blocked = PhoneUtils.rejectCall(context); if (blocked) { - notifyBlocked(context, numberInfo); + notificationService.notifyCallBlocked(numberInfo); numberInfoService.blockedCall(numberInfo); @@ -62,7 +65,7 @@ public class PhoneStateHandler { } if (!blocked && showNotifications) { - startIndication(context, numberInfo); + notificationService.startCallIndication(numberInfo); } } @@ -79,21 +82,9 @@ public class PhoneStateHandler { isOffHook = false; - stopAllIndication(context); + notificationService.stopAllCallsIndication(); postEvent(new CallEndedEvent()); } - private void startIndication(Context context, NumberInfo numberInfo) { - NotificationHelper.showIncomingCallNotification(context, numberInfo); - } - - private void stopAllIndication(Context context) { - NotificationHelper.hideIncomingCallNotification(context); - } - - private void notifyBlocked(Context context, NumberInfo numberInfo) { - NotificationHelper.showBlockedCallNotification(context, numberInfo); - } - } diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/data/Config.java b/app/src/main/java/dummydomain/yetanothercallblocker/data/Config.java index 95ae3ae..e7588ef 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/data/Config.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/data/Config.java @@ -5,6 +5,7 @@ import android.text.TextUtils; import java.util.concurrent.TimeUnit; +import dummydomain.yetanothercallblocker.NotificationService; import dummydomain.yetanothercallblocker.PermissionHelper; import dummydomain.yetanothercallblocker.PhoneStateHandler; import dummydomain.yetanothercallblocker.data.db.BlacklistDao; @@ -148,7 +149,11 @@ public class Config { communityDatabase, featuredDatabase, contactsProvider, blacklistService); YacbHolder.setNumberInfoService(numberInfoService); - YacbHolder.setPhoneStateHandler(new PhoneStateHandler(settings, numberInfoService)); + NotificationService notificationService = new NotificationService(context); + YacbHolder.setNotificationService(notificationService); + + YacbHolder.setPhoneStateHandler( + new PhoneStateHandler(settings, numberInfoService, notificationService)); } } diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/data/YacbHolder.java b/app/src/main/java/dummydomain/yetanothercallblocker/data/YacbHolder.java index 5ba413f..2ab8b09 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/data/YacbHolder.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/data/YacbHolder.java @@ -1,5 +1,8 @@ package dummydomain.yetanothercallblocker.data; +import android.annotation.SuppressLint; + +import dummydomain.yetanothercallblocker.NotificationService; import dummydomain.yetanothercallblocker.PhoneStateHandler; import dummydomain.yetanothercallblocker.data.db.BlacklistDao; import dummydomain.yetanothercallblocker.sia.model.CommunityReviewsLoader; @@ -23,6 +26,9 @@ public class YacbHolder { private static NumberInfoService numberInfoService; + @SuppressLint("StaticFieldLeak") + private static NotificationService notificationService; + private static PhoneStateHandler phoneStateHandler; static void setWebService(WebService webService) { @@ -61,6 +67,10 @@ public class YacbHolder { YacbHolder.numberInfoService = numberInfoService; } + static void setNotificationService(NotificationService notificationService) { + YacbHolder.notificationService = notificationService; + } + static void setPhoneStateHandler(PhoneStateHandler phoneStateHandler) { YacbHolder.phoneStateHandler = phoneStateHandler; } @@ -101,6 +111,10 @@ public class YacbHolder { return numberInfoService; } + public static NotificationService getNotificationService() { + return notificationService; + } + public static PhoneStateHandler getPhoneStateHandler() { return phoneStateHandler; }