blob: d3432a7ed8ffbc6fb914c4364bd2beaffee490fa [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
alision2ec64f92013-06-17 17:28:58 -040034import android.content.Context;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050035import android.database.Cursor;
36import android.provider.ContactsContract;
Alexandre Lisionbe54c342014-01-21 17:10:33 -050037import android.support.v4.content.AsyncTaskLoader;
Alexandre Lisionaad014e2014-01-14 17:45:49 -050038import 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();
Alexandre Lisionaad014e2014-01-14 17:45:49 -050070 CallContact.ContactBuilder builder = CallContact.ContactBuilder.getInstance();
71 for (HistoryCall call : list) {
72 CallContact contact;
Alexandre Lisionbcff9822014-01-20 10:19:09 -050073 if (call.getContactID() == CallContact.DEFAULT_ID) {
Alexandre Lisionaad014e2014-01-14 17:45:49 -050074 contact = CallContact.ContactBuilder.buildUnknownContact(call.getNumber());
75 } else {
76 Cursor result = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
77 ContactsContract.Contacts._ID + " = ?",
78 new String[]{String.valueOf(call.getContactID())}, null);
79 int iID = result.getColumnIndex(ContactsContract.Contacts._ID);
80 int iName = result.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
81 int iPhoto = result.getColumnIndex(ContactsContract.Contacts.PHOTO_ID);
Alexandre Lisionaad014e2014-01-14 17:45:49 -050082
83 if (result.moveToFirst()) {
84 builder.startNewContact(result.getLong(iID), result.getString(iName), result.getLong(iPhoto));
Alexandre Lision707f9082014-01-16 15:09:07 -050085 builder.addPhoneNumber(call.getNumber(), 0);
Alexandre Lisionaad014e2014-01-14 17:45:49 -050086 contact = builder.build();
87 } else {
88 contact = CallContact.ContactBuilder.buildUnknownContact(call.getNumber());
89 }
Alexandre Lisionf9885fb2014-01-21 11:57:51 -050090 result.close();
Alexandre Lisionaad014e2014-01-14 17:45:49 -050091 }
Alexandre Lisionbcff9822014-01-20 10:19:09 -050092
93 if (historyEntries.containsKey(call.getNumber())) {
94 // It's a direct match
95 historyEntries.get(call.getNumber()).addHistoryCall(call, contact);
96 } else {
97 // Maybe we can extract the extension @ account pattern
98 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
99 Matcher m = p.matcher(call.getNumber());
100 if (m.find()) {
101
102 if (historyEntries.containsKey(m.group(1) + "@" + m.group(2))) {
103 historyEntries.get(m.group(1) + "@" + m.group(2)).addHistoryCall(call, contact);
104 } else {
Alexandre Lisionf9885fb2014-01-21 11:57:51 -0500105 HistoryEntry e = new HistoryEntry(call.getAccountID(), contact);
Alexandre Lisionbcff9822014-01-20 10:19:09 -0500106 e.addHistoryCall(call, contact);
107 historyEntries.put(m.group(1) + "@" + m.group(2), e);
108 }
109
110 } else {
Alexandre Lisionf9885fb2014-01-21 11:57:51 -0500111 HistoryEntry e = new HistoryEntry(call.getAccountID(), contact);
Alexandre Lisionbcff9822014-01-20 10:19:09 -0500112 e.addHistoryCall(call, contact);
113 historyEntries.put(call.getNumber(), e);
114 }
115
116 }
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500117 }
118 } catch (SQLException e) {
119 e.printStackTrace();
alision1005ba12013-06-19 13:52:44 -0400120 }
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500121
122/*
alision2ec64f92013-06-17 17:28:58 -0400123 try {
124 ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory();
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500125
alision2ec64f92013-06-17 17:28:58 -0400126 for (HashMap<String, String> entry : history) {
alision2ec64f92013-06-17 17:28:58 -0400127
Alexandre Lision6158d1a2014-01-07 17:09:18 -0500128 CallContact contact;
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500129 String contactName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY);
Alexandre Lision6711ab22013-09-16 15:15:38 -0400130 String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY);
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500131 if (contactName.isEmpty()) {
132 contact = ContactBuilder.buildUnknownContact(number_called);
133 } else {
134 contact = ContactBuilder.getInstance().buildSimpleContact(contactName, number_called);
135 }
alision50fa0722013-06-25 17:29:44 -0400136
alision2ec64f92013-06-17 17:28:58 -0400137 if (historyEntries.containsKey(number_called)) {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500138 // It's a direct match
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500139 historyEntries.get(number_called).addHistoryCall(new HistoryCall(entry), contact);
alision2ec64f92013-06-17 17:28:58 -0400140 } else {
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500141 // Maybe we can extract the extension @ account pattern
alision2ec64f92013-06-17 17:28:58 -0400142 Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>");
143 Matcher m = p.matcher(number_called);
144 if (m.find()) {
alision2ec64f92013-06-17 17:28:58 -0400145
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500146 if (historyEntries.containsKey(m.group(1) + "@" + m.group(2))) {
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500147 historyEntries.get(m.group(1) + "@" + m.group(2)).addHistoryCall(new HistoryCall(entry), contact);
alision907bde72013-06-20 14:40:37 -0400148 } else {
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500149 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact);
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500150 e.addHistoryCall(new HistoryCall(entry), contact);
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500151 historyEntries.put(m.group(1) + "@" + m.group(2), e);
alision2ec64f92013-06-17 17:28:58 -0400152 }
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500153
alision2ec64f92013-06-17 17:28:58 -0400154 } else {
Alexandre Lisiona458ba22013-12-11 15:04:11 -0500155
156 HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact);
Alexandre Lision2b4f94e2013-12-12 10:15:47 -0500157 e.addHistoryCall(new HistoryCall(entry), contact);
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500158 historyEntries.put(number_called, e);
alision2ec64f92013-06-17 17:28:58 -0400159 }
Alexandre Lisionec71b2d2013-11-05 10:21:08 -0500160
alision2ec64f92013-06-17 17:28:58 -0400161 }
162
163 }
164
165 } catch (RemoteException e) {
166 Log.i(TAG, e.toString());
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500167 }*/
Alexandre Lisionbcff9822014-01-20 10:19:09 -0500168 return new ArrayList<HistoryEntry>(historyEntries.values());
alision2ec64f92013-06-17 17:28:58 -0400169 }
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500170
171
alision2ec64f92013-06-17 17:28:58 -0400172}