diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java b/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java index e58638e..a0ffd5b 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/CallReceiver.java @@ -6,12 +6,12 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; -import androidx.annotation.NonNull; - import android.os.Build; import android.telecom.TelecomManager; import android.telephony.TelephonyManager; +import androidx.annotation.NonNull; + import com.android.internal.telephony.ITelephony; import org.slf4j.Logger; @@ -21,8 +21,6 @@ import java.lang.reflect.Method; import dummydomain.yetanothercallblocker.sia.DatabaseSingleton; import dummydomain.yetanothercallblocker.sia.model.NumberInfo; -import dummydomain.yetanothercallblocker.sia.model.database.CommunityDatabaseItem; -import dummydomain.yetanothercallblocker.sia.model.database.FeaturedDatabaseItem; public class CallReceiver extends BroadcastReceiver { @@ -58,7 +56,7 @@ public class CallReceiver extends BroadcastReceiver { } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(telephonyExtraState)) { if (incomingNumber == null) return; - NumberInfo numberInfo = getNumberInfo(incomingNumber); + NumberInfo numberInfo = DatabaseSingleton.getNumberInfo(incomingNumber); boolean blocked = false; if (!isOnCall && numberInfo.rating == NumberInfo.Rating.NEGATIVE @@ -77,41 +75,6 @@ public class CallReceiver extends BroadcastReceiver { } } - protected NumberInfo getNumberInfo(String number) { - LOG.debug("getNumberInfo({}) started", number); - // TODO: check number format - - CommunityDatabaseItem communityItem = DatabaseSingleton.getCommunityDatabase() - .getDbItemByNumber(number); - LOG.trace("getNumberInfo() communityItem={}", communityItem); - - FeaturedDatabaseItem featuredItem = DatabaseSingleton.getFeaturedDatabase() - .getDbItemByNumber(number); - LOG.trace("getNumberInfo() featuredItem={}", featuredItem); - - NumberInfo numberInfo = new NumberInfo(); - numberInfo.number = number; - if (featuredItem != null) numberInfo.name = featuredItem.getName(); - - if (communityItem != null && communityItem.hasRatings()) { - if (communityItem.getNegativeRatingsCount() > communityItem.getPositiveRatingsCount() - + communityItem.getNeutralRatingsCount()) { - numberInfo.rating = NumberInfo.Rating.NEGATIVE; - } else if (communityItem.getPositiveRatingsCount() > communityItem.getNeutralRatingsCount() - + communityItem.getNegativeRatingsCount()) { - numberInfo.rating = NumberInfo.Rating.POSITIVE; - } else if (communityItem.getNeutralRatingsCount() > communityItem.getPositiveRatingsCount() - + communityItem.getNegativeRatingsCount()) { - numberInfo.rating = NumberInfo.Rating.NEUTRAL; - } - } - numberInfo.communityDatabaseItem = communityItem; - - LOG.trace("getNumberInfo() rating={}", numberInfo.rating); - - return numberInfo; - } - @SuppressLint("MissingPermission") private boolean rejectCall(@NonNull Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/sia/DatabaseSingleton.java b/app/src/main/java/dummydomain/yetanothercallblocker/sia/DatabaseSingleton.java index b9f8e5e..a3b1b7c 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/sia/DatabaseSingleton.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/sia/DatabaseSingleton.java @@ -1,7 +1,13 @@ package dummydomain.yetanothercallblocker.sia; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import dummydomain.yetanothercallblocker.sia.model.NumberInfo; import dummydomain.yetanothercallblocker.sia.model.database.CommunityDatabase; +import dummydomain.yetanothercallblocker.sia.model.database.CommunityDatabaseItem; import dummydomain.yetanothercallblocker.sia.model.database.FeaturedDatabase; +import dummydomain.yetanothercallblocker.sia.model.database.FeaturedDatabaseItem; import static dummydomain.yetanothercallblocker.sia.SiaConstants.*; @@ -10,6 +16,8 @@ public class DatabaseSingleton { private static final CommunityDatabase COMMUNITY_DATABASE = new CommunityDatabase(SIA_PATH_PREFIX, SIA_SECONDARY_PATH_PREFIX); private static final FeaturedDatabase FEATURED_DATABASE = new FeaturedDatabase(SIA_PATH_PREFIX); + private static final Logger LOG = LoggerFactory.getLogger(DatabaseSingleton.class); + public static CommunityDatabase getCommunityDatabase() { return COMMUNITY_DATABASE; } @@ -18,4 +26,39 @@ public class DatabaseSingleton { return FEATURED_DATABASE; } + public static NumberInfo getNumberInfo(String number) { + LOG.debug("getNumberInfo({}) started", number); + // TODO: check number format + + CommunityDatabaseItem communityItem = DatabaseSingleton.getCommunityDatabase() + .getDbItemByNumber(number); + LOG.trace("getNumberInfo() communityItem={}", communityItem); + + FeaturedDatabaseItem featuredItem = DatabaseSingleton.getFeaturedDatabase() + .getDbItemByNumber(number); + LOG.trace("getNumberInfo() featuredItem={}", featuredItem); + + NumberInfo numberInfo = new NumberInfo(); + numberInfo.number = number; + if (featuredItem != null) numberInfo.name = featuredItem.getName(); + + if (communityItem != null && communityItem.hasRatings()) { + if (communityItem.getNegativeRatingsCount() > communityItem.getPositiveRatingsCount() + + communityItem.getNeutralRatingsCount()) { + numberInfo.rating = NumberInfo.Rating.NEGATIVE; + } else if (communityItem.getPositiveRatingsCount() > communityItem.getNeutralRatingsCount() + + communityItem.getNegativeRatingsCount()) { + numberInfo.rating = NumberInfo.Rating.POSITIVE; + } else if (communityItem.getNeutralRatingsCount() > communityItem.getPositiveRatingsCount() + + communityItem.getNegativeRatingsCount()) { + numberInfo.rating = NumberInfo.Rating.NEUTRAL; + } + } + numberInfo.communityDatabaseItem = communityItem; + + LOG.trace("getNumberInfo() rating={}", numberInfo.rating); + + return numberInfo; + } + }