Refactor ContactsHelper

This commit is contained in:
xynngh 2020-10-18 13:09:54 +04:00
parent 55b0f4ef01
commit 69dbdbc2fd
3 changed files with 23 additions and 15 deletions

View File

@ -1,12 +1,10 @@
package dummydomain.yetanothercallblocker.data;
import android.content.Context;
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;
import dummydomain.yetanothercallblocker.data.db.YacbDaoSessionFactory;
@ -136,13 +134,8 @@ public class Config {
settings::setBlacklistIsNotEmpty, blacklistDao);
YacbHolder.setBlacklistService(blacklistService);
ContactsProvider contactsProvider = number -> {
if (settings.getUseContacts() && PermissionHelper.hasContactsPermission(context)) {
String contactName = ContactsHelper.getContactName(context, number);
if (!TextUtils.isEmpty(contactName)) return new ContactItem(contactName);
}
return null;
};
ContactsProvider contactsProvider = number ->
settings.getUseContacts() ? ContactsHelper.getContact(context, number) : null;
NumberInfoService numberInfoService = new NumberInfoService(
settings, NumberUtils::isHiddenNumber, NumberUtils::normalizeNumber,

View File

@ -2,16 +2,19 @@ package dummydomain.yetanothercallblocker.data;
public class ContactItem {
public long id;
public String displayName;
public ContactItem(String displayName) {
public ContactItem(long id, String displayName) {
this.id = id;
this.displayName = displayName;
}
@Override
public String toString() {
return "ContactItem{" +
"displayName='" + displayName + '\'' +
"id=" + id +
", displayName='" + displayName + '\'' +
'}';
}
}

View File

@ -7,14 +7,18 @@ import android.net.Uri;
import android.provider.ContactsContract;
import android.text.TextUtils;
import dummydomain.yetanothercallblocker.PermissionHelper;
public class ContactsHelper {
private static final String[] PROJECTION = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
public static String getContactName(Context context, String number) {
public static ContactItem getContact(Context context, String number) {
if (TextUtils.isEmpty(number)) return null;
if (!PermissionHelper.hasContactsPermission(context)) return null;
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
@ -22,8 +26,16 @@ public class ContactsHelper {
ContentResolver resolver = context.getContentResolver();
try (Cursor cursor = resolver.query(uri, PROJECTION, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
return cursor.getString(cursor.getColumnIndex(
ContactsContract.PhoneLookup.DISPLAY_NAME));
ContactItem contact = new ContactItem(
cursor.getLong(cursor.getColumnIndex(
ContactsContract.Contacts._ID)),
cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME))
);
if (TextUtils.isEmpty(contact.displayName)) return null; // TODO: check
return contact;
}
}