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