Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 1 | /* |
| 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 Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 33 | package org.sflphone.history; |
| 34 | |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 35 | import android.os.Parcel; |
| 36 | import android.os.Parcelable; |
| 37 | import com.j256.ormlite.field.DatabaseField; |
Alexandre Lision | 945e461 | 2014-01-15 17:40:31 -0500 | [diff] [blame] | 38 | import org.sflphone.model.SipCall; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 39 | import org.sflphone.service.ServiceConstants; |
| 40 | |
| 41 | import java.sql.Timestamp; |
| 42 | import java.text.SimpleDateFormat; |
| 43 | import java.util.Date; |
| 44 | import java.util.HashMap; |
| 45 | import java.util.Locale; |
| 46 | import java.util.TimeZone; |
| 47 | |
| 48 | public class HistoryCall implements Parcelable { |
| 49 | |
Alexandre Lision | 67c70b4 | 2014-01-16 13:57:15 -0500 | [diff] [blame] | 50 | @DatabaseField(index = true, columnName="TIMESTAMP_START") |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 51 | long call_start; |
| 52 | @DatabaseField |
| 53 | long call_end; |
| 54 | @DatabaseField |
| 55 | String number; |
| 56 | @DatabaseField |
| 57 | boolean missed; |
| 58 | @DatabaseField |
Alexandre Lision | 183bf45 | 2014-01-17 11:21:59 -0500 | [diff] [blame] | 59 | int direction; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 60 | @DatabaseField |
| 61 | String recordPath; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 62 | @DatabaseField |
| 63 | String accountID; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 64 | @DatabaseField |
| 65 | long contactID; |
| 66 | @DatabaseField |
Alexandre Lision | 707f908 | 2014-01-16 15:09:07 -0500 | [diff] [blame] | 67 | String callID; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 68 | |
Alexandre Lision | 67c70b4 | 2014-01-16 13:57:15 -0500 | [diff] [blame] | 69 | public String getAccountID() { |
| 70 | return accountID; |
| 71 | } |
| 72 | |
| 73 | public long getContactID() { |
| 74 | return contactID; |
| 75 | } |
| 76 | |
Alexandre Lision | 945e461 | 2014-01-15 17:40:31 -0500 | [diff] [blame] | 77 | public HistoryCall(SipCall call) { |
| 78 | call_start = call.getTimestampStart_(); |
| 79 | call_end = call.getTimestampEnd_(); |
Alexandre Lision | 707f908 | 2014-01-16 15:09:07 -0500 | [diff] [blame] | 80 | accountID = call.getAccount().getAccountID(); |
Alexandre Lision | 945e461 | 2014-01-15 17:40:31 -0500 | [diff] [blame] | 81 | number = call.getContact().getPhones().get(0).getNumber(); |
Alexandre Lision | 183bf45 | 2014-01-17 11:21:59 -0500 | [diff] [blame] | 82 | missed = call.isRinging() && call.isIncoming(); |
| 83 | direction = call.getCallType(); |
Alexandre Lision | 945e461 | 2014-01-15 17:40:31 -0500 | [diff] [blame] | 84 | recordPath = call.getRecordPath(); |
Alexandre Lision | 707f908 | 2014-01-16 15:09:07 -0500 | [diff] [blame] | 85 | contactID = call.getContact().getId(); |
| 86 | callID = call.getCallId(); |
Alexandre Lision | 945e461 | 2014-01-15 17:40:31 -0500 | [diff] [blame] | 87 | } |
| 88 | |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 89 | /* Needed by ORMLite */ |
| 90 | public HistoryCall() { |
| 91 | } |
| 92 | |
| 93 | public String getDirection() { |
Alexandre Lision | 183bf45 | 2014-01-17 11:21:59 -0500 | [diff] [blame] | 94 | 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 Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | public String getDate() { |
Alexandre Lision | 945e461 | 2014-01-15 17:40:31 -0500 | [diff] [blame] | 105 | return HistoryTimeModel.timeToHistoryConst(call_start); |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 106 | } |
| 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 Lision | 183bf45 | 2014-01-17 11:21:59 -0500 | [diff] [blame] | 155 | dest.writeInt(direction); |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 156 | dest.writeString(recordPath); |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 157 | dest.writeLong(contactID); |
Alexandre Lision | 707f908 | 2014-01-16 15:09:07 -0500 | [diff] [blame] | 158 | dest.writeString(callID); |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 159 | } |
| 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 Lision | 183bf45 | 2014-01-17 11:21:59 -0500 | [diff] [blame] | 177 | direction = in.readInt(); |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 178 | recordPath = in.readString(); |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 179 | contactID = in.readLong(); |
Alexandre Lision | 707f908 | 2014-01-16 15:09:07 -0500 | [diff] [blame] | 180 | callID = in.readString(); |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | public boolean hasRecord() { |
| 184 | return recordPath.length() > 0; |
| 185 | } |
| 186 | |
| 187 | public boolean isIncoming() { |
Alexandre Lision | 183bf45 | 2014-01-17 11:21:59 -0500 | [diff] [blame] | 188 | return direction == SipCall.direction.CALL_TYPE_INCOMING; |
Alexandre Lision | b4e6061 | 2014-01-14 17:47:23 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | public boolean isMissed() { |
| 192 | return missed; |
| 193 | } |
| 194 | |
Alexandre Lision | aad014e | 2014-01-14 17:45:49 -0500 | [diff] [blame] | 195 | } |