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