blob: 8448d63e024bc0e53068ff8568fa2475b8aa18a7 [file] [log] [blame]
alision2ec64f92013-06-17 17:28:58 -04001package com.savoirfairelinux.sflphone.loaders;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.regex.Matcher;
6import java.util.regex.Pattern;
7
8import android.content.AsyncTaskLoader;
9import android.content.Context;
10import android.database.Cursor;
11import android.os.RemoteException;
12import android.provider.ContactsContract;
13import android.provider.ContactsContract.Contacts;
14import android.util.Log;
15
16import com.savoirfairelinux.sflphone.model.CallContact;
17import com.savoirfairelinux.sflphone.model.CallContact.ContactBuilder;
18import com.savoirfairelinux.sflphone.model.HistoryEntry;
19import com.savoirfairelinux.sflphone.model.HistoryEntry.HistoryCall;
20import com.savoirfairelinux.sflphone.service.ISipService;
21import com.savoirfairelinux.sflphone.service.ServiceConstants;
22
23public 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
37 @Override
38 public ArrayList<HistoryEntry> loadInBackground() {
39
40 historyEntries = new HashMap<String, HistoryEntry>();
41
alision1005ba12013-06-19 13:52:44 -040042 if(service == null){
43 return new ArrayList<HistoryEntry>();
44 }
alision2ec64f92013-06-17 17:28:58 -040045 try {
46 ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory();
47 Log.i(TAG, "history size:" + history.size());
48 CallContact.ContactBuilder builder = new CallContact.ContactBuilder();
49 for (HashMap<String, String> entry : history) {
50 entry.get(ServiceConstants.HISTORY_ACCOUNT_ID_KEY);
51 long timestampEnd = Long.parseLong(entry.get(ServiceConstants.HISTORY_TIMESTAMP_STOP_KEY));
52 long timestampStart = Long.parseLong(entry.get(ServiceConstants.HISTORY_TIMESTAMP_START_KEY));
alision907bde72013-06-20 14:40:37 -040053 String call_state = entry.get(ServiceConstants.HISTORY_STATE_KEY);
alision2ec64f92013-06-17 17:28:58 -040054
55 String number_called = entry.get(ServiceConstants.HISTORY_PEER_NUMBER_KEY);
56 CallContact c = null;
57 if (historyEntries.containsKey(number_called)) {
alision907bde72013-06-20 14:40:37 -040058 historyEntries.get(number_called).addHistoryCall(new HistoryCall(timestampStart, timestampEnd, number_called, call_state));
alision2ec64f92013-06-17 17:28:58 -040059 } else {
60
61 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
62 Matcher m = p.matcher(number_called);
63 if (m.find()) {
64 String s1 = m.group(1);
65 System.out.println(s1);
66 String s2 = m.group(2);
67 System.out.println(s2);
68
69 Cursor result = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
70 ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + m.group(1), null, null);
71
72 if (result.getCount() > 0) {
73 result.moveToFirst();
74 builder.startNewContact(result.getLong(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)),
75 result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),
76 result.getLong(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID)));
77 builder.addPhoneNumber(number_called, 0);
78 c = builder.build();
alision907bde72013-06-20 14:40:37 -040079 } else {
80 c = ContactBuilder.buildUnknownContact(number_called);
alision2ec64f92013-06-17 17:28:58 -040081 }
82 result.close();
83 } else {
84 c = ContactBuilder.buildUnknownContact(number_called);
85 }
alision907bde72013-06-20 14:40:37 -040086 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.HISTORY_ACCOUNT_ID_KEY), c);
87 e.addHistoryCall(new HistoryCall(timestampStart, timestampEnd, number_called, call_state));
alision2ec64f92013-06-17 17:28:58 -040088 historyEntries.put(number_called, e);
89 }
90
91 }
92
93 } catch (RemoteException e) {
94 Log.i(TAG, e.toString());
95 }
96 return new ArrayList<HistoryEntry>(historyEntries.values());
97 }
98}