blob: 7415f9286e8febe109de4b2dc66ea8f24c6ee361 [file] [log] [blame]
Santos Cordon89647a62013-07-16 13:38:09 -07001/*
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 com.android.phone;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.AsyncResult;
24import android.os.Handler;
25import android.os.IBinder;
26import android.os.Message;
27import android.os.RemoteException;
28import android.os.SystemProperties;
29import android.util.Log;
30
Santos Cordonf4046882013-07-25 18:49:27 -070031import com.android.services.telephony.common.Call;
Santos Cordon345350e2013-07-19 17:16:14 -070032import com.android.services.telephony.common.ICallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070033import com.android.services.telephony.common.ICallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070034
35/**
Santos Cordon345350e2013-07-19 17:16:14 -070036 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070037 */
Santos Cordon63a84242013-07-23 13:32:52 -070038public class CallHandlerServiceProxy extends Handler implements CallModeler.Listener {
Santos Cordon89647a62013-07-16 13:38:09 -070039
Santos Cordon345350e2013-07-19 17:16:14 -070040 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Santos Cordon89647a62013-07-16 13:38:09 -070041 private static final boolean DBG =
42 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070043
Santos Cordon89647a62013-07-16 13:38:09 -070044
45 private Context mContext;
Santos Cordon63a84242013-07-23 13:32:52 -070046 private CallModeler mCallModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070047 private ServiceConnection mConnection;
Santos Cordon345350e2013-07-19 17:16:14 -070048 private ICallHandlerService mCallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070049 private CallCommandService mCallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070050
Santos Cordon63a84242013-07-23 13:32:52 -070051 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordoncba1b442013-07-18 12:43:58 -070052 CallCommandService callCommandService) {
Santos Cordon89647a62013-07-16 13:38:09 -070053 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070054 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070055 mCallModeler = callModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070056
Santos Cordon63a84242013-07-23 13:32:52 -070057 mCallModeler.addListener(this);
Santos Cordon89647a62013-07-16 13:38:09 -070058 setupServiceConnection();
59 }
60
61 @Override
Santos Cordon63a84242013-07-23 13:32:52 -070062 public void onNewCall(int callId) {
63 if (mCallHandlerService != null) {
64 try {
Santos Cordonf4046882013-07-25 18:49:27 -070065 mCallHandlerService.onIncomingCall(new Call(callId));
Santos Cordon63a84242013-07-23 13:32:52 -070066 } catch (RemoteException e) {
67 Log.e(TAG, "Remote exception handling onIncomingCall", e);
68 }
69 } else {
70 Log.wtf(TAG, "Call handle service has not connected! Cannot accept incoming call.");
71 }
72 }
Santos Cordon89647a62013-07-16 13:38:09 -070073
Santos Cordon63a84242013-07-23 13:32:52 -070074 @Override
75 public void onDisconnect(int callId) {
76 if (mCallHandlerService != null) {
77 try {
Santos Cordonf4046882013-07-25 18:49:27 -070078 mCallHandlerService.onDisconnect(new Call(callId));
Santos Cordon63a84242013-07-23 13:32:52 -070079 } catch (RemoteException e) {
80 Log.e(TAG, "Remote exception handling onDisconnect ", e);
81 }
Santos Cordon89647a62013-07-16 13:38:09 -070082 }
83 }
84
85 /**
Santos Cordon345350e2013-07-19 17:16:14 -070086 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -070087 */
88 private void setupServiceConnection() {
89 mConnection = new ServiceConnection() {
90 @Override
91 public void onServiceConnected(ComponentName className, IBinder service) {
92 if (DBG) {
93 Log.d(TAG, "Service Connected");
94 }
Santos Cordon345350e2013-07-19 17:16:14 -070095 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Santos Cordon89647a62013-07-16 13:38:09 -070096 }
97
98 @Override
99 public void onServiceDisconnected(ComponentName className) {
Chiao Chenge41661c2013-07-23 13:28:26 -0700100 // TODO(klp): handle the case where the in call ui crashed or gets destroyed.
101 // In the near term, we need to re-bind to the service when ever it's gone.
102 // Longer term, we need a way to catch the crash and allow the users to choose
103 // a different in-call screen.
104 Log.e(TAG, "Yikes! no in call ui!");
Santos Cordon345350e2013-07-19 17:16:14 -0700105 mCallHandlerService = null;
Santos Cordon89647a62013-07-16 13:38:09 -0700106 }
107 };
108
Santos Cordon345350e2013-07-19 17:16:14 -0700109 Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
Santos Cordon89647a62013-07-16 13:38:09 -0700110 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Santos Cordon345350e2013-07-19 17:16:14 -0700111 Log.e(TAG, "Cound not bind to ICallHandlerService");
Santos Cordon89647a62013-07-16 13:38:09 -0700112 }
113 }
114
115 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700116 * Called when the in-call UI service is connected. Send command interface to in-call.
117 */
Santos Cordon63a84242013-07-23 13:32:52 -0700118 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
119 mCallHandlerService = callHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -0700120
121 try {
Santos Cordon345350e2013-07-19 17:16:14 -0700122 mCallHandlerService.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700123 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700124 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700125 }
126 }
127}