OSDN Git Service

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