alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.loaders; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.HashMap; |
| 5 | import java.util.regex.Matcher; |
| 6 | import java.util.regex.Pattern; |
| 7 | |
| 8 | import android.content.AsyncTaskLoader; |
| 9 | import android.content.Context; |
| 10 | import android.database.Cursor; |
| 11 | import android.os.RemoteException; |
| 12 | import android.provider.ContactsContract; |
| 13 | import android.provider.ContactsContract.Contacts; |
| 14 | import android.util.Log; |
| 15 | |
| 16 | import com.savoirfairelinux.sflphone.model.CallContact; |
| 17 | import com.savoirfairelinux.sflphone.model.CallContact.ContactBuilder; |
| 18 | import com.savoirfairelinux.sflphone.model.HistoryEntry; |
| 19 | import com.savoirfairelinux.sflphone.model.HistoryEntry.HistoryCall; |
| 20 | import com.savoirfairelinux.sflphone.service.ISipService; |
| 21 | import com.savoirfairelinux.sflphone.service.ServiceConstants; |
| 22 | |
| 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 | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 37 | @SuppressWarnings("unchecked") // Hashmap runtime cast |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 38 | @Override |
| 39 | public ArrayList<HistoryEntry> loadInBackground() { |
| 40 | |
| 41 | historyEntries = new HashMap<String, HistoryEntry>(); |
| 42 | |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 43 | if (service == null) { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 44 | return new ArrayList<HistoryEntry>(); |
| 45 | } |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 46 | try { |
| 47 | ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory(); |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 48 | // Log.i(TAG, "history size:" + history.size()); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 49 | CallContact.ContactBuilder builder = new CallContact.ContactBuilder(); |
| 50 | for (HashMap<String, String> entry : history) { |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 51 | 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); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 55 | |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 56 | String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 57 | |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 58 | // Log.w(TAG, "----------------------Record" + entry.get(ServiceConstants.history.RECORDING_PATH_KEY)); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 59 | CallContact c = null; |
| 60 | if (historyEntries.containsKey(number_called)) { |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 61 | historyEntries.get(number_called).addHistoryCall( |
| 62 | new HistoryCall(timestampStart, timestampEnd, number_called, call_state, entry |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 63 | .get(ServiceConstants.history.RECORDING_PATH_KEY))); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 64 | } else { |
| 65 | |
| 66 | Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>"); |
| 67 | Matcher m = p.matcher(number_called); |
| 68 | if (m.find()) { |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 69 | // String s1 = m.group(1); |
| 70 | // System.out.println(s1); |
| 71 | // String s2 = m.group(2); |
| 72 | // System.out.println(s2); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 73 | |
| 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(); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 84 | } else { |
| 85 | c = ContactBuilder.buildUnknownContact(number_called); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 86 | } |
| 87 | result.close(); |
| 88 | } else { |
| 89 | c = ContactBuilder.buildUnknownContact(number_called); |
| 90 | } |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 91 | HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), c); |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 92 | e.addHistoryCall(new HistoryCall(timestampStart, timestampEnd, number_called, call_state, entry |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 93 | .get(ServiceConstants.history.RECORDING_PATH_KEY))); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 94 | 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 | } |