blob: 4d2639182cd02bbd51b1f8abeeeac22100055602 [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
67 long callID;
68
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_();
80 number = call.getContact().getPhones().get(0).getNumber();
Alexandre Lision67c70b42014-01-16 13:57:15 -050081 missed = call.isRinging() || call.isIncoming();
Alexandre Lision945e4612014-01-15 17:40:31 -050082 direction = call.getCallTypeString();
83 recordPath = call.getRecordPath();
84 }
85
Alexandre Lisionb4e60612014-01-14 17:47:23 -050086 /* Needed by ORMLite */
87 public HistoryCall() {
88 }
89
90 public String getDirection() {
91 return direction;
92 }
93
94 public String getDate() {
Alexandre Lision945e4612014-01-15 17:40:31 -050095 return HistoryTimeModel.timeToHistoryConst(call_start);
Alexandre Lisionb4e60612014-01-14 17:47:23 -050096 }
97
98 public String getStartString(String format) {
99 Timestamp stamp = new Timestamp(call_start * 1000); // in milliseconds
100 Date date = new Date(stamp.getTime());
101 SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
102 sdf.setTimeZone(TimeZone.getDefault());
103 String formattedDate = sdf.format(date);
104 return formattedDate;
105
106 }
107
108 public String getDurationString() {
109
110 long duration = call_end - call_start;
111 if (duration < 60)
112 return String.format(Locale.getDefault(), "%02d secs", duration);
113
114 if (duration < 3600)
115 return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60));
116
117 return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60));
118
119 }
120
121 public long getDuration() {
122 return call_end - call_start;
123 }
124
125 public String getRecordPath() {
126 return recordPath;
127 }
128
129 public String getNumber() {
130 return number;
131 }
132
133 @Override
134 public int describeContents() {
135 return 0;
136 }
137
138 @Override
139 public void writeToParcel(Parcel dest, int flags) {
140 dest.writeLong(call_start);
141 dest.writeLong(call_end);
142 dest.writeString(accountID);
143 dest.writeString(number);
144 dest.writeByte((byte) (missed ? 1 : 0));
145 dest.writeString(direction);
146 dest.writeString(recordPath);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500147 dest.writeLong(contactID);
148 dest.writeLong(callID);
149 }
150
151 public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() {
152 public HistoryCall createFromParcel(Parcel in) {
153 return new HistoryCall(in);
154 }
155
156 public HistoryCall[] newArray(int size) {
157 return new HistoryCall[size];
158 }
159 };
160
161 private HistoryCall(Parcel in) {
162 call_start = in.readLong();
163 call_end = in.readLong();
164 accountID = in.readString();
165 number = in.readString();
166 missed = in.readByte() == 1 ? true : false;
167 direction = in.readString();
168 recordPath = in.readString();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500169 contactID = in.readLong();
170 callID = in.readLong();
171 }
172
173 public boolean hasRecord() {
174 return recordPath.length() > 0;
175 }
176
177 public boolean isIncoming() {
Alexandre Lisiona9ee4eb2014-01-15 16:20:35 -0500178 return true;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500179 }
180
181 public boolean isMissed() {
182 return missed;
183 }
184
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500185}