use of fr.neamar.kiss.pojo.ContactsPojo in project KISS by Neamar.
the class LoadContactsPojos method doInBackground.
@Override
protected ArrayList<ContactsPojo> doInBackground(Void... params) {
long start = System.nanoTime();
// Run query
Cursor cur = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { ContactsContract.Contacts.LOOKUP_KEY, ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.STARRED, ContactsContract.CommonDataKinds.Phone.IS_PRIMARY, ContactsContract.Contacts.PHOTO_ID, ContactsContract.CommonDataKinds.Phone.CONTACT_ID }, null, null, ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED + " DESC");
// Prevent duplicates by keeping in memory encountered phones.
// The string key is "phone" + "|" + "name" (so if two contacts
// with distinct name share same number, they both get displayed)
Map<String, ArrayList<ContactsPojo>> mapContacts = new HashMap<>();
if (cur != null) {
if (cur.getCount() > 0) {
int lookupIndex = cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
int timesContactedIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TIMES_CONTACTED);
int displayNameIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int numberIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int starredIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED);
int isPrimaryIndex = cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY);
int photoIdIndex = cur.getColumnIndex(ContactsContract.Contacts.PHOTO_ID);
while (cur.moveToNext()) {
ContactsPojo contact = new ContactsPojo();
contact.lookupKey = cur.getString(lookupIndex);
contact.timesContacted = cur.getInt(timesContactedIndex);
contact.setName(cur.getString(displayNameIndex));
contact.phone = cur.getString(numberIndex);
if (contact.phone == null) {
contact.phone = "";
}
contact.phoneSimplified = PhoneNormalizer.simplifyPhoneNumber(contact.phone);
contact.starred = cur.getInt(starredIndex) != 0;
contact.primary = cur.getInt(isPrimaryIndex) != 0;
String photoId = cur.getString(photoIdIndex);
if (photoId != null) {
contact.icon = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, Long.parseLong(photoId));
}
contact.id = pojoScheme + contact.lookupKey + contact.phone;
if (contact.name != null) {
contact.nameNormalized = StringNormalizer.normalize(contact.name);
if (mapContacts.containsKey(contact.lookupKey))
mapContacts.get(contact.lookupKey).add(contact);
else {
ArrayList<ContactsPojo> phones = new ArrayList<>();
phones.add(contact);
mapContacts.put(contact.lookupKey, phones);
}
}
}
}
cur.close();
}
// Retrieve contacts' nicknames
Cursor nickCursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Nickname.NAME, ContactsContract.Data.LOOKUP_KEY }, ContactsContract.Data.MIMETYPE + "= ?", new String[] { ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE }, null);
if (nickCursor != null) {
if (nickCursor.getCount() > 0) {
int lookupKeyIndex = nickCursor.getColumnIndex(ContactsContract.Data.LOOKUP_KEY);
int nickNameIndex = nickCursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME);
while (nickCursor.moveToNext()) {
String lookupKey = nickCursor.getString(lookupKeyIndex);
String nick = nickCursor.getString(nickNameIndex);
if (nick != null && lookupKey != null && mapContacts.containsKey(lookupKey)) {
for (ContactsPojo contact : mapContacts.get(lookupKey)) {
contact.setNickname(nick);
}
}
}
}
nickCursor.close();
}
ArrayList<ContactsPojo> contacts = new ArrayList<>();
Pattern phoneFormatter = Pattern.compile("[ \\.\\(\\)]");
for (List<ContactsPojo> phones : mapContacts.values()) {
// Find primary phone and add this one.
Boolean hasPrimary = false;
for (ContactsPojo contact : phones) {
if (contact.primary) {
contacts.add(contact);
hasPrimary = true;
break;
}
}
// If not available, add all (excluding duplicates).
if (!hasPrimary) {
Map<String, Boolean> added = new HashMap<>();
for (ContactsPojo contact : phones) {
String uniqueKey = phoneFormatter.matcher(contact.phone).replaceAll("");
//uniqueKey = uniqueKey.replaceAll("^\\+1", "0");
if (!added.containsKey(uniqueKey)) {
added.put(uniqueKey, true);
contacts.add(contact);
}
}
}
}
long end = System.nanoTime();
Log.i("time", Long.toString((end - start) / 1000000) + " milliseconds to list contacts");
return contacts;
}
use of fr.neamar.kiss.pojo.ContactsPojo in project KISS by Neamar.
the class IncomingSmsHandler method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
// Only handle SMS received
if (!intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
return;
}
// Stop if contacts are not enabled
DataHandler dataHandler = KissApplication.getDataHandler(context);
ContactsProvider contactsProvider = dataHandler.getContactsProvider();
if (contactsProvider == null) {
// Contacts have been disabled from settings
return;
}
// Get the SMS message passed in, if any
Bundle bundle = intent.getExtras();
if (bundle == null) {
return;
}
// Retrieve the SMS message received.
// Since we're not interested in content, we can safely discard
// all records but the first one
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus == null) {
return;
}
SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);
// Now, retrieve the contact by its lookup key on our contactsProvider
ContactsPojo contactPojo = contactsProvider.findByPhone(msg.getOriginatingAddress());
if (contactPojo != null) {
// We have a match!
dataHandler.addToHistory(contactPojo.id);
}
}
use of fr.neamar.kiss.pojo.ContactsPojo in project KISS by Neamar.
the class IncomingCallHandler method onReceive.
@Override
public void onReceive(final Context context, Intent intent) {
try {
DataHandler dataHandler = KissApplication.getDataHandler(context);
ContactsProvider contactsProvider = dataHandler.getContactsProvider();
// Stop if contacts are not enabled
if (contactsProvider == null) {
return;
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if (phoneNumber == null) {
// Skipping (private call)
return;
}
ContactsPojo contactPojo = contactsProvider.findByPhone(phoneNumber);
if (contactPojo != null) {
dataHandler.addToHistory(contactPojo.id);
}
}
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
use of fr.neamar.kiss.pojo.ContactsPojo in project KISS by Neamar.
the class ContactsProvider method getResults.
public ArrayList<Pojo> getResults(String query) {
query = StringNormalizer.normalize(query);
ArrayList<Pojo> results = new ArrayList<>();
// Search people with composed names, e.g "jean-marie"
// (not part of the StringNormalizer class, since we want to keep dashes on other providers)
query = query.replaceAll("-", " ");
int relevance;
int matchPositionStart;
int matchPositionEnd;
String contactNameNormalized;
final String queryWithSpace = " " + query;
for (ContactsPojo contact : pojos) {
relevance = 0;
contactNameNormalized = contact.nameNormalized;
boolean alias = false;
matchPositionStart = 0;
matchPositionEnd = 0;
if (contactNameNormalized.startsWith(query)) {
relevance = 50;
matchPositionEnd = matchPositionStart + query.length();
} else if ((matchPositionStart = contactNameNormalized.indexOf(queryWithSpace)) > -1) {
relevance = 40;
matchPositionEnd = matchPositionStart + queryWithSpace.length();
} else if (contact.nickname.contains(query)) {
alias = true;
contact.displayName = contact.name + " <small>(" + contact.nickname.replaceFirst("(?i)(" + Pattern.quote(query) + ")", "{$1}") + ")</small>";
relevance = 30;
} else if (query.length() > 2) {
matchPositionStart = 0;
matchPositionEnd = 0;
if (contact.phoneSimplified.startsWith(query)) {
relevance = 10;
} else if (contact.phoneSimplified.contains(query)) {
relevance = 5;
}
}
if (relevance > 0) {
// Increase relevance according to number of times the contacts
// was phoned :
relevance += contact.timesContacted;
// Increase relevance for starred contacts:
if (contact.starred)
relevance += 30;
// Decrease for home numbers:
if (contact.homeNumber)
relevance -= 1;
if (!alias)
contact.setDisplayNameHighlightRegion(matchPositionStart, matchPositionEnd);
contact.relevance = relevance;
results.add(contact);
// can return hundreds of results which are then slow to sort and display)
if (results.size() > 50) {
break;
}
}
}
return results;
}
Aggregations