InCallService.java revision d7b4b81274e8a0f885be553ea8e153ea5447798d
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.annotation.SdkConstant;
20import android.annotation.SystemApi;
21import android.app.Service;
22import android.content.Intent;
23import android.os.Handler;
24import android.os.IBinder;
25import android.os.Looper;
26import android.os.Message;
27import android.view.Surface;
28
29import com.android.internal.os.SomeArgs;
30import com.android.internal.telecom.IInCallAdapter;
31import com.android.internal.telecom.IInCallService;
32
33import java.lang.String;
34import java.util.Collections;
35import java.util.List;
36
37/**
38 * This service is implemented by any app that wishes to provide the user-interface for managing
39 * phone calls. Telecom binds to this service while there exists a live (active or incoming) call,
40 * and uses it to notify the in-call app of any live and and recently disconnected calls.
41 */
42public abstract class InCallService extends Service {
43
44    /**
45     * The {@link Intent} that must be declared as handled by the service.
46     */
47    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
48    public static final String SERVICE_INTERFACE = "android.telecom.InCallService";
49
50    private static final int MSG_SET_IN_CALL_ADAPTER = 1;
51    private static final int MSG_ADD_CALL = 2;
52    private static final int MSG_UPDATE_CALL = 3;
53    private static final int MSG_SET_POST_DIAL_WAIT = 4;
54    private static final int MSG_ON_AUDIO_STATE_CHANGED = 5;
55    private static final int MSG_BRING_TO_FOREGROUND = 6;
56    private static final int MSG_ON_CAN_ADD_CALL_CHANGED = 7;
57
58    /** Default Handler used to consolidate binder method calls onto a single thread. */
59    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
60        @Override
61        public void handleMessage(Message msg) {
62            if (mPhone == null && msg.what != MSG_SET_IN_CALL_ADAPTER) {
63                return;
64            }
65
66            switch (msg.what) {
67                case MSG_SET_IN_CALL_ADAPTER:
68                    mPhone = new Phone(new InCallAdapter((IInCallAdapter) msg.obj));
69                    mPhone.addListener(mPhoneListener);
70                    onPhoneCreated(mPhone);
71                    break;
72                case MSG_ADD_CALL:
73                    mPhone.internalAddCall((ParcelableCall) msg.obj);
74                    break;
75                case MSG_UPDATE_CALL:
76                    mPhone.internalUpdateCall((ParcelableCall) msg.obj);
77                    break;
78                case MSG_SET_POST_DIAL_WAIT: {
79                    SomeArgs args = (SomeArgs) msg.obj;
80                    try {
81                        String callId = (String) args.arg1;
82                        String remaining = (String) args.arg2;
83                        mPhone.internalSetPostDialWait(callId, remaining);
84                    } finally {
85                        args.recycle();
86                    }
87                    break;
88                }
89                case MSG_ON_AUDIO_STATE_CHANGED:
90                    mPhone.internalAudioStateChanged((AudioState) msg.obj);
91                    break;
92                case MSG_BRING_TO_FOREGROUND:
93                    mPhone.internalBringToForeground(msg.arg1 == 1);
94                    break;
95                case MSG_ON_CAN_ADD_CALL_CHANGED:
96                    mPhone.internalSetCanAddCall(msg.arg1 == 1);
97                    break;
98                default:
99                    break;
100            }
101        }
102    };
103
104    /** Manages the binder calls so that the implementor does not need to deal with it. */
105    private final class InCallServiceBinder extends IInCallService.Stub {
106        @Override
107        public void setInCallAdapter(IInCallAdapter inCallAdapter) {
108            mHandler.obtainMessage(MSG_SET_IN_CALL_ADAPTER, inCallAdapter).sendToTarget();
109        }
110
111        @Override
112        public void addCall(ParcelableCall call) {
113            mHandler.obtainMessage(MSG_ADD_CALL, call).sendToTarget();
114        }
115
116        @Override
117        public void updateCall(ParcelableCall call) {
118            mHandler.obtainMessage(MSG_UPDATE_CALL, call).sendToTarget();
119        }
120
121        @Override
122        public void setPostDial(String callId, String remaining) {
123            // TODO: Unused
124        }
125
126        @Override
127        public void setPostDialWait(String callId, String remaining) {
128            SomeArgs args = SomeArgs.obtain();
129            args.arg1 = callId;
130            args.arg2 = remaining;
131            mHandler.obtainMessage(MSG_SET_POST_DIAL_WAIT, args).sendToTarget();
132        }
133
134        @Override
135        public void onAudioStateChanged(AudioState audioState) {
136            mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, audioState).sendToTarget();
137        }
138
139        @Override
140        public void bringToForeground(boolean showDialpad) {
141            mHandler.obtainMessage(MSG_BRING_TO_FOREGROUND, showDialpad ? 1 : 0, 0).sendToTarget();
142        }
143
144        @Override
145        public void onCanAddCallChanged(boolean canAddCall) {
146            mHandler.obtainMessage(MSG_ON_CAN_ADD_CALL_CHANGED, canAddCall ? 1 : 0, 0)
147                    .sendToTarget();
148        }
149    }
150
151    private Phone.Listener mPhoneListener = new Phone.Listener() {
152        /** ${inheritDoc} */
153        @Override
154        public void onAudioStateChanged(Phone phone, AudioState audioState) {
155            InCallService.this.onAudioStateChanged(audioState);
156        }
157
158        /** ${inheritDoc} */
159        @Override
160        public void onBringToForeground(Phone phone, boolean showDialpad) {
161            InCallService.this.onBringToForeground(showDialpad);
162        }
163
164        /** ${inheritDoc} */
165        @Override
166        public void onCallAdded(Phone phone, Call call) {
167            InCallService.this.onCallAdded(call);
168        }
169
170        /** ${inheritDoc} */
171        @Override
172        public void onCallRemoved(Phone phone, Call call) {
173            InCallService.this.onCallRemoved(call);
174        }
175
176        /** ${inheritDoc} */
177        @Override
178        public void onCanAddCallChanged(Phone phone, boolean canAddCall) {
179            InCallService.this.onCanAddCallChanged(canAddCall);
180        }
181
182    };
183
184    private Phone mPhone;
185
186    public InCallService() {
187    }
188
189    @Override
190    public IBinder onBind(Intent intent) {
191        return new InCallServiceBinder();
192    }
193
194    @Override
195    public boolean onUnbind(Intent intent) {
196        if (mPhone != null) {
197            Phone oldPhone = mPhone;
198            mPhone = null;
199
200            oldPhone.destroy();
201            // destroy sets all the calls to disconnected if any live ones still exist. Therefore,
202            // it is important to remove the Listener *after* the call to destroy so that
203            // InCallService.on* callbacks are appropriately called.
204            oldPhone.removeListener(mPhoneListener);
205
206            onPhoneDestroyed(oldPhone);
207        }
208
209        return false;
210    }
211
212    /**
213     * Obtain the {@code Phone} associated with this {@code InCallService}.
214     *
215     * @return The {@code Phone} object associated with this {@code InCallService}, or {@code null}
216     *         if the {@code InCallService} is not in a state where it has an associated
217     *         {@code Phone}.
218     * @hide
219     * @deprecated Use direct methods on InCallService instead of {@link Phone}.
220     */
221    @SystemApi
222    @Deprecated
223    public Phone getPhone() {
224        return mPhone;
225    }
226
227    /**
228     * Obtains the current list of {@code Call}s to be displayed by this in-call experience.
229     *
230     * @return A list of the relevant {@code Call}s.
231     */
232    public final List<Call> getCalls() {
233        return mPhone == null ? Collections.<Call>emptyList() : mPhone.getCalls();
234    }
235
236    /**
237     * Returns if the device can support additional calls.
238     *
239     * @return Whether the phone supports adding more calls.
240     */
241    public final boolean canAddCall() {
242        return mPhone == null ? false : mPhone.canAddCall();
243    }
244
245    /**
246     * Obtains the current phone call audio state.
247     *
248     * @return An object encapsulating the audio state. Returns null if the service is not
249     *         fully initialized.
250     */
251    public final AudioState getAudioState() {
252        return mPhone == null ? null : mPhone.getAudioState();
253    }
254
255    /**
256     * Sets the microphone mute state. When this request is honored, there will be change to
257     * the {@link #getAudioState()}.
258     *
259     * @param state {@code true} if the microphone should be muted; {@code false} otherwise.
260     */
261    public final void setMuted(boolean state) {
262        if (mPhone != null) {
263            mPhone.setMuted(state);
264        }
265    }
266
267    /**
268     * Sets the audio route (speaker, bluetooth, etc...).  When this request is honored, there will
269     * be change to the {@link #getAudioState()}.
270     *
271     * @param route The audio route to use.
272     */
273    public final void setAudioRoute(int route) {
274        if (mPhone != null) {
275            mPhone.setAudioRoute(route);
276        }
277    }
278
279    /**
280     * Invoked when the {@code Phone} has been created. This is a signal to the in-call experience
281     * to start displaying in-call information to the user. Each instance of {@code InCallService}
282     * will have only one {@code Phone}, and this method will be called exactly once in the lifetime
283     * of the {@code InCallService}.
284     *
285     * @param phone The {@code Phone} object associated with this {@code InCallService}.
286     * @hide
287     * @deprecated Use direct methods on InCallService instead of {@link Phone}.
288     */
289    @SystemApi
290    @Deprecated
291    public void onPhoneCreated(Phone phone) {
292    }
293
294    /**
295     * Invoked when a {@code Phone} has been destroyed. This is a signal to the in-call experience
296     * to stop displaying in-call information to the user. This method will be called exactly once
297     * in the lifetime of the {@code InCallService}, and it will always be called after a previous
298     * call to {@link #onPhoneCreated(Phone)}.
299     *
300     * @param phone The {@code Phone} object associated with this {@code InCallService}.
301     * @hide
302     * @deprecated Use direct methods on InCallService instead of {@link Phone}.
303     */
304    @SystemApi
305    @Deprecated
306    public void onPhoneDestroyed(Phone phone) {
307    }
308
309    /**
310     * Called when the audio state changes.
311     *
312     * @param audioState The new {@link AudioState}.
313     */
314    public void onAudioStateChanged(AudioState audioState) {
315    }
316
317    /**
318     * Called to bring the in-call screen to the foreground. The in-call experience should
319     * respond immediately by coming to the foreground to inform the user of the state of
320     * ongoing {@code Call}s.
321     *
322     * @param showDialpad If true, put up the dialpad when the screen is shown.
323     */
324    public void onBringToForeground(boolean showDialpad) {
325    }
326
327    /**
328     * Called when a {@code Call} has been added to this in-call session. The in-call user
329     * experience should add necessary state listeners to the specified {@code Call} and
330     * immediately start to show the user information about the existence
331     * and nature of this {@code Call}. Subsequent invocations of {@link #getCalls()} will
332     * include this {@code Call}.
333     *
334     * @param call A newly added {@code Call}.
335     */
336    public void onCallAdded(Call call) {
337    }
338
339    /**
340     * Called when a {@code Call} has been removed from this in-call session. The in-call user
341     * experience should remove any state listeners from the specified {@code Call} and
342     * immediately stop displaying any information about this {@code Call}.
343     * Subsequent invocations of {@link #getCalls()} will no longer include this {@code Call}.
344     *
345     * @param call A newly removed {@code Call}.
346     */
347    public void onCallRemoved(Call call) {
348    }
349
350    /**
351     * Called when the ability to add more calls changes.  If the phone cannot
352     * support more calls then {@code canAddCall} is set to {@code false}.  If it can, then it
353     * is set to {@code true}. This can be used to control the visibility of UI to add more calls.
354     *
355     * @param canAddCall Indicates whether an additional call can be added.
356     */
357    public void onCanAddCallChanged(boolean canAddCall) {
358    }
359
360    /**
361     * Class to invoke functionality related to video calls.
362     */
363    public static abstract class VideoCall {
364
365        /**
366         * Registers a callback to receive commands and state changes for video calls.
367         *
368         * @param callback The video call callback.
369         */
370        public abstract void registerCallback(VideoCall.Callback callback);
371
372        /**
373         * Clears the video call listener set via {@link #setVideoCallListener(Listener)}.
374         */
375        public abstract void removeVideoCallListener();
376
377        /**
378         * Sets the camera to be used for video recording in a video call.
379         *
380         * @param cameraId The id of the camera.
381         */
382        public abstract void setCamera(String cameraId);
383
384        /**
385         * Sets the surface to be used for displaying a preview of what the user's camera is
386         * currently capturing.  When video transmission is enabled, this is the video signal which
387         * is sent to the remote device.
388         *
389         * @param surface The surface.
390         */
391        public abstract void setPreviewSurface(Surface surface);
392
393        /**
394         * Sets the surface to be used for displaying the video received from the remote device.
395         *
396         * @param surface The surface.
397         */
398        public abstract void setDisplaySurface(Surface surface);
399
400        /**
401         * Sets the device orientation, in degrees.  Assumes that a standard portrait orientation of
402         * the device is 0 degrees.
403         *
404         * @param rotation The device orientation, in degrees.
405         */
406        public abstract void setDeviceOrientation(int rotation);
407
408        /**
409         * Sets camera zoom ratio.
410         *
411         * @param value The camera zoom ratio.
412         */
413        public abstract void setZoom(float value);
414
415        /**
416         * Issues a request to modify the properties of the current session.  The request is sent to
417         * the remote device where it it handled by
418         * {@link VideoCall.Callback#onSessionModifyRequestReceived}.
419         * Some examples of session modification requests: upgrade call from audio to video,
420         * downgrade call from video to audio, pause video.
421         *
422         * @param requestProfile The requested call video properties.
423         */
424        public abstract void sendSessionModifyRequest(VideoProfile requestProfile);
425
426        /**
427         * Provides a response to a request to change the current call session video
428         * properties.
429         * This is in response to a request the InCall UI has received via
430         * {@link VideoCall.Callback#onSessionModifyRequestReceived}.
431         * The response is handled on the remove device by
432         * {@link VideoCall.Callback#onSessionModifyResponseReceived}.
433         *
434         * @param responseProfile The response call video properties.
435         */
436        public abstract void sendSessionModifyResponse(VideoProfile responseProfile);
437
438        /**
439         * Issues a request to the video provider to retrieve the camera capabilities.
440         * Camera capabilities are reported back to the caller via
441         * {@link VideoCall.Callback#onCameraCapabilitiesChanged(CameraCapabilities)}.
442         */
443        public abstract void requestCameraCapabilities();
444
445        /**
446         * Issues a request to the video telephony framework to retrieve the cumulative data usage for
447         * the current call.  Data usage is reported back to the caller via
448         * {@link VideoCall.Callback#onCallDataUsageChanged}.
449         */
450        public abstract void requestCallDataUsage();
451
452        /**
453         * Provides the video telephony framework with the URI of an image to be displayed to remote
454         * devices when the video signal is paused.
455         *
456         * @param uri URI of image to display.
457         */
458        public abstract void setPauseImage(String uri);
459
460        /**
461         * Callback class which invokes callbacks after video call actions occur.
462         */
463        public static abstract class Callback {
464            /**
465             * Called when a session modification request is received from the remote device.
466             * The remote request is sent via
467             * {@link Connection.VideoProvider#onSendSessionModifyRequest}. The InCall UI
468             * is responsible for potentially prompting the user whether they wish to accept the new
469             * call profile (e.g. prompt user if they wish to accept an upgrade from an audio to a
470             * video call) and should call
471             * {@link Connection.VideoProvider#onSendSessionModifyResponse} to indicate
472             * the video settings the user has agreed to.
473             *
474             * @param videoProfile The requested video call profile.
475             */
476            public abstract void onSessionModifyRequestReceived(VideoProfile videoProfile);
477
478            /**
479             * Called when a response to a session modification request is received from the remote
480             * device. The remote InCall UI sends the response using
481             * {@link Connection.VideoProvider#onSendSessionModifyResponse}.
482             *
483             * @param status Status of the session modify request.  Valid values are
484             *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS},
485             *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_FAIL},
486             *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_INVALID}
487             * @param requestedProfile The original request which was sent to the remote device.
488             * @param responseProfile The actual profile changes made by the remote device.
489             */
490            public abstract void onSessionModifyResponseReceived(int status,
491                    VideoProfile requestedProfile, VideoProfile responseProfile);
492
493            /**
494             * Handles events related to the current session which the client may wish to handle.
495             * These are separate from requested changes to the session due to the underlying
496             * protocol or connection.
497             *
498             * Valid values are:
499             * {@link Connection.VideoProvider#SESSION_EVENT_RX_PAUSE},
500             * {@link Connection.VideoProvider#SESSION_EVENT_RX_RESUME},
501             * {@link Connection.VideoProvider#SESSION_EVENT_TX_START},
502             * {@link Connection.VideoProvider#SESSION_EVENT_TX_STOP},
503             * {@link Connection.VideoProvider#SESSION_EVENT_CAMERA_FAILURE},
504             * {@link Connection.VideoProvider#SESSION_EVENT_CAMERA_READY}
505             *
506             * @param event The event.
507             */
508            public abstract void onCallSessionEvent(int event);
509
510            /**
511             * Handles a change to the video dimensions from the remote caller (peer). This could
512             * happen if, for example, the peer changes orientation of their device.
513             *
514             * @param width  The updated peer video width.
515             * @param height The updated peer video height.
516             */
517            public abstract void onPeerDimensionsChanged(int width, int height);
518
519            /**
520             * Handles a change to the video quality.
521             *
522             * @param videoQuality  The updated peer video quality.
523             */
524            public abstract void onVideoQualityChanged(int videoQuality);
525
526            /**
527             * Handles an update to the total data used for the current session.
528             *
529             * @param dataUsage The updated data usage.
530             */
531            public abstract void onCallDataUsageChanged(long dataUsage);
532
533            /**
534             * Handles a change in camera capabilities.
535             *
536             * @param cameraCapabilities The changed camera capabilities.
537             */
538            public abstract void onCameraCapabilitiesChanged(CameraCapabilities cameraCapabilities);
539        }
540    }
541}
542