Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.loaders; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.regex.Matcher; |
| 37 | import java.util.regex.Pattern; |
| 38 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 39 | import org.sflphone.model.CallContact; |
| 40 | import org.sflphone.model.CallContact.ContactBuilder; |
| 41 | import org.sflphone.model.HistoryEntry; |
| 42 | import org.sflphone.model.HistoryEntry.HistoryCall; |
| 43 | import org.sflphone.service.ISipService; |
| 44 | import org.sflphone.service.ServiceConstants; |
| 45 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 46 | import android.content.Context; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 47 | import android.os.RemoteException; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 48 | import android.provider.ContactsContract.Contacts; |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 49 | import android.support.v4.content.AsyncTaskLoader; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 50 | import android.util.Log; |
| 51 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 52 | public class HistoryLoader extends AsyncTaskLoader<ArrayList<HistoryEntry>> { |
| 53 | |
| 54 | private static final String TAG = HistoryLoader.class.getSimpleName(); |
| 55 | private ISipService service; |
| 56 | HashMap<String, HistoryEntry> historyEntries; |
| 57 | |
| 58 | static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME, Contacts.PHOTO_ID, Contacts.LOOKUP_KEY, |
| 59 | Contacts.STARRED }; |
| 60 | |
| 61 | public HistoryLoader(Context context, ISipService isip) { |
| 62 | super(context); |
| 63 | service = isip; |
| 64 | } |
| 65 | |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 66 | @SuppressWarnings("unchecked") |
| 67 | // Hashmap runtime cast |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 68 | @Override |
| 69 | public ArrayList<HistoryEntry> loadInBackground() { |
| 70 | |
| 71 | historyEntries = new HashMap<String, HistoryEntry>(); |
| 72 | |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 73 | if (service == null) { |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 74 | return new ArrayList<HistoryEntry>(); |
| 75 | } |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 76 | try { |
| 77 | ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory(); |
Alexandre Lision | 6158d1a | 2014-01-07 17:09:18 -0500 | [diff] [blame^] | 78 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 79 | for (HashMap<String, String> entry : history) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 80 | |
Alexandre Lision | 6158d1a | 2014-01-07 17:09:18 -0500 | [diff] [blame^] | 81 | CallContact contact; |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 82 | String contactName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY); |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 83 | String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY); |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 84 | if (contactName.isEmpty()) { |
| 85 | contact = ContactBuilder.buildUnknownContact(number_called); |
| 86 | } else { |
| 87 | contact = ContactBuilder.getInstance().buildSimpleContact(contactName, number_called); |
| 88 | } |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 89 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 90 | if (historyEntries.containsKey(number_called)) { |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 91 | // It's a direct match |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 92 | historyEntries.get(number_called).addHistoryCall(new HistoryCall(entry), contact); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 93 | } else { |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 94 | // Maybe we can extract the extension @ account pattern |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 95 | Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>"); |
| 96 | Matcher m = p.matcher(number_called); |
| 97 | if (m.find()) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 98 | |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 99 | if (historyEntries.containsKey(m.group(1) + "@" + m.group(2))) { |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 100 | historyEntries.get(m.group(1) + "@" + m.group(2)).addHistoryCall(new HistoryCall(entry), contact); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 101 | } else { |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 102 | HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact); |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 103 | e.addHistoryCall(new HistoryCall(entry), contact); |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 104 | historyEntries.put(m.group(1) + "@" + m.group(2), e); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 105 | } |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 106 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 107 | } else { |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 108 | |
| 109 | HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact); |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 110 | e.addHistoryCall(new HistoryCall(entry), contact); |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 111 | historyEntries.put(number_called, e); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 112 | } |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 113 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | } catch (RemoteException e) { |
| 119 | Log.i(TAG, e.toString()); |
| 120 | } |
| 121 | return new ArrayList<HistoryEntry>(historyEntries.values()); |
| 122 | } |
| 123 | } |