blob: ac13f4888ab1d0a029fd373ef2d50ebe799082ac [file] [log] [blame]
Alexandre Lisionb4e60612014-01-14 17:47:23 -05001/*
2 * Copyright (C) 2004-2014 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
32
Alexandre Lisionaad014e2014-01-14 17:45:49 -050033package org.sflphone.history;
34
Alexandre Lisionb4e60612014-01-14 17:47:23 -050035import android.os.Parcel;
36import android.os.Parcelable;
37import com.j256.ormlite.field.DatabaseField;
Alexandre Lision945e4612014-01-15 17:40:31 -050038import org.sflphone.model.SipCall;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050039import org.sflphone.service.ServiceConstants;
40
41import java.sql.Timestamp;
42import java.text.SimpleDateFormat;
43import java.util.Date;
44import java.util.HashMap;
45import java.util.Locale;
46import java.util.TimeZone;
47
48public class HistoryCall implements Parcelable {
49
Alexandre Lision67c70b42014-01-16 13:57:15 -050050 @DatabaseField(index = true, columnName="TIMESTAMP_START")
Alexandre Lisionb4e60612014-01-14 17:47:23 -050051 long call_start;
52 @DatabaseField
53 long call_end;
54 @DatabaseField
55 String number;
56 @DatabaseField
57 boolean missed;
58 @DatabaseField
59 String direction;
60 @DatabaseField
61 String recordPath;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050062 @DatabaseField
63 String accountID;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050064 @DatabaseField
65 long contactID;
66 @DatabaseField
Alexandre Lision707f9082014-01-16 15:09:07 -050067 String callID;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050068
Alexandre Lision67c70b42014-01-16 13:57:15 -050069 public String getAccountID() {
70 return accountID;
71 }
72
73 public long getContactID() {
74 return contactID;
75 }
76
Alexandre Lision945e4612014-01-15 17:40:31 -050077 public HistoryCall(SipCall call) {
78 call_start = call.getTimestampStart_();
79 call_end = call.getTimestampEnd_();
Alexandre Lision707f9082014-01-16 15:09:07 -050080 accountID = call.getAccount().getAccountID();
Alexandre Lision945e4612014-01-15 17:40:31 -050081 number = call.getContact().getPhones().get(0).getNumber();
Alexandre Lision67c70b42014-01-16 13:57:15 -050082 missed = call.isRinging() || call.isIncoming();
Alexandre Lision945e4612014-01-15 17:40:31 -050083 direction = call.getCallTypeString();
84 recordPath = call.getRecordPath();
Alexandre Lision707f9082014-01-16 15:09:07 -050085 contactID = call.getContact().getId();
86 callID = call.getCallId();
Alexandre Lision945e4612014-01-15 17:40:31 -050087 }
88
Alexandre Lisionb4e60612014-01-14 17:47:23 -050089 /* Needed by ORMLite */
90 public HistoryCall() {
91 }
92
93 public String getDirection() {
94 return direction;
95 }
96
97 public String getDate() {
Alexandre Lision945e4612014-01-15 17:40:31 -050098 return HistoryTimeModel.timeToHistoryConst(call_start);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050099 }
100
101 public String getStartString(String format) {
102 Timestamp stamp = new Timestamp(call_start * 1000); // in milliseconds
103 Date date = new Date(stamp.getTime());
104 SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
105 sdf.setTimeZone(TimeZone.getDefault());
106 String formattedDate = sdf.format(date);
107 return formattedDate;
108
109 }
110
111 public String getDurationString() {
112
113 long duration = call_end - call_start;
114 if (duration < 60)
115 return String.format(Locale.getDefault(), "%02d secs", duration);
116
117 if (duration < 3600)
118 return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60));
119
120 return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60));
121
122 }
123
124 public long getDuration() {
125 return call_end - call_start;
126 }
127
128 public String getRecordPath() {
129 return recordPath;
130 }
131
132 public String getNumber() {
133 return number;
134 }
135
136 @Override
137 public int describeContents() {
138 return 0;
139 }
140
141 @Override
142 public void writeToParcel(Parcel dest, int flags) {
143 dest.writeLong(call_start);
144 dest.writeLong(call_end);
145 dest.writeString(accountID);
146 dest.writeString(number);
147 dest.writeByte((byte) (missed ? 1 : 0));
148 dest.writeString(direction);
149 dest.writeString(recordPath);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500150 dest.writeLong(contactID);
Alexandre Lision707f9082014-01-16 15:09:07 -0500151 dest.writeString(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500152 }
153
154 public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() {
155 public HistoryCall createFromParcel(Parcel in) {
156 return new HistoryCall(in);
157 }
158
159 public HistoryCall[] newArray(int size) {
160 return new HistoryCall[size];
161 }
162 };
163
164 private HistoryCall(Parcel in) {
165 call_start = in.readLong();
166 call_end = in.readLong();
167 accountID = in.readString();
168 number = in.readString();
169 missed = in.readByte() == 1 ? true : false;
170 direction = in.readString();
171 recordPath = in.readString();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500172 contactID = in.readLong();
Alexandre Lision707f9082014-01-16 15:09:07 -0500173 callID = in.readString();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500174 }
175
176 public boolean hasRecord() {
177 return recordPath.length() > 0;
178 }
179
180 public boolean isIncoming() {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500181 return true;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500182 }
183
184 public boolean isMissed() {
185 return missed;
186 }
187
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500188}