blob: 41f4c27c1601357c38b99af84254d369fad47da3 [file] [log] [blame]
Alexandre Lision064e1e02013-10-01 16:18:42 -04001package org.sflphone.loaders;
alision2ec64f92013-06-17 17:28:58 -04002
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.regex.Matcher;
6import java.util.regex.Pattern;
7
Alexandre Lision064e1e02013-10-01 16:18:42 -04008import org.sflphone.model.CallContact;
9import org.sflphone.model.CallContact.ContactBuilder;
10import org.sflphone.model.HistoryEntry;
11import org.sflphone.model.HistoryEntry.HistoryCall;
12import org.sflphone.service.ISipService;
13import org.sflphone.service.ServiceConstants;
14
alision2ec64f92013-06-17 17:28:58 -040015import android.content.AsyncTaskLoader;
16import android.content.Context;
17import android.database.Cursor;
18import android.os.RemoteException;
19import android.provider.ContactsContract;
20import android.provider.ContactsContract.Contacts;
21import android.util.Log;
22
alision2ec64f92013-06-17 17:28:58 -040023public class HistoryLoader extends AsyncTaskLoader<ArrayList<HistoryEntry>> {
24
25 private static final String TAG = HistoryLoader.class.getSimpleName();
26 private ISipService service;
27 HashMap<String, HistoryEntry> historyEntries;
28
29 static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY,
30 Contacts.STARRED };
31
32 public HistoryLoader(Context context, ISipService isip) {
33 super(context);
34 service = isip;
35 }
36
Alexandre Lision72e37322013-11-04 17:14:11 -050037 @SuppressWarnings("unchecked")
38 // Hashmap runtime cast
alision2ec64f92013-06-17 17:28:58 -040039 @Override
40 public ArrayList<HistoryEntry> loadInBackground() {
41
42 historyEntries = new HashMap<String, HistoryEntry>();
43
alision50fa0722013-06-25 17:29:44 -040044 if (service == null) {
alision1005ba12013-06-19 13:52:44 -040045 return new ArrayList<HistoryEntry>();
46 }
alision2ec64f92013-06-17 17:28:58 -040047 try {
48 ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory();
alision2ec64f92013-06-17 17:28:58 -040049 for (HashMap<String, String> entry : history) {
alision2ec64f92013-06-17 17:28:58 -040050
Alexandre Lision6711ab22013-09-16 15:15:38 -040051 String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY);
alision50fa0722013-06-25 17:29:44 -040052
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050053 Log.w(TAG, "----------------------Number" + number_called);
alision2ec64f92013-06-17 17:28:58 -040054 CallContact c = null;
55 if (historyEntries.containsKey(number_called)) {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050056 // It's a direct match
Alexandre Lision72e37322013-11-04 17:14:11 -050057 historyEntries.get(number_called).addHistoryCall(new HistoryCall(entry));
alision2ec64f92013-06-17 17:28:58 -040058 } else {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050059 // Maybe we can extract the extension @ account pattern
alision2ec64f92013-06-17 17:28:58 -040060 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
61 Matcher m = p.matcher(number_called);
62 if (m.find()) {
alision2ec64f92013-06-17 17:28:58 -040063
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050064 Log.i(TAG, "Pattern found:" + m.group(1));
65 if (historyEntries.containsKey(m.group(1) + "@" + m.group(2))) {
66 historyEntries.get(m.group(1) + "@" + m.group(2)).addHistoryCall(new HistoryCall(entry));
alision907bde72013-06-20 14:40:37 -040067 } else {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050068 c = ContactBuilder.buildUnknownContact(m.group(1) + "@" + m.group(2));
69 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), c);
70 e.addHistoryCall(new HistoryCall(entry));
71 historyEntries.put(m.group(1) + "@" + m.group(2), e);
alision2ec64f92013-06-17 17:28:58 -040072 }
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050073
alision2ec64f92013-06-17 17:28:58 -040074 } else {
75 c = ContactBuilder.buildUnknownContact(number_called);
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050076 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), c);
77 e.addHistoryCall(new HistoryCall(entry));
78 historyEntries.put(number_called, e);
alision2ec64f92013-06-17 17:28:58 -040079 }
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050080
alision2ec64f92013-06-17 17:28:58 -040081 }
82
83 }
84
85 } catch (RemoteException e) {
86 Log.i(TAG, e.toString());
87 }
88 return new ArrayList<HistoryEntry>(historyEntries.values());
89 }
90}