Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.loaders; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.regex.Matcher; |
| 6 | import java.util.regex.Pattern; |
| 7 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 8 | import org.sflphone.model.CallContact; |
| 9 | import org.sflphone.model.CallContact.ContactBuilder; |
| 10 | import org.sflphone.model.HistoryEntry; |
| 11 | import org.sflphone.model.HistoryEntry.HistoryCall; |
| 12 | import org.sflphone.service.ISipService; |
| 13 | import org.sflphone.service.ServiceConstants; |
| 14 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 15 | import android.content.AsyncTaskLoader; |
| 16 | import android.content.Context; |
| 17 | import android.database.Cursor; |
| 18 | import android.os.RemoteException; |
| 19 | import android.provider.ContactsContract; |
| 20 | import android.provider.ContactsContract.Contacts; |
| 21 | import android.util.Log; |
| 22 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 23 | public 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 Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 37 | @SuppressWarnings("unchecked") |
| 38 | // Hashmap runtime cast |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 39 | @Override |
| 40 | public ArrayList<HistoryEntry> loadInBackground() { |
| 41 | |
| 42 | historyEntries = new HashMap<String, HistoryEntry>(); |
| 43 | |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 44 | if (service == null) { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 45 | return new ArrayList<HistoryEntry>(); |
| 46 | } |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 47 | try { |
| 48 | ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory(); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 49 | for (HashMap<String, String> entry : history) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 50 | |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 51 | String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 52 | |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame^] | 53 | Log.w(TAG, "----------------------Number" + number_called); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 54 | CallContact c = null; |
| 55 | if (historyEntries.containsKey(number_called)) { |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame^] | 56 | // It's a direct match |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 57 | historyEntries.get(number_called).addHistoryCall(new HistoryCall(entry)); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 58 | } else { |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame^] | 59 | // Maybe we can extract the extension @ account pattern |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 60 | Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>"); |
| 61 | Matcher m = p.matcher(number_called); |
| 62 | if (m.find()) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 63 | |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame^] | 64 | 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)); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 67 | } else { |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame^] | 68 | 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); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 72 | } |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame^] | 73 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 74 | } else { |
| 75 | c = ContactBuilder.buildUnknownContact(number_called); |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame^] | 76 | HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), c); |
| 77 | e.addHistoryCall(new HistoryCall(entry)); |
| 78 | historyEntries.put(number_called, e); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 79 | } |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame^] | 80 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | } catch (RemoteException e) { |
| 86 | Log.i(TAG, e.toString()); |
| 87 | } |
| 88 | return new ArrayList<HistoryEntry>(historyEntries.values()); |
| 89 | } |
| 90 | } |