SipConnectionBase.java revision b7b7a62112b79571adf74372c5f5366fd62d0031
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 PostDialState getPostDialState() {
145        if (VDBG) log("getPostDialState: ret=" + mPostDialState);
146        return mPostDialState;
147    }
148
149    @Override
150    public void proceedAfterWaitChar() {
151        if (DBG) log("proceedAfterWaitChar: ignore");
152    }
153
154    @Override
155    public void proceedAfterWildChar(String str) {
156        if (DBG) log("proceedAfterWildChar: ignore");
157    }
158
159    @Override
160    public void cancelPostDial() {
161        if (DBG) log("cancelPostDial: ignore");
162    }
163
164    protected abstract Phone getPhone();
165
166    @Override
167    public String getRemainingPostDialString() {
168        if (mPostDialState == PostDialState.CANCELLED
169            || mPostDialState == PostDialState.COMPLETE
170            || mPostDialString == null
171            || mPostDialString.length() <= mNextPostDialChar) {
172            if (DBG) log("getRemaingPostDialString: ret empty string");
173            return "";
174        }
175
176        return mPostDialString.substring(mNextPostDialChar);
177    }
178
179    private void log(String msg) {
180        Rlog.d(LOG_TAG, msg);
181    }
182
183    @Override
184    public int getNumberPresentation() {
185        // TODO: add PRESENTATION_URL
186        if (VDBG) log("getNumberPresentation: ret=PRESENTATION_ALLOWED");
187        return PhoneConstants.PRESENTATION_ALLOWED;
188    }
189
190    @Override
191    public UUSInfo getUUSInfo() {
192        // FIXME: what's this for SIP?
193        if (VDBG) log("getUUSInfo: ? ret=null");
194        return null;
195    }
196
197    @Override
198    public int getPreciseDisconnectCause() {
199        return 0;
200    }
201}
202