1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.telephony.sip;
18
19import com.android.internal.telephony.Call;
20import com.android.internal.telephony.Connection;
21import com.android.internal.telephony.Phone;
22import com.android.internal.telephony.PhoneConstants;
23import com.android.internal.telephony.UUSInfo;
24
25import android.os.SystemClock;
26import android.telephony.DisconnectCause;
27import android.telephony.Rlog;
28import android.telephony.PhoneNumberUtils;
29
30abstract class SipConnectionBase extends Connection {
31    private static final String LOG_TAG = "SipConnBase";
32    private static final boolean DBG = true;
33    private static final boolean VDBG = false; // STOPSHIP if true
34
35    /*
36     * These time/timespan values are based on System.currentTimeMillis(),
37     * i.e., "wall clock" time.
38     */
39    private long mCreateTime;
40    private long mConnectTime;
41    private long mDisconnectTime;
42
43    /*
44     * These time/timespan values are based on SystemClock.elapsedRealTime(),
45     * i.e., time since boot.  They are appropriate for comparison and
46     * calculating deltas.
47     */
48    private long mConnectTimeReal;
49    private long mDuration = -1L;
50    private long mHoldingStartTime;  // The time when the Connection last transitioned
51                            // into HOLDING
52
53    SipConnectionBase(String dialString) {
54        super(PhoneConstants.PHONE_TYPE_SIP);
55        if (DBG) log("SipConnectionBase: ctor dialString=" + SipPhone.hidePii(dialString));
56        mPostDialString = PhoneNumberUtils.extractPostDialPortion(dialString);
57
58        mCreateTime = System.currentTimeMillis();
59    }
60
61    protected void setState(Call.State state) {
62        if (DBG) log("setState: state=" + state);
63        switch (state) {
64            case ACTIVE:
65                if (mConnectTime == 0) {
66                    mConnectTimeReal = SystemClock.elapsedRealtime();
67                    mConnectTime = System.currentTimeMillis();
68                }
69                break;
70            case DISCONNECTED:
71                mDuration = getDurationMillis();
72                mDisconnectTime = System.currentTimeMillis();
73                break;
74            case HOLDING:
75                mHoldingStartTime = SystemClock.elapsedRealtime();
76                break;
77            default:
78                // Ignore
79                break;
80        }
81    }
82
83    @Override
84    public long getCreateTime() {
85        if (VDBG) log("getCreateTime: ret=" + mCreateTime);
86        return mCreateTime;
87    }
88
89    @Override
90    public long getConnectTime() {
91        if (VDBG) log("getConnectTime: ret=" + mConnectTime);
92        return mConnectTime;
93    }
94
95    @Override
96    public long getDisconnectTime() {
97        if (VDBG) log("getDisconnectTime: ret=" + mDisconnectTime);
98        return mDisconnectTime;
99    }
100
101    @Override
102    public long getDurationMillis() {
103        long dur;
104        if (mConnectTimeReal == 0) {
105            dur = 0;
106        } else if (mDuration < 0) {
107            dur = SystemClock.elapsedRealtime() - mConnectTimeReal;
108        } else {
109            dur = mDuration;
110        }
111        if (VDBG) log("getDurationMillis: ret=" + dur);
112        return dur;
113    }
114
115    @Override
116    public long getHoldDurationMillis() {
117        long dur;
118        if (getState() != Call.State.HOLDING) {
119            // If not holding, return 0
120            dur = 0;
121        } else {
122            dur = SystemClock.elapsedRealtime() - mHoldingStartTime;
123        }
124        if (VDBG) log("getHoldDurationMillis: ret=" + dur);
125        return dur;
126    }
127
128    void setDisconnectCause(int cause) {
129        if (DBG) log("setDisconnectCause: prev=" + mCause + " new=" + cause);
130        mCause = cause;
131    }
132
133    @Override
134    public String getVendorDisconnectCause() {
135      return null;
136    }
137
138    @Override
139    public void proceedAfterWaitChar() {
140        if (DBG) log("proceedAfterWaitChar: ignore");
141    }
142
143    @Override
144    public void proceedAfterWildChar(String str) {
145        if (DBG) log("proceedAfterWildChar: ignore");
146    }
147
148    @Override
149    public void cancelPostDial() {
150        if (DBG) log("cancelPostDial: ignore");
151    }
152
153    protected abstract Phone getPhone();
154
155    private void log(String msg) {
156        Rlog.d(LOG_TAG, msg);
157    }
158
159    @Override
160    public int getNumberPresentation() {
161        // TODO: add PRESENTATION_URL
162        if (VDBG) log("getNumberPresentation: ret=PRESENTATION_ALLOWED");
163        return PhoneConstants.PRESENTATION_ALLOWED;
164    }
165
166    @Override
167    public UUSInfo getUUSInfo() {
168        // FIXME: what's this for SIP?
169        if (VDBG) log("getUUSInfo: ? ret=null");
170        return null;
171    }
172
173    @Override
174    public int getPreciseDisconnectCause() {
175        return 0;
176    }
177
178    @Override
179    public long getHoldingStartTime() {
180        return mHoldingStartTime;
181    }
182
183    @Override
184    public long getConnectTimeReal() {
185        return mConnectTimeReal;
186    }
187
188    @Override
189    public Connection getOrigConnection() {
190        return null;
191    }
192
193    @Override
194    public boolean isMultiparty() {
195        return false;
196    }
197}
198