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    private String mPostDialString;      // outgoing calls only
36    private int mNextPostDialChar;       // index into postDialString
37    /*
38     * These time/timespan values are based on System.currentTimeMillis(),
39     * i.e., "wall clock" time.
40     */
41    private long mCreateTime;
42    private long mConnectTime;
43    private long mDisconnectTime;
44
45    /*
46     * These time/timespan values are based on SystemClock.elapsedRealTime(),
47     * i.e., time since boot.  They are appropriate for comparison and
48     * calculating deltas.
49     */
50    private long mConnectTimeReal;
51    private long mDuration = -1L;
52    private long mHoldingStartTime;  // The time when the Connection last transitioned
53                            // into HOLDING
54
55    private int mCause = DisconnectCause.NOT_DISCONNECTED;
56    private PostDialState mPostDialState = PostDialState.NOT_STARTED;
57
58    SipConnectionBase(String dialString) {
59        if (DBG) log("SipConnectionBase: ctor dialString=" + dialString);
60        mPostDialString = PhoneNumberUtils.extractPostDialPortion(dialString);
61
62        mCreateTime = System.currentTimeMillis();
63    }
64
65    protected void setState(Call.State state) {
66        if (DBG) log("setState: state=" + state);
67        switch (state) {
68            case ACTIVE:
69                if (mConnectTime == 0) {
70                    mConnectTimeReal = SystemClock.elapsedRealtime();
71                    mConnectTime = System.currentTimeMillis();
72                }
73                break;
74            case DISCONNECTED:
75                mDuration = getDurationMillis();
76                mDisconnectTime = System.currentTimeMillis();
77                break;
78            case HOLDING:
79                mHoldingStartTime = SystemClock.elapsedRealtime();
80                break;
81            default:
82                // Ignore
83                break;
84        }
85    }
86
87    @Override
88    public long getCreateTime() {
89        if (VDBG) log("getCreateTime: ret=" + mCreateTime);
90        return mCreateTime;
91    }
92
93    @Override
94    public long getConnectTime() {
95        if (VDBG) log("getConnectTime: ret=" + mConnectTime);
96        return mConnectTime;
97    }
98
99    @Override
100    public long getDisconnectTime() {
101        if (VDBG) log("getDisconnectTime: ret=" + mDisconnectTime);
102        return mDisconnectTime;
103    }
104
105    @Override
106    public long getDurationMillis() {
107        long dur;
108        if (mConnectTimeReal == 0) {
109            dur = 0;
110        } else if (mDuration < 0) {
111            dur = SystemClock.elapsedRealtime() - mConnectTimeReal;
112        } else {
113            dur = mDuration;
114        }
115        if (VDBG) log("getDurationMillis: ret=" + dur);
116        return dur;
117    }
118
119    @Override
120    public long getHoldDurationMillis() {
121        long dur;
122        if (getState() != Call.State.HOLDING) {
123            // If not holding, return 0
124            dur = 0;
125        } else {
126            dur = SystemClock.elapsedRealtime() - mHoldingStartTime;
127        }
128        if (VDBG) log("getHoldDurationMillis: ret=" + dur);
129        return dur;
130    }
131
132    @Override
133    public int getDisconnectCause() {
134        if (VDBG) log("getDisconnectCause: ret=" + mCause);
135        return mCause;
136    }
137
138    void setDisconnectCause(int cause) {
139        if (DBG) log("setDisconnectCause: prev=" + mCause + " new=" + cause);
140        mCause = cause;
141    }
142
143    @Override
144    public String getVendorDisconnectCause() {
145      return null;
146    }
147
148    @Override
149    public PostDialState getPostDialState() {
150        if (VDBG) log("getPostDialState: ret=" + mPostDialState);
151        return mPostDialState;
152    }
153
154    @Override
155    public void proceedAfterWaitChar() {
156        if (DBG) log("proceedAfterWaitChar: ignore");
157    }
158
159    @Override
160    public void proceedAfterWildChar(String str) {
161        if (DBG) log("proceedAfterWildChar: ignore");
162    }
163
164    @Override
165    public void cancelPostDial() {
166        if (DBG) log("cancelPostDial: ignore");
167    }
168
169    protected abstract Phone getPhone();
170
171    @Override
172    public String getRemainingPostDialString() {
173        if (mPostDialState == PostDialState.CANCELLED
174            || mPostDialState == PostDialState.COMPLETE
175            || mPostDialString == null
176            || mPostDialString.length() <= mNextPostDialChar) {
177            if (DBG) log("getRemaingPostDialString: ret empty string");
178            return "";
179        }
180
181        return mPostDialString.substring(mNextPostDialChar);
182    }
183
184    private void log(String msg) {
185        Rlog.d(LOG_TAG, msg);
186    }
187
188    @Override
189    public int getNumberPresentation() {
190        // TODO: add PRESENTATION_URL
191        if (VDBG) log("getNumberPresentation: ret=PRESENTATION_ALLOWED");
192        return PhoneConstants.PRESENTATION_ALLOWED;
193    }
194
195    @Override
196    public UUSInfo getUUSInfo() {
197        // FIXME: what's this for SIP?
198        if (VDBG) log("getUUSInfo: ? ret=null");
199        return null;
200    }
201
202    @Override
203    public int getPreciseDisconnectCause() {
204        return 0;
205    }
206
207    @Override
208    public long getHoldingStartTime() {
209        return mHoldingStartTime;
210    }
211
212    @Override
213    public long getConnectTimeReal() {
214        return mConnectTimeReal;
215    }
216
217    @Override
218    public Connection getOrigConnection() {
219        return null;
220    }
221
222    @Override
223    public boolean isMultiparty() {
224        return false;
225    }
226}
227