Skip to content
Snippets Groups Projects
Commit b0668a80 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

contacts: use contact key for retreival

Tuleap: #328
Change-Id: I4f7d5fd466d3038cb5342120bbc4dbbfafd99f91
parent 06637f6c
No related branches found
No related tags found
No related merge requests found
...@@ -66,6 +66,9 @@ public class HistoryCall implements Parcelable { ...@@ -66,6 +66,9 @@ public class HistoryCall implements Parcelable {
public long getContactID() { public long getContactID() {
return contactID; return contactID;
} }
public String getContactKey() {
return contactKey;
}
public HistoryCall(SipCall call) { public HistoryCall(SipCall call) {
call_start = call.getTimestampStart(); call_start = call.getTimestampStart();
......
...@@ -72,19 +72,9 @@ public class HistoryText ...@@ -72,19 +72,9 @@ public class HistoryText
return contactID; return contactID;
} }
/* public String getContactKey() {
public HistoryText(String account, String from, String msg, CallContact contact, boolean incoming) { return contactKey;
time = System.currentTimeMillis();
accountID = account;
number = from;
direction = incoming ? TextMessage.direction.INCOMING : TextMessage.direction.OUTGOING;
if (contact != null) {
contactID = contact.getId();
contactKey = contact.getKey();
} }
//callID = call.getCallId();
message = msg;
}*/
/* Needed by ORMLite */ /* Needed by ORMLite */
public HistoryText() { public HistoryText() {
...@@ -105,15 +95,6 @@ public class HistoryText ...@@ -105,15 +95,6 @@ public class HistoryText
return new Date(time); return new Date(time);
} }
/*
public String getTimeString(String format) {
Timestamp stamp = new Timestamp(time); // in milliseconds
Date date = new Date(stamp.getTime());
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(date);
}*/
public String getNumber() { public String getNumber() {
return number; return number;
} }
......
...@@ -607,7 +607,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -607,7 +607,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
CallContact c = systemContactCache.get(id); CallContact c = systemContactCache.get(id);
if (c == null) { if (c == null) {
Log.w(TAG, "getContactById : cache miss for " + id); Log.w(TAG, "getContactById : cache miss for " + id);
c = findById(getContentResolver(), id); c = findById(getContentResolver(), id, null);
systemContactCache.put(id, c); systemContactCache.put(id, c);
} }
return c; return c;
...@@ -674,7 +674,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -674,7 +674,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
private static final String ID_SELECTION = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?"; private static final String ID_SELECTION = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?";
private static void lookupDetails(@NonNull ContentResolver res, @NonNull CallContact c) { private static void lookupDetails(@NonNull ContentResolver res, @NonNull CallContact c) {
Log.w(TAG, "lookupDetails " + c.getKey()); //Log.w(TAG, "lookupDetails " + c.getKey());
try { try {
Cursor cPhones = res.query( Cursor cPhones = res.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
...@@ -716,43 +716,16 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -716,43 +716,16 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
} }
} }
public static CallContact findByKey(@NonNull ContentResolver res, String key) { public static CallContact findById(@NonNull ContentResolver res, long id, String key) {
Log.e(TAG, "findByKey " + key);
CallContact contact = null; CallContact contact = null;
try { try {
Cursor result = res.query(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, key), CONTACT_PROJECTION, Uri contentUri;
null, null, null); if (key != null)
if (result == null) contentUri = ContactsContract.Contacts.lookupContact(res, ContactsContract.Contacts.getLookupUri(id, key));
return null; else
if (result.moveToFirst()) { contentUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
int iID = result.getColumnIndex(ContactsContract.Data._ID);
int iKey = result.getColumnIndex(ContactsContract.Data.LOOKUP_KEY);
int iName = result.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
int iPhoto = result.getColumnIndex(ContactsContract.Data.PHOTO_ID);
int iStared = result.getColumnIndex(ContactsContract.Data.STARRED);
long cid = result.getLong(iID);
Log.w(TAG, "Contact id:" + cid + " key:" + result.getString(iKey));
contact = new CallContact(cid, result.getString(iKey), result.getString(iName), result.getLong(iPhoto));
if (result.getInt(iStared) != 0)
contact.setStared();
lookupDetails(res, contact);
}
result.close();
} catch(Exception e) {
Log.w(TAG, e);
}
return contact;
}
public static CallContact findById(@NonNull ContentResolver res, long id) { Cursor result = res.query(contentUri, CONTACT_PROJECTION, null, null, null);
Log.e(TAG, "findById " + id);
CallContact contact = null;
try {
Cursor result = res.query(ContactsContract.Contacts.CONTENT_URI, CONTACT_PROJECTION,
ContactsContract.Contacts._ID + " = ?",
new String[]{String.valueOf(id)}, null);
if (result == null) if (result == null)
return null; return null;
...@@ -775,10 +748,8 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -775,10 +748,8 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, e); Log.w(TAG, e);
} }
/*if (contact == null || contact.getPhones().isEmpty()) { if (contact == null)
Log.w(TAG, "findById " + id + " can't find contact."); Log.w(TAG, "findById " + id + " can't find contact.");
return null;
}*/
return contact; return contact;
} }
...@@ -884,16 +855,16 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -884,16 +855,16 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
return null; return null;
} }
CallContact getCreateContact(long contact_id, String cnumber) { CallContact getCreateContact(long contact_id, String contact_key, String cnumber) {
String number = CallContact.canonicalNumber(cnumber); String number = CallContact.canonicalNumber(cnumber);
//Log.w(TAG, "getCreateContact : " + cnumber + " " + number + " " + contact_id); //Log.w(TAG, "getCreateContact : " + cnumber + " " + number + " " + contact_id + " " + contact_key);
CallContact contact; CallContact contact;
if (contact_id <= CallContact.DEFAULT_ID) { if (contact_id <= CallContact.DEFAULT_ID) {
contact = getByNumber(localNumberCache, number); contact = getByNumber(localNumberCache, number);
} else { } else {
contact = localContactCache.get(contact_id); contact = localContactCache.get(contact_id);
if (contact == null) { if (contact == null) {
contact = canUseContacts ? findById(cr, contact_id) : CallContact.buildUnknown(number); contact = canUseContacts ? findById(cr, contact_id, contact_key) : CallContact.buildUnknown(number);
if (contact != null) if (contact != null)
contact.addPhoneNumber(cnumber); contact.addPhoneNumber(cnumber);
else { else {
...@@ -916,7 +887,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -916,7 +887,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
for (HistoryCall call : history) { for (HistoryCall call : history) {
//Log.w(TAG, "History call : " + call.getNumber() + " " + call.call_start + " " + call.getEndDate().toString() + " " + call.getContactID()); //Log.w(TAG, "History call : " + call.getNumber() + " " + call.call_start + " " + call.getEndDate().toString() + " " + call.getContactID());
CallContact contact = getCreateContact(call.getContactID(), call.getNumber()); CallContact contact = getCreateContact(call.getContactID(), call.getContactKey(), call.getNumber());
Map.Entry<String, Conversation> merge = null; Map.Entry<String, Conversation> merge = null;
for (Map.Entry<String, Conversation> ce : ret.entrySet()) { for (Map.Entry<String, Conversation> ce : ret.entrySet()) {
...@@ -949,7 +920,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -949,7 +920,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
for (HistoryText htext : historyTexts) { for (HistoryText htext : historyTexts) {
//Log.w(TAG, "History text : " + htext.getNumber() + " " + htext.getDate() + " " + htext.getCallId() + " " + htext.getAccountID() + " " + htext.getMessage()); //Log.w(TAG, "History text : " + htext.getNumber() + " " + htext.getDate() + " " + htext.getCallId() + " " + htext.getAccountID() + " " + htext.getMessage());
CallContact contact = getCreateContact(htext.getContactID(), htext.getNumber()); CallContact contact = getCreateContact(htext.getContactID(), htext.getContactKey(), htext.getNumber());
Pair<HistoryEntry, HistoryCall> p = findHistoryByCallId(ret, htext.getCallId()); Pair<HistoryEntry, HistoryCall> p = findHistoryByCallId(ret, htext.getCallId());
if (contact == null && p != null) if (contact == null && p != null)
...@@ -993,7 +964,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -993,7 +964,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
List<SipCall> calls = conf.getParticipants(); List<SipCall> calls = conf.getParticipants();
if (calls.size() == 1) { if (calls.size() == 1) {
SipCall call = calls.get(0); SipCall call = calls.get(0);
CallContact contact = getCreateContact(-1, call.getNumber()); CallContact contact = getCreateContact(-1, null, call.getNumber());
call.setContact(contact); call.setContact(contact);
Conversation conv = null; Conversation conv = null;
...@@ -1020,7 +991,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP ...@@ -1020,7 +991,7 @@ public class LocalService extends Service implements SharedPreferences.OnSharedP
if (!ret.containsKey(key)) if (!ret.containsKey(key))
ret.put(key, new Conversation(contact)); ret.put(key, new Conversation(contact));
} }
} catch (RemoteException | SQLException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return ret; return ret;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment