1/*
2 * Copyright (C) 2006 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.gsm;
18
19import com.android.internal.telephony.Call;
20import com.android.internal.telephony.CallStateException;
21import com.android.internal.telephony.Connection;
22import com.android.internal.telephony.DriverCall;
23import com.android.internal.telephony.Phone;
24
25import java.util.List;
26
27/**
28 * {@hide}
29 */
30class GsmCall extends Call {
31    /*************************** Instance Variables **************************/
32
33    /*package*/ GsmCallTracker mOwner;
34
35
36    /***************************** Class Methods *****************************/
37
38    static State
39    stateFromDCState (DriverCall.State dcState) {
40        switch (dcState) {
41            case ACTIVE:        return State.ACTIVE;
42            case HOLDING:       return State.HOLDING;
43            case DIALING:       return State.DIALING;
44            case ALERTING:      return State.ALERTING;
45            case INCOMING:      return State.INCOMING;
46            case WAITING:       return State.WAITING;
47            default:            throw new RuntimeException ("illegal call state:" + dcState);
48        }
49    }
50
51
52    /****************************** Constructors *****************************/
53    /*package*/
54    GsmCall (GsmCallTracker owner) {
55        mOwner = owner;
56    }
57
58    public void dispose() {
59    }
60
61    /************************** Overridden from Call *************************/
62
63    @Override
64    public List<Connection>
65    getConnections() {
66        // FIXME should return Collections.unmodifiableList();
67        return mConnections;
68    }
69
70    @Override
71    public Phone
72    getPhone() {
73        return mOwner.mPhone;
74    }
75
76    @Override
77    public boolean
78    isMultiparty() {
79        return mConnections.size() > 1;
80    }
81
82    /** Please note: if this is the foreground call and a
83     *  background call exists, the background call will be resumed
84     *  because an AT+CHLD=1 will be sent
85     */
86    @Override
87    public void
88    hangup() throws CallStateException {
89        mOwner.hangup(this);
90    }
91
92    @Override
93    public String
94    toString() {
95        return mState.toString();
96    }
97
98    //***** Called from GsmConnection
99
100    /*package*/ void
101    attach(Connection conn, DriverCall dc) {
102        mConnections.add(conn);
103
104        mState = stateFromDCState (dc.state);
105    }
106
107    /*package*/ void
108    attachFake(Connection conn, State state) {
109        mConnections.add(conn);
110
111        mState = state;
112    }
113
114    /**
115     * Called by GsmConnection when it has disconnected
116     */
117    boolean
118    connectionDisconnected(GsmConnection conn) {
119        if (mState != State.DISCONNECTED) {
120            /* If only disconnected connections remain, we are disconnected*/
121
122            boolean hasOnlyDisconnectedConnections = true;
123
124            for (int i = 0, s = mConnections.size()  ; i < s; i ++) {
125                if (mConnections.get(i).getState()
126                    != State.DISCONNECTED
127                ) {
128                    hasOnlyDisconnectedConnections = false;
129                    break;
130                }
131            }
132
133            if (hasOnlyDisconnectedConnections) {
134                mState = State.DISCONNECTED;
135                return true;
136            }
137        }
138
139        return false;
140    }
141
142    /*package*/ void
143    detach(GsmConnection conn) {
144        mConnections.remove(conn);
145
146        if (mConnections.size() == 0) {
147            mState = State.IDLE;
148        }
149    }
150
151    /*package*/ boolean
152    update (GsmConnection conn, DriverCall dc) {
153        State newState;
154        boolean changed = false;
155
156        newState = stateFromDCState(dc.state);
157
158        if (newState != mState) {
159            mState = newState;
160            changed = true;
161        }
162
163        return changed;
164    }
165
166    /**
167     * @return true if there's no space in this call for additional
168     * connections to be added via "conference"
169     */
170    /*package*/ boolean
171    isFull() {
172        return mConnections.size() == GsmCallTracker.MAX_CONNECTIONS_PER_CALL;
173    }
174
175    //***** Called from GsmCallTracker
176
177
178    /**
179     * Called when this Call is being hung up locally (eg, user pressed "end")
180     * Note that at this point, the hangup request has been dispatched to the radio
181     * but no response has yet been received so update() has not yet been called
182     */
183    void
184    onHangupLocal() {
185        for (int i = 0, s = mConnections.size()
186                ; i < s; i++
187        ) {
188            GsmConnection cn = (GsmConnection)mConnections.get(i);
189
190            cn.onHangupLocal();
191        }
192        mState = State.DISCONNECTING;
193    }
194
195    /**
196     * Called when it's time to clean up disconnected Connection objects
197     */
198    void
199    clearDisconnected() {
200        for (int i = mConnections.size() - 1 ; i >= 0 ; i--) {
201            GsmConnection cn = (GsmConnection)mConnections.get(i);
202
203            if (cn.getState() == State.DISCONNECTED) {
204                mConnections.remove(i);
205            }
206        }
207
208        if (mConnections.size() == 0) {
209            mState = State.IDLE;
210        }
211    }
212}
213
214