InCallService.java revision ef9f6f957d897ea0ed82114185b8fa3fefd4917b
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.SystemApi; 20import android.annotation.SdkConstant; 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; 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 43public 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 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 switch (msg.what) { 63 case MSG_SET_IN_CALL_ADAPTER: 64 mPhone = new Phone(new InCallAdapter((IInCallAdapter) msg.obj)); 65 onPhoneCreated(mPhone); 66 break; 67 case MSG_ADD_CALL: 68 mPhone.internalAddCall((ParcelableCall) msg.obj); 69 break; 70 case MSG_UPDATE_CALL: 71 mPhone.internalUpdateCall((ParcelableCall) msg.obj); 72 break; 73 case MSG_SET_POST_DIAL_WAIT: { 74 SomeArgs args = (SomeArgs) msg.obj; 75 try { 76 String callId = (String) args.arg1; 77 String remaining = (String) args.arg2; 78 mPhone.internalSetPostDialWait(callId, remaining); 79 } finally { 80 args.recycle(); 81 } 82 break; 83 } 84 case MSG_ON_AUDIO_STATE_CHANGED: 85 mPhone.internalAudioStateChanged((AudioState) msg.obj); 86 break; 87 case MSG_BRING_TO_FOREGROUND: 88 mPhone.internalBringToForeground(msg.arg1 == 1); 89 break; 90 default: 91 break; 92 } 93 } 94 }; 95 96 /** Manages the binder calls so that the implementor does not need to deal with it. */ 97 private final class InCallServiceBinder extends IInCallService.Stub { 98 @Override 99 public void setInCallAdapter(IInCallAdapter inCallAdapter) { 100 mHandler.obtainMessage(MSG_SET_IN_CALL_ADAPTER, inCallAdapter).sendToTarget(); 101 } 102 103 @Override 104 public void addCall(ParcelableCall call) { 105 mHandler.obtainMessage(MSG_ADD_CALL, call).sendToTarget(); 106 } 107 108 @Override 109 public void updateCall(ParcelableCall call) { 110 mHandler.obtainMessage(MSG_UPDATE_CALL, call).sendToTarget(); 111 } 112 113 @Override 114 public void setPostDial(String callId, String remaining) { 115 // TODO: Unused 116 } 117 118 @Override 119 public void setPostDialWait(String callId, String remaining) { 120 SomeArgs args = SomeArgs.obtain(); 121 args.arg1 = callId; 122 args.arg2 = remaining; 123 mHandler.obtainMessage(MSG_SET_POST_DIAL_WAIT, args).sendToTarget(); 124 } 125 126 @Override 127 public void onAudioStateChanged(AudioState audioState) { 128 mHandler.obtainMessage(MSG_ON_AUDIO_STATE_CHANGED, audioState).sendToTarget(); 129 } 130 131 @Override 132 public void bringToForeground(boolean showDialpad) { 133 mHandler.obtainMessage(MSG_BRING_TO_FOREGROUND, showDialpad ? 1 : 0, 0).sendToTarget(); 134 } 135 } 136 137 private Phone mPhone; 138 139 public InCallService() { 140 } 141 142 @Override 143 public IBinder onBind(Intent intent) { 144 return new InCallServiceBinder(); 145 } 146 147 @Override 148 public boolean onUnbind(Intent intent) { 149 if (mPhone != null) { 150 Phone oldPhone = mPhone; 151 mPhone = null; 152 153 oldPhone.destroy(); 154 onPhoneDestroyed(oldPhone); 155 } 156 return false; 157 } 158 159 /** 160 * Obtain the {@code Phone} associated with this {@code InCallService}. 161 * 162 * @return The {@code Phone} object associated with this {@code InCallService}, or {@code null} 163 * if the {@code InCallService} is not in a state where it has an associated 164 * {@code Phone}. 165 */ 166 public Phone getPhone() { 167 return mPhone; 168 } 169 170 /** 171 * Invoked when the {@code Phone} has been created. This is a signal to the in-call experience 172 * to start displaying in-call information to the user. Each instance of {@code InCallService} 173 * will have only one {@code Phone}, and this method will be called exactly once in the lifetime 174 * of the {@code InCallService}. 175 * 176 * @param phone The {@code Phone} object associated with this {@code InCallService}. 177 */ 178 public void onPhoneCreated(Phone phone) { 179 } 180 181 /** 182 * Invoked when a {@code Phone} has been destroyed. This is a signal to the in-call experience 183 * to stop displaying in-call information to the user. This method will be called exactly once 184 * in the lifetime of the {@code InCallService}, and it will always be called after a previous 185 * call to {@link #onPhoneCreated(Phone)}. 186 * 187 * @param phone The {@code Phone} object associated with this {@code InCallService}. 188 */ 189 public void onPhoneDestroyed(Phone phone) { 190 } 191 192 /** 193 * Class to invoke functionality related to video calls. 194 * @hide 195 */ 196 public static abstract class VideoCall { 197 198 /** 199 * Sets a listener to invoke callback methods in the InCallUI after performing video 200 * telephony actions. 201 * 202 * @param videoCallListener The call video client. 203 */ 204 public abstract void setVideoCallListener(VideoCall.Listener videoCallListener); 205 206 /** 207 * Sets the camera to be used for video recording in a video call. 208 * 209 * @param cameraId The id of the camera. 210 */ 211 public abstract void setCamera(String cameraId); 212 213 /** 214 * Sets the surface to be used for displaying a preview of what the user's camera is 215 * currently capturing. When video transmission is enabled, this is the video signal which 216 * is sent to the remote device. 217 * 218 * @param surface The surface. 219 */ 220 public abstract void setPreviewSurface(Surface surface); 221 222 /** 223 * Sets the surface to be used for displaying the video received from the remote device. 224 * 225 * @param surface The surface. 226 */ 227 public abstract void setDisplaySurface(Surface surface); 228 229 /** 230 * Sets the device orientation, in degrees. Assumes that a standard portrait orientation of 231 * the device is 0 degrees. 232 * 233 * @param rotation The device orientation, in degrees. 234 */ 235 public abstract void setDeviceOrientation(int rotation); 236 237 /** 238 * Sets camera zoom ratio. 239 * 240 * @param value The camera zoom ratio. 241 */ 242 public abstract void setZoom(float value); 243 244 /** 245 * Issues a request to modify the properties of the current session. The request is sent to 246 * the remote device where it it handled by 247 * {@link VideoCall.Listener#onSessionModifyRequestReceived}. 248 * Some examples of session modification requests: upgrade call from audio to video, 249 * downgrade call from video to audio, pause video. 250 * 251 * @param requestProfile The requested call video properties. 252 */ 253 public abstract void sendSessionModifyRequest(VideoProfile requestProfile); 254 255 /** 256 * Provides a response to a request to change the current call session video 257 * properties. 258 * This is in response to a request the InCall UI has received via 259 * {@link VideoCall.Listener#onSessionModifyRequestReceived}. 260 * The response is handled on the remove device by 261 * {@link VideoCall.Listener#onSessionModifyResponseReceived}. 262 * 263 * @param responseProfile The response call video properties. 264 */ 265 public abstract void sendSessionModifyResponse(VideoProfile responseProfile); 266 267 /** 268 * Issues a request to the video provider to retrieve the camera capabilities. 269 * Camera capabilities are reported back to the caller via 270 * {@link VideoCall.Listener#onCameraCapabilitiesChanged(CameraCapabilities)}. 271 */ 272 public abstract void requestCameraCapabilities(); 273 274 /** 275 * Issues a request to the video telephony framework to retrieve the cumulative data usage for 276 * the current call. Data usage is reported back to the caller via 277 * {@link VideoCall.Listener#onCallDataUsageChanged}. 278 */ 279 public abstract void requestCallDataUsage(); 280 281 /** 282 * Provides the video telephony framework with the URI of an image to be displayed to remote 283 * devices when the video signal is paused. 284 * 285 * @param uri URI of image to display. 286 */ 287 public abstract void setPauseImage(String uri); 288 289 /** 290 * Listener class which invokes callbacks after video call actions occur. 291 * @hide 292 */ 293 public static abstract class Listener { 294 /** 295 * Called when a session modification request is received from the remote device. 296 * The remote request is sent via 297 * {@link Connection.VideoProvider#onSendSessionModifyRequest}. The InCall UI 298 * is responsible for potentially prompting the user whether they wish to accept the new 299 * call profile (e.g. prompt user if they wish to accept an upgrade from an audio to a 300 * video call) and should call 301 * {@link Connection.VideoProvider#onSendSessionModifyResponse} to indicate 302 * the video settings the user has agreed to. 303 * 304 * @param videoProfile The requested video call profile. 305 */ 306 public abstract void onSessionModifyRequestReceived(VideoProfile videoProfile); 307 308 /** 309 * Called when a response to a session modification request is received from the remote 310 * device. The remote InCall UI sends the response using 311 * {@link Connection.VideoProvider#onSendSessionModifyResponse}. 312 * 313 * @param status Status of the session modify request. Valid values are 314 * {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_SUCCESS}, 315 * {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_FAIL}, 316 * {@link Connection.VideoProvider#SESSION_MODIFY_REQUEST_INVALID} 317 * @param requestedProfile The original request which was sent to the remote device. 318 * @param responseProfile The actual profile changes made by the remote device. 319 */ 320 public abstract void onSessionModifyResponseReceived(int status, 321 VideoProfile requestedProfile, VideoProfile responseProfile); 322 323 /** 324 * Handles events related to the current session which the client may wish to handle. 325 * These are separate from requested changes to the session due to the underlying 326 * protocol or connection. 327 * 328 * Valid values are: 329 * {@link Connection.VideoProvider#SESSION_EVENT_RX_PAUSE}, 330 * {@link Connection.VideoProvider#SESSION_EVENT_RX_RESUME}, 331 * {@link Connection.VideoProvider#SESSION_EVENT_TX_START}, 332 * {@link Connection.VideoProvider#SESSION_EVENT_TX_STOP}, 333 * {@link Connection.VideoProvider#SESSION_EVENT_CAMERA_FAILURE}, 334 * {@link Connection.VideoProvider#SESSION_EVENT_CAMERA_READY} 335 * 336 * @param event The event. 337 */ 338 public abstract void onCallSessionEvent(int event); 339 340 /** 341 * Handles a change to the video dimensions from the remote caller (peer). This could 342 * happen if, for example, the peer changes orientation of their device. 343 * 344 * @param width The updated peer video width. 345 * @param height The updated peer video height. 346 */ 347 public abstract void onPeerDimensionsChanged(int width, int height); 348 349 /** 350 * Handles an update to the total data used for the current session. 351 * 352 * @param dataUsage The updated data usage. 353 */ 354 public abstract void onCallDataUsageChanged(int dataUsage); 355 356 /** 357 * Handles a change in camera capabilities. 358 * 359 * @param cameraCapabilities The changed camera capabilities. 360 */ 361 public abstract void onCameraCapabilitiesChanged( 362 CameraCapabilities cameraCapabilities); 363 } 364 } 365} 366