blob: f0b48d1ed2e19554bce731e25d5f622d9ed292c6 [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
Alexandre Lision6e8931e2013-09-19 16:49:34 -040037 @SuppressWarnings("unchecked") // Hashmap runtime cast
alision2ec64f92013-06-17 17:28:58 -040038 @Override
39 public ArrayList<HistoryEntry> loadInBackground() {
40
41 historyEntries = new HashMap<String, HistoryEntry>();
42
alision50fa0722013-06-25 17:29:44 -040043 if (service == null) {
alision1005ba12013-06-19 13:52:44 -040044 return new ArrayList<HistoryEntry>();
45 }
alision2ec64f92013-06-17 17:28:58 -040046 try {
47 ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory();
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040048// Log.i(TAG, "history size:" + history.size());
alision2ec64f92013-06-17 17:28:58 -040049 CallContact.ContactBuilder builder = new CallContact.ContactBuilder();
50 for (HashMap<String, String> entry : history) {
Alexandre Lision6711ab22013-09-16 15:15:38 -040051 entry.get(ServiceConstants.history.ACCOUNT_ID_KEY);
52 long timestampEnd = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_STOP_KEY));
53 long timestampStart = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_START_KEY));
54 String call_state = entry.get(ServiceConstants.history.STATE_KEY);
alision2ec64f92013-06-17 17:28:58 -040055
Alexandre Lision6711ab22013-09-16 15:15:38 -040056 String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY);
alision50fa0722013-06-25 17:29:44 -040057
Alexandre Lision6711ab22013-09-16 15:15:38 -040058// Log.w(TAG, "----------------------Record" + entry.get(ServiceConstants.history.RECORDING_PATH_KEY));
alision2ec64f92013-06-17 17:28:58 -040059 CallContact c = null;
60 if (historyEntries.containsKey(number_called)) {
alision50fa0722013-06-25 17:29:44 -040061 historyEntries.get(number_called).addHistoryCall(
62 new HistoryCall(timestampStart, timestampEnd, number_called, call_state, entry
Alexandre Lision6711ab22013-09-16 15:15:38 -040063 .get(ServiceConstants.history.RECORDING_PATH_KEY)));
alision2ec64f92013-06-17 17:28:58 -040064 } else {
65
66 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
67 Matcher m = p.matcher(number_called);
68 if (m.find()) {
alision50fa0722013-06-25 17:29:44 -040069 // String s1 = m.group(1);
70 // System.out.println(s1);
71 // String s2 = m.group(2);
72 // System.out.println(s2);
alision2ec64f92013-06-17 17:28:58 -040073
74 Cursor result = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
75 ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + m.group(1), null, null);
76
77 if (result.getCount() > 0) {
78 result.moveToFirst();
79 builder.startNewContact(result.getLong(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)),
80 result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),
81 result.getLong(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID)));
82 builder.addPhoneNumber(number_called, 0);
83 c = builder.build();
alision907bde72013-06-20 14:40:37 -040084 } else {
85 c = ContactBuilder.buildUnknownContact(number_called);
alision2ec64f92013-06-17 17:28:58 -040086 }
87 result.close();
88 } else {
89 c = ContactBuilder.buildUnknownContact(number_called);
90 }
Alexandre Lision6711ab22013-09-16 15:15:38 -040091 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), c);
alision50fa0722013-06-25 17:29:44 -040092 e.addHistoryCall(new HistoryCall(timestampStart, timestampEnd, number_called, call_state, entry
Alexandre Lision6711ab22013-09-16 15:15:38 -040093 .get(ServiceConstants.history.RECORDING_PATH_KEY)));
alision2ec64f92013-06-17 17:28:58 -040094 historyEntries.put(number_called, e);
95 }
96
97 }
98
99 } catch (RemoteException e) {
100 Log.i(TAG, e.toString());
101 }
102 return new ArrayList<HistoryEntry>(historyEntries.values());
103 }
104}