blob: 24641c06adb59a4ef844246fa56f33832112c1f2 [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
Santos Cordona3d05142013-07-29 11:25:17 -070035import java.util.List;
36
Santos Cordon89647a62013-07-16 13:38:09 -070037/**
Santos Cordon345350e2013-07-19 17:16:14 -070038 * This class is responsible for passing through call state changes to the CallHandlerService.
Santos Cordon89647a62013-07-16 13:38:09 -070039 */
Santos Cordon63a84242013-07-23 13:32:52 -070040public class CallHandlerServiceProxy extends Handler implements CallModeler.Listener {
Santos Cordon89647a62013-07-16 13:38:09 -070041
Santos Cordon345350e2013-07-19 17:16:14 -070042 private static final String TAG = CallHandlerServiceProxy.class.getSimpleName();
Santos Cordon89647a62013-07-16 13:38:09 -070043 private static final boolean DBG =
44 (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
Chiao Chenge41661c2013-07-23 13:28:26 -070045
Santos Cordon89647a62013-07-16 13:38:09 -070046
47 private Context mContext;
Santos Cordon63a84242013-07-23 13:32:52 -070048 private CallModeler mCallModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070049 private ServiceConnection mConnection;
Santos Cordon345350e2013-07-19 17:16:14 -070050 private ICallHandlerService mCallHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -070051 private CallCommandService mCallCommandService;
Santos Cordon89647a62013-07-16 13:38:09 -070052
Santos Cordon63a84242013-07-23 13:32:52 -070053 public CallHandlerServiceProxy(Context context, CallModeler callModeler,
Santos Cordoncba1b442013-07-18 12:43:58 -070054 CallCommandService callCommandService) {
Santos Cordon89647a62013-07-16 13:38:09 -070055 mContext = context;
Santos Cordoncba1b442013-07-18 12:43:58 -070056 mCallCommandService = callCommandService;
Santos Cordon63a84242013-07-23 13:32:52 -070057 mCallModeler = callModeler;
Santos Cordon89647a62013-07-16 13:38:09 -070058
Santos Cordon63a84242013-07-23 13:32:52 -070059 mCallModeler.addListener(this);
Santos Cordon89647a62013-07-16 13:38:09 -070060 setupServiceConnection();
61 }
62
63 @Override
Santos Cordon995c8162013-07-29 09:22:22 -070064 public void onNewCall(Call call) {
Santos Cordon63a84242013-07-23 13:32:52 -070065 if (mCallHandlerService != null) {
66 try {
Santos Cordon995c8162013-07-29 09:22:22 -070067 mCallHandlerService.onIncomingCall(call);
Santos Cordon63a84242013-07-23 13:32:52 -070068 } catch (RemoteException e) {
69 Log.e(TAG, "Remote exception handling onIncomingCall", e);
70 }
71 } else {
72 Log.wtf(TAG, "Call handle service has not connected! Cannot accept incoming call.");
73 }
74 }
Santos Cordon89647a62013-07-16 13:38:09 -070075
Santos Cordon63a84242013-07-23 13:32:52 -070076 @Override
Santos Cordon995c8162013-07-29 09:22:22 -070077 public void onDisconnect(Call call) {
Santos Cordon63a84242013-07-23 13:32:52 -070078 if (mCallHandlerService != null) {
79 try {
Santos Cordon995c8162013-07-29 09:22:22 -070080 mCallHandlerService.onDisconnect(call);
Santos Cordon63a84242013-07-23 13:32:52 -070081 } catch (RemoteException e) {
82 Log.e(TAG, "Remote exception handling onDisconnect ", e);
83 }
Santos Cordon89647a62013-07-16 13:38:09 -070084 }
85 }
86
Santos Cordona3d05142013-07-29 11:25:17 -070087 @Override
88 public void onUpdate(List<Call> calls) {
89 if (mCallHandlerService != null) {
90 try {
91 mCallHandlerService.onUpdate(calls);
92 } catch (RemoteException e) {
93 Log.e(TAG, "Remote exception handling onUpdate", e);
94 }
95 }
96 }
97
Santos Cordon89647a62013-07-16 13:38:09 -070098 /**
Santos Cordon345350e2013-07-19 17:16:14 -070099 * Sets up the connection with ICallHandlerService
Santos Cordon89647a62013-07-16 13:38:09 -0700100 */
101 private void setupServiceConnection() {
102 mConnection = new ServiceConnection() {
103 @Override
104 public void onServiceConnected(ComponentName className, IBinder service) {
105 if (DBG) {
106 Log.d(TAG, "Service Connected");
107 }
Santos Cordon345350e2013-07-19 17:16:14 -0700108 onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service));
Santos Cordon89647a62013-07-16 13:38:09 -0700109 }
110
111 @Override
112 public void onServiceDisconnected(ComponentName className) {
Chiao Chenge41661c2013-07-23 13:28:26 -0700113 // TODO(klp): handle the case where the in call ui crashed or gets destroyed.
114 // In the near term, we need to re-bind to the service when ever it's gone.
115 // Longer term, we need a way to catch the crash and allow the users to choose
116 // a different in-call screen.
117 Log.e(TAG, "Yikes! no in call ui!");
Santos Cordon345350e2013-07-19 17:16:14 -0700118 mCallHandlerService = null;
Santos Cordon89647a62013-07-16 13:38:09 -0700119 }
120 };
121
Santos Cordon345350e2013-07-19 17:16:14 -0700122 Intent serviceIntent = new Intent(ICallHandlerService.class.getName());
Santos Cordon89647a62013-07-16 13:38:09 -0700123 if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) {
Santos Cordon345350e2013-07-19 17:16:14 -0700124 Log.e(TAG, "Cound not bind to ICallHandlerService");
Santos Cordon89647a62013-07-16 13:38:09 -0700125 }
126 }
127
128 /**
Santos Cordoncba1b442013-07-18 12:43:58 -0700129 * Called when the in-call UI service is connected. Send command interface to in-call.
130 */
Santos Cordon63a84242013-07-23 13:32:52 -0700131 private void onCallHandlerServiceConnected(ICallHandlerService callHandlerService) {
132 mCallHandlerService = callHandlerService;
Santos Cordoncba1b442013-07-18 12:43:58 -0700133
134 try {
Santos Cordon345350e2013-07-19 17:16:14 -0700135 mCallHandlerService.setCallCommandService(mCallCommandService);
Santos Cordoncba1b442013-07-18 12:43:58 -0700136 } catch (RemoteException e) {
Santos Cordon63a84242013-07-23 13:32:52 -0700137 Log.e(TAG, "Remote exception calling CallHandlerService::setCallCommandService", e);
Santos Cordon89647a62013-07-16 13:38:09 -0700138 }
139 }
140}