blob: 91a83bf28ede0ee57ae2914c0b8b783d92fd04d6 [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
42 try {
43 ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory();
44 Log.i(TAG, "history size:" + history.size());
45 CallContact.ContactBuilder builder = new CallContact.ContactBuilder();
46 for (HashMap<String, String> entry : history) {
47 entry.get(ServiceConstants.HISTORY_ACCOUNT_ID_KEY);
48 long timestampEnd = Long.parseLong(entry.get(ServiceConstants.HISTORY_TIMESTAMP_STOP_KEY));
49 long timestampStart = Long.parseLong(entry.get(ServiceConstants.HISTORY_TIMESTAMP_START_KEY));
50
51 System.out.println("HISTORY_TIMESTAMP_START_KEY" + entry.get(ServiceConstants.HISTORY_TIMESTAMP_START_KEY));
52 System.out.println("HISTORY_INCOMING_STRING" + entry.get(ServiceConstants.HISTORY_INCOMING_STRING));
53 System.out.println("HISTORY_OUTGOING_STRING" + entry.get(ServiceConstants.HISTORY_OUTGOING_STRING));
54 System.out.println("HISTORY_MISSED_STRING" + entry.get(ServiceConstants.HISTORY_MISSED_STRING));
55
56 System.out.println("HISTORY_STATE_KEY" + entry.get(ServiceConstants.HISTORY_STATE_KEY));
57
58 String number_called = entry.get(ServiceConstants.HISTORY_PEER_NUMBER_KEY);
59 CallContact c = null;
60 if (historyEntries.containsKey(number_called)) {
61 historyEntries.get(number_called).addHistoryCall(new HistoryCall(timestampStart, timestampEnd, number_called));
62 } else {
63
64 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
65 Matcher m = p.matcher(number_called);
66 if (m.find()) {
67 String s1 = m.group(1);
68 System.out.println(s1);
69 String s2 = m.group(2);
70 System.out.println(s2);
71
72 Cursor result = getContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
73 ContactsContract.CommonDataKinds.Phone.NUMBER + " = " + m.group(1), null, null);
74
75 if (result.getCount() > 0) {
76 result.moveToFirst();
77 builder.startNewContact(result.getLong(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)),
78 result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)),
79 result.getLong(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID)));
80 builder.addPhoneNumber(number_called, 0);
81 c = builder.build();
82 }
83 result.close();
84 } else {
85 c = ContactBuilder.buildUnknownContact(number_called);
86 }
87 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.HISTORY_ACCOUNT_ID_KEY), c, timestampStart, timestampEnd,
88 number_called);
89 historyEntries.put(number_called, e);
90 }
91
92 }
93
94 } catch (RemoteException e) {
95 Log.i(TAG, e.toString());
96 }
97 return new ArrayList<HistoryEntry>(historyEntries.values());
98 }
99}