Extract NotificationService

This commit is contained in:
xynngh
2020-09-09 16:18:28 +04:00
parent b06a3d47fb
commit 29f36b883a
4 changed files with 54 additions and 17 deletions

View File

@@ -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);
}
}

View File

@@ -19,12 +19,15 @@ public class PhoneStateHandler {
private final Settings settings; private final Settings settings;
private final NumberInfoService numberInfoService; private final NumberInfoService numberInfoService;
private final NotificationService notificationService;
private boolean isOffHook; private boolean isOffHook;
public PhoneStateHandler(Settings settings, NumberInfoService numberInfoService) { public PhoneStateHandler(Settings settings, NumberInfoService numberInfoService,
NotificationService notificationService) {
this.settings = settings; this.settings = settings;
this.numberInfoService = numberInfoService; this.numberInfoService = numberInfoService;
this.notificationService = notificationService;
} }
public void onRinging(Context context, String phoneNumber) { public void onRinging(Context context, String phoneNumber) {
@@ -53,7 +56,7 @@ public class PhoneStateHandler {
blocked = PhoneUtils.rejectCall(context); blocked = PhoneUtils.rejectCall(context);
if (blocked) { if (blocked) {
notifyBlocked(context, numberInfo); notificationService.notifyCallBlocked(numberInfo);
numberInfoService.blockedCall(numberInfo); numberInfoService.blockedCall(numberInfo);
@@ -62,7 +65,7 @@ public class PhoneStateHandler {
} }
if (!blocked && showNotifications) { if (!blocked && showNotifications) {
startIndication(context, numberInfo); notificationService.startCallIndication(numberInfo);
} }
} }
@@ -79,21 +82,9 @@ public class PhoneStateHandler {
isOffHook = false; isOffHook = false;
stopAllIndication(context); notificationService.stopAllCallsIndication();
postEvent(new CallEndedEvent()); 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);
}
} }

View File

@@ -5,6 +5,7 @@ import android.text.TextUtils;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import dummydomain.yetanothercallblocker.NotificationService;
import dummydomain.yetanothercallblocker.PermissionHelper; import dummydomain.yetanothercallblocker.PermissionHelper;
import dummydomain.yetanothercallblocker.PhoneStateHandler; import dummydomain.yetanothercallblocker.PhoneStateHandler;
import dummydomain.yetanothercallblocker.data.db.BlacklistDao; import dummydomain.yetanothercallblocker.data.db.BlacklistDao;
@@ -148,7 +149,11 @@ public class Config {
communityDatabase, featuredDatabase, contactsProvider, blacklistService); communityDatabase, featuredDatabase, contactsProvider, blacklistService);
YacbHolder.setNumberInfoService(numberInfoService); YacbHolder.setNumberInfoService(numberInfoService);
YacbHolder.setPhoneStateHandler(new PhoneStateHandler(settings, numberInfoService)); NotificationService notificationService = new NotificationService(context);
YacbHolder.setNotificationService(notificationService);
YacbHolder.setPhoneStateHandler(
new PhoneStateHandler(settings, numberInfoService, notificationService));
} }
} }

View File

@@ -1,5 +1,8 @@
package dummydomain.yetanothercallblocker.data; package dummydomain.yetanothercallblocker.data;
import android.annotation.SuppressLint;
import dummydomain.yetanothercallblocker.NotificationService;
import dummydomain.yetanothercallblocker.PhoneStateHandler; import dummydomain.yetanothercallblocker.PhoneStateHandler;
import dummydomain.yetanothercallblocker.data.db.BlacklistDao; import dummydomain.yetanothercallblocker.data.db.BlacklistDao;
import dummydomain.yetanothercallblocker.sia.model.CommunityReviewsLoader; import dummydomain.yetanothercallblocker.sia.model.CommunityReviewsLoader;
@@ -23,6 +26,9 @@ public class YacbHolder {
private static NumberInfoService numberInfoService; private static NumberInfoService numberInfoService;
@SuppressLint("StaticFieldLeak")
private static NotificationService notificationService;
private static PhoneStateHandler phoneStateHandler; private static PhoneStateHandler phoneStateHandler;
static void setWebService(WebService webService) { static void setWebService(WebService webService) {
@@ -61,6 +67,10 @@ public class YacbHolder {
YacbHolder.numberInfoService = numberInfoService; YacbHolder.numberInfoService = numberInfoService;
} }
static void setNotificationService(NotificationService notificationService) {
YacbHolder.notificationService = notificationService;
}
static void setPhoneStateHandler(PhoneStateHandler phoneStateHandler) { static void setPhoneStateHandler(PhoneStateHandler phoneStateHandler) {
YacbHolder.phoneStateHandler = phoneStateHandler; YacbHolder.phoneStateHandler = phoneStateHandler;
} }
@@ -101,6 +111,10 @@ public class YacbHolder {
return numberInfoService; return numberInfoService;
} }
public static NotificationService getNotificationService() {
return notificationService;
}
public static PhoneStateHandler getPhoneStateHandler() { public static PhoneStateHandler getPhoneStateHandler() {
return phoneStateHandler; return phoneStateHandler;
} }