Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -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 | 2e52d39 | 2013-11-06 15:14:31 -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 | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 32 | package org.sflphone.history; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 33 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 34 | import android.os.Parcel; |
| 35 | import android.os.Parcelable; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 36 | import org.sflphone.model.CallContact; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.NavigableMap; |
| 40 | import java.util.TreeMap; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 41 | |
| 42 | public class HistoryEntry implements Parcelable { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 43 | |
| 44 | private CallContact contact; |
| 45 | private NavigableMap<Long, HistoryCall> calls; |
| 46 | private String accountID; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 47 | int missed_sum; |
| 48 | int outgoing_sum; |
| 49 | int incoming_sum; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 50 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 51 | public HistoryEntry(String account, CallContact c) { |
| 52 | contact = c; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 53 | calls = new TreeMap<Long, HistoryCall>(); |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 54 | accountID = account; |
| 55 | missed_sum = outgoing_sum = incoming_sum = 0; |
| 56 | } |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 57 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 58 | public String getAccountID() { |
| 59 | return accountID; |
| 60 | } |
| 61 | |
| 62 | public void setAccountID(String accountID) { |
| 63 | this.accountID = accountID; |
| 64 | } |
| 65 | |
| 66 | public NavigableMap<Long, HistoryCall> getCalls() { |
| 67 | return calls; |
| 68 | } |
| 69 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 70 | public CallContact getContact() { |
| 71 | return contact; |
| 72 | } |
| 73 | |
| 74 | public void setContact(CallContact contact) { |
| 75 | this.contact = contact; |
| 76 | } |
| 77 | |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 78 | /** |
| 79 | * Each call is associated with a contact. |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 80 | * When adding a call to an HIstoryEntry, this methods also verifies if we can update |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 81 | * the contact (if contact is Unknown, replace it) |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 82 | * |
| 83 | * @param historyCall The call to put in this HistoryEntry |
| 84 | * @param linkedTo The associated CallContact |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 85 | */ |
| 86 | public void addHistoryCall(HistoryCall historyCall, CallContact linkedTo) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 87 | calls.put(historyCall.call_start, historyCall); |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 88 | if (historyCall.isIncoming()) { |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame] | 89 | ++incoming_sum; |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 90 | } else { |
| 91 | ++outgoing_sum; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 92 | } |
Alexandre Lision | a458ba2 | 2013-12-11 15:04:11 -0500 | [diff] [blame] | 93 | if (historyCall.isMissed()) |
| 94 | missed_sum++; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 95 | |
| 96 | if (contact.isUnknown() && !linkedTo.isUnknown()) |
Alexandre Lision | 2b4f94e | 2013-12-12 10:15:47 -0500 | [diff] [blame] | 97 | setContact(linkedTo); |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | public String getNumber() { |
| 101 | return calls.lastEntry().getValue().number; |
| 102 | } |
| 103 | |
| 104 | public String getTotalDuration() { |
| 105 | int duration = 0; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 106 | ArrayList<HistoryCall> all_calls = new ArrayList<HistoryCall>(calls.values()); |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 107 | for (int i = 0; i < all_calls.size(); ++i) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 108 | duration += all_calls.get(i).getDuration(); |
| 109 | } |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 110 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 111 | if (duration < 60) |
| 112 | return duration + "s"; |
| 113 | |
| 114 | return duration / 60 + "min"; |
| 115 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 116 | |
| 117 | public int getMissed_sum() { |
| 118 | return missed_sum; |
| 119 | } |
| 120 | |
| 121 | public int getOutgoing_sum() { |
| 122 | return outgoing_sum; |
| 123 | } |
| 124 | |
| 125 | public int getIncoming_sum() { |
| 126 | return incoming_sum; |
| 127 | } |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 128 | |
| 129 | @Override |
| 130 | public int describeContents() { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void writeToParcel(Parcel dest, int flags) { |
| 136 | |
| 137 | dest.writeParcelable(contact, 0); |
| 138 | |
| 139 | dest.writeList(new ArrayList<HistoryCall>(calls.values())); |
| 140 | dest.writeList(new ArrayList<Long>(calls.keySet())); |
| 141 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 142 | dest.writeString(accountID); |
| 143 | dest.writeInt(missed_sum); |
| 144 | dest.writeInt(outgoing_sum); |
| 145 | dest.writeInt(incoming_sum); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | public static final Parcelable.Creator<HistoryEntry> CREATOR = new Parcelable.Creator<HistoryEntry>() { |
| 150 | public HistoryEntry createFromParcel(Parcel in) { |
| 151 | return new HistoryEntry(in); |
| 152 | } |
| 153 | |
| 154 | public HistoryEntry[] newArray(int size) { |
| 155 | return new HistoryEntry[size]; |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | private HistoryEntry(Parcel in) { |
| 160 | contact = in.readParcelable(CallContact.class.getClassLoader()); |
| 161 | |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 162 | ArrayList<HistoryCall> values = new ArrayList<HistoryCall>(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 163 | in.readList(values, HistoryCall.class.getClassLoader()); |
| 164 | |
| 165 | ArrayList<Long> keys = new ArrayList<Long>(); |
| 166 | in.readList(keys, Long.class.getClassLoader()); |
| 167 | |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 168 | calls = new TreeMap<Long, HistoryCall>(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 169 | for (int i = 0; i < keys.size(); ++i) { |
| 170 | calls.put(keys.get(i), values.get(i)); |
| 171 | } |
| 172 | |
| 173 | accountID = in.readString(); |
| 174 | missed_sum = in.readInt(); |
| 175 | outgoing_sum = in.readInt(); |
| 176 | incoming_sum = in.readInt(); |
| 177 | } |
| 178 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 179 | } |