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 
17 package android.telecom;
18 
19 import android.annotation.SystemApi;
20 import android.annotation.SdkConstant;
21 import android.app.Service;
22 import android.content.Intent;
23 import android.os.Handler;
24 import android.os.IBinder;
25 import android.os.Looper;
26 import android.os.Message;
27 import android.view.Surface;
28 
29 import com.android.internal.os.SomeArgs;
30 import com.android.internal.telecom.IInCallAdapter;
31 import com.android.internal.telecom.IInCallService;
32 
33 import java.lang.String;
34 
35 /**
36  * This service is implemented by any app that wishes to provide the user-interface for managing
37  * phone calls. Telecom binds to this service while there exists a live (active or incoming) call,
38  * and uses it to notify the in-call app of any live and and recently disconnected calls.
39  *
40  * {@hide}
41  */
42 @SystemApi
43 public abstract class InCallService extends Service {
44 
45     /**
46      * The {@link Intent} that must be declared as handled by the service.
47      */
48     @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
49     public static final String SERVICE_INTERFACE = "android.telecom.InCallService";
50 
51     private static final int MSG_SET_IN_CALL_ADAPTER = 1;
52     private static final int MSG_ADD_CALL = 2;
53     private static final int MSG_UPDATE_CALL = 3;
54     private static final int MSG_SET_POST_DIAL_WAIT = 4;
55     private static final int MSG_ON_AUDIO_STATE_CHANGED = 5;
56     private static final int MSG_BRING_TO_FOREGROUND = 6;
57     private static final int MSG_ON_CAN_ADD_CALL_CHANGED = 7;
58 
59     /** Default Handler used to consolidate binder method calls onto a single thread. */
60     private final Handler mHandler = new Handler(Looper.getMainLooper()) {
61         @Override
62         public void handleMessage(Message msg) {
63             if (mPhone == null && msg.what != MSG_SET_IN_CALL_ADAPTER) {
64                 return;
65             }
66 
67             switch (msg.what) {
68                 case MSG_SET_IN_CALL_ADAPTER:
69                     mPhone = new Phone(new InCallAdapter((IInCallAdapter) msg.obj));
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
setInCallAdapter(IInCallAdapter inCallAdapter)107         public void setInCallAdapter(IInCallAdapter inCallAdapter) {
108             mHandler.obtainMessage(MSG_SET_IN_CALL_ADAPTER, inCallAdapter).sendToTarget();
109         }
110 
111         @Override
addCall(ParcelableCall call)112         public void addCall(ParcelableCall call) {
113             mHandler.obtainMessage(MSG_ADD_CALL, call).sendToTarget();
114         }
115 
116         @Override
updateCall(ParcelableCall call)117         public void updateCall(ParcelableCall call) {
118             mHandler.obtainMessage(MSG_UPDATE_CALL, call).sendToTarget();
119         }
120 
121         @Override
setPostDial(String callId, String remaining)122         public void setPostDial(String callId, String remaining) {
123             // TODO: Unused
124         }
125 
126         @Override
setPostDialWait(String callId, String remaining)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
onAudioStateChanged(AudioState audioState)135         public void onAudioStateChanged(AudioState audioState) {
136             mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, audioState).sendToTarget();
137         }
138 
139         @Override
bringToForeground(boolean showDialpad)140         public void bringToForeground(boolean showDialpad) {
141             mHandler.obtainMessage(MSG_BRING_TO_FOREGROUND, showDialpad ? 1 : 0, 0).sendToTarget();
142         }
143 
144         @Override
onCanAddCallChanged(boolean canAddCall)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 mPhone;
152 
InCallService()153     public InCallService() {
154     }
155 
156     @Override
onBind(Intent intent)157     public IBinder onBind(Intent intent) {
158         return new InCallServiceBinder();
159     }
160 
161     @Override
onUnbind(Intent intent)162     public boolean onUnbind(Intent intent) {
163         if (mPhone != null) {
164             Phone oldPhone = mPhone;
165             mPhone = null;
166 
167             oldPhone.destroy();
168             onPhoneDestroyed(oldPhone);
169         }
170         return false;
171     }
172 
173     /**
174      * Obtain the {@code Phone} associated with this {@code InCallService}.
175      *
176      * @return The {@code Phone} object associated with this {@code InCallService}, or {@code null}
177      *         if the {@code InCallService} is not in a state where it has an associated
178      *         {@code Phone}.
179      */
getPhone()180     public Phone getPhone() {
181         return mPhone;
182     }
183 
184     /**
185      * Invoked when the {@code Phone} has been created. This is a signal to the in-call experience
186      * to start displaying in-call information to the user. Each instance of {@code InCallService}
187      * will have only one {@code Phone}, and this method will be called exactly once in the lifetime
188      * of the {@code InCallService}.
189      *
190      * @param phone The {@code Phone} object associated with this {@code InCallService}.
191      */
onPhoneCreated(Phone phone)192     public void onPhoneCreated(Phone phone) {
193     }
194 
195     /**
196      * Invoked when a {@code Phone} has been destroyed. This is a signal to the in-call experience
197      * to stop displaying in-call information to the user. This method will be called exactly once
198      * in the lifetime of the {@code InCallService}, and it will always be called after a previous
199      * call to {@link #onPhoneCreated(Phone)}.
200      *
201      * @param phone The {@code Phone} object associated with this {@code InCallService}.
202      */
onPhoneDestroyed(Phone phone)203     public void onPhoneDestroyed(Phone phone) {
204     }
205 
206     /**
207      * Class to invoke functionality related to video calls.
208      * @hide
209      */
210     public static abstract class VideoCall {
211 
212         /**
213          * Sets a listener to invoke callback methods in the InCallUI after performing video
214          * telephony actions.
215          *
216          * @param videoCallListener The call video client.
217          */
setVideoCallListener(VideoCall.Listener videoCallListener)218         public abstract void setVideoCallListener(VideoCall.Listener videoCallListener);
219 
220         /**
221          * Sets the camera to be used for video recording in a video call.
222          *
223          * @param cameraId The id of the camera.
224          */
setCamera(String cameraId)225         public abstract void setCamera(String cameraId);
226 
227         /**
228          * Sets the surface to be used for displaying a preview of what the user's camera is
229          * currently capturing.  When video transmission is enabled, this is the video signal which
230          * is sent to the remote device.
231          *
232          * @param surface The surface.
233          */
setPreviewSurface(Surface surface)234         public abstract void setPreviewSurface(Surface surface);
235 
236         /**
237          * Sets the surface to be used for displaying the video received from the remote device.
238          *
239          * @param surface The surface.
240          */
setDisplaySurface(Surface surface)241         public abstract void setDisplaySurface(Surface surface);
242 
243         /**
244          * Sets the device orientation, in degrees.  Assumes that a standard portrait orientation of
245          * the device is 0 degrees.
246          *
247          * @param rotation The device orientation, in degrees.
248          */
setDeviceOrientation(int rotation)249         public abstract void setDeviceOrientation(int rotation);
250 
251         /**
252          * Sets camera zoom ratio.
253          *
254          * @param value The camera zoom ratio.
255          */
setZoom(float value)256         public abstract void setZoom(float value);
257 
258         /**
259          * Issues a request to modify the properties of the current session.  The request is sent to
260          * the remote device where it it handled by
261          * {@link VideoCall.Listener#onSessionModifyRequestReceived}.
262          * Some examples of session modification requests: upgrade call from audio to video,
263          * downgrade call from video to audio, pause video.
264          *
265          * @param requestProfile The requested call video properties.
266          */
sendSessionModifyRequest(VideoProfile requestProfile)267         public abstract void sendSessionModifyRequest(VideoProfile requestProfile);
268 
269         /**
270          * Provides a response to a request to change the current call session video
271          * properties.
272          * This is in response to a request the InCall UI has received via
273          * {@link VideoCall.Listener#onSessionModifyRequestReceived}.
274          * The response is handled on the remove device by
275          * {@link VideoCall.Listener#onSessionModifyResponseReceived}.
276          *
277          * @param responseProfile The response call video properties.
278          */
sendSessionModifyResponse(VideoProfile responseProfile)279         public abstract void sendSessionModifyResponse(VideoProfile responseProfile);
280 
281         /**
282          * Issues a request to the video provider to retrieve the camera capabilities.
283          * Camera capabilities are reported back to the caller via
284          * {@link VideoCall.Listener#onCameraCapabilitiesChanged(CameraCapabilities)}.
285          */
requestCameraCapabilities()286         public abstract void requestCameraCapabilities();
287 
288         /**
289          * Issues a request to the video telephony framework to retrieve the cumulative data usage for
290          * the current call.  Data usage is reported back to the caller via
291          * {@link VideoCall.Listener#onCallDataUsageChanged}.
292          */
requestCallDataUsage()293         public abstract void requestCallDataUsage();
294 
295         /**
296          * Provides the video telephony framework with the URI of an image to be displayed to remote
297          * devices when the video signal is paused.
298          *
299          * @param uri URI of image to display.
300          */
setPauseImage(String uri)301         public abstract void setPauseImage(String uri);
302 
303         /**
304          * Listener class which invokes callbacks after video call actions occur.
305          * @hide
306          */
307         public static abstract class Listener {
308             /**
309              * Called when a session modification request is received from the remote device.
310              * The remote request is sent via
311              * {@link Connection.VideoProvider#onSendSessionModifyRequest}. The InCall UI
312              * is responsible for potentially prompting the user whether they wish to accept the new
313              * call profile (e.g. prompt user if they wish to accept an upgrade from an audio to a
314              * video call) and should call
315              * {@link Connection.VideoProvider#onSendSessionModifyResponse} to indicate
316              * the video settings the user has agreed to.
317              *
318              * @param videoProfile The requested video call profile.
319              */
onSessionModifyRequestReceived(VideoProfile videoProfile)320             public abstract void onSessionModifyRequestReceived(VideoProfile videoProfile);
321 
322             /**
323              * Called when a response to a session modification request is received from the remote
324              * device. The remote InCall UI sends the response using
325              * {@link Connection.VideoProvider#onSendSessionModifyResponse}.
326              *
327              * @param status Status of the session modify request.  Valid values are
328              *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS},
329              *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_FAIL},
330              *               {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_INVALID}
331              * @param requestedProfile The original request which was sent to the remote device.
332              * @param responseProfile The actual profile changes made by the remote device.
333              */
onSessionModifyResponseReceived(int status, VideoProfile requestedProfile, VideoProfile responseProfile)334             public abstract void onSessionModifyResponseReceived(int status,
335                     VideoProfile requestedProfile, VideoProfile responseProfile);
336 
337             /**
338              * Handles events related to the current session which the client may wish to handle.
339              * These are separate from requested changes to the session due to the underlying
340              * protocol or connection.
341              *
342              * Valid values are:
343              * {@link Connection.VideoProvider#SESSION_EVENT_RX_PAUSE},
344              * {@link Connection.VideoProvider#SESSION_EVENT_RX_RESUME},
345              * {@link Connection.VideoProvider#SESSION_EVENT_TX_START},
346              * {@link Connection.VideoProvider#SESSION_EVENT_TX_STOP},
347              * {@link Connection.VideoProvider#SESSION_EVENT_CAMERA_FAILURE},
348              * {@link Connection.VideoProvider#SESSION_EVENT_CAMERA_READY}
349              *
350              * @param event The event.
351              */
onCallSessionEvent(int event)352             public abstract void onCallSessionEvent(int event);
353 
354             /**
355              * Handles a change to the video dimensions from the remote caller (peer). This could
356              * happen if, for example, the peer changes orientation of their device.
357              *
358              * @param width  The updated peer video width.
359              * @param height The updated peer video height.
360              */
onPeerDimensionsChanged(int width, int height)361             public abstract void onPeerDimensionsChanged(int width, int height);
362 
363             /**
364              * Handles an update to the total data used for the current session.
365              *
366              * @param dataUsage The updated data usage.
367              */
onCallDataUsageChanged(int dataUsage)368             public abstract void onCallDataUsageChanged(int dataUsage);
369 
370             /**
371              * Handles a change in camera capabilities.
372              *
373              * @param cameraCapabilities The changed camera capabilities.
374              */
onCameraCapabilitiesChanged( CameraCapabilities cameraCapabilities)375             public abstract void onCameraCapabilitiesChanged(
376                     CameraCapabilities cameraCapabilities);
377         }
378     }
379 }
380