Add support for numbers without international prefix
This commit is contained in:
parent
6578b8eb1a
commit
4c6ba5e3b3
|
@ -68,7 +68,8 @@ public class CallReceiver extends BroadcastReceiver {
|
|||
|
||||
if (blockingEnabled || showNotifications) {
|
||||
NumberInfoService numberInfoService = YacbHolder.getNumberInfoService();
|
||||
NumberInfo numberInfo = numberInfoService.getNumberInfo(incomingNumber, false);
|
||||
NumberInfo numberInfo = numberInfoService.getNumberInfo(incomingNumber,
|
||||
settings.getCachedAutoDetectedCountryCode(), false);
|
||||
|
||||
boolean blocked = false;
|
||||
if (blockingEnabled && !isOnCall && numberInfoService.shouldBlock(numberInfo)) {
|
||||
|
|
|
@ -98,7 +98,8 @@ public class CallScreeningServiceImpl extends CallScreeningService {
|
|||
}
|
||||
|
||||
if (!ignore) {
|
||||
numberInfo = numberInfoService.getNumberInfo(number, false);
|
||||
numberInfo = numberInfoService.getNumberInfo(number,
|
||||
App.getSettings().getCachedAutoDetectedCountryCode(), false);
|
||||
|
||||
shouldBlock = numberInfoService.shouldBlock(numberInfo);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,8 @@ public class InfoDialogActivity extends AppCompatActivity {
|
|||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
NumberInfo numberInfo = YacbHolder.getNumberInfo(getIntent().getStringExtra(PARAM_NUMBER));
|
||||
NumberInfo numberInfo = YacbHolder.getNumberInfo(getIntent().getStringExtra(PARAM_NUMBER),
|
||||
App.getSettings().getCachedAutoDetectedCountryCode());
|
||||
|
||||
InfoDialogHelper.showDialog(this, numberInfo, (d) -> finish());
|
||||
}
|
||||
|
|
|
@ -225,7 +225,8 @@ public class MainActivity extends AppCompatActivity {
|
|||
MainActivity.this, settings.getNumberOfRecentCalls());
|
||||
|
||||
for (CallLogItem item : items) {
|
||||
item.numberInfo = YacbHolder.getNumberInfo(item.number);
|
||||
item.numberInfo = YacbHolder.getNumberInfo(
|
||||
item.number, settings.getCachedAutoDetectedCountryCode());
|
||||
}
|
||||
|
||||
return items;
|
||||
|
|
|
@ -143,7 +143,7 @@ public class Config {
|
|||
};
|
||||
|
||||
NumberInfoService numberInfoService = new NumberInfoService(
|
||||
settings, NumberUtils::isHiddenNumber,
|
||||
settings, NumberUtils::isHiddenNumber, NumberUtils::normalizeNumber,
|
||||
communityDatabase, featuredDatabase, contactsProvider, blacklistService);
|
||||
YacbHolder.setNumberInfoService(numberInfoService);
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ public class NumberInfo {
|
|||
|
||||
// id
|
||||
public String number;
|
||||
public String normalizedNumber;
|
||||
|
||||
// info from various sources
|
||||
public boolean isHiddenNumber;
|
||||
|
|
|
@ -19,30 +19,36 @@ public class NumberInfoService {
|
|||
boolean isHiddenNumber(String number);
|
||||
}
|
||||
|
||||
public interface NumberNormalizer {
|
||||
String normalizeNumber(String number, String countryCode);
|
||||
}
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(NumberInfoService.class);
|
||||
|
||||
protected final Settings settings;
|
||||
|
||||
protected final HiddenNumberDetector hiddenNumberDetector;
|
||||
protected final NumberNormalizer numberNormalizer;
|
||||
protected final CommunityDatabase communityDatabase;
|
||||
protected final FeaturedDatabase featuredDatabase;
|
||||
protected final ContactsProvider contactsProvider;
|
||||
protected final BlacklistService blacklistService;
|
||||
|
||||
public NumberInfoService(Settings settings, HiddenNumberDetector hiddenNumberDetector,
|
||||
CommunityDatabase communityDatabase, FeaturedDatabase featuredDatabase,
|
||||
ContactsProvider contactsProvider, BlacklistService blacklistService) {
|
||||
NumberNormalizer numberNormalizer, CommunityDatabase communityDatabase,
|
||||
FeaturedDatabase featuredDatabase, ContactsProvider contactsProvider,
|
||||
BlacklistService blacklistService) {
|
||||
this.settings = settings;
|
||||
this.hiddenNumberDetector = hiddenNumberDetector;
|
||||
this.numberNormalizer = numberNormalizer;
|
||||
this.communityDatabase = communityDatabase;
|
||||
this.featuredDatabase = featuredDatabase;
|
||||
this.contactsProvider = contactsProvider;
|
||||
this.blacklistService = blacklistService;
|
||||
}
|
||||
|
||||
public NumberInfo getNumberInfo(String number, boolean full) {
|
||||
LOG.debug("getNumberInfo({}, {}) started", number, full);
|
||||
// TODO: check number format
|
||||
public NumberInfo getNumberInfo(String number, String countryCode, boolean full) {
|
||||
LOG.debug("getNumberInfo({}, {}, {}) started", number, countryCode, full);
|
||||
|
||||
NumberInfo numberInfo = new NumberInfo();
|
||||
numberInfo.number = number;
|
||||
|
@ -70,13 +76,17 @@ public class NumberInfoService {
|
|||
}
|
||||
LOG.trace("getNumberInfo() contactItem={}", numberInfo.contactItem);
|
||||
|
||||
String normalizedNumber = numberInfo.normalizedNumber
|
||||
= numberNormalizer.normalizeNumber(number, countryCode);
|
||||
LOG.trace("getNumberInfo() normalizedNumber={}", numberInfo.normalizedNumber);
|
||||
|
||||
if (communityDatabase != null) {
|
||||
numberInfo.communityDatabaseItem = communityDatabase.getDbItemByNumber(number);
|
||||
numberInfo.communityDatabaseItem = communityDatabase.getDbItemByNumber(normalizedNumber);
|
||||
}
|
||||
LOG.trace("getNumberInfo() communityItem={}", numberInfo.communityDatabaseItem);
|
||||
|
||||
if (featuredDatabase != null) {
|
||||
numberInfo.featuredDatabaseItem = featuredDatabase.getDbItemByNumber(number);
|
||||
numberInfo.featuredDatabaseItem = featuredDatabase.getDbItemByNumber(normalizedNumber);
|
||||
}
|
||||
LOG.trace("getNumberInfo() featuredItem={}", numberInfo.featuredDatabaseItem);
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package dummydomain.yetanothercallblocker.data;
|
||||
|
||||
import android.os.Build;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
@ -18,4 +20,18 @@ public class NumberUtils {
|
|||
return HIDDEN_NUMBERS.contains(number.toUpperCase(Locale.ENGLISH));
|
||||
}
|
||||
|
||||
public static String normalizeNumber(String number, String countryCode) {
|
||||
String normalizedNumber = null;
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // TODO: Android 4.* support
|
||||
normalizedNumber = PhoneNumberUtils.formatNumberToE164(number, countryCode);
|
||||
}
|
||||
|
||||
if (normalizedNumber == null) {
|
||||
normalizedNumber = PhoneNumberUtils.stripSeparators(number);
|
||||
}
|
||||
|
||||
return normalizedNumber;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -94,8 +94,8 @@ public class YacbHolder {
|
|||
return numberInfoService;
|
||||
}
|
||||
|
||||
public static NumberInfo getNumberInfo(String number) {
|
||||
return numberInfoService.getNumberInfo(number, true);
|
||||
public static NumberInfo getNumberInfo(String number, String countryCode) {
|
||||
return numberInfoService.getNumberInfo(number, countryCode, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue