Phone.java revision 1cf9b6bec12c027a0d551540a6e01f3ac2d0a9d4
1/*
2 * Copyright (C) 2013 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 android.telecom;
18
19import android.util.ArrayMap;
20
21import java.util.Collections;
22import java.util.List;
23import java.util.Map;
24import java.util.Objects;
25import java.util.concurrent.CopyOnWriteArrayList;
26
27/**
28 * A unified virtual device providing a means of voice (and other) communication on a device.
29 */
30public final class Phone {
31
32    public abstract static class Listener {
33        /**
34         * Called when the audio state changes.
35         *
36         * @param phone The {@code Phone} calling this method.
37         * @param audioState The new {@link AudioState}.
38         */
39        public void onAudioStateChanged(Phone phone, AudioState audioState) { }
40
41        /**
42         * Called to bring the in-call screen to the foreground. The in-call experience should
43         * respond immediately by coming to the foreground to inform the user of the state of
44         * ongoing {@code Call}s.
45         *
46         * @param phone The {@code Phone} calling this method.
47         * @param showDialpad If true, put up the dialpad when the screen is shown.
48         */
49        public void onBringToForeground(Phone phone, boolean showDialpad) { }
50
51        /**
52         * Called when a {@code Call} has been added to this in-call session. The in-call user
53         * experience should add necessary state listeners to the specified {@code Call} and
54         * immediately start to show the user information about the existence
55         * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
56         * include this {@code Call}.
57         *
58         * @param phone The {@code Phone} calling this method.
59         * @param call A newly added {@code Call}.
60         */
61        public void onCallAdded(Phone phone, Call call) { }
62
63        /**
64         * Called when a {@code Call} has been removed from this in-call session. The in-call user
65         * experience should remove any state listeners from the specified {@code Call} and
66         * immediately stop displaying any information about this {@code Call}.
67         * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
68         *
69         * @param phone The {@code Phone} calling this method.
70         * @param call A newly removed {@code Call}.
71         */
72        public void onCallRemoved(Phone phone, Call call) { }
73
74        /**
75         * Called when the {@code Phone} ability to add more calls changes.  If the phone cannot
76         * support more calls then {@code canAddCall} is set to {@code false}.  If it can, then it
77         * is set to {@code true}.
78         *
79         * @param phone The {@code Phone} calling this method.
80         * @param canAddCall Indicates whether an additional call can be added.
81         */
82        public void onCanAddCallChanged(Phone phone, boolean canAddCall) { }
83    }
84
85    // A Map allows us to track each Call by its Telecom-specified call ID
86    private final Map<String, Call> mCallByTelecomCallId = new ArrayMap<>();
87
88    // A List allows us to keep the Calls in a stable iteration order so that casually developed
89    // user interface components do not incur any spurious jank
90    private final List<Call> mCalls = new CopyOnWriteArrayList<>();
91
92    // An unmodifiable view of the above List can be safely shared with subclass implementations
93    private final List<Call> mUnmodifiableCalls = Collections.unmodifiableList(mCalls);
94
95    private final InCallAdapter mInCallAdapter;
96
97    private AudioState mAudioState;
98
99    private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
100
101    private boolean mCanAddCall = true;
102
103    /** {@hide} */
104    Phone(InCallAdapter adapter) {
105        mInCallAdapter = adapter;
106    }
107
108    /** {@hide} */
109    final void internalAddCall(ParcelableCall parcelableCall) {
110        Call call = new Call(this, parcelableCall.getId(), mInCallAdapter);
111        mCallByTelecomCallId.put(parcelableCall.getId(), call);
112        mCalls.add(call);
113        checkCallTree(parcelableCall);
114        call.internalUpdate(parcelableCall, mCallByTelecomCallId);
115        fireCallAdded(call);
116     }
117
118    /** {@hide} */
119    final void internalRemoveCall(Call call) {
120        mCallByTelecomCallId.remove(call.internalGetCallId());
121        mCalls.remove(call);
122        fireCallRemoved(call);
123    }
124
125    /** {@hide} */
126    final void internalUpdateCall(ParcelableCall parcelableCall) {
127         Call call = mCallByTelecomCallId.get(parcelableCall.getId());
128         if (call != null) {
129             checkCallTree(parcelableCall);
130             call.internalUpdate(parcelableCall, mCallByTelecomCallId);
131         }
132     }
133
134    /** {@hide} */
135    final void internalSetPostDialWait(String telecomId, String remaining) {
136        Call call = mCallByTelecomCallId.get(telecomId);
137        if (call != null) {
138            call.internalSetPostDialWait(remaining);
139        }
140    }
141
142    /** {@hide} */
143    final void internalAudioStateChanged(AudioState audioState) {
144        if (!Objects.equals(mAudioState, audioState)) {
145            mAudioState = audioState;
146            fireAudioStateChanged(audioState);
147        }
148    }
149
150    /** {@hide} */
151    final Call internalGetCallByTelecomId(String telecomId) {
152        return mCallByTelecomCallId.get(telecomId);
153    }
154
155    /** {@hide} */
156    final void internalBringToForeground(boolean showDialpad) {
157        fireBringToForeground(showDialpad);
158    }
159
160    /** {@hide} */
161    final void internalSetCanAddCall(boolean canAddCall) {
162        if (mCanAddCall != canAddCall) {
163            mCanAddCall = canAddCall;
164            fireCanAddCallChanged(canAddCall);
165        }
166    }
167
168    /**
169     * Called to destroy the phone and cleanup any lingering calls.
170     * @hide
171     */
172    final void destroy() {
173        for (Call call : mCalls) {
174            if (call.getState() != Call.STATE_DISCONNECTED) {
175                call.internalSetDisconnected();
176            }
177        }
178    }
179
180    /**
181     * Adds a listener to this {@code Phone}.
182     *
183     * @param listener A {@code Listener} object.
184     */
185    public final void addListener(Listener listener) {
186        mListeners.add(listener);
187    }
188
189    /**
190     * Removes a listener from this {@code Phone}.
191     *
192     * @param listener A {@code Listener} object.
193     */
194    public final void removeListener(Listener listener) {
195        if (listener != null) {
196            mListeners.remove(listener);
197        }
198    }
199
200    /**
201     * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
202     *
203     * @return A list of the relevant {@code Call}s.
204     */
205    public final List<Call> getCalls() {
206        return mUnmodifiableCalls;
207    }
208
209    /**
210     * Returns if the {@code Phone} can support additional calls.
211     *
212     * @return Whether the phone supports adding more calls.
213     */
214    public final boolean canAddCall() {
215        return mCanAddCall;
216    }
217
218    /**
219     * Sets the microphone mute state. When this request is honored, there will be change to
220     * the {@link #getAudioState()}.
221     *
222     * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
223     */
224    public final void setMuted(boolean state) {
225        mInCallAdapter.mute(state);
226    }
227
228    /**
229     * Sets the audio route (speaker, bluetooth, etc...).  When this request is honored, there will
230     * be change to the {@link #getAudioState()}.
231     *
232     * @param route The audio route to use.
233     */
234    public final void setAudioRoute(int route) {
235        mInCallAdapter.setAudioRoute(route);
236    }
237
238    /**
239     * Turns the proximity sensor on. When this request is made, the proximity sensor will
240     * become active, and the touch screen and display will be turned off when the user's face
241     * is detected to be in close proximity to the screen. This operation is a no-op on devices
242     * that do not have a proximity sensor.
243     *
244     * @hide
245     */
246    public final void setProximitySensorOn() {
247        mInCallAdapter.turnProximitySensorOn();
248    }
249
250    /**
251     * Turns the proximity sensor off. When this request is made, the proximity sensor will
252     * become inactive, and no longer affect the touch screen and display. This operation is a
253     * no-op on devices that do not have a proximity sensor.
254     *
255     * @param screenOnImmediately If true, the screen will be turned on immediately if it was
256     * previously off. Otherwise, the screen will only be turned on after the proximity sensor
257     * is no longer triggered.
258     *
259     * @hide
260     */
261    public final void setProximitySensorOff(boolean screenOnImmediately) {
262        mInCallAdapter.turnProximitySensorOff(screenOnImmediately);
263    }
264
265    /**
266     * Obtains the current phone call audio state of the {@code Phone}.
267     *
268     * @return An object encapsulating the audio state.
269     */
270    public final AudioState getAudioState() {
271        return mAudioState;
272    }
273
274    private void fireCallAdded(Call call) {
275        for (Listener listener : mListeners) {
276            listener.onCallAdded(this, call);
277        }
278    }
279
280    private void fireCallRemoved(Call call) {
281        for (Listener listener : mListeners) {
282            listener.onCallRemoved(this, call);
283        }
284    }
285
286    private void fireAudioStateChanged(AudioState audioState) {
287        for (Listener listener : mListeners) {
288            listener.onAudioStateChanged(this, audioState);
289        }
290    }
291
292    private void fireBringToForeground(boolean showDialpad) {
293        for (Listener listener : mListeners) {
294            listener.onBringToForeground(this, showDialpad);
295        }
296    }
297
298    private void fireCanAddCallChanged(boolean canAddCall) {
299        for (Listener listener : mListeners) {
300            listener.onCanAddCallChanged(this, canAddCall);
301        }
302    }
303
304    private void checkCallTree(ParcelableCall parcelableCall) {
305        if (parcelableCall.getParentCallId() != null &&
306                !mCallByTelecomCallId.containsKey(parcelableCall.getParentCallId())) {
307            Log.wtf(this, "ParcelableCall %s has nonexistent parent %s",
308                    parcelableCall.getId(), parcelableCall.getParentCallId());
309        }
310        if (parcelableCall.getChildCallIds() != null) {
311            for (int i = 0; i < parcelableCall.getChildCallIds().size(); i++) {
312                if (!mCallByTelecomCallId.containsKey(parcelableCall.getChildCallIds().get(i))) {
313                    Log.wtf(this, "ParcelableCall %s has nonexistent child %s",
314                            parcelableCall.getId(), parcelableCall.getChildCallIds().get(i));
315                }
316            }
317        }
318    }
319}
320