blob: f186f0f1e3b1fa04df9e0aeaa6d30f6a30d93e2a [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lisiona8b78722013-12-13 10:18:33 -05003 *
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
Alexandre Lisionaad014e2014-01-14 17:45:49 -050034import android.content.AsyncTaskLoader;
alision2ec64f92013-06-17 17:28:58 -040035import android.content.Context;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050036import android.database.Cursor;
37import android.provider.ContactsContract;
38import org.sflphone.history.HistoryCall;
39import org.sflphone.history.HistoryEntry;
40import org.sflphone.history.HistoryManager;
41import org.sflphone.model.CallContact;
42
43import java.sql.SQLException;
44import java.util.ArrayList;
Alexandre Lisionbcff9822014-01-20 10:19:09 -050045import java.util.HashMap;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050046import java.util.List;
Alexandre Lisionbcff9822014-01-20 10:19:09 -050047import java.util.regex.Matcher;
48import java.util.regex.Pattern;
alision2ec64f92013-06-17 17:28:58 -040049
alision2ec64f92013-06-17 17:28:58 -040050public class HistoryLoader extends AsyncTaskLoader<ArrayList<HistoryEntry>> {
51
52 private static final String TAG = HistoryLoader.class.getSimpleName();
alision2ec64f92013-06-17 17:28:58 -040053
Alexandre Lisionaad014e2014-01-14 17:45:49 -050054 private HistoryManager historyManager = null;
alision2ec64f92013-06-17 17:28:58 -040055
Alexandre Lisionaad014e2014-01-14 17:45:49 -050056 public HistoryLoader(Context context) {
alision2ec64f92013-06-17 17:28:58 -040057 super(context);
Alexandre Lisionaad014e2014-01-14 17:45:49 -050058 historyManager = new HistoryManager(context);
alision2ec64f92013-06-17 17:28:58 -040059 }
60
Alexandre Lision72e37322013-11-04 17:14:11 -050061 @SuppressWarnings("unchecked")
62 // Hashmap runtime cast
alision2ec64f92013-06-17 17:28:58 -040063 @Override
64 public ArrayList<HistoryEntry> loadInBackground() {
65
Alexandre Lisionbcff9822014-01-20 10:19:09 -050066 HashMap<String,HistoryEntry> historyEntries = new HashMap<String, HistoryEntry>();
alision2ec64f92013-06-17 17:28:58 -040067
Alexandre Lisionaad014e2014-01-14 17:45:49 -050068 try {
69 List<HistoryCall> list = historyManager.getAll();
70
71 HistoryEntry tmp;
72 CallContact.ContactBuilder builder = CallContact.ContactBuilder.getInstance();
73 for (HistoryCall call : list) {
74 CallContact contact;
Alexandre Lisionbcff9822014-01-20 10:19:09 -050075 if (call.getContactID() == CallContact.DEFAULT_ID) {
Alexandre Lisionaad014e2014-01-14 17:45:49 -050076 contact = CallContact.ContactBuilder.buildUnknownContact(call.getNumber());
77 } else {
78 Cursor result = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
79 ContactsContract.Contacts._ID + " = ?",
80 new String[]{String.valueOf(call.getContactID())}, null);
81 int iID = result.getColumnIndex(ContactsContract.Contacts._ID);
82 int iName = result.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
83 int iPhoto = result.getColumnIndex(ContactsContract.Contacts.PHOTO_ID);
Alexandre Lisionaad014e2014-01-14 17:45:49 -050084
85 if (result.moveToFirst()) {
86 builder.startNewContact(result.getLong(iID), result.getString(iName), result.getLong(iPhoto));
Alexandre Lision707f9082014-01-16 15:09:07 -050087 builder.addPhoneNumber(call.getNumber(), 0);
Alexandre Lisionaad014e2014-01-14 17:45:49 -050088 contact = builder.build();
89 } else {
90 contact = CallContact.ContactBuilder.buildUnknownContact(call.getNumber());
91 }
92 }
Alexandre Lisionbcff9822014-01-20 10:19:09 -050093
94 if (historyEntries.containsKey(call.getNumber())) {
95 // It's a direct match
96 historyEntries.get(call.getNumber()).addHistoryCall(call, contact);
97 } else {
98 // Maybe we can extract the extension @ account pattern
99 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
100 Matcher m = p.matcher(call.getNumber());
101 if (m.find()) {
102
103 if (historyEntries.containsKey(m.group(1) + "@" + m.group(2))) {
104 historyEntries.get(m.group(1) + "@" + m.group(2)).addHistoryCall(call, contact);
105 } else {
106 HistoryEntry e = new HistoryEntry(call.getNumber(), contact);
107 e.addHistoryCall(call, contact);
108 historyEntries.put(m.group(1) + "@" + m.group(2), e);
109 }
110
111 } else {
112 HistoryEntry e = new HistoryEntry(call.getNumber(), contact);
113 e.addHistoryCall(call, contact);
114 historyEntries.put(call.getNumber(), e);
115 }
116
117 }
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500118 }
119 } catch (SQLException e) {
120 e.printStackTrace();
alision1005ba12013-06-19 13:52:44 -0400121 }
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500122
123/*
alision2ec64f92013-06-17 17:28:58 -0400124 try {
125 ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory();
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500126
alision2ec64f92013-06-17 17:28:58 -0400127 for (HashMap<String, String> entry : history) {
alision2ec64f92013-06-17 17:28:58 -0400128
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500129 CallContact contact;
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500130 String contactName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY);
Alexandre Lision6711ab22013-09-16 15:15:38 -0400131 String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY);
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500132 if (contactName.isEmpty()) {
133 contact = ContactBuilder.buildUnknownContact(number_called);
134 } else {
135 contact = ContactBuilder.getInstance().buildSimpleContact(contactName, number_called);
136 }
alision50fa0722013-06-25 17:29:44 -0400137
alision2ec64f92013-06-17 17:28:58 -0400138 if (historyEntries.containsKey(number_called)) {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500139 // It's a direct match
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500140 historyEntries.get(number_called).addHistoryCall(new HistoryCall(entry), contact);
alision2ec64f92013-06-17 17:28:58 -0400141 } else {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500142 // Maybe we can extract the extension @ account pattern
alision2ec64f92013-06-17 17:28:58 -0400143 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
144 Matcher m = p.matcher(number_called);
145 if (m.find()) {
alision2ec64f92013-06-17 17:28:58 -0400146
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500147 if (historyEntries.containsKey(m.group(1) + "@" + m.group(2))) {
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500148 historyEntries.get(m.group(1) + "@" + m.group(2)).addHistoryCall(new HistoryCall(entry), contact);
alision907bde72013-06-20 14:40:37 -0400149 } else {
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500150 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact);
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500151 e.addHistoryCall(new HistoryCall(entry), contact);
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500152 historyEntries.put(m.group(1) + "@" + m.group(2), e);
alision2ec64f92013-06-17 17:28:58 -0400153 }
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500154
alision2ec64f92013-06-17 17:28:58 -0400155 } else {
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500156
157 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact);
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500158 e.addHistoryCall(new HistoryCall(entry), contact);
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500159 historyEntries.put(number_called, e);
alision2ec64f92013-06-17 17:28:58 -0400160 }
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500161
alision2ec64f92013-06-17 17:28:58 -0400162 }
163
164 }
165
166 } catch (RemoteException e) {
167 Log.i(TAG, e.toString());
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500168 }*/
Alexandre Lisionbcff9822014-01-20 10:19:09 -0500169 return new ArrayList<HistoryEntry>(historyEntries.values());
alision2ec64f92013-06-17 17:28:58 -0400170 }
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500171
172
alision2ec64f92013-06-17 17:28:58 -0400173}