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 
17 package com.android.internal.telephony;
18 
19 import android.telecom.ConferenceParticipant;
20 
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 import android.telephony.Rlog;
25 
26 /**
27  * {@hide}
28  */
29 public abstract class Call {
30     protected final String LOG_TAG = "Call";
31 
32     /* Enums */
33 
34     public enum State {
35         IDLE, ACTIVE, HOLDING, DIALING, ALERTING, INCOMING, WAITING, DISCONNECTED, DISCONNECTING;
36 
isAlive()37         public boolean isAlive() {
38             return !(this == IDLE || this == DISCONNECTED || this == DISCONNECTING);
39         }
40 
isRinging()41         public boolean isRinging() {
42             return this == INCOMING || this == WAITING;
43         }
44 
isDialing()45         public boolean isDialing() {
46             return this == DIALING || this == ALERTING;
47         }
48     }
49 
50     public static State
stateFromDCState(DriverCall.State dcState)51     stateFromDCState (DriverCall.State dcState) {
52         switch (dcState) {
53             case ACTIVE:        return State.ACTIVE;
54             case HOLDING:       return State.HOLDING;
55             case DIALING:       return State.DIALING;
56             case ALERTING:      return State.ALERTING;
57             case INCOMING:      return State.INCOMING;
58             case WAITING:       return State.WAITING;
59             default:            throw new RuntimeException ("illegal call state:" + dcState);
60         }
61     }
62 
63     public enum SrvccState {
64         NONE, STARTED, COMPLETED, FAILED, CANCELED;
65     }
66 
67     /* Instance Variables */
68 
69     public State mState = State.IDLE;
70 
71     public ArrayList<Connection> mConnections = new ArrayList<Connection>();
72 
73     // Flag to indicate if the current calling/caller information
74     // is accurate. If false the information is known to be accurate.
75     //
76     // For CDMA, during call waiting/3 way, there is no network response
77     // if call waiting is answered, network timed out, dropped, 3 way
78     // merged, etc.
79     protected boolean mIsGeneric = false;
80 
81     /* Instance Methods */
82 
83     /** Do not modify the List result!!! This list is not yours to keep
84      *  It will change across event loop iterations            top
85      */
86 
getConnections()87     public abstract List<Connection> getConnections();
getPhone()88     public abstract Phone getPhone();
isMultiparty()89     public abstract boolean isMultiparty();
hangup()90     public abstract void hangup() throws CallStateException;
91 
92 
93     /**
94      * hasConnection
95      *
96      * @param c a Connection object
97      * @return true if the call contains the connection object passed in
98      */
hasConnection(Connection c)99     public boolean hasConnection(Connection c) {
100         return c.getCall() == this;
101     }
102 
103     /**
104      * hasConnections
105      * @return true if the call contains one or more connections
106      */
hasConnections()107     public boolean hasConnections() {
108         List<Connection> connections = getConnections();
109 
110         if (connections == null) {
111             return false;
112         }
113 
114         return connections.size() > 0;
115     }
116 
117     /**
118      * getState
119      * @return state of class call
120      */
getState()121     public State getState() {
122         return mState;
123     }
124 
125     /**
126      * getConferenceParticipants
127      * @return List of conference participants.
128      */
getConferenceParticipants()129     public List<ConferenceParticipant> getConferenceParticipants() {
130         return null;
131     }
132 
133     /**
134      * isIdle
135      *
136      * FIXME rename
137      * @return true if the call contains only disconnected connections (if any)
138      */
isIdle()139     public boolean isIdle() {
140         return !getState().isAlive();
141     }
142 
143     /**
144      * Returns the Connection associated with this Call that was created
145      * first, or null if there are no Connections in this Call
146      */
147     public Connection
getEarliestConnection()148     getEarliestConnection() {
149         List<Connection> l;
150         long time = Long.MAX_VALUE;
151         Connection c;
152         Connection earliest = null;
153 
154         l = getConnections();
155 
156         if (l.size() == 0) {
157             return null;
158         }
159 
160         for (int i = 0, s = l.size() ; i < s ; i++) {
161             c = l.get(i);
162             long t;
163 
164             t = c.getCreateTime();
165 
166             if (t < time) {
167                 earliest = c;
168                 time = t;
169             }
170         }
171 
172         return earliest;
173     }
174 
175     public long
getEarliestCreateTime()176     getEarliestCreateTime() {
177         List<Connection> l;
178         long time = Long.MAX_VALUE;
179 
180         l = getConnections();
181 
182         if (l.size() == 0) {
183             return 0;
184         }
185 
186         for (int i = 0, s = l.size() ; i < s ; i++) {
187             Connection c = l.get(i);
188             long t;
189 
190             t = c.getCreateTime();
191 
192             time = t < time ? t : time;
193         }
194 
195         return time;
196     }
197 
198     public long
199     getEarliestConnectTime() {
200         long time = Long.MAX_VALUE;
201         List<Connection> l = getConnections();
202 
203         if (l.size() == 0) {
204             return 0;
205         }
206 
207         for (int i = 0, s = l.size() ; i < s ; i++) {
208             Connection c = l.get(i);
209             long t;
210 
211             t = c.getConnectTime();
212 
213             time = t < time ? t : time;
214         }
215 
216         return time;
217     }
218 
219 
220     public boolean
221     isDialingOrAlerting() {
222         return getState().isDialing();
223     }
224 
225     public boolean
226     isRinging() {
227         return getState().isRinging();
228     }
229 
230     /**
231      * Returns the Connection associated with this Call that was created
232      * last, or null if there are no Connections in this Call
233      */
234     public Connection
235     getLatestConnection() {
236         List<Connection> l = getConnections();
237         if (l.size() == 0) {
238             return null;
239         }
240 
241         long time = 0;
242         Connection latest = null;
243         for (int i = 0, s = l.size() ; i < s ; i++) {
244             Connection c = l.get(i);
245             long t = c.getCreateTime();
246 
247             if (t > time) {
248                 latest = c;
249                 time = t;
250             }
251         }
252 
253         return latest;
254     }
255 
256     /**
257      * To indicate if the connection information is accurate
258      * or not. false means accurate. Only used for CDMA.
259      */
260     public boolean isGeneric() {
261         return mIsGeneric;
262     }
263 
264     /**
265      * Set the generic instance variable
266      */
267     public void setGeneric(boolean generic) {
268         mIsGeneric = generic;
269     }
270 
271     /**
272      * Hangup call if it is alive
273      */
274     public void hangupIfAlive() {
275         if (getState().isAlive()) {
276             try {
277                 hangup();
278             } catch (CallStateException ex) {
279                 Rlog.w(LOG_TAG, " hangupIfActive: caught " + ex);
280             }
281         }
282     }
283 }
284