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