blob: 5b21f472da8b6de729b0f12abe06e36735922046 [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
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 Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.loaders;
alision2ec64f92013-06-17 17:28:58 -040033
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.regex.Matcher;
37import java.util.regex.Pattern;
38
Alexandre Lision064e1e02013-10-01 16:18:42 -040039import org.sflphone.model.CallContact;
40import org.sflphone.model.CallContact.ContactBuilder;
41import org.sflphone.model.HistoryEntry;
42import org.sflphone.model.HistoryEntry.HistoryCall;
43import org.sflphone.service.ISipService;
44import org.sflphone.service.ServiceConstants;
45
alision2ec64f92013-06-17 17:28:58 -040046import android.content.Context;
alision2ec64f92013-06-17 17:28:58 -040047import android.os.RemoteException;
alision2ec64f92013-06-17 17:28:58 -040048import android.provider.ContactsContract.Contacts;
Alexandre Lisiona8b78722013-12-13 10:18:33 -050049import android.support.v4.content.AsyncTaskLoader;
alision2ec64f92013-06-17 17:28:58 -040050import android.util.Log;
51
alision2ec64f92013-06-17 17:28:58 -040052public 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 Lision72e37322013-11-04 17:14:11 -050066 @SuppressWarnings("unchecked")
67 // Hashmap runtime cast
alision2ec64f92013-06-17 17:28:58 -040068 @Override
69 public ArrayList<HistoryEntry> loadInBackground() {
70
71 historyEntries = new HashMap<String, HistoryEntry>();
72
alision50fa0722013-06-25 17:29:44 -040073 if (service == null) {
alision1005ba12013-06-19 13:52:44 -040074 return new ArrayList<HistoryEntry>();
75 }
alision2ec64f92013-06-17 17:28:58 -040076 try {
77 ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory();
alision2ec64f92013-06-17 17:28:58 -040078 for (HashMap<String, String> entry : history) {
alision2ec64f92013-06-17 17:28:58 -040079
Alexandre Lisiona458ba22013-12-11 15:04:11 -050080 CallContact contact = null;
81 String contactName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY);
Alexandre Lision6711ab22013-09-16 15:15:38 -040082 String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY);
Alexandre Lisiona458ba22013-12-11 15:04:11 -050083 if (contactName.isEmpty()) {
84 contact = ContactBuilder.buildUnknownContact(number_called);
85 } else {
86 contact = ContactBuilder.getInstance().buildSimpleContact(contactName, number_called);
87 }
alision50fa0722013-06-25 17:29:44 -040088
alision2ec64f92013-06-17 17:28:58 -040089 if (historyEntries.containsKey(number_called)) {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050090 // It's a direct match
Alexandre Lision2b4f94e2013-12-12 10:15:47 -050091 historyEntries.get(number_called).addHistoryCall(new HistoryCall(entry), contact);
alision2ec64f92013-06-17 17:28:58 -040092 } else {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050093 // Maybe we can extract the extension @ account pattern
alision2ec64f92013-06-17 17:28:58 -040094 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
95 Matcher m = p.matcher(number_called);
96 if (m.find()) {
alision2ec64f92013-06-17 17:28:58 -040097
Alexandre Lisionec71b2d2013-11-05 10:21:08 -050098 if (historyEntries.containsKey(m.group(1) + "@" + m.group(2))) {
Alexandre Lision2b4f94e2013-12-12 10:15:47 -050099 historyEntries.get(m.group(1) + "@" + m.group(2)).addHistoryCall(new HistoryCall(entry), contact);
alision907bde72013-06-20 14:40:37 -0400100 } else {
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500101 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact);
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500102 e.addHistoryCall(new HistoryCall(entry), contact);
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500103 historyEntries.put(m.group(1) + "@" + m.group(2), e);
alision2ec64f92013-06-17 17:28:58 -0400104 }
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500105
alision2ec64f92013-06-17 17:28:58 -0400106 } else {
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500107
108 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact);
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500109 e.addHistoryCall(new HistoryCall(entry), contact);
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500110 historyEntries.put(number_called, e);
alision2ec64f92013-06-17 17:28:58 -0400111 }
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500112
alision2ec64f92013-06-17 17:28:58 -0400113 }
114
115 }
116
117 } catch (RemoteException e) {
118 Log.i(TAG, e.toString());
119 }
120 return new ArrayList<HistoryEntry>(historyEntries.values());
121 }
122}