blob: c2ef036f1665914c4eb4ecbc908c060ef6af8b6b [file] [log] [blame]
Alexandre Lision2e52d392013-11-06 15:14:31 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lision2e52d392013-11-06 15:14:31 -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 Lisionaad014e2014-01-14 17:45:49 -050032package org.sflphone.history;
alision2ec64f92013-06-17 17:28:58 -040033
Alexandre Lision2e52d392013-11-06 15:14:31 -050034import android.os.Parcel;
35import android.os.Parcelable;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050036import org.sflphone.model.CallContact;
37
38import java.util.ArrayList;
39import java.util.NavigableMap;
40import java.util.TreeMap;
Alexandre Lision2e52d392013-11-06 15:14:31 -050041
42public class HistoryEntry implements Parcelable {
alision2ec64f92013-06-17 17:28:58 -040043
44 private CallContact contact;
45 private NavigableMap<Long, HistoryCall> calls;
46 private String accountID;
alision907bde72013-06-20 14:40:37 -040047 int missed_sum;
48 int outgoing_sum;
49 int incoming_sum;
alision2ec64f92013-06-17 17:28:58 -040050
alision907bde72013-06-20 14:40:37 -040051 public HistoryEntry(String account, CallContact c) {
52 contact = c;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050053 calls = new TreeMap<Long, HistoryCall>();
alision907bde72013-06-20 14:40:37 -040054 accountID = account;
55 missed_sum = outgoing_sum = incoming_sum = 0;
56 }
Alexandre Lision72e37322013-11-04 17:14:11 -050057
alision2ec64f92013-06-17 17:28:58 -040058 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
alision2ec64f92013-06-17 17:28:58 -040070 public CallContact getContact() {
71 return contact;
72 }
73
74 public void setContact(CallContact contact) {
75 this.contact = contact;
76 }
77
Alexandre Lision2b4f94e2013-12-12 10:15:47 -050078 /**
79 * Each call is associated with a contact.
Alexandre Lisionb4e60612014-01-14 17:47:23 -050080 * When adding a call to an HIstoryEntry, this methods also verifies if we can update
Alexandre Lision2b4f94e2013-12-12 10:15:47 -050081 * the contact (if contact is Unknown, replace it)
Alexandre Lisionb4e60612014-01-14 17:47:23 -050082 *
83 * @param historyCall The call to put in this HistoryEntry
84 * @param linkedTo The associated CallContact
Alexandre Lision2b4f94e2013-12-12 10:15:47 -050085 */
86 public void addHistoryCall(HistoryCall historyCall, CallContact linkedTo) {
alision2ec64f92013-06-17 17:28:58 -040087 calls.put(historyCall.call_start, historyCall);
Alexandre Lisiona458ba22013-12-11 15:04:11 -050088 if (historyCall.isIncoming()) {
Alexandre Lisioncb2345c2013-12-09 15:39:13 -050089 ++incoming_sum;
Alexandre Lisiona458ba22013-12-11 15:04:11 -050090 } else {
91 ++outgoing_sum;
alision907bde72013-06-20 14:40:37 -040092 }
Alexandre Lisiona458ba22013-12-11 15:04:11 -050093 if (historyCall.isMissed())
94 missed_sum++;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050095
96 if (contact.isUnknown() && !linkedTo.isUnknown())
Alexandre Lision2b4f94e2013-12-12 10:15:47 -050097 setContact(linkedTo);
alision2ec64f92013-06-17 17:28:58 -040098 }
99
100 public String getNumber() {
101 return calls.lastEntry().getValue().number;
102 }
103
104 public String getTotalDuration() {
105 int duration = 0;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500106 ArrayList<HistoryCall> all_calls = new ArrayList<HistoryCall>(calls.values());
Alexandre Lision72e37322013-11-04 17:14:11 -0500107 for (int i = 0; i < all_calls.size(); ++i) {
alision2ec64f92013-06-17 17:28:58 -0400108 duration += all_calls.get(i).getDuration();
109 }
Alexandre Lision72e37322013-11-04 17:14:11 -0500110
alision2ec64f92013-06-17 17:28:58 -0400111 if (duration < 60)
112 return duration + "s";
113
114 return duration / 60 + "min";
115 }
alision907bde72013-06-20 14:40:37 -0400116
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 Lision2e52d392013-11-06 15:14:31 -0500128
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 Lision2e52d392013-11-06 15:14:31 -0500142 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 Lisionb4e60612014-01-14 17:47:23 -0500162 ArrayList<HistoryCall> values = new ArrayList<HistoryCall>();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500163 in.readList(values, HistoryCall.class.getClassLoader());
164
165 ArrayList<Long> keys = new ArrayList<Long>();
166 in.readList(keys, Long.class.getClassLoader());
167
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500168 calls = new TreeMap<Long, HistoryCall>();
Alexandre Lision2e52d392013-11-06 15:14:31 -0500169 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
alision2ec64f92013-06-17 17:28:58 -0400179}