/* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.internal.telephony.gsm; import android.content.Context; import android.os.AsyncResult; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.PowerManager; import android.os.Registrant; import android.os.SystemClock; import android.util.Log; import android.telephony.PhoneNumberUtils; import android.telephony.ServiceState; import android.text.TextUtils; import com.android.internal.telephony.*; import com.android.internal.telephony.IccCardApplicationStatus.AppState; import com.android.internal.telephony.uicc.UiccController; /** * {@hide} */ public class GsmConnection extends Connection { static final String LOG_TAG = "GSM"; //***** Instance Variables GsmCallTracker owner; GsmCall parent; String address; // MAY BE NULL!!! String dialString; // outgoing calls only String postDialString; // outgoing calls only boolean isIncoming; boolean disconnected; int index; // index in GsmCallTracker.connections[], -1 if unassigned // The GSM index is 1 + this /* * These time/timespan values are based on System.currentTimeMillis(), * i.e., "wall clock" time. */ long createTime; long connectTime; long disconnectTime; /* * These time/timespan values are based on SystemClock.elapsedRealTime(), * i.e., time since boot. They are appropriate for comparison and * calculating deltas. */ long connectTimeReal; long duration; long holdingStartTime; // The time when the Connection last transitioned // into HOLDING int nextPostDialChar; // index into postDialString DisconnectCause cause = DisconnectCause.NOT_DISCONNECTED; PostDialState postDialState = PostDialState.NOT_STARTED; int numberPresentation = PhoneConstants.PRESENTATION_ALLOWED; UUSInfo uusInfo; Handler h; private PowerManager.WakeLock mPartialWakeLock; //***** Event Constants static final int EVENT_DTMF_DONE = 1; static final int EVENT_PAUSE_DONE = 2; static final int EVENT_NEXT_POST_DIAL = 3; static final int EVENT_WAKE_LOCK_TIMEOUT = 4; //***** Constants static final int PAUSE_DELAY_FIRST_MILLIS = 100; static final int PAUSE_DELAY_MILLIS = 3 * 1000; static final int WAKE_LOCK_TIMEOUT_MILLIS = 60*1000; //***** Inner Classes class MyHandler extends Handler { MyHandler(Looper l) {super(l);} public void handleMessage(Message msg) { switch (msg.what) { case EVENT_NEXT_POST_DIAL: case EVENT_DTMF_DONE: case EVENT_PAUSE_DONE: processNextPostDialChar(); break; case EVENT_WAKE_LOCK_TIMEOUT: releaseWakeLock(); break; } } } //***** Constructors /** This is probably an MT call that we first saw in a CLCC response */ /*package*/ GsmConnection (Context context, DriverCall dc, GsmCallTracker ct, int index) { createWakeLock(context); acquireWakeLock(); owner = ct; h = new MyHandler(owner.getLooper()); address = dc.number; isIncoming = dc.isMT; createTime = System.currentTimeMillis(); cnapName = dc.name; cnapNamePresentation = dc.namePresentation; numberPresentation = dc.numberPresentation; uusInfo = dc.uusInfo; this.index = index; parent = parentFromDCState (dc.state); parent.attach(this, dc); } /** This is an MO call, created when dialing */ /*package*/ GsmConnection (Context context, String dialString, GsmCallTracker ct, GsmCall parent) { createWakeLock(context); acquireWakeLock(); owner = ct; h = new MyHandler(owner.getLooper()); this.dialString = dialString; this.address = PhoneNumberUtils.extractNetworkPortionAlt(dialString); this.postDialString = PhoneNumberUtils.extractPostDialPortion(dialString); index = -1; isIncoming = false; cnapName = null; cnapNamePresentation = PhoneConstants.PRESENTATION_ALLOWED; numberPresentation = PhoneConstants.PRESENTATION_ALLOWED; createTime = System.currentTimeMillis(); this.parent = parent; parent.attachFake(this, GsmCall.State.DIALING); } public void dispose() { } static boolean equalsHandlesNulls (Object a, Object b) { return (a == null) ? (b == null) : a.equals (b); } /*package*/ boolean compareTo(DriverCall c) { // On mobile originated (MO) calls, the phone number may have changed // due to a SIM Toolkit call control modification. // // We assume we know when MO calls are created (since we created them) // and therefore don't need to compare the phone number anyway. if (! (isIncoming || c.isMT)) return true; // ... but we can compare phone numbers on MT calls, and we have // no control over when they begin, so we might as well String cAddress = PhoneNumberUtils.stringFromStringAndTOA(c.number, c.TOA); return isIncoming == c.isMT && equalsHandlesNulls(address, cAddress); } public String getAddress() { return address; } public GsmCall getCall() { return parent; } public long getCreateTime() { return createTime; } public long getConnectTime() { return connectTime; } public long getDisconnectTime() { return disconnectTime; } public long getDurationMillis() { if (connectTimeReal == 0) { return 0; } else if (duration == 0) { return SystemClock.elapsedRealtime() - connectTimeReal; } else { return duration; } } public long getHoldDurationMillis() { if (getState() != GsmCall.State.HOLDING) { // If not holding, return 0 return 0; } else { return SystemClock.elapsedRealtime() - holdingStartTime; } } public DisconnectCause getDisconnectCause() { return cause; } public boolean isIncoming() { return isIncoming; } public GsmCall.State getState() { if (disconnected) { return GsmCall.State.DISCONNECTED; } else { return super.getState(); } } public void hangup() throws CallStateException { if (!disconnected) { owner.hangup(this); } else { throw new CallStateException ("disconnected"); } } public void separate() throws CallStateException { if (!disconnected) { owner.separate(this); } else { throw new CallStateException ("disconnected"); } } public PostDialState getPostDialState() { return postDialState; } public void proceedAfterWaitChar() { if (postDialState != PostDialState.WAIT) { Log.w(LOG_TAG, "GsmConnection.proceedAfterWaitChar(): Expected " + "getPostDialState() to be WAIT but was " + postDialState); return; } setPostDialState(PostDialState.STARTED); processNextPostDialChar(); } public void proceedAfterWildChar(String str) { if (postDialState != PostDialState.WILD) { Log.w(LOG_TAG, "GsmConnection.proceedAfterWaitChar(): Expected " + "getPostDialState() to be WILD but was " + postDialState); return; } setPostDialState(PostDialState.STARTED); if (false) { boolean playedTone = false; int len = (str != null ? str.length() : 0); for (int i=0; i