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.UUSInfo;
23
24import android.net.sip.SipAudioCall;
25import android.os.SystemClock;
26import android.util.Log;
27import android.telephony.PhoneNumberUtils;
28
29abstract class SipConnectionBase extends Connection {
30    private static final String LOG_TAG = "SIP_CONN";
31
32    private SipAudioCall mSipAudioCall;
33
34    private String dialString;          // outgoing calls only
35    private String postDialString;      // outgoing calls only
36    private int nextPostDialChar;       // index into postDialString
37    private boolean isIncoming;
38
39    /*
40     * These time/timespan values are based on System.currentTimeMillis(),
41     * i.e., "wall clock" time.
42     */
43    private long createTime;
44    private long connectTime;
45    private long disconnectTime;
46
47    /*
48     * These time/timespan values are based on SystemClock.elapsedRealTime(),
49     * i.e., time since boot.  They are appropriate for comparison and
50     * calculating deltas.
51     */
52    private long connectTimeReal;
53    private long duration = -1L;
54    private long holdingStartTime;  // The time when the Connection last transitioned
55                            // into HOLDING
56
57    private DisconnectCause mCause = DisconnectCause.NOT_DISCONNECTED;
58    private PostDialState postDialState = PostDialState.NOT_STARTED;
59
60    SipConnectionBase(String dialString) {
61        this.dialString = dialString;
62
63        postDialString = PhoneNumberUtils.extractPostDialPortion(dialString);
64
65        isIncoming = false;
66        createTime = System.currentTimeMillis();
67    }
68
69    protected void setState(Call.State state) {
70        switch (state) {
71            case ACTIVE:
72                if (connectTime == 0) {
73                    connectTimeReal = SystemClock.elapsedRealtime();
74                    connectTime = System.currentTimeMillis();
75                }
76                break;
77            case DISCONNECTED:
78                duration = getDurationMillis();
79                disconnectTime = System.currentTimeMillis();
80                break;
81            case HOLDING:
82                holdingStartTime = SystemClock.elapsedRealtime();
83                break;
84        }
85    }
86
87    @Override
88    public long getCreateTime() {
89        return createTime;
90    }
91
92    @Override
93    public long getConnectTime() {
94        return connectTime;
95    }
96
97    @Override
98    public long getDisconnectTime() {
99        return disconnectTime;
100    }
101
102    @Override
103    public long getDurationMillis() {
104        if (connectTimeReal == 0) {
105            return 0;
106        } else if (duration < 0) {
107            return SystemClock.elapsedRealtime() - connectTimeReal;
108        } else {
109            return duration;
110        }
111    }
112
113    @Override
114    public long getHoldDurationMillis() {
115        if (getState() != Call.State.HOLDING) {
116            // If not holding, return 0
117            return 0;
118        } else {
119            return SystemClock.elapsedRealtime() - holdingStartTime;
120        }
121    }
122
123    @Override
124    public DisconnectCause getDisconnectCause() {
125        return mCause;
126    }
127
128    void setDisconnectCause(DisconnectCause cause) {
129        mCause = cause;
130    }
131
132    @Override
133    public PostDialState getPostDialState() {
134        return postDialState;
135    }
136
137    @Override
138    public void proceedAfterWaitChar() {
139        // TODO
140    }
141
142    @Override
143    public void proceedAfterWildChar(String str) {
144        // TODO
145    }
146
147    @Override
148    public void cancelPostDial() {
149        // TODO
150    }
151
152    protected abstract Phone getPhone();
153
154    @Override
155    public String getRemainingPostDialString() {
156        if (postDialState == PostDialState.CANCELLED
157            || postDialState == PostDialState.COMPLETE
158            || postDialString == null
159            || postDialString.length() <= nextPostDialChar) {
160            return "";
161        }
162
163        return postDialString.substring(nextPostDialChar);
164    }
165
166    private void log(String msg) {
167        Log.d(LOG_TAG, "[SipConn] " + msg);
168    }
169
170    @Override
171    public int getNumberPresentation() {
172        // TODO: add PRESENTATION_URL
173        return Connection.PRESENTATION_ALLOWED;
174    }
175
176    @Override
177    public UUSInfo getUUSInfo() {
178        // FIXME: what's this for SIP?
179        return null;
180    }
181}
182