alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 1 | package com.savoirfairelinux.sflphone.model; |
| 2 | |
| 3 | import java.text.SimpleDateFormat; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Calendar; |
| 6 | import java.util.Locale; |
| 7 | import java.util.NavigableMap; |
| 8 | import java.util.TimeZone; |
| 9 | import java.util.TreeMap; |
| 10 | |
| 11 | public class HistoryEntry { |
| 12 | |
| 13 | private CallContact contact; |
| 14 | private NavigableMap<Long, HistoryCall> calls; |
| 15 | private String accountID; |
| 16 | |
| 17 | public String getAccountID() { |
| 18 | return accountID; |
| 19 | } |
| 20 | |
| 21 | public void setAccountID(String accountID) { |
| 22 | this.accountID = accountID; |
| 23 | } |
| 24 | |
| 25 | public NavigableMap<Long, HistoryCall> getCalls() { |
| 26 | return calls; |
| 27 | } |
| 28 | |
| 29 | public HistoryEntry(String account, CallContact c, long call_start, long call_end, String number) { |
| 30 | contact = c; |
| 31 | calls = new TreeMap<Long, HistoryEntry.HistoryCall>(); |
| 32 | calls.put(call_start, new HistoryCall(call_start, call_end, number)); |
| 33 | accountID = account; |
| 34 | } |
| 35 | |
| 36 | public static class HistoryCall { |
| 37 | long call_start; |
| 38 | long call_end; |
| 39 | String number; |
| 40 | |
| 41 | public HistoryCall(long start, long end, String n) { |
| 42 | call_start = start; |
| 43 | call_end = end; |
| 44 | number = n; |
| 45 | } |
| 46 | |
| 47 | public String getDate(String format) { |
| 48 | Calendar cal = Calendar.getInstance(); |
| 49 | TimeZone tz = cal.getTimeZone(); |
| 50 | SimpleDateFormat objFormatter = new SimpleDateFormat(format, Locale.CANADA); |
| 51 | objFormatter.setTimeZone(tz); |
| 52 | |
| 53 | Calendar objCalendar = Calendar.getInstance(tz); |
| 54 | objCalendar.setTimeInMillis(call_start * 1000); |
| 55 | String result = objFormatter.format(objCalendar.getTime()); |
| 56 | objCalendar.clear(); |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | public String getDurationString() { |
| 61 | long duration = call_end - call_start; |
| 62 | if (duration < 60) |
| 63 | return duration + "s"; |
| 64 | |
| 65 | return duration / 60 + "min"; |
| 66 | |
| 67 | } |
| 68 | |
| 69 | public long getDuration() { |
| 70 | return call_end - call_start; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | public CallContact getContact() { |
| 77 | return contact; |
| 78 | } |
| 79 | |
| 80 | public void setContact(CallContact contact) { |
| 81 | this.contact = contact; |
| 82 | } |
| 83 | |
| 84 | public void addHistoryCall(HistoryCall historyCall) { |
| 85 | calls.put(historyCall.call_start, historyCall); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | public String getNumber() { |
| 90 | return calls.lastEntry().getValue().number; |
| 91 | } |
| 92 | |
| 93 | public String getTotalDuration() { |
| 94 | int duration = 0; |
| 95 | ArrayList<HistoryCall> all_calls = new ArrayList<HistoryEntry.HistoryCall>(calls.values()); |
| 96 | for(int i = 0 ; i < all_calls.size() ; ++i){ |
| 97 | duration += all_calls.get(i).getDuration(); |
| 98 | } |
| 99 | |
| 100 | if (duration < 60) |
| 101 | return duration + "s"; |
| 102 | |
| 103 | return duration / 60 + "min"; |
| 104 | } |
| 105 | } |