blob: 972a1c44bfd752923dd172f0111fb67177304929 [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
Alexandre Lision183bf452014-01-17 11:21:59 -050059 int direction;
Alexandre Lisionb4e60612014-01-14 17:47:23 -050060 @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 Lision183bf452014-01-17 11:21:59 -050082 missed = call.isRinging() && call.isIncoming();
83 direction = call.getCallType();
Alexandre Lision945e4612014-01-15 17:40:31 -050084 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() {
Alexandre Lision183bf452014-01-17 11:21:59 -050094 switch (direction) {
95 case SipCall.direction.CALL_TYPE_INCOMING:
96 return "CALL_TYPE_INCOMING";
97 case SipCall.direction.CALL_TYPE_OUTGOING:
98 return "CALL_TYPE_OUTGOING";
99 default:
100 return "CALL_TYPE_UNDETERMINED";
101 }
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500102 }
103
104 public String getDate() {
Alexandre Lision945e4612014-01-15 17:40:31 -0500105 return HistoryTimeModel.timeToHistoryConst(call_start);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500106 }
107
108 public String getStartString(String format) {
109 Timestamp stamp = new Timestamp(call_start * 1000); // in milliseconds
110 Date date = new Date(stamp.getTime());
111 SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.getDefault());
112 sdf.setTimeZone(TimeZone.getDefault());
113 String formattedDate = sdf.format(date);
114 return formattedDate;
115
116 }
117
118 public String getDurationString() {
119
120 long duration = call_end - call_start;
121 if (duration < 60)
122 return String.format(Locale.getDefault(), "%02d secs", duration);
123
124 if (duration < 3600)
125 return String.format(Locale.getDefault(), "%02d mins %02d secs", (duration % 3600) / 60, (duration % 60));
126
127 return String.format(Locale.getDefault(), "%d h %02d mins %02d secs", duration / 3600, (duration % 3600) / 60, (duration % 60));
128
129 }
130
131 public long getDuration() {
132 return call_end - call_start;
133 }
134
135 public String getRecordPath() {
136 return recordPath;
137 }
138
139 public String getNumber() {
140 return number;
141 }
142
143 @Override
144 public int describeContents() {
145 return 0;
146 }
147
148 @Override
149 public void writeToParcel(Parcel dest, int flags) {
150 dest.writeLong(call_start);
151 dest.writeLong(call_end);
152 dest.writeString(accountID);
153 dest.writeString(number);
154 dest.writeByte((byte) (missed ? 1 : 0));
Alexandre Lision183bf452014-01-17 11:21:59 -0500155 dest.writeInt(direction);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500156 dest.writeString(recordPath);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500157 dest.writeLong(contactID);
Alexandre Lision707f9082014-01-16 15:09:07 -0500158 dest.writeString(callID);
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500159 }
160
161 public static final Parcelable.Creator<HistoryCall> CREATOR = new Parcelable.Creator<HistoryCall>() {
162 public HistoryCall createFromParcel(Parcel in) {
163 return new HistoryCall(in);
164 }
165
166 public HistoryCall[] newArray(int size) {
167 return new HistoryCall[size];
168 }
169 };
170
171 private HistoryCall(Parcel in) {
172 call_start = in.readLong();
173 call_end = in.readLong();
174 accountID = in.readString();
175 number = in.readString();
176 missed = in.readByte() == 1 ? true : false;
Alexandre Lision183bf452014-01-17 11:21:59 -0500177 direction = in.readInt();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500178 recordPath = in.readString();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500179 contactID = in.readLong();
Alexandre Lision707f9082014-01-16 15:09:07 -0500180 callID = in.readString();
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500181 }
182
183 public boolean hasRecord() {
184 return recordPath.length() > 0;
185 }
186
187 public boolean isIncoming() {
Alexandre Lision183bf452014-01-17 11:21:59 -0500188 return direction == SipCall.direction.CALL_TYPE_INCOMING;
Alexandre Lisionb4e60612014-01-14 17:47:23 -0500189 }
190
191 public boolean isMissed() {
192 return missed;
193 }
194
Alexandre Lisionaad014e2014-01-14 17:45:49 -0500195}