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