1/*
2 * Copyright (C) 2015 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;
18
19import java.util.List;
20
21/**
22 * {@hide}
23 */
24public class GsmCdmaCall extends Call {
25    /*************************** Instance Variables **************************/
26
27    /*package*/ GsmCdmaCallTracker mOwner;
28
29    /****************************** Constructors *****************************/
30    /*package*/
31    public GsmCdmaCall (GsmCdmaCallTracker owner) {
32        mOwner = owner;
33    }
34
35    /************************** Overridden from Call *************************/
36
37    @Override
38    public List<Connection> getConnections() {
39        // FIXME should return Collections.unmodifiableList();
40        return mConnections;
41    }
42
43    @Override
44    public Phone getPhone() {
45        return mOwner.getPhone();
46    }
47
48    @Override
49    public boolean isMultiparty() {
50        return mConnections.size() > 1;
51    }
52
53    /** Please note: if this is the foreground call and a
54     *  background call exists, the background call will be resumed
55     *  because an AT+CHLD=1 will be sent
56     */
57    @Override
58    public void hangup() throws CallStateException {
59        mOwner.hangup(this);
60    }
61
62    @Override
63    public String toString() {
64        return mState.toString();
65    }
66
67    //***** Called from GsmCdmaConnection
68
69    public void attach(Connection conn, DriverCall dc) {
70        mConnections.add(conn);
71
72        mState = stateFromDCState (dc.state);
73    }
74
75    public void attachFake(Connection conn, State state) {
76        mConnections.add(conn);
77
78        mState = state;
79    }
80
81    /**
82     * Called by GsmCdmaConnection when it has disconnected
83     */
84    public boolean connectionDisconnected(GsmCdmaConnection conn) {
85        if (mState != State.DISCONNECTED) {
86            /* If only disconnected connections remain, we are disconnected*/
87
88            boolean hasOnlyDisconnectedConnections = true;
89
90            for (int i = 0, s = mConnections.size(); i < s; i ++) {
91                if (mConnections.get(i).getState() != State.DISCONNECTED) {
92                    hasOnlyDisconnectedConnections = false;
93                    break;
94                }
95            }
96
97            if (hasOnlyDisconnectedConnections) {
98                mState = State.DISCONNECTED;
99                return true;
100            }
101        }
102
103        return false;
104    }
105
106    public void detach(GsmCdmaConnection conn) {
107        mConnections.remove(conn);
108
109        if (mConnections.size() == 0) {
110            mState = State.IDLE;
111        }
112    }
113
114    /*package*/ boolean update (GsmCdmaConnection conn, DriverCall dc) {
115        State newState;
116        boolean changed = false;
117
118        newState = stateFromDCState(dc.state);
119
120        if (newState != mState) {
121            mState = newState;
122            changed = true;
123        }
124
125        return changed;
126    }
127
128    /**
129     * @return true if there's no space in this call for additional
130     * connections to be added via "conference"
131     */
132    /*package*/ boolean isFull() {
133        return mConnections.size() == mOwner.getMaxConnectionsPerCall();
134    }
135
136    //***** Called from GsmCdmaCallTracker
137
138
139    /**
140     * Called when this Call is being hung up locally (eg, user pressed "end")
141     * Note that at this point, the hangup request has been dispatched to the radio
142     * but no response has yet been received so update() has not yet been called
143     */
144    void onHangupLocal() {
145        for (int i = 0, s = mConnections.size(); i < s; i++) {
146            GsmCdmaConnection cn = (GsmCdmaConnection)mConnections.get(i);
147
148            cn.onHangupLocal();
149        }
150        mState = State.DISCONNECTING;
151    }
152}