Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2013 Savoir-Faire Linux Inc. |
| 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.model; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 33 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 34 | import java.util.ArrayList; |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 35 | import java.util.HashMap; |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 36 | import java.util.Locale; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 37 | import java.util.NavigableMap; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 38 | import java.util.TreeMap; |
| 39 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 40 | import org.sflphone.service.ServiceConstants; |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 41 | import org.sflphone.utils.HistoryManager; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 42 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 43 | import android.os.Parcel; |
| 44 | import android.os.Parcelable; |
| 45 | |
| 46 | public class HistoryEntry implements Parcelable { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 47 | |
| 48 | private CallContact contact; |
| 49 | private NavigableMap<Long, HistoryCall> calls; |
| 50 | private String accountID; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 51 | int missed_sum; |
| 52 | int outgoing_sum; |
| 53 | int incoming_sum; |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 54 | |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 55 | public HistoryEntry(String account, CallContact c) { |
| 56 | contact = c; |
| 57 | calls = new TreeMap<Long, HistoryEntry.HistoryCall>(); |
| 58 | accountID = account; |
| 59 | missed_sum = outgoing_sum = incoming_sum = 0; |
| 60 | } |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 61 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 62 | public String getAccountID() { |
| 63 | return accountID; |
| 64 | } |
| 65 | |
| 66 | public void setAccountID(String accountID) { |
| 67 | this.accountID = accountID; |
| 68 | } |
| 69 | |
| 70 | public NavigableMap<Long, HistoryCall> getCalls() { |
| 71 | return calls; |
| 72 | } |
| 73 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 74 | public CallContact getContact() { |
| 75 | return contact; |
| 76 | } |
| 77 | |
| 78 | public void setContact(CallContact contact) { |
| 79 | this.contact = contact; |
| 80 | } |
| 81 | |
| 82 | public void addHistoryCall(HistoryCall historyCall) { |
| 83 | calls.put(historyCall.call_start, historyCall); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame^] | 84 | if (historyCall.getDirection().contentEquals(ServiceConstants.history.OUTGOING_STRING)) { |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 85 | ++outgoing_sum; |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame^] | 86 | } else if (historyCall.isIncoming()) { |
| 87 | ++incoming_sum; |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 88 | } |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | public String getNumber() { |
| 92 | return calls.lastEntry().getValue().number; |
| 93 | } |
| 94 | |
| 95 | public String getTotalDuration() { |
| 96 | int duration = 0; |
| 97 | ArrayList<HistoryCall> all_calls = new ArrayList<HistoryEntry.HistoryCall>(calls.values()); |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 98 | for (int i = 0; i < all_calls.size(); ++i) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 99 | duration += all_calls.get(i).getDuration(); |
| 100 | } |
Alexandre Lision | 72e3732 | 2013-11-04 17:14:11 -0500 | [diff] [blame] | 101 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 102 | if (duration < 60) |
| 103 | return duration + "s"; |
| 104 | |
| 105 | return duration / 60 + "min"; |
| 106 | } |
alision | 907bde7 | 2013-06-20 14:40:37 -0400 | [diff] [blame] | 107 | |
| 108 | public int getMissed_sum() { |
| 109 | return missed_sum; |
| 110 | } |
| 111 | |
| 112 | public int getOutgoing_sum() { |
| 113 | return outgoing_sum; |
| 114 | } |
| 115 | |
| 116 | public int getIncoming_sum() { |
| 117 | return incoming_sum; |
| 118 | } |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 119 | |
| 120 | @Override |
| 121 | public int describeContents() { |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void writeToParcel(Parcel dest, int flags) { |
| 127 | |
| 128 | dest.writeParcelable(contact, 0); |
| 129 | |
| 130 | dest.writeList(new ArrayList<HistoryCall>(calls.values())); |
| 131 | dest.writeList(new ArrayList<Long>(calls.keySet())); |
| 132 | |
| 133 | dest.writeMap(calls); |
| 134 | dest.writeString(accountID); |
| 135 | dest.writeInt(missed_sum); |
| 136 | dest.writeInt(outgoing_sum); |
| 137 | dest.writeInt(incoming_sum); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | public static final Parcelable.Creator<HistoryEntry> CREATOR = new Parcelable.Creator<HistoryEntry>() { |
| 142 | public HistoryEntry createFromParcel(Parcel in) { |
| 143 | return new HistoryEntry(in); |
| 144 | } |
| 145 | |
| 146 | public HistoryEntry[] newArray(int size) { |
| 147 | return new HistoryEntry[size]; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | private HistoryEntry(Parcel in) { |
| 152 | contact = in.readParcelable(CallContact.class.getClassLoader()); |
| 153 | |
| 154 | ArrayList<HistoryCall> values = new ArrayList<HistoryEntry.HistoryCall>(); |
| 155 | in.readList(values, HistoryCall.class.getClassLoader()); |
| 156 | |
| 157 | ArrayList<Long> keys = new ArrayList<Long>(); |
| 158 | in.readList(keys, Long.class.getClassLoader()); |
| 159 | |
| 160 | calls = new TreeMap<Long, HistoryEntry.HistoryCall>(); |
| 161 | for (int i = 0; i < keys.size(); ++i) { |
| 162 | calls.put(keys.get(i), values.get(i)); |
| 163 | } |
| 164 | |
| 165 | accountID = in.readString(); |
| 166 | missed_sum = in.readInt(); |
| 167 | outgoing_sum = in.readInt(); |
| 168 | incoming_sum = in.readInt(); |
| 169 | } |
| 170 | |
| 171 | public static class HistoryCall implements Parcelable { |
| 172 | long call_start; |
| 173 | long call_end; |
| 174 | String number; |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame^] | 175 | |
| 176 | boolean missed; |
| 177 | String direction; |
| 178 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 179 | String recordPath; |
| 180 | String timeFormatted; |
| 181 | String displayName; |
| 182 | |
| 183 | public HistoryCall(HashMap<String, String> entry) { |
| 184 | call_end = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_STOP_KEY)); |
| 185 | call_start = Long.parseLong(entry.get(ServiceConstants.history.TIMESTAMP_START_KEY)); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame^] | 186 | |
| 187 | direction = entry.get(ServiceConstants.history.DIRECTION_KEY); |
| 188 | missed = entry.get(ServiceConstants.history.MISSED_KEY).contentEquals("true"); |
| 189 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 190 | displayName = entry.get(ServiceConstants.history.DISPLAY_NAME_KEY); |
| 191 | recordPath = entry.get(ServiceConstants.history.RECORDING_PATH_KEY); |
| 192 | number = entry.get(ServiceConstants.history.PEER_NUMBER_KEY); |
| 193 | timeFormatted = HistoryManager.timeToHistoryConst(call_start); |
| 194 | } |
| 195 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame^] | 196 | public String getDirection() { |
| 197 | return direction; |
| 198 | } |
| 199 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 200 | public String getDate() { |
| 201 | return timeFormatted; |
| 202 | } |
| 203 | |
| 204 | public String getDurationString() { |
| 205 | |
| 206 | long duration = call_end - call_start; |
| 207 | if (duration < 60) |
| 208 | return String.format(Locale.getDefault(), "%02d secs", duration); |
| 209 | |
| 210 | if (duration < 3600) |
| 211 | return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60)); |
| 212 | |
| 213 | return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60)); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | public long getDuration() { |
| 218 | return call_end - call_start; |
| 219 | |
| 220 | } |
| 221 | |
| 222 | public String getRecordPath() { |
| 223 | return recordPath; |
| 224 | } |
| 225 | |
| 226 | public String getNumber() { |
| 227 | return number; |
| 228 | } |
| 229 | |
| 230 | public String getDisplayName() { |
| 231 | return displayName; |
| 232 | } |
| 233 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 234 | @Override |
| 235 | public int describeContents() { |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public void writeToParcel(Parcel dest, int flags) { |
| 241 | dest.writeLong(call_start); |
| 242 | dest.writeLong(call_end); |
| 243 | dest.writeString(number); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame^] | 244 | dest.writeByte((byte) (missed ? 1 : 0)); |
| 245 | dest.writeString(direction); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 246 | dest.writeString(recordPath); |
| 247 | dest.writeString(timeFormatted); |
| 248 | dest.writeString(displayName); |
| 249 | } |
| 250 | |
| 251 | public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() { |
| 252 | public HistoryCall createFromParcel(Parcel in) { |
| 253 | return new HistoryCall(in); |
| 254 | } |
| 255 | |
| 256 | public HistoryCall[] newArray(int size) { |
| 257 | return new HistoryCall[size]; |
| 258 | } |
| 259 | }; |
| 260 | |
| 261 | private HistoryCall(Parcel in) { |
| 262 | call_start = in.readLong(); |
| 263 | call_end = in.readLong(); |
| 264 | number = in.readString(); |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame^] | 265 | missed = in.readByte() == 1 ? true : false; |
| 266 | direction = in.readString(); |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 267 | recordPath = in.readString(); |
| 268 | timeFormatted = in.readString(); |
| 269 | displayName = in.readString(); |
| 270 | } |
| 271 | |
| 272 | public boolean hasRecord() { |
| 273 | return recordPath.length() > 0; |
| 274 | } |
| 275 | |
Alexandre Lision | cb2345c | 2013-12-09 15:39:13 -0500 | [diff] [blame^] | 276 | public boolean isIncoming() { |
| 277 | return direction.contentEquals(ServiceConstants.history.INCOMING_STRING); |
| 278 | } |
| 279 | |
| 280 | public boolean isMissed() { |
| 281 | return missed; |
| 282 | } |
| 283 | |
Alexandre Lision | 2e52d39 | 2013-11-06 15:14:31 -0500 | [diff] [blame] | 284 | } |
| 285 | |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 286 | } |