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 | |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 34 | import android.content.AsyncTaskLoader; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 35 | import android.content.Context; |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 36 | import android.database.Cursor; |
| 37 | import android.provider.ContactsContract; |
| 38 | import org.sflphone.history.HistoryCall; |
| 39 | import org.sflphone.history.HistoryEntry; |
| 40 | import org.sflphone.history.HistoryManager; |
| 41 | import org.sflphone.model.CallContact; |
| 42 | |
| 43 | import java.sql.SQLException; |
| 44 | import java.util.ArrayList; |
Alexandre Lision | bcff982 | 2014-01-20 10:19:09 -0500 | [diff] [blame^] | 45 | import java.util.HashMap; |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 46 | import java.util.List; |
Alexandre Lision | bcff982 | 2014-01-20 10:19:09 -0500 | [diff] [blame^] | 47 | import java.util.regex.Matcher; |
| 48 | import java.util.regex.Pattern; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 49 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 50 | public class HistoryLoader extends AsyncTaskLoader<ArrayList<HistoryEntry>> { |
| 51 | |
| 52 | private static final String TAG = HistoryLoader.class.getSimpleName(); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 53 | |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 54 | private HistoryManager historyManager = null; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 55 | |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 56 | public HistoryLoader(Context context) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 57 | super(context); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 58 | historyManager = new HistoryManager(context); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 61 | @SuppressWarnings("unchecked") |
| 62 | // Hashmap runtime cast |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 63 | @Override |
| 64 | public ArrayList<HistoryEntry> loadInBackground() { |
| 65 | |
Alexandre Lision | bcff982 | 2014-01-20 10:19:09 -0500 | [diff] [blame^] | 66 | HashMap<String,HistoryEntry> historyEntries = new HashMap<String, HistoryEntry>(); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 67 | |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 68 | 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 Lision | bcff982 | 2014-01-20 10:19:09 -0500 | [diff] [blame^] | 75 | if (call.getContactID() == CallContact.DEFAULT_ID) { |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 76 | 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 Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 84 | |
| 85 | if (result.moveToFirst()) { |
| 86 | builder.startNewContact(result.getLong(iID), result.getString(iName), result.getLong(iPhoto)); |
Alexandre Lision | 707f908 | 2014-01-16 15:09:07 -0500 | [diff] [blame] | 87 | builder.addPhoneNumber(call.getNumber(), 0); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 88 | contact = builder.build(); |
| 89 | } else { |
| 90 | contact = CallContact.ContactBuilder.buildUnknownContact(call.getNumber()); |
| 91 | } |
| 92 | } |
Alexandre Lision | bcff982 | 2014-01-20 10:19:09 -0500 | [diff] [blame^] | 93 | |
| 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 Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 118 | } |
| 119 | } catch (SQLException e) { |
| 120 | e.printStackTrace(); |
alision | 1005ba1 | 2013-06-19 13:52:44 -0400 | [diff] [blame] | 121 | } |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 122 | |
| 123 | /* |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 124 | try { |
| 125 | ArrayList<HashMap<String, String>> history = (ArrayList<HashMap<String, String>>) service.getHistory(); |
Alexandre Lision | 6158d1a | 2014-01-07 17:09:18 -0500 | [diff] [blame] | 126 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 127 | for (HashMap<String, String> entry : history) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 128 | |
Alexandre Lision | 6158d1a | 2014-01-07 17:09:18 -0500 | [diff] [blame] | 129 | CallContact contact; |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 130 | String contactName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY); |
Alexandre Lision | 6711ab2 | 2013-09-16 15:15:38 -0400 | [diff] [blame] | 131 | String number_called = entry.get(ServiceConstants.history.PEER_NUMBER_KEY); |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 132 | if (contactName.isEmpty()) { |
| 133 | contact = ContactBuilder.buildUnknownContact(number_called); |
| 134 | } else { |
| 135 | contact = ContactBuilder.getInstance().buildSimpleContact(contactName, number_called); |
| 136 | } |
alision | 50fa072 | 2013-06-25 17:29:44 -0400 | [diff] [blame] | 137 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 138 | if (historyEntries.containsKey(number_called)) { |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 139 | // It's a direct match |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 140 | historyEntries.get(number_called).addHistoryCall(new HistoryCall(entry), contact); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 141 | } else { |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 142 | // Maybe we can extract the extension @ account pattern |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 143 | Pattern p = Pattern.compile("<sip:([^@]+)@([^>]+)>"); |
| 144 | Matcher m = p.matcher(number_called); |
| 145 | if (m.find()) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 146 | |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 147 | if (historyEntries.containsKey(m.group(1) + "@" + m.group(2))) { |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 148 | historyEntries.get(m.group(1) + "@" + m.group(2)).addHistoryCall(new HistoryCall(entry), contact); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 149 | } else { |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 150 | HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact); |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 151 | e.addHistoryCall(new HistoryCall(entry), contact); |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 152 | historyEntries.put(m.group(1) + "@" + m.group(2), e); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 153 | } |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 154 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 155 | } else { |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 156 | |
| 157 | HistoryEntry e = new HistoryEntry(entry.get(ServiceConstants.history.ACCOUNT_ID_KEY), contact); |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 158 | e.addHistoryCall(new HistoryCall(entry), contact); |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 159 | historyEntries.put(number_called, e); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 160 | } |
Alexandre Lision | ec71b2d | 2013-11-05 10:21:08 -0500 | [diff] [blame] | 161 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } |
| 165 | |
| 166 | } catch (RemoteException e) { |
| 167 | Log.i(TAG, e.toString()); |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 168 | }*/ |
Alexandre Lision | bcff982 | 2014-01-20 10:19:09 -0500 | [diff] [blame^] | 169 | return new ArrayList<HistoryEntry>(historyEntries.values()); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 170 | } |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 171 | |
| 172 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 173 | } |